test_requests.py
This file
Path
test_requests.py
261 lines · 7.7 KiB · Python
Last changed by
test cleanupKenneth Reitzbefore adoption4a5b5bc
History of this fileTake it
basic testKenneth Reitzbefore adoption82cce02
lines 1–5why, one step back1#!/usr/bin/env python2# -*- coding: utf-8 -*-34"""Tests for Requests."""5Merge pull request #1024 from etscrivner/masterKenneth Reitzbefore adoption5c4aa0b
line 6why, one step back6import jsonbasic testsKenneth Reitzbefore adoption73ed0d0
line 7why, one step back7import osbasic testKenneth Reitzbefore adoption82cce02
lines 8–10why, one step back8import unittest910import requestsnew tests!Kenneth Reitzbefore adoption0569fe4
line 11why, one step back11from requests.auth import HTTPDigestAuthbasic testKenneth Reitzbefore adoption82cce02
line 12why, one step back12new tests!Kenneth Reitzbefore adoption0569fe4
line 13why, one step back13HTTPBIN = os.environ.get('HTTPBIN_URL', 'http://httpbin.org/')basic testKenneth Reitzbefore adoption82cce02
line 14why, one step back14new tests!Kenneth Reitzbefore adoption0569fe4
lines 15–18why, one step back15def httpbin(*suffix):16 """Returns url for HTTPBIN resource."""17 return HTTPBIN + '/'.join(suffix)18basic testKenneth Reitzbefore adoption82cce02
lines 19–20why, one step back19class RequestsTestCase(unittest.TestCase):20basic testsKenneth Reitzbefore adoption73ed0d0
lines 21–22why, one step back21 _multiprocess_can_split_ = True22basic testKenneth Reitzbefore adoption82cce02
lines 23–33why, one step back23 def setUp(self):24 """Create simple data set with headers."""25 pass2627 def tearDown(self):28 """Teardown."""29 pass3031 def test_assertion(self):32 assert 133basic testsKenneth Reitzbefore adoption73ed0d0
line 34why, one step back34 def test_entry_points(self):basic testKenneth Reitzbefore adoption82cce02
line 35why, one step back35basic testsKenneth Reitzbefore adoption73ed0d0
lines 36–43why, one step back36 requests.session37 requests.session().get38 requests.session().head39 requests.get40 requests.head41 requests.put42 requests.patch43 requests.postbasic testKenneth Reitzbefore adoption82cce02
line 44why, one step back44basic testsKenneth Reitzbefore adoption73ed0d0
lines 45–71why, one step back45 def test_invalid_url(self):46 self.assertRaises(ValueError, requests.get, 'hiwpefhipowhefopw')4748 def test_basic_building(self):49 req = requests.Request()50 req.url = 'http://kennethreitz.org/'51 req.data = {'life': '42'}5253 pr = req.prepare()54 assert pr.url == req.url55 assert pr.body == 'life=42'565758 def test_path_is_not_double_encoded(self):59 request = requests.Request('GET', "http://0.0.0.0/get/test case").prepare()6061 self.assertEqual(request.path_url, "/get/test%20case")6263 def test_params_are_added_before_fragment(self):64 request = requests.Request('GET',65 "http://example.com/path#fragment", params={"a": "b"}).prepare()66 self.assertEqual(request.url,67 "http://example.com/path?a=b#fragment")68 request = requests.Request('GET',69 "http://example.com/path?key=value#fragment", params={"a": "b"}).prepare()70 self.assertEqual(request.url,71 "http://example.com/path?key=value&a=b#fragment")new tests!Kenneth Reitzbefore adoption0569fe4
lines 72–88why, one step back7273 def test_HTTP_200_OK_GET(self):74 r = requests.get(httpbin('get'))75 self.assertEqual(r.status_code, 200)7677 def test_HTTP_200_OK_GET_ALTERNATIVE(self):78 r = requests.Request('GET', httpbin('get'))79 s = requests.Session()8081 r = s.send(r.prepare())8283 self.assertEqual(r.status_code, 200)8485 def test_HTTP_302_ALLOW_REDIRECT_GET(self):86 r = requests.get(httpbin('redirect', '1'))87 self.assertEqual(r.status_code, 200)88test cleanupKenneth Reitzbefore adoption4a5b5bc
lines 89–91why, one step back89 # def test_HTTP_302_ALLOW_REDIRECT_POST(self):90 # r = requests.post(httpbin('status', '302'), data={'some': 'data'})91 # self.assertEqual(r.status_code, 200)Merge remote-tracking branch 'origin/master'Kenneth Reitzbefore adoptionfcccc82
line 92why, one step back92new tests!Kenneth Reitzbefore adoption0569fe4
lines 93–189why, one step back93 def test_HTTP_200_OK_GET_WITH_PARAMS(self):94 heads = {'User-agent': 'Mozilla/5.0'}9596 r = requests.get(httpbin('user-agent'), headers=heads)9798 self.assertTrue(heads['User-agent'] in r.text)99 self.assertEqual(r.status_code, 200)100101 def test_HTTP_200_OK_GET_WITH_MIXED_PARAMS(self):102 heads = {'User-agent': 'Mozilla/5.0'}103104 r = requests.get(httpbin('get') + '?test=true', params={'q': 'test'}, headers=heads)105 self.assertEqual(r.status_code, 200)106107 def test_user_agent_transfers(self):108109 heads = {110 'User-agent':111 'Mozilla/5.0 (github.com/kennethreitz/requests)'112 }113114 r = requests.get(httpbin('user-agent'), headers=heads)115 self.assertTrue(heads['User-agent'] in r.text)116117 heads = {118 'user-agent':119 'Mozilla/5.0 (github.com/kennethreitz/requests)'120 }121122 r = requests.get(httpbin('user-agent'), headers=heads)123 self.assertTrue(heads['user-agent'] in r.text)124125 def test_HTTP_200_OK_HEAD(self):126 r = requests.head(httpbin('get'))127 self.assertEqual(r.status_code, 200)128129 def test_HTTP_200_OK_PUT(self):130 r = requests.put(httpbin('put'))131 self.assertEqual(r.status_code, 200)132133 def test_BASICAUTH_TUPLE_HTTP_200_OK_GET(self):134135136 auth = ('user', 'pass')137 url = httpbin('basic-auth', 'user', 'pass')138139 r = requests.get(url, auth=auth)140 self.assertEqual(r.status_code, 200)141142 r = requests.get(url)143 self.assertEqual(r.status_code, 401)144145 s = requests.session()146 s.auth = auth147 r = s.get(url)148 self.assertEqual(r.status_code, 200)149150 def test_DIGEST_HTTP_200_OK_GET(self):151152 auth = HTTPDigestAuth('user', 'pass')153 url = httpbin('digest-auth', 'auth', 'user', 'pass')154155 r = requests.get(url, auth=auth)156 self.assertEqual(r.status_code, 200)157158 r = requests.get(url)159 self.assertEqual(r.status_code, 401)160161 s = requests.session()162 s.auth = auth163 r = s.get(url)164 self.assertEqual(r.status_code, 200)165166 def test_DIGESTAUTH_WRONG_HTTP_401_GET(self):167168 auth = HTTPDigestAuth('user', 'wrongpass')169 url = httpbin('digest-auth', 'auth', 'user', 'pass')170171 r = requests.get(url, auth=auth)172 self.assertEqual(r.status_code, 401)173174 r = requests.get(url)175 self.assertEqual(r.status_code, 401)176177 s = requests.session()178 s.auth = auth179 r = s.get(url)180 self.assertEqual(r.status_code, 401)181182 def test_POSTBIN_GET_POST_FILES(self):183184 url = httpbin('post')185 post1 = requests.post(url).raise_for_status()186187 post1 = requests.post(url, data={'some': 'data'})188 self.assertEqual(post1.status_code, 200)189remove chardetKenneth Reitzbefore adoption480a382
line 190why, one step back190 with open('requirements.txt') as f:new tests!Kenneth Reitzbefore adoption0569fe4
lines 191–209why, one step back191 post2 = requests.post(url, files={'some': f})192 self.assertEqual(post2.status_code, 200)193194 post4 = requests.post(url, data='[{"some": "json"}]')195 self.assertEqual(post4.status_code, 200)196197 try:198 requests.post(url, files=['bad file data'])199 except ValueError:200 pass201202 def test_POSTBIN_GET_POST_FILES_WITH_DATA(self):203204 url = httpbin('post')205 post1 = requests.post(url).raise_for_status()206207 post1 = requests.post(url, data={'some': 'data'})208 self.assertEqual(post1.status_code, 200)209remove chardetKenneth Reitzbefore adoption480a382
line 210why, one step back210 with open('requirements.txt') as f:new tests!Kenneth Reitzbefore adoption0569fe4
lines 211–220why, one step back211 post2 = requests.post(url, data={'some': 'data'}, files={'some': f})212 self.assertEqual(post2.status_code, 200)213214 post4 = requests.post(url, data='[{"some": "json"}]')215 self.assertEqual(post4.status_code, 200)216217 try:218 requests.post(url, files=['bad file data'])219 except ValueError:220 passremove chardetKenneth Reitzbefore adoption480a382
lines 221–249why, one step back221222 def test_request_ok_set(self):223 r = requests.get(httpbin('status', '404'))224 self.assertEqual(r.ok, False)225226 def test_status_raising(self):227 r = requests.get(httpbin('status', '404'))228 self.assertRaises(requests.exceptions.HTTPError, r.raise_for_status)229230 r = requests.get(httpbin('status', '500'))231 self.assertFalse(r.ok)232233 def test_decompress_gzip(self):234 r = requests.get(httpbin('gzip'))235 r.content.decode('ascii')236237 def test_unicode_get(self):238 url = httpbin('/get')239 requests.get(url, params={'foo': 'føø'})240 requests.get(url, params={'føø': 'føø'})241 requests.get(url, params={'føø': 'føø'})242 requests.get(url, params={'foo': 'foo'})243 requests.get(httpbin('ø'), params={'foo': 'foo'})244245 def test_urlencoded_get_query_multivalued_param(self):246247 r = requests.get(httpbin('get'), params=dict(test=['foo', 'baz']))248 self.assertEqual(r.status_code, 200)249 self.assertEqual(r.url, httpbin('get?test=foo&test=baz'))Merge pull request #1024 from etscrivner/masterKenneth Reitzbefore adoption5c4aa0b
lines 250–256why, one step back250251 def test_different_encodings_dont_break_post(self):252 r = requests.post(httpbin('post'),253 data={'stuff': json.dumps({'a': 123})},254 params={'blah': 'asdf1234'},255 files={'file': ('test_requests.py', open(__file__, 'rb'))})256 self.assertEqual(r.status_code, 200)new tests!Kenneth Reitzbefore adoption0569fe4
lines 257–258why, one step back257258basic testsKenneth Reitzbefore adoption73ed0d0
lines 259–261why, one step back259260if __name__ == '__main__':261 unittest.main()