diff options
Diffstat (limited to 'tests/test_requests.py')
-rwxr-xr-x | tests/test_requests.py | 83 |
1 files changed, 72 insertions, 11 deletions
diff --git a/tests/test_requests.py b/tests/test_requests.py index ac8329e..530008a 100755 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -10,7 +10,6 @@ sys.path.insert(0, os.path.abspath('..')) import json import os -import sys import unittest import pickle @@ -52,8 +51,16 @@ class TestSetup(object): # time.sleep(1) _httpbin = True +class TestBaseMixin(object): + + def assertCookieHas(self, cookie, **kwargs): + """Assert that a cookie has various specified properties.""" + for attr, expected_value in kwargs.items(): + cookie_attr = getattr(cookie, attr) + message = 'Failed comparison for %s: %s != %s' % (attr, cookie_attr, expected_value) + self.assertEqual(cookie_attr, expected_value, message) -class RequestsTestSuite(TestSetup, unittest.TestCase): +class RequestsTestSuite(TestSetup, TestBaseMixin, unittest.TestCase): """Requests test cases.""" def test_entry_points(self): @@ -322,6 +329,37 @@ class RequestsTestSuite(TestSetup, unittest.TestCase): self.assertEqual(post2.status_code, 200) + def test_POSTBIN_GET_POST_FILES_STRINGS(self): + + for service in SERVICES: + + url = service('post') + + post1 = post(url, files={'fname.txt': 'fdata'}) + self.assertEqual(post1.status_code, 200) + + post2 = post(url, files={'fname.txt': 'fdata', 'fname2.txt':'more fdata'}) + self.assertEqual(post2.status_code, 200) + + post3 = post(url, files={'fname.txt': 'fdata', 'fname2.txt':open(__file__,'rb')}) + self.assertEqual(post3.status_code, 200) + + post4 = post(url, files={'fname.txt': 'fdata'}) + self.assertEqual(post4.status_code, 200) + + post5 = post(url, files={'file': ('file.txt', 'more fdata')}) + self.assertEqual(post5.status_code, 200) + + post6 = post(url, files={'fname.txt': '\xe9'}) + self.assertEqual(post6.status_code, 200) + + post7 = post(url, files={'fname.txt': 'fdata to verify'}) + rbody = json.loads(post7.text) + self.assertTrue(rbody.get('files', None)) + self.assertTrue(rbody['files'].get('fname.txt'), None) + self.assertEqual(rbody['files']['fname.txt'], 'fdata to verify') + + def test_nonzero_evaluation(self): for service in SERVICES: @@ -632,24 +670,24 @@ class RequestsTestSuite(TestSetup, unittest.TestCase): # Those cookies persist transparently. c = json.loads(r.text).get('cookies') - assert c == _c + self.assertEqual(c, _c) # Double check. r = get(httpbin('cookies'), cookies={}, session=s) c = json.loads(r.text).get('cookies') - assert c == _c + self.assertEqual(c, _c) # Remove a cookie by setting it's value to None. r = get(httpbin('cookies'), cookies={'bessie': None}, session=s) c = json.loads(r.text).get('cookies') del _c['bessie'] - assert c == _c + self.assertEqual(c, _c) # Test session-level cookies. s = requests.session(cookies=_c) r = get(httpbin('cookies'), session=s) c = json.loads(r.text).get('cookies') - assert c == _c + self.assertEqual(c, _c) # Have the server set a cookie. r = get(httpbin('cookies', 'set', 'k', 'v'), allow_redirects=True, session=s) @@ -698,9 +736,13 @@ class RequestsTestSuite(TestSetup, unittest.TestCase): ds = pickle.loads(pickle.dumps(s)) self.assertEqual(s.headers, ds.headers) - self.assertEqual(s.cookies, ds.cookies) self.assertEqual(s.auth, ds.auth) + # Cookie doesn't have a good __eq__, so verify manually: + self.assertEqual(len(ds.cookies), 1) + for cookie in ds.cookies: + self.assertCookieHas(cookie, name='a-cookie', value='cookie-value') + def test_unpickled_session_requests(self): s = requests.session() r = get(httpbin('cookies', 'set', 'k', 'v'), allow_redirects=True, session=s) @@ -837,10 +879,29 @@ class RequestsTestSuite(TestSetup, unittest.TestCase): r = requests.get(httpbin('status', '404')) r.text - def test_no_content(self): - r = requests.get(httpbin('status', "0"), config={"safe_mode":True}) - r.content - r.content + def test_max_redirects(self): + """Test the max_redirects config variable, normally and under safe_mode.""" + def unsafe_callable(): + requests.get(httpbin('redirect', '3'), config=dict(max_redirects=2)) + self.assertRaises(requests.exceptions.TooManyRedirects, unsafe_callable) + + # add safe mode + response = requests.get(httpbin('redirect', '3'), config=dict(safe_mode=True, max_redirects=2)) + self.assertTrue(response.content is None) + self.assertTrue(isinstance(response.error, requests.exceptions.TooManyRedirects)) + + def test_connection_keepalive_and_close(self): + """Test that we send 'Connection: close' when keep_alive is disabled.""" + # keep-alive should be on by default + r1 = requests.get(httpbin('get')) + # XXX due to proxying issues, test the header sent back by httpbin, rather than + # the header reported in its message body. See kennethreitz/httpbin#46 + self.assertEqual(r1.headers['Connection'].lower(), 'keep-alive') + + # but when we disable it, we should send a 'Connection: close' + # and get the same back: + r2 = requests.get(httpbin('get'), config=dict(keep_alive=False)) + self.assertEqual(r2.headers['Connection'].lower(), 'close') if __name__ == '__main__': unittest.main() |