Morsel, cookielib, getproxies, str, urljoin, urlparse, is_py3, builtin_str)
from requests.cookies import cookiejar_from_dict, morsel_to_cookie
from requests.exceptions import InvalidURL, MissingSchema
-from requests.models import PreparedRequest, Response
+from requests.models import PreparedRequest
from requests.structures import CaseInsensitiveDict
from requests.sessions import SessionRedirectMixin
-from requests.models import PreparedRequest, urlencode
+from requests.models import urlencode
from requests.hooks import default_hooks
try:
def test_set_cookie_on_301(self):
s = requests.session()
url = httpbin('cookies/set?foo=bar')
- r = s.get(url)
+ s.get(url)
assert s.cookies['foo'] == 'bar'
def test_cookie_sent_on_redirect(self):
def test_param_cookiejar_works(self):
cj = cookielib.CookieJar()
- cookiejar_from_dict({'foo' : 'bar'}, cj)
+ cookiejar_from_dict({'foo': 'bar'}, cj)
s = requests.session()
r = s.get(httpbin('cookies'), cookies=cj)
# Make sure the cookie was sent
assert post4.status_code == 200
with pytest.raises(ValueError):
- requests.post(url, files = ['bad file data'])
+ requests.post(url, files=['bad file data'])
def test_POSTBIN_GET_POST_FILES_WITH_DATA(self):
assert post1.status_code == 200
with open('requirements.txt') as f:
- post2 = requests.post(url, data={'some': 'data'}, files={'some': f})
+ post2 = requests.post(
+ url, data={'some': 'data'}, files={'some': f})
assert post2.status_code == 200
post4 = requests.post(url, data='[{"some": "json"}]')
assert post4.status_code == 200
with pytest.raises(ValueError):
- requests.post(url, files = ['bad file data'])
+ requests.post(url, files=['bad file data'])
def test_conflicting_post_params(self):
url = httpbin('post')
requests.get(httpbin('ø'), params={'foo': 'foo'})
def test_unicode_header_name(self):
- requests.put(httpbin('put'), headers={str('Content-Type'): 'application/octet-stream'}, data='\xff') # compat.str is unicode.
+ requests.put(
+ httpbin('put'),
+ headers={str('Content-Type'): 'application/octet-stream'},
+ data='\xff') # compat.str is unicode.
def test_pyopenssl_redirect(self):
requests.get('https://httpbin.org/status/301')
assert r.url == httpbin('get?test=foo&test=baz')
def test_different_encodings_dont_break_post(self):
- r = requests.post(httpbin('post'),
- data={'stuff': json.dumps({'a': 123})},
- params={'blah': 'asdf1234'},
- files={'file': ('test_requests.py', open(__file__, 'rb'))})
+ r = requests.post(
+ httpbin('post'),
+ data={'stuff': json.dumps({'a': 123})},
+ params={'blah': 'asdf1234'},
+ files={'file': ('test_requests.py', open(__file__, 'rb'))})
assert 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'))})
+ r = requests.post(
+ httpbin('post'),
+ data={'stuff': u('ëlïxr')},
+ files={'file': ('test_requests.py', open(__file__, 'rb'))})
assert 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'))})
+ r = requests.post(
+ httpbin('post'),
+ data={'stuff': u('ëlïxr').encode('utf-8')},
+ files={'file': ('test_requests.py', open(__file__, 'rb'))})
assert r.status_code == 200
- r = requests.post(httpbin('post'),
- data={'stuff': 'elixr'},
- files={'file': ('test_requests.py', open(__file__, 'rb'))})
+ r = requests.post(
+ httpbin('post'),
+ data={'stuff': 'elixr'},
+ files={'file': ('test_requests.py', open(__file__, 'rb'))})
assert r.status_code == 200
- r = requests.post(httpbin('post'),
- data={'stuff': 'elixr'.encode('utf-8')},
- files={'file': ('test_requests.py', open(__file__, 'rb'))})
+ r = requests.post(
+ httpbin('post'),
+ data={'stuff': 'elixr'.encode('utf-8')},
+ files={'file': ('test_requests.py', open(__file__, 'rb'))})
assert r.status_code == 200
def test_unicode_multipart_post_fieldnames(self):
def test_unicode_method_name(self):
files = {'file': open('test_requests.py', 'rb')}
- r = requests.request(method=u('POST'), url=httpbin('post'), files=files)
+ r = requests.request(
+ method=u('POST'), url=httpbin('post'), files=files)
assert r.status_code == 200
def test_custom_content_type(self):
- r = requests.post(httpbin('post'),
- data={'stuff': json.dumps({'a': 123})},
- files={'file1': ('test_requests.py', open(__file__, 'rb')),
- 'file2': ('test_requests', open(__file__, 'rb'),
- 'text/py-content-type')})
+ r = requests.post(
+ httpbin('post'),
+ data={'stuff': json.dumps({'a': 123})},
+ files={'file1': ('test_requests.py', open(__file__, 'rb')),
+ 'file2': ('test_requests', open(__file__, 'rb'),
+ 'text/py-content-type')})
assert r.status_code == 200
assert b"text/py-content-type" in r.request.body
prep = s.prepare_request(req)
resp = s.send(prep)
- assert resp.json()['headers']['Dummy-Auth-Test'] == 'dummy-auth-test-ok'
+ assert resp.json()['headers'][
+ 'Dummy-Auth-Test'] == 'dummy-auth-test-ok'
def test_links(self):
r = requests.Response()
# make sure one can use items multiple times
assert list(items) == list(items)
-
def test_time_elapsed_blank(self):
r = requests.get(httpbin('get'))
td = r.elapsed
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',
class TestCaseInsensitiveDict(unittest.TestCase):
def test_mapping_init(self):
- cid = CaseInsensitiveDict({'Foo': 'foo','BAr': 'bar'})
+ cid = CaseInsensitiveDict({'Foo': 'foo', 'BAr': 'bar'})
assert len(cid) == 2
assert 'foo' in cid
assert 'bar' in cid
cid['spam'] = 'blueval'
cid.update({'sPam': 'notblueval'})
assert cid['spam'] == 'notblueval'
- cid = CaseInsensitiveDict({'Foo': 'foo','BAr': 'bar'})
+ cid = CaseInsensitiveDict({'Foo': 'foo', 'BAr': 'bar'})
cid.update({'fOO': 'anotherfoo', 'bAR': 'anotherbar'})
assert len(cid) == 2
assert cid['foo'] == 'anotherfoo'
from requests.utils import super_len
assert super_len(StringIO.StringIO()) == 0
- assert super_len(StringIO.StringIO('with so much drama in the LBC')) == 29
+ assert super_len(
+ StringIO.StringIO('with so much drama in the LBC')) == 29
assert super_len(BytesIO()) == 0
- assert super_len(BytesIO(b"it's kinda hard bein' snoop d-o-double-g")) == 40
+ assert super_len(
+ BytesIO(b"it's kinda hard bein' snoop d-o-double-g")) == 40
try:
import cStringIO
except ImportError:
pass
else:
- assert super_len(cStringIO.StringIO('but some how, some way...')) == 25
+ assert super_len(
+ cStringIO.StringIO('but some how, some way...')) == 25
def test_get_environ_proxies_ip_ranges(self):
- """ Ensures that IP addresses are correctly matches with ranges in no_proxy variable """
+ """Ensures that IP addresses are correctly matches with ranges
+ in no_proxy variable."""
from requests.utils import get_environ_proxies
os.environ['no_proxy'] = "192.168.0.0/24,127.0.0.1,localhost.localdomain,172.16.1.1"
assert get_environ_proxies('http://192.168.0.1:5000/') == {}
assert get_environ_proxies('http://192.168.1.1/') != {}
def test_get_environ_proxies(self):
- """ Ensures that IP addresses are correctly matches with ranges in no_proxy variable """
+ """Ensures that IP addresses are correctly matches with ranges
+ in no_proxy variable."""
from requests.utils import get_environ_proxies
os.environ['no_proxy'] = "127.0.0.1,localhost.localdomain,192.168.0.0/24,172.16.1.1"
- assert get_environ_proxies('http://localhost.localdomain:5000/v1.0/') == {}
+ assert get_environ_proxies(
+ 'http://localhost.localdomain:5000/v1.0/') == {}
assert get_environ_proxies('http://www.requests.com/') != {}
def test_is_ipv4_address(self):
assert not address_in_network('172.16.0.1', '192.168.1.0/24')
def test_get_auth_from_url(self):
- """ Ensures that username and password in well-encoded URI as per RFC 3986 are correclty extracted """
+ """Ensures that username and password in well-encoded URI as per
+ RFC 3986 are correclty extracted."""
from requests.utils import get_auth_from_url
from requests.compat import quote
percent_encoding_test_chars = "%!*'();:@&=+$,/?#[] "
url_address = "request.com/url.html#test"
- url = "http://" + quote(percent_encoding_test_chars, '') + ':' + quote(percent_encoding_test_chars, '') + '@' + url_address
+ url = "http://" + quote(
+ percent_encoding_test_chars, '') + ':' + quote(
+ percent_encoding_test_chars, '') + '@' + url_address
(username, password) = get_auth_from_url(url)
assert username == percent_encoding_test_chars
assert password == percent_encoding_test_chars
class TestTimeout:
def test_stream_timeout(self):
try:
- r = requests.get('https://httpbin.org/delay/10', timeout=5.0)
+ requests.get('https://httpbin.org/delay/10', timeout=5.0)
except requests.exceptions.Timeout as e:
assert 'Read timed out' in e.args[0].args[0]
@pytest.fixture
def list_of_tuples():
return [
- (('a', 'b'), ('c', 'd')),
- (('c', 'd'), ('a', 'b')),
- (('a', 'b'), ('c', 'd'), ('e', 'f')),
- ]
+ (('a', 'b'), ('c', 'd')),
+ (('c', 'd'), ('a', 'b')),
+ (('a', 'b'), ('c', 'd'), ('e', 'f')),
+ ]
def test_data_argument_accepts_tuples(list_of_tuples):
)
assert p.body == urlencode(data)
+
def assert_copy(p, p_copy):
for attr in ('method', 'url', 'headers', '_cookies', 'body', 'hooks'):
assert getattr(p, attr) == getattr(p_copy, attr)
+
def test_prepared_request_empty_copy():
p = PreparedRequest()
assert_copy(p, p.copy())
+
def test_prepared_request_no_cookies_copy():
p = PreparedRequest()
p.prepare(
)
assert_copy(p, p.copy())
+
def test_prepared_request_complete_copy():
p = PreparedRequest()
p.prepare(