aboutsummaryrefslogtreecommitdiff
path: root/test_requests.py
diff options
context:
space:
mode:
authorSVN-Git Migration <python-modules-team@lists.alioth.debian.org>2015-10-08 13:41:28 -0700
committerSVN-Git Migration <python-modules-team@lists.alioth.debian.org>2015-10-08 13:41:28 -0700
commit653256249d44c67a0852d57a166948a9dc712ef4 (patch)
tree91efed3ad3640d3949be638ad4c4e45a63fd3864 /test_requests.py
parentc6ee35e00c5709435b3a6b664c44fceb70a918c6 (diff)
downloadpython-requests-653256249d44c67a0852d57a166948a9dc712ef4.tar
python-requests-653256249d44c67a0852d57a166948a9dc712ef4.tar.gz
Imported Upstream version 1.2.3
Diffstat (limited to 'test_requests.py')
-rwxr-xr-x[-rw-r--r--]test_requests.py306
1 files changed, 304 insertions, 2 deletions
diff --git a/test_requests.py b/test_requests.py
index 93b8123..4a4831e 100644..100755
--- a/test_requests.py
+++ b/test_requests.py
@@ -11,7 +11,11 @@ import pickle
import requests
from requests.auth import HTTPDigestAuth
-from requests.compat import str
+from requests.adapters import HTTPAdapter
+from requests.compat import str, cookielib
+from requests.cookies import cookiejar_from_dict
+from requests.exceptions import InvalidURL, MissingSchema
+from requests.structures import CaseInsensitiveDict
try:
import StringIO
@@ -50,7 +54,8 @@ class RequestsTestCase(unittest.TestCase):
requests.post
def test_invalid_url(self):
- self.assertRaises(ValueError, requests.get, 'hiwpefhipowhefopw')
+ self.assertRaises(MissingSchema, requests.get, 'hiwpefhipowhefopw')
+ self.assertRaises(InvalidURL, requests.get, 'http://')
def test_basic_building(self):
req = requests.Request()
@@ -124,6 +129,38 @@ class RequestsTestCase(unittest.TestCase):
r = s.get(httpbin('redirect/1')) # redirects to httpbin('get')
self.assertTrue("Cookie" in r.json()["headers"])
+ def test_cookie_removed_on_expire(self):
+ s = requests.session()
+ s.get(httpbin('cookies/set?foo=bar'))
+ self.assertTrue(s.cookies['foo'] == 'bar')
+ s.get(
+ httpbin('response-headers'),
+ params={
+ 'Set-Cookie':
+ 'foo=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT'
+ }
+ )
+ assert 'foo' not in s.cookies
+
+ def test_request_cookie_overrides_session_cookie(self):
+ s = requests.session()
+ s.cookies['foo'] = 'bar'
+ r = s.get(httpbin('cookies'), cookies={'foo': 'baz'})
+ assert r.json()['cookies']['foo'] == 'baz'
+ # Session cookie should not be modified
+ assert s.cookies['foo'] == 'bar'
+
+ def test_generic_cookiejar_works(self):
+ cj = cookielib.CookieJar()
+ cookiejar_from_dict({'foo': 'bar'}, cj)
+ s = requests.session()
+ s.cookies = cj
+ r = s.get(httpbin('cookies'))
+ # Make sure the cookie was sent
+ assert r.json()['cookies']['foo'] == 'bar'
+ # Make sure the session cj is still the custom one
+ assert s.cookies is cj
+
def test_user_agent_transfers(self):
heads = {
@@ -286,6 +323,38 @@ class RequestsTestCase(unittest.TestCase):
files={'file': ('test_requests.py', open(__file__, 'rb'))})
self.assertEqual(r.status_code, 200)
+ def test_unicode_multipart_post(self):
+ r = requests.post(httpbin('post'),
+ data={'stuff': u'ëlïxr'},
+ files={'file': ('test_requests.py', open(__file__, 'rb'))})
+ self.assertEqual(r.status_code, 200)
+
+ r = requests.post(httpbin('post'),
+ data={'stuff': u'ëlïxr'.encode('utf-8')},
+ files={'file': ('test_requests.py', open(__file__, 'rb'))})
+ self.assertEqual(r.status_code, 200)
+
+ r = requests.post(httpbin('post'),
+ data={'stuff': 'elixr'},
+ files={'file': ('test_requests.py', open(__file__, 'rb'))})
+ self.assertEqual(r.status_code, 200)
+
+ r = requests.post(httpbin('post'),
+ data={'stuff': 'elixr'.encode('utf-8')},
+ files={'file': ('test_requests.py', open(__file__, 'rb'))})
+ self.assertEqual(r.status_code, 200)
+
+ def test_unicode_multipart_post_fieldnames(self):
+ filename = os.path.splitext(__file__)[0] + '.py'
+ r = requests.Request(method='POST',
+ url=httpbin('post'),
+ data={'stuff'.encode('utf-8'): 'elixr'},
+ files={'file': ('test_requests.py',
+ open(filename, 'rb'))})
+ prep = r.prepare()
+ self.assertTrue(b'name="stuff"' in prep.body)
+ self.assertFalse(b'name="b\'stuff\'"' in prep.body)
+
def test_custom_content_type(self):
r = requests.post(httpbin('post'),
data={'stuff': json.dumps({'a': 123})},
@@ -367,6 +436,11 @@ class RequestsTestCase(unittest.TestCase):
def test_response_is_iterable(self):
r = requests.Response()
io = StringIO.StringIO('abc')
+ read_ = io.read
+
+ def read_mock(amt, decode_content=None):
+ return read_(amt)
+ setattr(io, 'read', read_mock)
r.raw = io
self.assertTrue(next(iter(r)))
io.close()
@@ -399,6 +473,234 @@ class RequestsTestCase(unittest.TestCase):
r = s.send(r.prepare())
self.assertEqual(r.status_code, 200)
+ def test_fixes_1329(self):
+ """
+ Ensure that header updates are done case-insensitively.
+ """
+ s = requests.Session()
+ s.headers.update({'ACCEPT': 'BOGUS'})
+ s.headers.update({'accept': 'application/json'})
+ r = s.get(httpbin('get'))
+ headers = r.request.headers
+ # ASCII encode because of key comparison changes in py3
+ self.assertEqual(
+ headers['accept'.encode('ascii')],
+ 'application/json'
+ )
+ self.assertEqual(
+ headers['Accept'.encode('ascii')],
+ 'application/json'
+ )
+ self.assertEqual(
+ headers['ACCEPT'.encode('ascii')],
+ 'application/json'
+ )
+
+ def test_transport_adapter_ordering(self):
+ s = requests.Session()
+ order = ['https://', 'http://']
+ self.assertEqual(order, list(s.adapters))
+ s.mount('http://git', HTTPAdapter())
+ s.mount('http://github', HTTPAdapter())
+ s.mount('http://github.com', HTTPAdapter())
+ s.mount('http://github.com/about/', HTTPAdapter())
+ order = [
+ 'http://github.com/about/',
+ 'http://github.com',
+ 'http://github',
+ 'http://git',
+ 'https://',
+ 'http://',
+ ]
+ self.assertEqual(order, list(s.adapters))
+ s.mount('http://gittip', HTTPAdapter())
+ s.mount('http://gittip.com', HTTPAdapter())
+ s.mount('http://gittip.com/about/', HTTPAdapter())
+ order = [
+ 'http://github.com/about/',
+ 'http://gittip.com/about/',
+ 'http://github.com',
+ 'http://gittip.com',
+ 'http://github',
+ 'http://gittip',
+ 'http://git',
+ 'https://',
+ 'http://',
+ ]
+ self.assertEqual(order, list(s.adapters))
+ s2 = requests.Session()
+ s2.adapters = {'http://': HTTPAdapter()}
+ s2.mount('https://', HTTPAdapter())
+ self.assertTrue('http://' in s2.adapters)
+ self.assertTrue('https://' in s2.adapters)
+
+ def test_header_remove_is_case_insensitive(self):
+ # From issue #1321
+ s = requests.Session()
+ s.headers['foo'] = 'bar'
+ r = s.get(httpbin('get'), headers={'FOO': None})
+ assert 'foo' not in r.request.headers
+
+ def test_params_are_merged_case_sensitive(self):
+ s = requests.Session()
+ s.params['foo'] = 'bar'
+ r = s.get(httpbin('get'), params={'FOO': 'bar'})
+ assert r.json()['args'] == {'foo': 'bar', 'FOO': 'bar'}
+
+
+ def test_long_authinfo_in_url(self):
+ url = 'http://{0}:{1}@{2}:9000/path?query#frag'.format(
+ 'E8A3BE87-9E3F-4620-8858-95478E385B5B',
+ 'EA770032-DA4D-4D84-8CE9-29C6D910BF1E',
+ 'exactly-------------sixty-----------three------------characters',
+ )
+ r = requests.Request('GET', url).prepare()
+ self.assertEqual(r.url, url)
+
+
+class TestCaseInsensitiveDict(unittest.TestCase):
+
+ def test_mapping_init(self):
+ cid = CaseInsensitiveDict({'Foo': 'foo','BAr': 'bar'})
+ self.assertEqual(len(cid), 2)
+ self.assertTrue('foo' in cid)
+ self.assertTrue('bar' in cid)
+
+ def test_iterable_init(self):
+ cid = CaseInsensitiveDict([('Foo', 'foo'), ('BAr', 'bar')])
+ self.assertEqual(len(cid), 2)
+ self.assertTrue('foo' in cid)
+ self.assertTrue('bar' in cid)
+
+ def test_kwargs_init(self):
+ cid = CaseInsensitiveDict(FOO='foo', BAr='bar')
+ self.assertEqual(len(cid), 2)
+ self.assertTrue('foo' in cid)
+ self.assertTrue('bar' in cid)
+
+ def test_docstring_example(self):
+ cid = CaseInsensitiveDict()
+ cid['Accept'] = 'application/json'
+ self.assertEqual(cid['aCCEPT'], 'application/json')
+ self.assertEqual(list(cid), ['Accept'])
+
+ def test_len(self):
+ cid = CaseInsensitiveDict({'a': 'a', 'b': 'b'})
+ cid['A'] = 'a'
+ self.assertEqual(len(cid), 2)
+
+ def test_getitem(self):
+ cid = CaseInsensitiveDict({'Spam': 'blueval'})
+ self.assertEqual(cid['spam'], 'blueval')
+ self.assertEqual(cid['SPAM'], 'blueval')
+
+ def test_fixes_649(self):
+ """__setitem__ should behave case-insensitively."""
+ cid = CaseInsensitiveDict()
+ cid['spam'] = 'oneval'
+ cid['Spam'] = 'twoval'
+ cid['sPAM'] = 'redval'
+ cid['SPAM'] = 'blueval'
+ self.assertEqual(cid['spam'], 'blueval')
+ self.assertEqual(cid['SPAM'], 'blueval')
+ self.assertEqual(list(cid.keys()), ['SPAM'])
+
+ def test_delitem(self):
+ cid = CaseInsensitiveDict()
+ cid['Spam'] = 'someval'
+ del cid['sPam']
+ self.assertFalse('spam' in cid)
+ self.assertEqual(len(cid), 0)
+
+ def test_contains(self):
+ cid = CaseInsensitiveDict()
+ cid['Spam'] = 'someval'
+ self.assertTrue('Spam' in cid)
+ self.assertTrue('spam' in cid)
+ self.assertTrue('SPAM' in cid)
+ self.assertTrue('sPam' in cid)
+ self.assertFalse('notspam' in cid)
+
+ def test_get(self):
+ cid = CaseInsensitiveDict()
+ cid['spam'] = 'oneval'
+ cid['SPAM'] = 'blueval'
+ self.assertEqual(cid.get('spam'), 'blueval')
+ self.assertEqual(cid.get('SPAM'), 'blueval')
+ self.assertEqual(cid.get('sPam'), 'blueval')
+ self.assertEqual(cid.get('notspam', 'default'), 'default')
+
+ def test_update(self):
+ cid = CaseInsensitiveDict()
+ cid['spam'] = 'blueval'
+ cid.update({'sPam': 'notblueval'})
+ self.assertEqual(cid['spam'], 'notblueval')
+ cid = CaseInsensitiveDict({'Foo': 'foo','BAr': 'bar'})
+ cid.update({'fOO': 'anotherfoo', 'bAR': 'anotherbar'})
+ self.assertEqual(len(cid), 2)
+ self.assertEqual(cid['foo'], 'anotherfoo')
+ self.assertEqual(cid['bar'], 'anotherbar')
+
+ def test_update_retains_unchanged(self):
+ cid = CaseInsensitiveDict({'foo': 'foo', 'bar': 'bar'})
+ cid.update({'foo': 'newfoo'})
+ self.assertEquals(cid['bar'], 'bar')
+
+ def test_iter(self):
+ cid = CaseInsensitiveDict({'Spam': 'spam', 'Eggs': 'eggs'})
+ keys = frozenset(['Spam', 'Eggs'])
+ self.assertEqual(frozenset(iter(cid)), keys)
+
+ def test_equality(self):
+ cid = CaseInsensitiveDict({'SPAM': 'blueval', 'Eggs': 'redval'})
+ othercid = CaseInsensitiveDict({'spam': 'blueval', 'eggs': 'redval'})
+ self.assertEqual(cid, othercid)
+ del othercid['spam']
+ self.assertNotEqual(cid, othercid)
+ self.assertEqual(cid, {'spam': 'blueval', 'eggs': 'redval'})
+
+ def test_setdefault(self):
+ cid = CaseInsensitiveDict({'Spam': 'blueval'})
+ self.assertEqual(
+ cid.setdefault('spam', 'notblueval'),
+ 'blueval'
+ )
+ self.assertEqual(
+ cid.setdefault('notspam', 'notblueval'),
+ 'notblueval'
+ )
+
+ def test_lower_items(self):
+ cid = CaseInsensitiveDict({
+ 'Accept': 'application/json',
+ 'user-Agent': 'requests',
+ })
+ keyset = frozenset(lowerkey for lowerkey, v in cid.lower_items())
+ lowerkeyset = frozenset(['accept', 'user-agent'])
+ self.assertEqual(keyset, lowerkeyset)
+
+ def test_preserve_key_case(self):
+ cid = CaseInsensitiveDict({
+ 'Accept': 'application/json',
+ 'user-Agent': 'requests',
+ })
+ keyset = frozenset(['Accept', 'user-Agent'])
+ self.assertEqual(frozenset(i[0] for i in cid.items()), keyset)
+ self.assertEqual(frozenset(cid.keys()), keyset)
+ self.assertEqual(frozenset(cid), keyset)
+
+ def test_preserve_last_key_case(self):
+ cid = CaseInsensitiveDict({
+ 'Accept': 'application/json',
+ 'user-Agent': 'requests',
+ })
+ cid.update({'ACCEPT': 'application/json'})
+ cid['USER-AGENT'] = 'requests'
+ keyset = frozenset(['ACCEPT', 'USER-AGENT'])
+ self.assertEqual(frozenset(i[0] for i in cid.items()), keyset)
+ self.assertEqual(frozenset(cid.keys()), keyset)
+ self.assertEqual(frozenset(cid), keyset)
+
if __name__ == '__main__':
unittest.main()