1
"""Tests for Requests."""
13
from unittest import mock
17
from urllib3.util import Timeout as Urllib3Timeout
20
from requests.adapters import HTTPAdapter
21
from requests.auth import HTTPDigestAuth, _basic_auth_str
22
from requests.compat import (
32
from requests.cookies import cookiejar_from_dict, morsel_to_cookie
33
from requests.exceptions import (
49
UnrewindableBodyError,
51
from requests.exceptions import SSLError as RequestsSSLError
52
from requests.hooks import default_hooks
53
from requests.models import PreparedRequest, urlencode
54
from requests.sessions import SessionRedirectMixin
55
from requests.structures import CaseInsensitiveDict
57
from . import SNIMissingWarning
58
from .compat import StringIO
59
from .testserver.server import TLSServer, consume_socket_content
60
from .utils import override_environ
57
from . import SNIMissingWarning
58
from .compat import StringIO
59
from .testserver.server import TLSServer, consume_socket_content
60
from .utils import override_environ
62
# Requests to this URL should always fail with a connection timeout (nothing
63
# listening on that port)
64
TARPIT = "http://10.255.255.1"
66
# This is to avoid waiting the timeout of using TARPIT
67
INVALID_PROXY = "http://localhost:1"
70
from ssl import SSLContext
75
HAS_MODERN_SSL = False
85
digest_auth_algo = ("MD5", "SHA-256", "SHA-512")
87
def test_entry_points(self):
89
requests.session().get
90
requests.session().head
96
# Not really an entry point, but people rely on it.
97
from requests.packages.urllib3.poolmanager import PoolManager # noqa:F401
99
@pytest.mark.parametrize(
102
(MissingSchema, "hiwpefhipowhefopw"),
103
(InvalidSchema, "localhost:3128"),
104
(InvalidSchema, "localhost.localdomain:3128/"),
105
(InvalidSchema, "10.122.1.1:3128/"),
106
(InvalidURL, "http://"),
107
(InvalidURL, "http://*example.com"),
108
(InvalidURL, "http://.example.com"),
111
def test_invalid_url(self, exception, url):
112
with pytest.raises(exception):
111
def test_invalid_url(self, exception, url):
112
with pytest.raises(exception):
115
def test_basic_building(self):
116
req = requests.Request()
117
req.url = "http://kennethreitz.org/"
118
req.data = {"life": "42"}
121
assert pr.url == req.url
122
assert pr.body == "life=42"
124
@pytest.mark.parametrize("method", ("GET", "HEAD"))
125
def test_no_content_length(self, httpbin, method):
126
req = requests.Request(method, httpbin(method.lower())).prepare()
127
assert "Content-Length" not in req.headers
129
@pytest.mark.parametrize("method", ("POST", "PUT", "PATCH", "OPTIONS"))
130
def test_no_body_content_length(self, httpbin, method):
131
req = requests.Request(method, httpbin(method.lower())).prepare()
132
assert req.headers["Content-Length"] == "0"
134
@pytest.mark.parametrize("method", ("POST", "PUT", "PATCH", "OPTIONS"))
135
def test_empty_content_length(self, httpbin, method):
136
req = requests.Request(method, httpbin(method.lower()), data="").prepare()
137
assert req.headers["Content-Length"] == "0"
139
def test_override_content_length(self, httpbin):
140
headers = {"Content-Length": "not zero"}
141
r = requests.Request("POST", httpbin("post"), headers=headers).prepare()
142
assert "Content-Length" in r.headers
143
assert r.headers["Content-Length"] == "not zero"
139
def test_override_content_length(self, httpbin):
140
headers = {"Content-Length": "not zero"}
141
r = requests.Request("POST", httpbin("post"), headers=headers).prepare()
142
assert "Content-Length" in r.headers
143
assert r.headers["Content-Length"] == "not zero"
145
def test_path_is_not_double_encoded(self):
146
request = requests.Request("GET", "http://0.0.0.0/get/test case").prepare()
148
assert request.path_url == "/get/test%20case"
150
@pytest.mark.parametrize(
154
"http://example.com/path#fragment",
155
"http://example.com/path?a=b#fragment",
158
"http://example.com/path?key=value#fragment",
159
"http://example.com/path?key=value&a=b#fragment",
163
def test_params_are_added_before_fragment(self, url, expected):
164
request = requests.Request("GET", url, params={"a": "b"}).prepare()
165
assert request.url == expected
167
def test_params_original_order_is_preserved_by_default(self):
168
param_ordered_dict = collections.OrderedDict(
169
(("z", 1), ("a", 1), ("k", 1), ("d", 1))
171
session = requests.Session()
172
request = requests.Request(
173
"GET", "http://example.com/", params=param_ordered_dict
175
prep = session.prepare_request(request)
176
assert prep.url == "http://example.com/?z=1&a=1&k=1&d=1"
172
request = requests.Request(
173
"GET", "http://example.com/", params=param_ordered_dict
175
prep = session.prepare_request(request)
176
assert prep.url == "http://example.com/?z=1&a=1&k=1&d=1"
178
def test_params_bytes_are_encoded(self):
179
request = requests.Request(
180
"GET", "http://example.com", params=b"test=foo"
182
assert request.url == "http://example.com/?test=foo"
184
def test_binary_put(self):
185
request = requests.Request(
186
"PUT", "http://example.com", data="ööö".encode()
188
assert isinstance(request.body, bytes)
190
def test_whitespaces_are_removed_from_url(self):
191
# Test for issue #3696
192
request = requests.Request("GET", " http://example.com").prepare()
193
assert request.url == "http://example.com/"
195
@pytest.mark.parametrize("scheme", ("http://", "HTTP://", "hTTp://", "HttP://"))
196
def test_mixed_case_scheme_acceptable(self, httpbin, scheme):
197
s = requests.Session()
198
s.proxies = getproxies()
199
parts = urlparse(httpbin("get"))
200
url = scheme + parts.netloc + parts.path
201
r = requests.Request("GET", url)
202
r = s.send(r.prepare())
203
assert r.status_code == 200, f"failed for scheme {scheme}"
205
def test_HTTP_200_OK_GET_ALTERNATIVE(self, httpbin):
206
r = requests.Request("GET", httpbin("get"))
207
s = requests.Session()
208
s.proxies = getproxies()
205
def test_HTTP_200_OK_GET_ALTERNATIVE(self, httpbin):
206
r = requests.Request("GET", httpbin("get"))
207
s = requests.Session()
208
s.proxies = getproxies()
210
r = s.send(r.prepare())
212
assert r.status_code == 200
214
def test_HTTP_302_ALLOW_REDIRECT_GET(self, httpbin):
215
r = requests.get(httpbin("redirect", "1"))
216
assert r.status_code == 200
217
assert r.history[0].status_code == 302
218
assert r.history[0].is_redirect
220
def test_redirect_history_no_self_reference(self, httpbin):
221
r = requests.get(httpbin("redirect", "3"))
222
assert r.status_code == 200
223
assert len(r.history) == 3
224
for i, resp in enumerate(r.history):
225
assert resp not in resp.history
226
assert resp.history == r.history[:i]
228
def test_HTTP_307_ALLOW_REDIRECT_POST(self, httpbin):
230
httpbin("redirect-to"),
232
params={"url": "post", "status_code": 307},
234
assert r.status_code == 200
235
assert r.history[0].status_code == 307
236
assert r.history[0].is_redirect
237
assert r.json()["data"] == "test"
239
def test_HTTP_307_ALLOW_REDIRECT_POST_WITH_SEEKABLE(self, httpbin):
242
httpbin("redirect-to"),
243
data=io.BytesIO(byte_str),
244
params={"url": "post", "status_code": 307},
246
assert r.status_code == 200
247
assert r.history[0].status_code == 307
248
assert r.history[0].is_redirect
249
assert r.json()["data"] == byte_str.decode("utf-8")
246
assert r.status_code == 200
247
assert r.history[0].status_code == 307
248
assert r.history[0].is_redirect
249
assert r.json()["data"] == byte_str.decode("utf-8")
251
def test_HTTP_302_TOO_MANY_REDIRECTS(self, httpbin):
253
requests.get(httpbin("relative-redirect", "50"))
254
except TooManyRedirects as e:
255
url = httpbin("relative-redirect", "20")
256
assert e.request.url == url
257
assert e.response.url == url
258
assert len(e.response.history) == 30
260
pytest.fail("Expected redirect to raise TooManyRedirects but it did not")
262
def test_HTTP_302_TOO_MANY_REDIRECTS_WITH_PARAMS(self, httpbin):
263
s = requests.session()
266
s.get(httpbin("relative-redirect", "50"))
267
except TooManyRedirects as e:
268
url = httpbin("relative-redirect", "45")
269
assert e.request.url == url
270
assert e.response.url == url
271
assert len(e.response.history) == 5
274
"Expected custom max number of redirects to be respected but was not"
277
def test_http_301_changes_post_to_get(self, httpbin):
278
r = requests.post(httpbin("status", "301"))
279
assert r.status_code == 200
280
assert r.request.method == "GET"
281
assert r.history[0].status_code == 301
282
assert r.history[0].is_redirect
278
r = requests.post(httpbin("status", "301"))
279
assert r.status_code == 200
280
assert r.request.method == "GET"
281
assert r.history[0].status_code == 301
282
assert r.history[0].is_redirect
284
def test_http_301_doesnt_change_head_to_get(self, httpbin):
285
r = requests.head(httpbin("status", "301"), allow_redirects=True)
287
assert r.status_code == 200
288
assert r.request.method == "HEAD"
289
assert r.history[0].status_code == 301
290
assert r.history[0].is_redirect
292
def test_http_302_changes_post_to_get(self, httpbin):
293
r = requests.post(httpbin("status", "302"))
294
assert r.status_code == 200
295
assert r.request.method == "GET"
296
assert r.history[0].status_code == 302
297
assert r.history[0].is_redirect
299
def test_http_302_doesnt_change_head_to_get(self, httpbin):
300
r = requests.head(httpbin("status", "302"), allow_redirects=True)
301
assert r.status_code == 200
302
assert r.request.method == "HEAD"
303
assert r.history[0].status_code == 302
304
assert r.history[0].is_redirect
306
def test_http_303_changes_post_to_get(self, httpbin):
307
r = requests.post(httpbin("status", "303"))
308
assert r.status_code == 200
309
assert r.request.method == "GET"
310
assert r.history[0].status_code == 303
311
assert r.history[0].is_redirect
313
def test_http_303_doesnt_change_head_to_get(self, httpbin):
314
r = requests.head(httpbin("status", "303"), allow_redirects=True)
315
assert r.status_code == 200
316
assert r.request.method == "HEAD"
317
assert r.history[0].status_code == 303
318
assert r.history[0].is_redirect
314
r = requests.head(httpbin("status", "303"), allow_redirects=True)
315
assert r.status_code == 200
316
assert r.request.method == "HEAD"
317
assert r.history[0].status_code == 303
318
assert r.history[0].is_redirect
320
def test_header_and_body_removal_on_redirect(self, httpbin):
321
purged_headers = ("Content-Length", "Content-Type")
322
ses = requests.Session()
323
req = requests.Request("POST", httpbin("post"), data={"test": "data"})
324
prep = ses.prepare_request(req)
325
resp = ses.send(prep)
327
# Mimic a redirect response
328
resp.status_code = 302
329
resp.headers["location"] = "get"
331
# Run request through resolve_redirects
332
next_resp = next(ses.resolve_redirects(resp, prep))
333
assert next_resp.request.body is None
334
for header in purged_headers:
335
assert header not in next_resp.request.headers
337
def test_transfer_enc_removal_on_redirect(self, httpbin):
338
purged_headers = ("Transfer-Encoding", "Content-Type")
339
ses = requests.Session()
340
req = requests.Request("POST", httpbin("post"), data=(b"x" for x in range(1)))
341
prep = ses.prepare_request(req)
342
assert "Transfer-Encoding" in prep.headers
344
# Create Response to avoid https://github.com/kevin1024/pytest-httpbin/issues/33
345
resp = requests.Response()
346
resp.raw = io.BytesIO(b"the content")
348
setattr(resp.raw, "release_conn", lambda *args: args)
344
# Create Response to avoid https://github.com/kevin1024/pytest-httpbin/issues/33
345
resp = requests.Response()
346
resp.raw = io.BytesIO(b"the content")
348
setattr(resp.raw, "release_conn", lambda *args: args)
350
# Mimic a redirect response
351
resp.status_code = 302
352
resp.headers["location"] = httpbin("get")
354
# Run request through resolve_redirect
355
next_resp = next(ses.resolve_redirects(resp, prep))
356
assert next_resp.request.body is None
357
for header in purged_headers:
358
assert header not in next_resp.request.headers
360
def test_fragment_maintained_on_redirect(self, httpbin):
361
fragment = "#view=edit&token=hunter2"
362
r = requests.get(httpbin("redirect-to?url=get") + fragment)
364
assert len(r.history) > 0
365
assert r.history[0].request.url == httpbin("redirect-to?url=get") + fragment
366
assert r.url == httpbin("get") + fragment
368
def test_HTTP_200_OK_GET_WITH_PARAMS(self, httpbin):
369
heads = {"User-agent": "Mozilla/5.0"}
371
r = requests.get(httpbin("user-agent"), headers=heads)
373
assert heads["User-agent"] in r.text
374
assert r.status_code == 200
376
def test_HTTP_200_OK_GET_WITH_MIXED_PARAMS(self, httpbin):
377
heads = {"User-agent": "Mozilla/5.0"}
380
httpbin("get") + "?test=true", params={"q": "test"}, headers=heads
382
assert r.status_code == 200
380
httpbin("get") + "?test=true", params={"q": "test"}, headers=heads
382
assert r.status_code == 200
384
def test_set_cookie_on_301(self, httpbin):
385
s = requests.session()
386
url = httpbin("cookies/set?foo=bar")
388
assert s.cookies["foo"] == "bar"
390
def test_cookie_sent_on_redirect(self, httpbin):
391
s = requests.session()
392
s.get(httpbin("cookies/set?foo=bar"))
393
r = s.get(httpbin("redirect/1")) # redirects to httpbin('get')
394
assert "Cookie" in r.json()["headers"]
396
def test_cookie_removed_on_expire(self, httpbin):
397
s = requests.session()
398
s.get(httpbin("cookies/set?foo=bar"))
399
assert s.cookies["foo"] == "bar"
401
httpbin("response-headers"),
402
params={"Set-Cookie": "foo=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT"},
404
assert "foo" not in s.cookies
406
def test_cookie_quote_wrapped(self, httpbin):
407
s = requests.session()
408
s.get(httpbin('cookies/set?foo="bar:baz"'))
409
assert s.cookies["foo"] == '"bar:baz"'
411
def test_cookie_persists_via_api(self, httpbin):
412
s = requests.session()
413
r = s.get(httpbin("redirect/1"), cookies={"foo": "bar"})
414
assert "foo" in r.request.headers["Cookie"]
415
assert "foo" in r.history[0].request.headers["Cookie"]
417
def test_request_cookie_overrides_session_cookie(self, httpbin):
418
s = requests.session()
419
s.cookies["foo"] = "bar"
420
r = s.get(httpbin("cookies"), cookies={"foo": "baz"})
421
assert r.json()["cookies"]["foo"] == "baz"
422
# Session cookie should not be modified
423
assert s.cookies["foo"] == "bar"
419
s.cookies["foo"] = "bar"
420
r = s.get(httpbin("cookies"), cookies={"foo": "baz"})
421
assert r.json()["cookies"]["foo"] == "baz"
422
# Session cookie should not be modified
423
assert s.cookies["foo"] == "bar"
425
def test_request_cookies_not_persisted(self, httpbin):
426
s = requests.session()
427
s.get(httpbin("cookies"), cookies={"foo": "baz"})
428
# Sending a request with cookies should not add cookies to the session
431
def test_generic_cookiejar_works(self, httpbin):
432
cj = cookielib.CookieJar()
433
cookiejar_from_dict({"foo": "bar"}, cj)
434
s = requests.session()
436
r = s.get(httpbin("cookies"))
437
# Make sure the cookie was sent
438
assert r.json()["cookies"]["foo"] == "bar"
439
# Make sure the session cj is still the custom one
440
assert s.cookies is cj
442
def test_param_cookiejar_works(self, httpbin):
443
cj = cookielib.CookieJar()
444
cookiejar_from_dict({"foo": "bar"}, cj)
445
s = requests.session()
446
r = s.get(httpbin("cookies"), cookies=cj)
447
# Make sure the cookie was sent
448
assert r.json()["cookies"]["foo"] == "bar"
450
def test_cookielib_cookiejar_on_redirect(self, httpbin):
451
"""Tests resolve_redirect doesn't fail when merging cookies
452
with non-RequestsCookieJar cookiejar.
456
cj = cookiejar_from_dict({"foo": "bar"}, cookielib.CookieJar())
457
s = requests.Session()
458
s.cookies = cookiejar_from_dict({"cookie": "tasty"})
456
cj = cookiejar_from_dict({"foo": "bar"}, cookielib.CookieJar())
457
s = requests.Session()
458
s.cookies = cookiejar_from_dict({"cookie": "tasty"})
460
# Prepare request without using Session
461
req = requests.Request("GET", httpbin("headers"), cookies=cj)
462
prep_req = req.prepare()
464
# Send request and simulate redirect
465
resp = s.send(prep_req)
466
resp.status_code = 302
467
resp.headers["location"] = httpbin("get")
468
redirects = s.resolve_redirects(resp, prep_req)
469
resp = next(redirects)
471
# Verify CookieJar isn't being converted to RequestsCookieJar
472
assert isinstance(prep_req._cookies, cookielib.CookieJar)
473
assert isinstance(resp.request._cookies, cookielib.CookieJar)
474
assert not isinstance(resp.request._cookies, requests.cookies.RequestsCookieJar)
477
for c in resp.request._cookies:
478
cookies[c.name] = c.value
479
assert cookies["foo"] == "bar"
480
assert cookies["cookie"] == "tasty"
482
def test_requests_in_history_are_not_overridden(self, httpbin):
483
resp = requests.get(httpbin("redirect/3"))
484
urls = [r.url for r in resp.history]
485
req_urls = [r.request.url for r in resp.history]
486
assert urls == req_urls
488
def test_history_is_always_a_list(self, httpbin):
489
"""Show that even with redirects, Response.history is always a list."""
490
resp = requests.get(httpbin("get"))
491
assert isinstance(resp.history, list)
492
resp = requests.get(httpbin("redirect/1"))
493
assert isinstance(resp.history, list)
494
assert not isinstance(resp.history, tuple)
490
resp = requests.get(httpbin("get"))
491
assert isinstance(resp.history, list)
492
resp = requests.get(httpbin("redirect/1"))
493
assert isinstance(resp.history, list)
494
assert not isinstance(resp.history, tuple)
496
def test_headers_on_session_with_None_are_not_sent(self, httpbin):
497
"""Do not send headers in Session.headers with None values."""
498
ses = requests.Session()
499
ses.headers["Accept-Encoding"] = None
500
req = requests.Request("GET", httpbin("get"))
501
prep = ses.prepare_request(req)
502
assert "Accept-Encoding" not in prep.headers
504
def test_headers_preserve_order(self, httpbin):
505
"""Preserve order when headers provided as OrderedDict."""
506
ses = requests.Session()
507
ses.headers = collections.OrderedDict()
508
ses.headers["Accept-Encoding"] = "identity"
509
ses.headers["First"] = "1"
510
ses.headers["Second"] = "2"
511
headers = collections.OrderedDict([("Third", "3"), ("Fourth", "4")])
512
headers["Fifth"] = "5"
513
headers["Second"] = "222"
514
req = requests.Request("GET", httpbin("get"), headers=headers)
515
prep = ses.prepare_request(req)
516
items = list(prep.headers.items())
517
assert items[0] == ("Accept-Encoding", "identity")
518
assert items[1] == ("First", "1")
519
assert items[2] == ("Second", "222")
520
assert items[3] == ("Third", "3")
521
assert items[4] == ("Fourth", "4")
522
assert items[5] == ("Fifth", "5")
518
assert items[1] == ("First", "1")
519
assert items[2] == ("Second", "222")
520
assert items[3] == ("Third", "3")
521
assert items[4] == ("Fourth", "4")
522
assert items[5] == ("Fifth", "5")
524
@pytest.mark.parametrize("key", ("User-agent", "user-agent"))
525
def test_user_agent_transfers(self, httpbin, key):
526
heads = {key: "Mozilla/5.0 (github.com/psf/requests)"}
528
r = requests.get(httpbin("user-agent"), headers=heads)
529
assert heads[key] in r.text
531
def test_HTTP_200_OK_HEAD(self, httpbin):
532
r = requests.head(httpbin("get"))
533
assert r.status_code == 200
535
def test_HTTP_200_OK_PUT(self, httpbin):
536
r = requests.put(httpbin("put"))
537
assert r.status_code == 200
539
def test_BASICAUTH_TUPLE_HTTP_200_OK_GET(self, httpbin):
540
auth = ("user", "pass")
541
url = httpbin("basic-auth", "user", "pass")
543
r = requests.get(url, auth=auth)
544
assert r.status_code == 200
546
r = requests.get(url)
547
assert r.status_code == 401
549
s = requests.session()
552
assert r.status_code == 200
554
@pytest.mark.parametrize(
555
"username, password",
558
("имя".encode(), "пароль".encode()),
563
def test_set_basicauth(self, httpbin, username, password):
564
auth = (username, password)
563
def test_set_basicauth(self, httpbin, username, password):
564
auth = (username, password)
567
r = requests.Request("GET", url, auth=auth)
570
assert p.headers["Authorization"] == _basic_auth_str(username, password)
572
def test_basicauth_encodes_byte_strings(self):
573
"""Ensure b'test' formats as the byte string "test" rather
574
than the unicode string "b'test'" in Python 3.
576
auth = (b"\xc5\xafsername", b"test\xc6\xb6")
577
r = requests.Request("GET", "http://localhost", auth=auth)
580
assert p.headers["Authorization"] == "Basic xa9zZXJuYW1lOnRlc3TGtg=="
582
@pytest.mark.parametrize(
585
# Connecting to an unknown domain should raise a ConnectionError
586
("http://doesnotexist.google.com", ConnectionError),
587
# Connecting to an invalid port should raise a ConnectionError
588
("http://localhost:1", ConnectionError),
589
# Inputting a URL that cannot be parsed should raise an InvalidURL error
590
("http://fe80::5054:ff:fe5a:fc0", InvalidURL),
593
def test_errors(self, url, exception):
594
with pytest.raises(exception):
595
requests.get(url, timeout=1)
597
def test_proxy_error(self):
598
# any proxy related error (address resolution, no route to host, etc) should result in a ProxyError
599
with pytest.raises(ProxyError):
601
"http://localhost:1", proxies={"http": "non-resolvable-address"}
598
# any proxy related error (address resolution, no route to host, etc) should result in a ProxyError
599
with pytest.raises(ProxyError):
601
"http://localhost:1", proxies={"http": "non-resolvable-address"}
604
def test_proxy_error_on_bad_url(self, httpbin, httpbin_secure):
605
with pytest.raises(InvalidProxyURL):
606
requests.get(httpbin_secure(), proxies={"https": "http:/badproxyurl:3128"})
608
with pytest.raises(InvalidProxyURL):
609
requests.get(httpbin(), proxies={"http": "http://:8080"})
611
with pytest.raises(InvalidProxyURL):
612
requests.get(httpbin_secure(), proxies={"https": "https://"})
614
with pytest.raises(InvalidProxyURL):
615
requests.get(httpbin(), proxies={"http": "http:///example.com:8080"})
617
def test_respect_proxy_env_on_send_self_prepared_request(self, httpbin):
618
with override_environ(http_proxy=INVALID_PROXY):
619
with pytest.raises(ProxyError):
620
session = requests.Session()
621
request = requests.Request("GET", httpbin())
622
session.send(request.prepare())
624
def test_respect_proxy_env_on_send_session_prepared_request(self, httpbin):
625
with override_environ(http_proxy=INVALID_PROXY):
626
with pytest.raises(ProxyError):
627
session = requests.Session()
628
request = requests.Request("GET", httpbin())
629
prepared = session.prepare_request(request)
630
session.send(prepared)
626
with pytest.raises(ProxyError):
627
session = requests.Session()
628
request = requests.Request("GET", httpbin())
629
prepared = session.prepare_request(request)
630
session.send(prepared)
632
def test_respect_proxy_env_on_send_with_redirects(self, httpbin):
633
with override_environ(http_proxy=INVALID_PROXY):
634
with pytest.raises(ProxyError):
635
session = requests.Session()
636
url = httpbin("redirect/1")
638
request = requests.Request("GET", url)
639
session.send(request.prepare())
641
def test_respect_proxy_env_on_get(self, httpbin):
642
with override_environ(http_proxy=INVALID_PROXY):
643
with pytest.raises(ProxyError):
644
session = requests.Session()
645
session.get(httpbin())
647
def test_respect_proxy_env_on_request(self, httpbin):
648
with override_environ(http_proxy=INVALID_PROXY):
649
with pytest.raises(ProxyError):
650
session = requests.Session()
651
session.request(method="GET", url=httpbin())
653
def test_proxy_authorization_preserved_on_request(self, httpbin):
654
proxy_auth_value = "Bearer XXX"
655
session = requests.Session()
656
session.headers.update({"Proxy-Authorization": proxy_auth_value})
657
resp = session.request(method="GET", url=httpbin("get"))
658
sent_headers = resp.json().get("headers", {})
654
proxy_auth_value = "Bearer XXX"
655
session = requests.Session()
656
session.headers.update({"Proxy-Authorization": proxy_auth_value})
657
resp = session.request(method="GET", url=httpbin("get"))
658
sent_headers = resp.json().get("headers", {})
660
assert sent_headers.get("Proxy-Authorization") == proxy_auth_value
662
@pytest.mark.parametrize(
663
"url,has_proxy_auth",
665
("http://example.com", True),
666
("https://example.com", False),
669
def test_proxy_authorization_not_appended_to_https_request(
670
self, url, has_proxy_auth
672
session = requests.Session()
674
"http": "http://test:pass@localhost:8080",
675
"https": "http://test:pass@localhost:8090",
677
req = requests.Request("GET", url)
679
session.rebuild_proxies(prep, proxies)
681
assert ("Proxy-Authorization" in prep.headers) is has_proxy_auth
683
def test_basicauth_with_netrc(self, httpbin):
684
auth = ("user", "pass")
685
wrong_auth = ("wronguser", "wrongpass")
686
url = httpbin("basic-auth", "user", "pass")
688
old_auth = requests.sessions.get_netrc_auth
692
def get_netrc_auth_mock(url):
695
requests.sessions.get_netrc_auth = get_netrc_auth_mock
697
# Should use netrc and work.
698
r = requests.get(url)
699
assert r.status_code == 200
695
requests.sessions.get_netrc_auth = get_netrc_auth_mock
697
# Should use netrc and work.
698
r = requests.get(url)
699
assert r.status_code == 200
701
# Given auth should override and fail.
702
r = requests.get(url, auth=wrong_auth)
703
assert r.status_code == 401
705
s = requests.session()
707
# Should use netrc and work.
709
assert r.status_code == 200
711
# Given auth should override and fail.
714
assert r.status_code == 401
716
requests.sessions.get_netrc_auth = old_auth
718
def test_basicauth_with_netrc_leak(self, httpbin):
719
url1 = httpbin("basic-auth", "user", "pass")
720
url = url1[len("http://") :]
721
domain = url.split(":")[0]
722
url = f"http://example.com:@{url}"
725
with tempfile.NamedTemporaryFile(mode="w", delete=False) as fp:
726
fp.write("machine example.com\n")
727
fp.write("login wronguser\n")
728
fp.write("password wrongpass\n")
729
fp.write(f"machine {domain}\n")
730
fp.write("login user\n")
731
fp.write("password pass\n")
735
old_netrc = os.environ.get("NETRC", "")
736
os.environ["NETRC"] = netrc_file
735
old_netrc = os.environ.get("NETRC", "")
736
os.environ["NETRC"] = netrc_file
740
# Make sure that we don't use the example.com credentials
742
r = requests.get(url)
743
assert r.status_code == 200
745
os.environ["NETRC"] = old_netrc
746
os.unlink(netrc_file)
748
def test_DIGEST_HTTP_200_OK_GET(self, httpbin):
749
for authtype in self.digest_auth_algo:
750
auth = HTTPDigestAuth("user", "pass")
751
url = httpbin("digest-auth", "auth", "user", "pass", authtype, "never")
753
r = requests.get(url, auth=auth)
754
assert r.status_code == 200
756
r = requests.get(url)
757
assert r.status_code == 401
758
print(r.headers["WWW-Authenticate"])
760
s = requests.session()
761
s.auth = HTTPDigestAuth("user", "pass")
763
assert r.status_code == 200
765
def test_DIGEST_AUTH_RETURNS_COOKIE(self, httpbin):
766
for authtype in self.digest_auth_algo:
767
url = httpbin("digest-auth", "auth", "user", "pass", authtype)
768
auth = HTTPDigestAuth("user", "pass")
769
r = requests.get(url)
770
assert r.cookies["fake"] == "fake_value"
772
r = requests.get(url, auth=auth)
773
assert r.status_code == 200
769
r = requests.get(url)
770
assert r.cookies["fake"] == "fake_value"
772
r = requests.get(url, auth=auth)
773
assert r.status_code == 200
775
def test_DIGEST_AUTH_SETS_SESSION_COOKIES(self, httpbin):
776
for authtype in self.digest_auth_algo:
777
url = httpbin("digest-auth", "auth", "user", "pass", authtype)
778
auth = HTTPDigestAuth("user", "pass")
779
s = requests.Session()
780
s.get(url, auth=auth)
781
assert s.cookies["fake"] == "fake_value"
783
def test_DIGEST_STREAM(self, httpbin):
784
for authtype in self.digest_auth_algo:
785
auth = HTTPDigestAuth("user", "pass")
786
url = httpbin("digest-auth", "auth", "user", "pass", authtype)
788
r = requests.get(url, auth=auth, stream=True)
789
assert r.raw.read() != b""
791
r = requests.get(url, auth=auth, stream=False)
792
assert r.raw.read() == b""
794
def test_DIGESTAUTH_WRONG_HTTP_401_GET(self, httpbin):
795
for authtype in self.digest_auth_algo:
796
auth = HTTPDigestAuth("user", "wrongpass")
797
url = httpbin("digest-auth", "auth", "user", "pass", authtype)
799
r = requests.get(url, auth=auth)
800
assert r.status_code == 401
802
r = requests.get(url)
803
assert r.status_code == 401
805
s = requests.session()
808
assert r.status_code == 401
805
s = requests.session()
808
assert r.status_code == 401
810
def test_DIGESTAUTH_QUOTES_QOP_VALUE(self, httpbin):
811
for authtype in self.digest_auth_algo:
812
auth = HTTPDigestAuth("user", "pass")
813
url = httpbin("digest-auth", "auth", "user", "pass", authtype)
815
r = requests.get(url, auth=auth)
816
assert '"auth"' in r.request.headers["Authorization"]
818
def test_POSTBIN_GET_POST_FILES(self, httpbin):
819
url = httpbin("post")
820
requests.post(url).raise_for_status()
822
post1 = requests.post(url, data={"some": "data"})
823
assert post1.status_code == 200
825
with open("requirements-dev.txt") as f:
826
post2 = requests.post(url, files={"some": f})
827
assert post2.status_code == 200
829
post4 = requests.post(url, data='[{"some": "json"}]')
830
assert post4.status_code == 200
832
with pytest.raises(ValueError):
833
requests.post(url, files=["bad file data"])
835
def test_invalid_files_input(self, httpbin):
836
url = httpbin("post")
837
post = requests.post(url, files={"random-file-1": None, "random-file-2": 1})
838
assert b'name="random-file-1"' not in post.request.body
839
assert b'name="random-file-2"' in post.request.body
841
def test_POSTBIN_SEEKED_OBJECT_WITH_NO_ITER(self, httpbin):
843
def __init__(self, data):
844
self.data = data.encode()
845
self.length = len(self.data)
843
def __init__(self, data):
844
self.data = data.encode()
845
self.length = len(self.data)
851
def read(self, size=None):
853
ret = self.data[self.index : self.index + size]
856
ret = self.data[self.index :]
857
self.index = self.length
863
def seek(self, offset, where=0):
869
self.index = self.length + offset
871
test = TestStream("test")
872
post1 = requests.post(httpbin("post"), data=test)
873
assert post1.status_code == 200
874
assert post1.json()["data"] == "test"
876
test = TestStream("test")
878
post2 = requests.post(httpbin("post"), data=test)
879
assert post2.status_code == 200
880
assert post2.json()["data"] == "st"
882
def test_POSTBIN_GET_POST_FILES_WITH_DATA(self, httpbin):
883
url = httpbin("post")
884
requests.post(url).raise_for_status()
880
assert post2.json()["data"] == "st"
882
def test_POSTBIN_GET_POST_FILES_WITH_DATA(self, httpbin):
883
url = httpbin("post")
884
requests.post(url).raise_for_status()
886
post1 = requests.post(url, data={"some": "data"})
887
assert post1.status_code == 200
889
with open("requirements-dev.txt") as f:
890
post2 = requests.post(url, data={"some": "data"}, files={"some": f})
891
assert post2.status_code == 200
893
post4 = requests.post(url, data='[{"some": "json"}]')
894
assert post4.status_code == 200
896
with pytest.raises(ValueError):
897
requests.post(url, files=["bad file data"])
899
def test_post_with_custom_mapping(self, httpbin):
900
class CustomMapping(MutableMapping):
901
def __init__(self, *args, **kwargs):
902
self.data = dict(*args, **kwargs)
904
def __delitem__(self, key):
907
def __getitem__(self, key):
908
return self.data[key]
910
def __setitem__(self, key, value):
911
self.data[key] = value
914
return iter(self.data)
917
return len(self.data)
919
data = CustomMapping({"some": "data"})
920
url = httpbin("post")
921
found_json = requests.post(url, data=data).json().get("form")
922
assert found_json == {"some": "data"}
919
data = CustomMapping({"some": "data"})
920
url = httpbin("post")
921
found_json = requests.post(url, data=data).json().get("form")
922
assert found_json == {"some": "data"}
924
def test_conflicting_post_params(self, httpbin):
925
url = httpbin("post")
926
with open("requirements-dev.txt") as f:
927
with pytest.raises(ValueError):
928
requests.post(url, data='[{"some": "data"}]', files={"some": f})
930
def test_request_ok_set(self, httpbin):
931
r = requests.get(httpbin("status", "404"))
934
def test_status_raising(self, httpbin):
935
r = requests.get(httpbin("status", "404"))
936
with pytest.raises(requests.exceptions.HTTPError):
939
r = requests.get(httpbin("status", "500"))
942
def test_decompress_gzip(self, httpbin):
943
r = requests.get(httpbin("gzip"))
944
r.content.decode("ascii")
946
@pytest.mark.parametrize(
949
("/get", {"foo": "føø"}),
950
("/get", {"føø": "føø"}),
951
("/get", {"føø": "føø"}),
952
("/get", {"foo": "foo"}),
953
("ø", {"foo": "foo"}),
956
def test_unicode_get(self, httpbin, url, params):
957
requests.get(httpbin(url), params=params)
959
def test_unicode_header_name(self, httpbin):
962
headers={"Content-Type": "application/octet-stream"},
964
) # compat.str is unicode.
962
headers={"Content-Type": "application/octet-stream"},
964
) # compat.str is unicode.
966
def test_pyopenssl_redirect(self, httpbin_secure, httpbin_ca_bundle):
967
requests.get(httpbin_secure("status", "301"), verify=httpbin_ca_bundle)
969
def test_invalid_ca_certificate_path(self, httpbin_secure):
970
INVALID_PATH = "/garbage"
971
with pytest.raises(IOError) as e:
972
requests.get(httpbin_secure(), verify=INVALID_PATH)
975
== f"Could not find a suitable TLS CA certificate bundle, invalid path: {INVALID_PATH}"
978
def test_invalid_ssl_certificate_files(self, httpbin_secure):
979
INVALID_PATH = "/garbage"
980
with pytest.raises(IOError) as e:
981
requests.get(httpbin_secure(), cert=INVALID_PATH)
984
== f"Could not find the TLS certificate file, invalid path: {INVALID_PATH}"
987
with pytest.raises(IOError) as e:
988
requests.get(httpbin_secure(), cert=(".", INVALID_PATH))
989
assert str(e.value) == (
990
f"Could not find the TLS key file, invalid path: {INVALID_PATH}"
993
@pytest.mark.parametrize(
997
({"REQUESTS_CA_BUNDLE": "/some/path"}, "/some/path"),
998
({"REQUESTS_CA_BUNDLE": ""}, True),
999
({"CURL_CA_BUNDLE": "/some/path"}, "/some/path"),
1000
({"CURL_CA_BUNDLE": ""}, True),
1001
({"REQUESTS_CA_BUNDLE": "", "CURL_CA_BUNDLE": ""}, True),
1004
"REQUESTS_CA_BUNDLE": "/some/path",
1005
"CURL_CA_BUNDLE": "/curl/path",
1011
"REQUESTS_CA_BUNDLE": "",
1012
"CURL_CA_BUNDLE": "/curl/path",
1018
def test_env_cert_bundles(self, httpbin, env, expected):
1019
s = requests.Session()
1020
with mock.patch("os.environ", env):
1021
settings = s.merge_environment_settings(
1022
url=httpbin("get"), proxies={}, stream=False, verify=True, cert=None
1024
assert settings["verify"] == expected
1020
with mock.patch("os.environ", env):
1021
settings = s.merge_environment_settings(
1022
url=httpbin("get"), proxies={}, stream=False, verify=True, cert=None
1024
assert settings["verify"] == expected
1026
def test_http_with_certificate(self, httpbin):
1027
r = requests.get(httpbin(), cert=".")
1028
assert r.status_code == 200
1030
@pytest.mark.skipif(
1031
SNIMissingWarning is None,
1032
reason="urllib3 2.0 removed that warning and errors out instead",
1034
def test_https_warnings(self, nosan_server):
1035
"""warnings are emitted with requests.get"""
1036
host, port, ca_bundle = nosan_server
1037
if HAS_MODERN_SSL or HAS_PYOPENSSL:
1038
warnings_expected = ("SubjectAltNameWarning",)
1040
warnings_expected = (
1041
"SNIMissingWarning",
1042
"InsecurePlatformWarning",
1043
"SubjectAltNameWarning",
1046
with pytest.warns() as warning_records:
1047
warnings.simplefilter("always")
1048
requests.get(f"https://localhost:{port}/", verify=ca_bundle)
1052
for item in warning_records
1053
if item.category.__name__ != "ResourceWarning"
1056
warnings_category = tuple(item.category.__name__ for item in warning_records)
1057
assert warnings_category == warnings_expected
1053
if item.category.__name__ != "ResourceWarning"
1056
warnings_category = tuple(item.category.__name__ for item in warning_records)
1057
assert warnings_category == warnings_expected
1059
def test_certificate_failure(self, httpbin_secure):
1061
When underlying SSL problems occur, an SSLError is raised.
1063
with pytest.raises(RequestsSSLError):
1064
# Our local httpbin does not have a trusted CA, so this call will
1065
# fail if we use our default trust bundle.
1066
requests.get(httpbin_secure("status", "200"))
1068
def test_urlencoded_get_query_multivalued_param(self, httpbin):
1069
r = requests.get(httpbin("get"), params={"test": ["foo", "baz"]})
1070
assert r.status_code == 200
1071
assert r.url == httpbin("get?test=foo&test=baz")
1073
def test_form_encoded_post_query_multivalued_element(self, httpbin):
1074
r = requests.Request(
1075
method="POST", url=httpbin("post"), data=dict(test=["foo", "baz"])
1078
assert prep.body == "test=foo&test=baz"
1080
def test_different_encodings_dont_break_post(self, httpbin):
1081
with open(__file__, "rb") as f:
1084
data={"stuff": json.dumps({"a": 123})},
1085
params={"blah": "asdf1234"},
1086
files={"file": ("test_requests.py", f)},
1088
assert r.status_code == 200
1084
data={"stuff": json.dumps({"a": 123})},
1085
params={"blah": "asdf1234"},
1086
files={"file": ("test_requests.py", f)},
1088
assert r.status_code == 200
1090
def test_post_named_tempfile(self, httpbin):
1091
with tempfile.NamedTemporaryFile(mode="w+") as f:
1092
f.write("named temp file contents\n")
1098
assert r.status_code == 200
1099
assert r.json()["files"]["file"] == "named temp file contents\n"
1101
def test_post_getattr_proxy_read_only(self, httpbin):
1105
self._file = io.BytesIO(b"streamed body")
1107
def __getattr__(self, name):
1108
if name == "__iter__":
1109
raise AttributeError(name)
1110
return getattr(self._file, name)
1112
r = requests.post(httpbin("post"), data=ReadProxy())
1113
assert r.status_code == 200
1114
assert r.json()["data"] == "streamed body"
1116
@pytest.mark.parametrize(
1120
{"stuff": "ëlïxr".encode()},
1122
{"stuff": b"elixr"},
1125
def test_unicode_multipart_post(self, httpbin, data):
1126
with open(__file__, "rb") as f:
1130
files={"file": ("test_requests.py", f)},
1132
assert r.status_code == 200
1130
files={"file": ("test_requests.py", f)},
1132
assert r.status_code == 200
1134
def test_unicode_multipart_post_fieldnames(self, httpbin):
1135
filename = os.path.splitext(__file__)[0] + ".py"
1136
with open(filename, "rb") as f:
1137
r = requests.Request(
1139
url=httpbin("post"),
1140
data={b"stuff": "elixr"},
1141
files={"file": ("test_requests.py", f)},
1145
assert b'name="stuff"' in prep.body
1146
assert b"name=\"b'stuff'\"" not in prep.body
1148
def test_unicode_method_name(self, httpbin):
1149
with open(__file__, "rb") as f:
1151
r = requests.request(
1153
url=httpbin("post"),
1156
assert r.status_code == 200
1158
def test_unicode_method_name_with_request_object(self, httpbin):
1159
s = requests.Session()
1160
with open(__file__, "rb") as f:
1162
req = requests.Request("POST", httpbin("post"), files=files)
1163
prep = s.prepare_request(req)
1164
assert isinstance(prep.method, builtin_str)
1165
assert prep.method == "POST"
1168
assert resp.status_code == 200
1164
assert isinstance(prep.method, builtin_str)
1165
assert prep.method == "POST"
1168
assert resp.status_code == 200
1170
def test_non_prepared_request_error(self):
1171
s = requests.Session()
1172
req = requests.Request("POST", "/")
1174
with pytest.raises(ValueError) as e:
1176
assert str(e.value) == "You can only send PreparedRequests."
1178
def test_custom_content_type(self, httpbin):
1179
with open(__file__, "rb") as f1:
1180
with open(__file__, "rb") as f2:
1181
data = {"stuff": json.dumps({"a": 123})}
1183
"file1": ("test_requests.py", f1),
1184
"file2": ("test_requests", f2, "text/py-content-type"),
1186
r = requests.post(httpbin("post"), data=data, files=files)
1187
assert r.status_code == 200
1188
assert b"text/py-content-type" in r.request.body
1190
def test_hook_receives_request_arguments(self, httpbin):
1191
def hook(resp, **kwargs):
1192
assert resp is not None
1195
s = requests.Session()
1196
r = requests.Request("GET", httpbin(), hooks={"response": hook})
1197
prep = s.prepare_request(r)
1200
def test_session_hooks_are_used_with_no_request_hooks(self, httpbin):
1201
def hook(*args, **kwargs):
1200
def test_session_hooks_are_used_with_no_request_hooks(self, httpbin):
1201
def hook(*args, **kwargs):
1204
s = requests.Session()
1205
s.hooks["response"].append(hook)
1206
r = requests.Request("GET", httpbin())
1207
prep = s.prepare_request(r)
1208
assert prep.hooks["response"] != []
1209
assert prep.hooks["response"] == [hook]
1211
def test_session_hooks_are_overridden_by_request_hooks(self, httpbin):
1212
def hook1(*args, **kwargs):
1215
def hook2(*args, **kwargs):
1218
assert hook1 is not hook2
1219
s = requests.Session()
1220
s.hooks["response"].append(hook2)
1221
r = requests.Request("GET", httpbin(), hooks={"response": [hook1]})
1222
prep = s.prepare_request(r)
1223
assert prep.hooks["response"] == [hook1]
1225
def test_prepared_request_hook(self, httpbin):
1226
def hook(resp, **kwargs):
1227
resp.hook_working = True
1230
req = requests.Request("GET", httpbin(), hooks={"response": hook})
1231
prep = req.prepare()
1233
s = requests.Session()
1234
s.proxies = getproxies()
1237
assert hasattr(resp, "hook_working")
1239
def test_prepared_from_session(self, httpbin):
1240
class DummyAuth(requests.auth.AuthBase):
1241
def __call__(self, r):
1242
r.headers["Dummy-Auth-Test"] = "dummy-auth-test-ok"
1239
def test_prepared_from_session(self, httpbin):
1240
class DummyAuth(requests.auth.AuthBase):
1241
def __call__(self, r):
1242
r.headers["Dummy-Auth-Test"] = "dummy-auth-test-ok"
1245
req = requests.Request("GET", httpbin("headers"))
1248
s = requests.Session()
1249
s.auth = DummyAuth()
1251
prep = s.prepare_request(req)
1254
assert resp.json()["headers"]["Dummy-Auth-Test"] == "dummy-auth-test-ok"
1256
def test_prepare_request_with_bytestring_url(self):
1257
req = requests.Request("GET", b"https://httpbin.org/")
1258
s = requests.Session()
1259
prep = s.prepare_request(req)
1260
assert prep.url == "https://httpbin.org/"
1262
def test_request_with_bytestring_host(self, httpbin):
1263
s = requests.Session()
1266
httpbin("cookies/set?cookie=value"),
1267
allow_redirects=False,
1268
headers={"Host": b"httpbin.org"},
1270
assert resp.cookies.get("cookie") == "value"
1272
def test_links(self):
1273
r = requests.Response()
1275
"cache-control": "public, max-age=60, s-maxage=60",
1276
"connection": "keep-alive",
1277
"content-encoding": "gzip",
1278
"content-type": "application/json; charset=utf-8",
1279
"date": "Sat, 26 Jan 2013 16:47:56 GMT",
1280
"etag": '"6ff6a73c0e446c1f61614769e3ceb778"',
1281
"last-modified": "Sat, 26 Jan 2013 16:22:39 GMT",
1283
"<https://api.github.com/users/kennethreitz/repos?"
1284
'page=2&per_page=10>; rel="next", <https://api.github.'
1285
"com/users/kennethreitz/repos?page=7&per_page=10>; "
1288
"server": "GitHub.com",
1291
"x-content-type-options": "nosniff",
1292
"x-github-media-type": "github.beta",
1293
"x-ratelimit-limit": "60",
1294
"x-ratelimit-remaining": "57",
1296
assert r.links["next"]["rel"] == "next"
1292
"x-github-media-type": "github.beta",
1293
"x-ratelimit-limit": "60",
1294
"x-ratelimit-remaining": "57",
1296
assert r.links["next"]["rel"] == "next"
1298
def test_cookie_parameters(self):
1300
value = "some_value"
1303
rest = {"HttpOnly": True}
1305
jar = requests.cookies.RequestsCookieJar()
1306
jar.set(key, value, secure=secure, domain=domain, rest=rest)
1308
assert len(jar) == 1
1309
assert "some_cookie" in jar
1311
cookie = list(jar)[0]
1312
assert cookie.secure == secure
1313
assert cookie.domain == domain
1314
assert cookie._rest["HttpOnly"] == rest["HttpOnly"]
1316
def test_cookie_as_dict_keeps_len(self):
1318
value = "some_value"
1320
key1 = "some_cookie1"
1321
value1 = "some_value1"
1323
jar = requests.cookies.RequestsCookieJar()
1325
jar.set(key1, value1)
1328
d2 = dict(jar.iteritems())
1329
d3 = dict(jar.items())
1331
assert len(jar) == 2
1336
def test_cookie_as_dict_keeps_items(self):
1338
value = "some_value"
1340
key1 = "some_cookie1"
1341
value1 = "some_value1"
1343
jar = requests.cookies.RequestsCookieJar()
1345
jar.set(key1, value1)
1341
value1 = "some_value1"
1343
jar = requests.cookies.RequestsCookieJar()
1345
jar.set(key1, value1)
1348
d2 = dict(jar.iteritems())
1349
d3 = dict(jar.items())
1351
assert d1["some_cookie"] == "some_value"
1352
assert d2["some_cookie"] == "some_value"
1353
assert d3["some_cookie1"] == "some_value1"
1355
def test_cookie_as_dict_keys(self):
1357
value = "some_value"
1359
key1 = "some_cookie1"
1360
value1 = "some_value1"
1362
jar = requests.cookies.RequestsCookieJar()
1364
jar.set(key1, value1)
1367
assert keys == list(keys)
1368
# make sure one can use keys multiple times
1369
assert list(keys) == list(keys)
1371
def test_cookie_as_dict_values(self):
1373
value = "some_value"
1375
key1 = "some_cookie1"
1376
value1 = "some_value1"
1378
jar = requests.cookies.RequestsCookieJar()
1380
jar.set(key1, value1)
1382
values = jar.values()
1383
assert values == list(values)
1384
# make sure one can use values multiple times
1385
assert list(values) == list(values)
1387
def test_cookie_as_dict_items(self):
1389
value = "some_value"
1391
key1 = "some_cookie1"
1392
value1 = "some_value1"
1389
value = "some_value"
1391
key1 = "some_cookie1"
1392
value1 = "some_value1"
1394
jar = requests.cookies.RequestsCookieJar()
1396
jar.set(key1, value1)
1399
assert items == list(items)
1400
# make sure one can use items multiple times
1401
assert list(items) == list(items)
1403
def test_cookie_duplicate_names_different_domains(self):
1405
value = "some_value"
1406
domain1 = "test1.com"
1407
domain2 = "test2.com"
1409
jar = requests.cookies.RequestsCookieJar()
1410
jar.set(key, value, domain=domain1)
1411
jar.set(key, value, domain=domain2)
1414
assert len(items) == 2
1416
# Verify that CookieConflictError is raised if domain is not specified
1417
with pytest.raises(requests.cookies.CookieConflictError):
1420
# Verify that CookieConflictError is not raised if domain is specified
1421
cookie = jar.get(key, domain=domain1)
1422
assert cookie == value
1424
def test_cookie_duplicate_names_raises_cookie_conflict_error(self):
1426
value = "some_value"
1429
jar = requests.cookies.RequestsCookieJar()
1430
jar.set(key, value, path=path)
1432
with pytest.raises(requests.cookies.CookieConflictError):
1429
jar = requests.cookies.RequestsCookieJar()
1430
jar.set(key, value, path=path)
1432
with pytest.raises(requests.cookies.CookieConflictError):
1435
def test_cookie_policy_copy(self):
1436
class MyCookiePolicy(cookielib.DefaultCookiePolicy):
1439
jar = requests.cookies.RequestsCookieJar()
1440
jar.set_policy(MyCookiePolicy())
1441
assert isinstance(jar.copy().get_policy(), MyCookiePolicy)
1443
def test_time_elapsed_blank(self, httpbin):
1444
r = requests.get(httpbin("get"))
1447
td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6
1449
assert total_seconds > 0.0
1451
def test_empty_response_has_content_none(self):
1452
r = requests.Response()
1453
assert r.content is None
1455
def test_response_is_iterable(self):
1456
r = requests.Response()
1457
io = StringIO.StringIO("abc")
1460
def read_mock(amt, decode_content=None):
1463
setattr(io, "read", read_mock)
1465
assert next(iter(r))
1468
def test_response_decode_unicode(self):
1469
"""When called with decode_unicode, Response.iter_content should always
1472
r = requests.Response()
1473
r._content_consumed = True
1474
r._content = b"the content"
1475
r.encoding = "ascii"
1472
r = requests.Response()
1473
r._content_consumed = True
1474
r._content = b"the content"
1475
r.encoding = "ascii"
1477
chunks = r.iter_content(decode_unicode=True)
1478
assert all(isinstance(chunk, str) for chunk in chunks)
1480
# also for streaming
1481
r = requests.Response()
1482
r.raw = io.BytesIO(b"the content")
1483
r.encoding = "ascii"
1484
chunks = r.iter_content(decode_unicode=True)
1485
assert all(isinstance(chunk, str) for chunk in chunks)
1487
def test_response_reason_unicode(self):
1488
# check for unicode HTTP status
1489
r = requests.Response()
1490
r.url = "unicode URL"
1491
r.reason = "Komponenttia ei löydy".encode()
1494
assert not r.ok # old behaviour - crashes here
1496
def test_response_reason_unicode_fallback(self):
1497
# check raise_status falls back to ISO-8859-1
1498
r = requests.Response()
1500
reason = "Komponenttia ei löydy"
1501
r.reason = reason.encode("latin-1")
1504
with pytest.raises(requests.exceptions.HTTPError) as e:
1505
r.raise_for_status()
1506
assert reason in e.value.args[0]
1508
def test_response_chunk_size_type(self):
1509
"""Ensure that chunk_size is passed as None or an integer, otherwise
1512
r = requests.Response()
1513
r.raw = io.BytesIO(b"the content")
1514
chunks = r.iter_content(1)
1515
assert all(len(chunk) == 1 for chunk in chunks)
1512
r = requests.Response()
1513
r.raw = io.BytesIO(b"the content")
1514
chunks = r.iter_content(1)
1515
assert all(len(chunk) == 1 for chunk in chunks)
1517
r = requests.Response()
1518
r.raw = io.BytesIO(b"the content")
1519
chunks = r.iter_content(None)
1520
assert list(chunks) == [b"the content"]
1522
r = requests.Response()
1523
r.raw = io.BytesIO(b"the content")
1524
with pytest.raises(TypeError):
1525
chunks = r.iter_content("1024")
1527
@pytest.mark.parametrize(
1528
"exception, args, expected",
1530
(urllib3.exceptions.ProtocolError, tuple(), ChunkedEncodingError),
1531
(urllib3.exceptions.DecodeError, tuple(), ContentDecodingError),
1532
(urllib3.exceptions.ReadTimeoutError, (None, "", ""), ConnectionError),
1533
(urllib3.exceptions.SSLError, tuple(), RequestsSSLError),
1536
def test_iter_content_wraps_exceptions(self, httpbin, exception, args, expected):
1537
r = requests.Response()
1539
# ReadTimeoutError can't be initialized by mock
1540
# so we'll manually create the instance with args
1541
r.raw.stream.side_effect = exception(*args)
1543
with pytest.raises(expected):
1544
next(r.iter_content(1024))
1546
def test_request_and_response_are_pickleable(self, httpbin):
1547
r = requests.get(httpbin("get"))
1543
with pytest.raises(expected):
1544
next(r.iter_content(1024))
1546
def test_request_and_response_are_pickleable(self, httpbin):
1547
r = requests.get(httpbin("get"))
1549
# verify we can pickle the original request
1550
assert pickle.loads(pickle.dumps(r.request))
1552
# verify we can pickle the response and that we have access to
1553
# the original request.
1554
pr = pickle.loads(pickle.dumps(r))
1555
assert r.request.url == pr.request.url
1556
assert r.request.headers == pr.request.headers
1558
def test_prepared_request_is_pickleable(self, httpbin):
1559
p = requests.Request("GET", httpbin("get")).prepare()
1561
# Verify PreparedRequest can be pickled and unpickled
1562
r = pickle.loads(pickle.dumps(p))
1563
assert r.url == p.url
1564
assert r.headers == p.headers
1565
assert r.body == p.body
1567
# Verify unpickled PreparedRequest sends properly
1568
s = requests.Session()
1570
assert resp.status_code == 200
1572
def test_prepared_request_with_file_is_pickleable(self, httpbin):
1573
with open(__file__, "rb") as f:
1574
r = requests.Request("POST", httpbin("post"), files={"file": f})
1577
# Verify PreparedRequest can be pickled and unpickled
1578
r = pickle.loads(pickle.dumps(p))
1579
assert r.url == p.url
1580
assert r.headers == p.headers
1581
assert r.body == p.body
1577
# Verify PreparedRequest can be pickled and unpickled
1578
r = pickle.loads(pickle.dumps(p))
1579
assert r.url == p.url
1580
assert r.headers == p.headers
1581
assert r.body == p.body
1583
# Verify unpickled PreparedRequest sends properly
1584
s = requests.Session()
1586
assert resp.status_code == 200
1588
def test_prepared_request_with_hook_is_pickleable(self, httpbin):
1589
r = requests.Request("GET", httpbin("get"), hooks=default_hooks())
1592
# Verify PreparedRequest can be pickled
1593
r = pickle.loads(pickle.dumps(p))
1594
assert r.url == p.url
1595
assert r.headers == p.headers
1596
assert r.body == p.body
1597
assert r.hooks == p.hooks
1599
# Verify unpickled PreparedRequest sends properly
1600
s = requests.Session()
1602
assert resp.status_code == 200
1604
def test_cannot_send_unprepared_requests(self, httpbin):
1605
r = requests.Request(url=httpbin())
1606
with pytest.raises(ValueError):
1607
requests.Session().send(r)
1609
def test_http_error(self):
1610
error = requests.exceptions.HTTPError()
1611
assert not error.response
1612
response = requests.Response()
1613
error = requests.exceptions.HTTPError(response=response)
1614
assert error.response == response
1615
error = requests.exceptions.HTTPError("message", response=response)
1616
assert str(error) == "message"
1617
assert error.response == response
1613
error = requests.exceptions.HTTPError(response=response)
1614
assert error.response == response
1615
error = requests.exceptions.HTTPError("message", response=response)
1616
assert str(error) == "message"
1617
assert error.response == response
1619
def test_session_pickling(self, httpbin):
1620
r = requests.Request("GET", httpbin("get"))
1621
s = requests.Session()
1623
s = pickle.loads(pickle.dumps(s))
1624
s.proxies = getproxies()
1626
r = s.send(r.prepare())
1627
assert r.status_code == 200
1629
def test_fixes_1329(self, httpbin):
1630
"""Ensure that header updates are done case-insensitively."""
1631
s = requests.Session()
1632
s.headers.update({"ACCEPT": "BOGUS"})
1633
s.headers.update({"accept": "application/json"})
1634
r = s.get(httpbin("get"))
1635
headers = r.request.headers
1636
assert headers["accept"] == "application/json"
1637
assert headers["Accept"] == "application/json"
1638
assert headers["ACCEPT"] == "application/json"
1640
def test_uppercase_scheme_redirect(self, httpbin):
1641
parts = urlparse(httpbin("html"))
1642
url = "HTTP://" + parts.netloc + parts.path
1643
r = requests.get(httpbin("redirect-to"), params={"url": url})
1644
assert r.status_code == 200
1645
assert r.url.lower() == url.lower()
1647
def test_transport_adapter_ordering(self):
1648
s = requests.Session()
1649
order = ["https://", "http://"]
1650
assert order == list(s.adapters)
1651
s.mount("http://git", HTTPAdapter())
1652
s.mount("http://github", HTTPAdapter())
1653
s.mount("http://github.com", HTTPAdapter())
1654
s.mount("http://github.com/about/", HTTPAdapter())
1656
"http://github.com/about/",
1657
"http://github.com",
1663
assert order == list(s.adapters)
1664
s.mount("http://gittip", HTTPAdapter())
1665
s.mount("http://gittip.com", HTTPAdapter())
1666
s.mount("http://gittip.com/about/", HTTPAdapter())
1668
"http://github.com/about/",
1669
"http://gittip.com/about/",
1670
"http://github.com",
1671
"http://gittip.com",
1678
assert order == list(s.adapters)
1679
s2 = requests.Session()
1680
s2.adapters = {"http://": HTTPAdapter()}
1678
assert order == list(s.adapters)
1679
s2 = requests.Session()
1680
s2.adapters = {"http://": HTTPAdapter()}
1681
s2.mount("https://", HTTPAdapter())
1682
assert "http://" in s2.adapters
1683
assert "https://" in s2.adapters
1685
def test_session_get_adapter_prefix_matching(self):
1686
prefix = "https://example.com"
1687
more_specific_prefix = prefix + "/some/path"
1689
url_matching_only_prefix = prefix + "/another/path"
1690
url_matching_more_specific_prefix = more_specific_prefix + "/longer/path"
1691
url_not_matching_prefix = "https://another.example.com/"
1693
s = requests.Session()
1694
prefix_adapter = HTTPAdapter()
1695
more_specific_prefix_adapter = HTTPAdapter()
1696
s.mount(prefix, prefix_adapter)
1697
s.mount(more_specific_prefix, more_specific_prefix_adapter)
1699
assert s.get_adapter(url_matching_only_prefix) is prefix_adapter
1701
s.get_adapter(url_matching_more_specific_prefix)
1702
is more_specific_prefix_adapter
1704
assert s.get_adapter(url_not_matching_prefix) not in (
1706
more_specific_prefix_adapter,
1709
def test_session_get_adapter_prefix_matching_mixed_case(self):
1710
mixed_case_prefix = "hTtPs://eXamPle.CoM/MixEd_CAse_PREfix"
1711
url_matching_prefix = mixed_case_prefix + "/full_url"
1709
def test_session_get_adapter_prefix_matching_mixed_case(self):
1710
mixed_case_prefix = "hTtPs://eXamPle.CoM/MixEd_CAse_PREfix"
1711
url_matching_prefix = mixed_case_prefix + "/full_url"
1713
s = requests.Session()
1714
my_adapter = HTTPAdapter()
1715
s.mount(mixed_case_prefix, my_adapter)
1717
assert s.get_adapter(url_matching_prefix) is my_adapter
1719
def test_session_get_adapter_prefix_matching_is_case_insensitive(self):
1720
mixed_case_prefix = "hTtPs://eXamPle.CoM/MixEd_CAse_PREfix"
1721
url_matching_prefix_with_different_case = (
1722
"HtTpS://exaMPLe.cOm/MiXeD_caSE_preFIX/another_url"
1725
s = requests.Session()
1726
my_adapter = HTTPAdapter()
1727
s.mount(mixed_case_prefix, my_adapter)
1729
assert s.get_adapter(url_matching_prefix_with_different_case) is my_adapter
1731
def test_session_get_adapter_prefix_with_trailing_slash(self):
1733
prefix = "https://example.com/" # trailing slash
1734
url_matching_prefix = "https://example.com/some/path"
1735
url_not_matching_prefix = "https://example.com.other.com/some/path"
1737
s = requests.Session()
1738
adapter = HTTPAdapter()
1739
s.mount(prefix, adapter)
1741
assert s.get_adapter(url_matching_prefix) is adapter
1742
assert s.get_adapter(url_not_matching_prefix) is not adapter
1744
def test_session_get_adapter_prefix_without_trailing_slash(self):
1746
prefix = "https://example.com" # no trailing slash
1747
url_matching_prefix = "https://example.com/some/path"
1748
url_extended_hostname = "https://example.com.other.com/some/path"
1744
def test_session_get_adapter_prefix_without_trailing_slash(self):
1746
prefix = "https://example.com" # no trailing slash
1747
url_matching_prefix = "https://example.com/some/path"
1748
url_extended_hostname = "https://example.com.other.com/some/path"
1750
s = requests.Session()
1751
adapter = HTTPAdapter()
1752
s.mount(prefix, adapter)
1754
assert s.get_adapter(url_matching_prefix) is adapter
1755
assert s.get_adapter(url_extended_hostname) is adapter
1757
def test_header_remove_is_case_insensitive(self, httpbin):
1759
s = requests.Session()
1760
s.headers["foo"] = "bar"
1761
r = s.get(httpbin("get"), headers={"FOO": None})
1762
assert "foo" not in r.request.headers
1764
def test_params_are_merged_case_sensitive(self, httpbin):
1765
s = requests.Session()
1766
s.params["foo"] = "bar"
1767
r = s.get(httpbin("get"), params={"FOO": "bar"})
1768
assert r.json()["args"] == {"foo": "bar", "FOO": "bar"}
1770
def test_long_authinfo_in_url(self):
1771
url = "http://{}:{}@{}:9000/path?query#frag".format(
1772
"E8A3BE87-9E3F-4620-8858-95478E385B5B",
1773
"EA770032-DA4D-4D84-8CE9-29C6D910BF1E",
1774
"exactly-------------sixty-----------three------------characters",
1776
r = requests.Request("GET", url).prepare()
1779
def test_header_keys_are_native(self, httpbin):
1780
headers = {"unicode": "blah", b"byte": "blah"}
1781
r = requests.Request("GET", httpbin("get"), headers=headers)
1779
def test_header_keys_are_native(self, httpbin):
1780
headers = {"unicode": "blah", b"byte": "blah"}
1781
r = requests.Request("GET", httpbin("get"), headers=headers)
1784
# This is testing that they are builtin strings. A bit weird, but there
1786
assert "unicode" in p.headers.keys()
1787
assert "byte" in p.headers.keys()
1789
def test_header_validation(self, httpbin):
1790
"""Ensure prepare_headers regex isn't flagging valid header contents."""
1792
"foo": "bar baz qux",
1797
r = requests.get(httpbin("get"), headers=valid_headers)
1798
for key in valid_headers.keys():
1799
assert valid_headers[key] == r.request.headers[key]
1801
@pytest.mark.parametrize(
1802
"invalid_header, key",
1804
({"foo": 3}, "foo"),
1805
({"bar": {"foo": "bar"}}, "bar"),
1806
({"baz": ["foo", "bar"]}, "baz"),
1809
def test_header_value_not_str(self, httpbin, invalid_header, key):
1810
"""Ensure the header value is of type string or bytes as
1811
per discussion in GH issue #3386
1813
with pytest.raises(InvalidHeader) as excinfo:
1814
requests.get(httpbin("get"), headers=invalid_header)
1815
assert key in str(excinfo.value)
1817
@pytest.mark.parametrize(
1820
{"foo": "bar\r\nbaz: qux"},
1821
{"foo": "bar\n\rbaz: qux"},
1822
{"foo": "bar\nbaz: qux"},
1823
{"foo": "bar\rbaz: qux"},
1835
def test_header_no_return_chars(self, httpbin, invalid_header):
1836
"""Ensure that a header containing return character sequences raise an
1837
exception. Otherwise, multiple headers are created from single string.
1839
with pytest.raises(InvalidHeader):
1840
requests.get(httpbin("get"), headers=invalid_header)
1836
"""Ensure that a header containing return character sequences raise an
1837
exception. Otherwise, multiple headers are created from single string.
1839
with pytest.raises(InvalidHeader):
1840
requests.get(httpbin("get"), headers=invalid_header)
1842
@pytest.mark.parametrize(
1854
def test_header_no_leading_space(self, httpbin, invalid_header):
1855
"""Ensure headers containing leading whitespace raise
1856
InvalidHeader Error before sending.
1858
with pytest.raises(InvalidHeader):
1859
requests.get(httpbin("get"), headers=invalid_header)
1861
def test_header_with_subclass_types(self, httpbin):
1862
"""If the subclasses does not behave *exactly* like
1863
the base bytes/str classes, this is not supported.
1864
This test is for backwards compatibility.
1867
class MyString(str):
1870
class MyBytes(bytes):
1873
r_str = requests.get(httpbin("get"), headers={MyString("x-custom"): "myheader"})
1874
assert r_str.request.headers["x-custom"] == "myheader"
1876
r_bytes = requests.get(
1877
httpbin("get"), headers={MyBytes(b"x-custom"): b"myheader"}
1879
assert r_bytes.request.headers["x-custom"] == b"myheader"
1876
r_bytes = requests.get(
1877
httpbin("get"), headers={MyBytes(b"x-custom"): b"myheader"}
1879
assert r_bytes.request.headers["x-custom"] == b"myheader"
1881
r_mixed = requests.get(
1882
httpbin("get"), headers={MyString("x-custom"): MyBytes(b"myheader")}
1884
assert r_mixed.request.headers["x-custom"] == b"myheader"
1886
@pytest.mark.parametrize("files", ("foo", b"foo", bytearray(b"foo")))
1887
def test_can_send_objects_with_files(self, httpbin, files):
1888
data = {"a": "this is a string"}
1889
files = {"b": files}
1890
r = requests.Request("POST", httpbin("post"), data=data, files=files)
1892
assert "multipart/form-data" in p.headers["Content-Type"]
1894
def test_can_send_file_object_with_non_string_filename(self, httpbin):
1897
r = requests.Request("POST", httpbin("post"), files={"f": f})
1900
assert "multipart/form-data" in p.headers["Content-Type"]
1902
def test_autoset_header_values_are_native(self, httpbin):
1903
data = "this is a string"
1905
req = requests.Request("POST", httpbin("post"), data=data)
1908
assert p.headers["Content-Length"] == length
1910
def test_nonhttp_schemes_dont_check_URLs(self):
1912
"data:image/gif;base64,R0lGODlhAQABAHAAACH5BAUAAAAALAAAAAABAAEAAAICRAEAOw==",
1913
"file:///etc/passwd",
1914
"magnet:?xt=urn:btih:be08f00302bc2d1d3cfa3af02024fa647a271431",
1916
for test_url in test_urls:
1917
req = requests.Request("GET", test_url)
1918
preq = req.prepare()
1919
assert test_url == preq.url
1916
for test_url in test_urls:
1917
req = requests.Request("GET", test_url)
1918
preq = req.prepare()
1919
assert test_url == preq.url
1921
def test_auth_is_stripped_on_http_downgrade(
1922
self, httpbin, httpbin_secure, httpbin_ca_bundle
1925
httpbin_secure("redirect-to"),
1926
params={"url": httpbin("get")},
1927
auth=("user", "pass"),
1928
verify=httpbin_ca_bundle,
1930
assert r.history[0].request.headers["Authorization"]
1931
assert "Authorization" not in r.request.headers
1933
def test_auth_is_retained_for_redirect_on_host(self, httpbin):
1934
r = requests.get(httpbin("redirect/1"), auth=("user", "pass"))
1935
h1 = r.history[0].request.headers["Authorization"]
1936
h2 = r.request.headers["Authorization"]
1940
def test_should_strip_auth_host_change(self):
1941
s = requests.Session()
1942
assert s.should_strip_auth(
1943
"http://example.com/foo", "http://another.example.com/"
1946
def test_should_strip_auth_http_downgrade(self):
1947
s = requests.Session()
1948
assert s.should_strip_auth("https://example.com/foo", "http://example.com/bar")
1950
def test_should_strip_auth_https_upgrade(self):
1951
s = requests.Session()
1952
assert not s.should_strip_auth(
1953
"http://example.com/foo", "https://example.com/bar"
1955
assert not s.should_strip_auth(
1956
"http://example.com:80/foo", "https://example.com/bar"
1958
assert not s.should_strip_auth(
1959
"http://example.com/foo", "https://example.com:443/bar"
1961
# Non-standard ports should trigger stripping
1962
assert s.should_strip_auth(
1963
"http://example.com:8080/foo", "https://example.com/bar"
1965
assert s.should_strip_auth(
1966
"http://example.com/foo", "https://example.com:8443/bar"
1963
"http://example.com:8080/foo", "https://example.com/bar"
1965
assert s.should_strip_auth(
1966
"http://example.com/foo", "https://example.com:8443/bar"
1969
def test_should_strip_auth_port_change(self):
1970
s = requests.Session()
1971
assert s.should_strip_auth(
1972
"http://example.com:1234/foo", "https://example.com:4321/bar"
1975
@pytest.mark.parametrize(
1978
("https://example.com:443/foo", "https://example.com/bar"),
1979
("http://example.com:80/foo", "http://example.com/bar"),
1980
("https://example.com/foo", "https://example.com:443/bar"),
1981
("http://example.com/foo", "http://example.com:80/bar"),
1984
def test_should_strip_auth_default_port(self, old_uri, new_uri):
1985
s = requests.Session()
1986
assert not s.should_strip_auth(old_uri, new_uri)
1988
def test_manual_redirect_with_partial_body_read(self, httpbin):
1989
s = requests.Session()
1990
r1 = s.get(httpbin("redirect/2"), allow_redirects=False, stream=True)
1991
assert r1.is_redirect
1992
rg = s.resolve_redirects(r1, r1.request, stream=True)
1994
# read only the first eight bytes of the response body,
1995
# then follow the redirect
1998
assert r2.is_redirect
2000
# read all of the response via iter_content,
2001
# then follow the redirect
2002
for _ in r2.iter_content():
2005
assert not r3.is_redirect
2001
# then follow the redirect
2002
for _ in r2.iter_content():
2005
assert not r3.is_redirect
2007
def test_prepare_body_position_non_stream(self):
2009
prep = requests.Request("GET", "http://example.com", data=data).prepare()
2010
assert prep._body_position is None
2012
def test_rewind_body(self):
2013
data = io.BytesIO(b"the data")
2014
prep = requests.Request("GET", "http://example.com", data=data).prepare()
2015
assert prep._body_position == 0
2016
assert prep.body.read() == b"the data"
2018
# the data has all been read
2019
assert prep.body.read() == b""
2022
requests.utils.rewind_body(prep)
2023
assert prep.body.read() == b"the data"
2025
def test_rewind_partially_read_body(self):
2026
data = io.BytesIO(b"the data")
2027
data.read(4) # read some data
2028
prep = requests.Request("GET", "http://example.com", data=data).prepare()
2029
assert prep._body_position == 4
2030
assert prep.body.read() == b"data"
2032
# the data has all been read
2033
assert prep.body.read() == b""
2036
requests.utils.rewind_body(prep)
2037
assert prep.body.read() == b"data"
2039
def test_rewind_body_no_seek(self):
2041
def __init__(self, data):
2041
def __init__(self, data):
2050
data = BadFileObj("the data")
2051
prep = requests.Request("GET", "http://example.com", data=data).prepare()
2052
assert prep._body_position == 0
2054
with pytest.raises(UnrewindableBodyError) as e:
2055
requests.utils.rewind_body(prep)
2057
assert "Unable to rewind request body" in str(e)
2059
def test_rewind_body_failed_seek(self):
2061
def __init__(self, data):
2067
def seek(self, pos, whence=0):
2073
data = BadFileObj("the data")
2074
prep = requests.Request("GET", "http://example.com", data=data).prepare()
2075
assert prep._body_position == 0
2077
with pytest.raises(UnrewindableBodyError) as e:
2078
requests.utils.rewind_body(prep)
2080
assert "error occurred when rewinding request body" in str(e)
2082
def test_rewind_body_failed_tell(self):
2084
def __init__(self, data):
2093
data = BadFileObj("the data")
2094
prep = requests.Request("GET", "http://example.com", data=data).prepare()
2095
assert prep._body_position is not None
2097
with pytest.raises(UnrewindableBodyError) as e:
2098
requests.utils.rewind_body(prep)
2100
assert "Unable to rewind request body" in str(e)
2102
def test_getattr_proxy_stream_follows_redirect(self, httpbin):
2103
"""Ensure stream wrappers that don't implement __iter__ directly are still detected."""
2107
self._file = io.BytesIO(b"data")
2109
def __getattr__(self, name):
2110
return getattr(self._file, name)
2113
httpbin("redirect-to?url=/post&status_code=307"), data=AttrProxy()
2115
assert r.json()["data"] == "data"
2117
def _patch_adapter_gzipped_redirect(self, session, url):
2118
adapter = session.get_adapter(url=url)
2119
org_build_response = adapter.build_response
2120
self._patched_response = False
2122
def build_response(*args, **kwargs):
2123
resp = org_build_response(*args, **kwargs)
2124
if not self._patched_response:
2125
resp.raw.headers["content-encoding"] = "gzip"
2126
self._patched_response = True
2123
resp = org_build_response(*args, **kwargs)
2124
if not self._patched_response:
2125
resp.raw.headers["content-encoding"] = "gzip"
2126
self._patched_response = True
2129
adapter.build_response = build_response
2131
def test_redirect_with_wrong_gzipped_header(self, httpbin):
2132
s = requests.Session()
2133
url = httpbin("redirect/1")
2134
self._patch_adapter_gzipped_redirect(s, url)
2137
@pytest.mark.parametrize(
2138
"username, password, auth_str",
2140
("test", "test", "Basic dGVzdDp0ZXN0"),
2144
"Basic 0LjQvNGPOtC/0LDRgNC+0LvRjA==",
2148
def test_basic_auth_str_is_always_native(self, username, password, auth_str):
2149
s = _basic_auth_str(username, password)
2150
assert isinstance(s, builtin_str)
2151
assert s == auth_str
2153
def test_requests_history_is_saved(self, httpbin):
2154
r = requests.get(httpbin("redirect/5"))
2155
total = r.history[-1].history
2157
for item in r.history:
2158
assert item.history == total[0:i]
2161
def test_json_param_post_content_type_works(self, httpbin):
2162
r = requests.post(httpbin("post"), json={"life": 42})
2163
assert r.status_code == 200
2164
assert "application/json" in r.request.headers["Content-Type"]
2165
assert {"life": 42} == r.json()["json"]
2161
def test_json_param_post_content_type_works(self, httpbin):
2162
r = requests.post(httpbin("post"), json={"life": 42})
2163
assert r.status_code == 200
2164
assert "application/json" in r.request.headers["Content-Type"]
2165
assert {"life": 42} == r.json()["json"]
2167
def test_json_param_post_should_not_override_data_param(self, httpbin):
2168
r = requests.Request(
2170
url=httpbin("post"),
2171
data={"stuff": "elixr"},
2172
json={"music": "flute"},
2175
assert "stuff=elixr" == prep.body
2177
def test_response_iter_lines(self, httpbin):
2178
r = requests.get(httpbin("stream/4"), stream=True)
2179
assert r.status_code == 200
2183
assert len(list(it)) == 3
2185
def test_response_context_manager(self, httpbin):
2186
with requests.get(httpbin("stream/4"), stream=True) as response:
2187
assert isinstance(response, requests.Response)
2189
assert response.raw.closed
2191
def test_unconsumed_session_response_closes_connection(self, httpbin):
2192
s = requests.session()
2194
with contextlib.closing(s.get(httpbin("stream/4"), stream=True)) as response:
2197
assert response._content_consumed is False
2198
assert response.raw.closed
2201
def test_response_iter_lines_reentrant(self, httpbin):
2202
"""Response.iter_lines() is not reentrant safe"""
2203
r = requests.get(httpbin("stream/4"), stream=True)
2204
assert r.status_code == 200
2201
def test_response_iter_lines_reentrant(self, httpbin):
2202
"""Response.iter_lines() is not reentrant safe"""
2203
r = requests.get(httpbin("stream/4"), stream=True)
2204
assert r.status_code == 200
2206
next(r.iter_lines())
2207
assert len(list(r.iter_lines())) == 3
2209
def test_session_close_proxy_clear(self):
2214
session = requests.Session()
2215
with mock.patch.dict(session.adapters["http://"].proxy_manager, proxies):
2217
proxies["one"].clear.assert_called_once_with()
2218
proxies["two"].clear.assert_called_once_with()
2220
def test_proxy_auth(self):
2221
adapter = HTTPAdapter()
2222
headers = adapter.proxy_headers("http://user:pass@httpbin.org")
2223
assert headers == {"Proxy-Authorization": "Basic dXNlcjpwYXNz"}
2225
def test_proxy_auth_empty_pass(self):
2226
adapter = HTTPAdapter()
2227
headers = adapter.proxy_headers("http://user:@httpbin.org")
2228
assert headers == {"Proxy-Authorization": "Basic dXNlcjo="}
2230
def test_response_json_when_content_is_None(self, httpbin):
2231
r = requests.get(httpbin("/status/204"))
2232
# Make sure r.content is None
2235
r._content_consumed = False
2237
assert r.content is None
2238
with pytest.raises(ValueError):
2235
r._content_consumed = False
2237
assert r.content is None
2238
with pytest.raises(ValueError):
2241
def test_response_without_release_conn(self):
2242
"""Test `close` call for non-urllib3-like raw objects.
2243
Should work when `release_conn` attr doesn't exist on `response.raw`.
2245
resp = requests.Response()
2246
resp.raw = StringIO.StringIO("test")
2247
assert not resp.raw.closed
2249
assert resp.raw.closed
2251
def test_empty_stream_with_auth_does_not_set_content_length_header(self, httpbin):
2252
"""Ensure that a byte stream with size 0 will not set both a Content-Length
2253
and Transfer-Encoding header.
2255
auth = ("user", "pass")
2256
url = httpbin("post")
2257
file_obj = io.BytesIO(b"")
2258
r = requests.Request("POST", url, auth=auth, data=file_obj)
2259
prepared_request = r.prepare()
2260
assert "Transfer-Encoding" in prepared_request.headers
2261
assert "Content-Length" not in prepared_request.headers
2263
def test_stream_with_auth_does_not_set_transfer_encoding_header(self, httpbin):
2264
"""Ensure that a byte stream with size > 0 will not set both a Content-Length
2265
and Transfer-Encoding header.
2267
auth = ("user", "pass")
2268
url = httpbin("post")
2269
file_obj = io.BytesIO(b"test data")
2270
r = requests.Request("POST", url, auth=auth, data=file_obj)
2271
prepared_request = r.prepare()
2272
assert "Transfer-Encoding" not in prepared_request.headers
2273
assert "Content-Length" in prepared_request.headers
2269
file_obj = io.BytesIO(b"test data")
2270
r = requests.Request("POST", url, auth=auth, data=file_obj)
2271
prepared_request = r.prepare()
2272
assert "Transfer-Encoding" not in prepared_request.headers
2273
assert "Content-Length" in prepared_request.headers
2275
def test_chunked_upload_does_not_set_content_length_header(self, httpbin):
2276
"""Ensure that requests with a generator body stream using
2277
Transfer-Encoding: chunked, not a Content-Length header.
2279
data = (i for i in [b"a", b"b", b"c"])
2280
url = httpbin("post")
2281
r = requests.Request("POST", url, data=data)
2282
prepared_request = r.prepare()
2283
assert "Transfer-Encoding" in prepared_request.headers
2284
assert "Content-Length" not in prepared_request.headers
2286
def test_custom_redirect_mixin(self, httpbin):
2287
"""Tests a custom mixin to overwrite ``get_redirect_target``.
2289
Ensures a subclassed ``requests.Session`` can handle a certain type of
2290
malformed redirect responses.
2292
1. original request receives a proper response: 302 redirect
2293
2. following the redirect, a malformed response is given:
2294
status code = HTTP 200
2295
location = alternate url
2296
3. the custom session catches the edge case and follows the redirect
2298
url_final = httpbin("html")
2299
querystring_malformed = urlencode({"location": url_final})
2300
url_redirect_malformed = httpbin("response-headers?%s" % querystring_malformed)
2301
querystring_redirect = urlencode({"url": url_redirect_malformed})
2302
url_redirect = httpbin("redirect-to?%s" % querystring_redirect)
2305
url_redirect_malformed,
2305
url_redirect_malformed,
2309
class CustomRedirectSession(requests.Session):
2310
def get_redirect_target(self, resp):
2312
if resp.is_redirect:
2313
return resp.headers["location"]
2314
# edge case - check to see if 'location' is in headers anyways
2315
location = resp.headers.get("location")
2316
if location and (location != resp.url):
2320
session = CustomRedirectSession()
2321
r = session.get(urls_test[0])
2322
assert len(r.history) == 2
2323
assert r.status_code == 200
2324
assert r.history[0].status_code == 302
2325
assert r.history[0].is_redirect
2326
assert r.history[1].status_code == 200
2327
assert not r.history[1].is_redirect
2328
assert r.url == urls_test[2]
2331
class TestCaseInsensitiveDict:
2332
@pytest.mark.parametrize(
2335
CaseInsensitiveDict({"Foo": "foo", "BAr": "bar"}),
2336
CaseInsensitiveDict([("Foo", "foo"), ("BAr", "bar")]),
2337
CaseInsensitiveDict(FOO="foo", BAr="bar"),
2340
def test_init(self, cid):
2341
assert len(cid) == 2
2345
def test_docstring_example(self):
2346
cid = CaseInsensitiveDict()
2347
cid["Accept"] = "application/json"
2348
assert cid["aCCEPT"] == "application/json"
2349
assert list(cid) == ["Accept"]
2345
def test_docstring_example(self):
2346
cid = CaseInsensitiveDict()
2347
cid["Accept"] = "application/json"
2348
assert cid["aCCEPT"] == "application/json"
2349
assert list(cid) == ["Accept"]
2352
cid = CaseInsensitiveDict({"a": "a", "b": "b"})
2354
assert len(cid) == 2
2356
def test_getitem(self):
2357
cid = CaseInsensitiveDict({"Spam": "blueval"})
2358
assert cid["spam"] == "blueval"
2359
assert cid["SPAM"] == "blueval"
2361
def test_fixes_649(self):
2362
"""__setitem__ should behave case-insensitively."""
2363
cid = CaseInsensitiveDict()
2364
cid["spam"] = "oneval"
2365
cid["Spam"] = "twoval"
2366
cid["sPAM"] = "redval"
2367
cid["SPAM"] = "blueval"
2368
assert cid["spam"] == "blueval"
2369
assert cid["SPAM"] == "blueval"
2370
assert list(cid.keys()) == ["SPAM"]
2372
def test_delitem(self):
2373
cid = CaseInsensitiveDict()
2374
cid["Spam"] = "someval"
2376
assert "spam" not in cid
2377
assert len(cid) == 0
2379
def test_contains(self):
2380
cid = CaseInsensitiveDict()
2381
cid["Spam"] = "someval"
2382
assert "Spam" in cid
2383
assert "spam" in cid
2384
assert "SPAM" in cid
2385
assert "sPam" in cid
2386
assert "notspam" not in cid
2389
cid = CaseInsensitiveDict()
2390
cid["spam"] = "oneval"
2391
cid["SPAM"] = "blueval"
2392
assert cid.get("spam") == "blueval"
2393
assert cid.get("SPAM") == "blueval"
2394
assert cid.get("sPam") == "blueval"
2395
assert cid.get("notspam", "default") == "default"
2391
cid["SPAM"] = "blueval"
2392
assert cid.get("spam") == "blueval"
2393
assert cid.get("SPAM") == "blueval"
2394
assert cid.get("sPam") == "blueval"
2395
assert cid.get("notspam", "default") == "default"
2397
def test_update(self):
2398
cid = CaseInsensitiveDict()
2399
cid["spam"] = "blueval"
2400
cid.update({"sPam": "notblueval"})
2401
assert cid["spam"] == "notblueval"
2402
cid = CaseInsensitiveDict({"Foo": "foo", "BAr": "bar"})
2403
cid.update({"fOO": "anotherfoo", "bAR": "anotherbar"})
2404
assert len(cid) == 2
2405
assert cid["foo"] == "anotherfoo"
2406
assert cid["bar"] == "anotherbar"
2408
def test_update_retains_unchanged(self):
2409
cid = CaseInsensitiveDict({"foo": "foo", "bar": "bar"})
2410
cid.update({"foo": "newfoo"})
2411
assert cid["bar"] == "bar"
2413
def test_iter(self):
2414
cid = CaseInsensitiveDict({"Spam": "spam", "Eggs": "eggs"})
2415
keys = frozenset(["Spam", "Eggs"])
2416
assert frozenset(iter(cid)) == keys
2418
def test_equality(self):
2419
cid = CaseInsensitiveDict({"SPAM": "blueval", "Eggs": "redval"})
2420
othercid = CaseInsensitiveDict({"spam": "blueval", "eggs": "redval"})
2421
assert cid == othercid
2422
del othercid["spam"]
2423
assert cid != othercid
2424
assert cid == {"spam": "blueval", "eggs": "redval"}
2425
assert cid != object()
2427
def test_setdefault(self):
2428
cid = CaseInsensitiveDict({"Spam": "blueval"})
2429
assert cid.setdefault("spam", "notblueval") == "blueval"
2430
assert cid.setdefault("notspam", "notblueval") == "notblueval"
2427
def test_setdefault(self):
2428
cid = CaseInsensitiveDict({"Spam": "blueval"})
2429
assert cid.setdefault("spam", "notblueval") == "blueval"
2430
assert cid.setdefault("notspam", "notblueval") == "notblueval"
2432
def test_lower_items(self):
2433
cid = CaseInsensitiveDict(
2435
"Accept": "application/json",
2436
"user-Agent": "requests",
2439
keyset = frozenset(lowerkey for lowerkey, v in cid.lower_items())
2440
lowerkeyset = frozenset(["accept", "user-agent"])
2441
assert keyset == lowerkeyset
2443
def test_preserve_key_case(self):
2444
cid = CaseInsensitiveDict(
2446
"Accept": "application/json",
2447
"user-Agent": "requests",
2450
keyset = frozenset(["Accept", "user-Agent"])
2451
assert frozenset(i[0] for i in cid.items()) == keyset
2452
assert frozenset(cid.keys()) == keyset
2453
assert frozenset(cid) == keyset
2455
def test_preserve_last_key_case(self):
2456
cid = CaseInsensitiveDict(
2458
"Accept": "application/json",
2459
"user-Agent": "requests",
2462
cid.update({"ACCEPT": "application/json"})
2463
cid["USER-AGENT"] = "requests"
2464
keyset = frozenset(["ACCEPT", "USER-AGENT"])
2465
assert frozenset(i[0] for i in cid.items()) == keyset
2466
assert frozenset(cid.keys()) == keyset
2467
assert frozenset(cid) == keyset
2463
cid["USER-AGENT"] = "requests"
2464
keyset = frozenset(["ACCEPT", "USER-AGENT"])
2465
assert frozenset(i[0] for i in cid.items()) == keyset
2466
assert frozenset(cid.keys()) == keyset
2467
assert frozenset(cid) == keyset
2469
def test_copy(self):
2470
cid = CaseInsensitiveDict(
2472
"Accept": "application/json",
2473
"user-Agent": "requests",
2476
cid_copy = cid.copy()
2477
assert cid == cid_copy
2478
cid["changed"] = True
2479
assert cid != cid_copy
2482
class TestMorselToCookieExpires:
2483
"""Tests for morsel_to_cookie when morsel contains expires."""
2485
def test_expires_valid_str(self):
2486
"""Test case where we convert expires from string time."""
2489
morsel["expires"] = "Thu, 01-Jan-1970 00:00:01 GMT"
2490
cookie = morsel_to_cookie(morsel)
2491
assert cookie.expires == 1
2493
@pytest.mark.parametrize(
2497
("woops", ValueError),
2500
def test_expires_invalid_int(self, value, exception):
2501
"""Test case where an invalid type is passed for expires."""
2503
morsel["expires"] = value
2504
with pytest.raises(exception):
2505
morsel_to_cookie(morsel)
2507
def test_expires_none(self):
2508
"""Test case where expires is None."""
2504
with pytest.raises(exception):
2505
morsel_to_cookie(morsel)
2507
def test_expires_none(self):
2508
"""Test case where expires is None."""
2511
morsel["expires"] = None
2512
cookie = morsel_to_cookie(morsel)
2513
assert cookie.expires is None
2516
class TestMorselToCookieMaxAge:
2517
"""Tests for morsel_to_cookie when morsel contains max-age."""
2519
def test_max_age_valid_int(self):
2520
"""Test case where a valid max age in seconds is passed."""
2523
morsel["max-age"] = 60
2524
cookie = morsel_to_cookie(morsel)
2525
assert isinstance(cookie.expires, int)
2527
def test_max_age_invalid_str(self):
2528
"""Test case where a invalid max age is passed."""
2531
morsel["max-age"] = "woops"
2532
with pytest.raises(TypeError):
2533
morsel_to_cookie(morsel)
2537
def test_stream_timeout(self, httpbin):
2539
requests.get(httpbin("delay/10"), timeout=2.0)
2540
except requests.exceptions.Timeout as e:
2541
assert "Read timed out" in e.args[0].args[0]
2543
@pytest.mark.parametrize(
2544
"timeout, error_text",
2546
((3, 4, 5), "(connect, read)"),
2547
("foo", "must be an int, float or None"),
2550
def test_invalid_timeout(self, httpbin, timeout, error_text):
2551
with pytest.raises(ValueError) as e:
2552
requests.get(httpbin("get"), timeout=timeout)
2553
assert error_text in str(e)
2550
def test_invalid_timeout(self, httpbin, timeout, error_text):
2551
with pytest.raises(ValueError) as e:
2552
requests.get(httpbin("get"), timeout=timeout)
2553
assert error_text in str(e)
2555
@pytest.mark.parametrize("timeout", (None, Urllib3Timeout(connect=None, read=None)))
2556
def test_none_timeout(self, httpbin, timeout):
2557
"""Check that you can set None as a valid timeout value.
2559
To actually test this behavior, we'd want to check that setting the
2560
timeout to None actually lets the request block past the system default
2561
timeout. However, this would make the test suite unbearably slow.
2562
Instead we verify that setting the timeout to None does not prevent the
2563
request from succeeding.
2565
r = requests.get(httpbin("get"), timeout=timeout)
2566
assert r.status_code == 200
2568
@pytest.mark.parametrize(
2569
"timeout", ((None, 0.1), Urllib3Timeout(connect=None, read=0.1))
2571
def test_read_timeout(self, httpbin, timeout):
2573
requests.get(httpbin("delay/10"), timeout=timeout)
2574
pytest.fail("The recv() request should time out.")
2578
@pytest.mark.parametrize(
2579
"timeout", ((0.1, None), Urllib3Timeout(connect=0.1, read=None))
2581
def test_connect_timeout(self, timeout):
2583
requests.get(TARPIT, timeout=timeout)
2584
pytest.fail("The connect() request should time out.")
2585
except ConnectTimeout as e:
2586
assert isinstance(e, ConnectionError)
2587
assert isinstance(e, Timeout)
2583
requests.get(TARPIT, timeout=timeout)
2584
pytest.fail("The connect() request should time out.")
2585
except ConnectTimeout as e:
2586
assert isinstance(e, ConnectionError)
2587
assert isinstance(e, Timeout)
2589
@pytest.mark.parametrize(
2590
"timeout", ((0.1, 0.1), Urllib3Timeout(connect=0.1, read=0.1))
2592
def test_total_timeout_connect(self, timeout):
2594
requests.get(TARPIT, timeout=timeout)
2595
pytest.fail("The connect() request should time out.")
2596
except ConnectTimeout:
2599
def test_encoded_methods(self, httpbin):
2600
"""See: https://github.com/psf/requests/issues/2316"""
2601
r = requests.request(b"GET", httpbin("get"))
2605
SendCall = collections.namedtuple("SendCall", ("args", "kwargs"))
2608
class RedirectSession(SessionRedirectMixin):
2609
def __init__(self, order_of_redirects):
2610
self.redirects = order_of_redirects
2612
self.max_redirects = 30
2614
self.trust_env = False
2616
def send(self, *args, **kwargs):
2617
self.calls.append(SendCall(args, kwargs))
2618
return self.build_response()
2620
def build_response(self):
2621
request = self.calls[-1].args[0]
2622
r = requests.Response()
2626
r.status_code = int(self.redirects.pop(0))
2626
r.status_code = int(self.redirects.pop(0))
2630
r.headers = CaseInsensitiveDict({"Location": "/"})
2631
r.raw = self._build_raw()
2635
def _build_raw(self):
2636
string = StringIO.StringIO("")
2637
setattr(string, "release_conn", lambda *args: args)
2641
def test_json_encodes_as_bytes():
2642
# urllib3 expects bodies as bytes-like objects
2643
body = {"key": "value"}
2644
p = PreparedRequest()
2645
p.prepare(method="GET", url="https://www.example.com/", json=body)
2646
assert isinstance(p.body, bytes)
2649
def test_requests_are_updated_each_time(httpbin):
2650
session = RedirectSession([303, 307])
2651
prep = requests.Request("POST", httpbin("post")).prepare()
2652
r0 = session.send(prep)
2653
assert r0.request.method == "POST"
2654
assert session.calls[-1] == SendCall((r0.request,), {})
2655
redirect_generator = session.resolve_redirects(r0, prep)
2656
default_keyword_args = {
2661
"allow_redirects": False,
2664
for response in redirect_generator:
2665
assert response.request.method == "GET"
2666
send_call = SendCall((response.request,), default_keyword_args)
2667
assert session.calls[-1] == send_call
2664
for response in redirect_generator:
2665
assert response.request.method == "GET"
2666
send_call = SendCall((response.request,), default_keyword_args)
2667
assert session.calls[-1] == send_call
2670
@pytest.mark.parametrize(
2673
("http_proxy", "http://example.com", "socks5://proxy.com:9876"),
2674
("https_proxy", "https://example.com", "socks5://proxy.com:9876"),
2675
("all_proxy", "http://example.com", "socks5://proxy.com:9876"),
2676
("all_proxy", "https://example.com", "socks5://proxy.com:9876"),
2679
def test_proxy_env_vars_override_default(var, url, proxy):
2680
session = requests.Session()
2681
prep = PreparedRequest()
2682
prep.prepare(method="GET", url=url)
2684
kwargs = {var: proxy}
2685
scheme = urlparse(url).scheme
2686
with override_environ(**kwargs):
2687
proxies = session.rebuild_proxies(prep, {})
2688
assert scheme in proxies
2689
assert proxies[scheme] == proxy
2692
@pytest.mark.parametrize(
2695
(("a", "b"), ("c", "d")),
2696
(("c", "d"), ("a", "b")),
2697
(("a", "b"), ("c", "d"), ("e", "f")),
2700
def test_data_argument_accepts_tuples(data):
2701
"""Ensure that the data argument will accept tuples of strings
2702
and properly encode them.
2704
p = PreparedRequest()
2706
method="GET", url="http://www.example.com", data=data, hooks=default_hooks()
2708
assert p.body == urlencode(data)
2704
p = PreparedRequest()
2706
method="GET", url="http://www.example.com", data=data, hooks=default_hooks()
2708
assert p.body == urlencode(data)
2711
@pytest.mark.parametrize(
2717
"url": "http://www.example.com",
2719
"hooks": default_hooks(),
2723
"url": "http://www.example.com",
2725
"hooks": default_hooks(),
2726
"cookies": {"foo": "bar"},
2728
{"method": "GET", "url": "http://www.example.com/üniçø∂é"},
2731
def test_prepared_copy(kwargs):
2732
p = PreparedRequest()
2736
for attr in ("method", "url", "headers", "_cookies", "body", "hooks"):
2737
assert getattr(p, attr) == getattr(copy, attr)
2740
def test_urllib3_retries(httpbin):
2741
from urllib3.util import Retry
2743
s = requests.Session()
2744
s.mount("http://", HTTPAdapter(max_retries=Retry(total=2, status_forcelist=[500])))
2746
with pytest.raises(RetryError):
2747
s.get(httpbin("status/500"))
2750
def test_urllib3_pool_connection_closed(httpbin):
2751
s = requests.Session()
2752
s.mount("http://", HTTPAdapter(pool_connections=0, pool_maxsize=0))
2755
s.get(httpbin("status/200"))
2756
except ConnectionError as e:
2757
assert "Pool is closed." in str(e)
2755
s.get(httpbin("status/200"))
2756
except ConnectionError as e:
2757
assert "Pool is closed." in str(e)
2760
class TestPreparingURLs:
2761
@pytest.mark.parametrize(
2764
("http://google.com", "http://google.com/"),
2765
("http://ジェーピーニック.jp", "http://xn--hckqz9bzb1cyrb.jp/"),
2766
("http://xn--n3h.net/", "http://xn--n3h.net/"),
2767
("http://ジェーピーニック.jp".encode(), "http://xn--hckqz9bzb1cyrb.jp/"),
2768
("http://straße.de/straße", "http://xn--strae-oqa.de/stra%C3%9Fe"),
2770
"http://straße.de/straße".encode(),
2771
"http://xn--strae-oqa.de/stra%C3%9Fe",
2774
"http://Königsgäßchen.de/straße",
2775
"http://xn--knigsgchen-b4a3dun.de/stra%C3%9Fe",
2778
"http://Königsgäßchen.de/straße".encode(),
2779
"http://xn--knigsgchen-b4a3dun.de/stra%C3%9Fe",
2781
(b"http://xn--n3h.net/", "http://xn--n3h.net/"),
2783
b"http://[1200:0000:ab00:1234:0000:2552:7777:1313]:12345/",
2784
"http://[1200:0000:ab00:1234:0000:2552:7777:1313]:12345/",
2787
"http://[1200:0000:ab00:1234:0000:2552:7777:1313]:12345/",
2788
"http://[1200:0000:ab00:1234:0000:2552:7777:1313]:12345/",
2792
def test_preparing_url(self, url, expected):
2793
def normalize_percent_encode(x):
2794
# Helper function that normalizes equivalent
2795
# percent-encoded bytes before comparisons
2796
for c in re.findall(r"%[a-fA-F0-9]{2}", x):
2797
x = x.replace(c, c.upper())
2794
# Helper function that normalizes equivalent
2795
# percent-encoded bytes before comparisons
2796
for c in re.findall(r"%[a-fA-F0-9]{2}", x):
2797
x = x.replace(c, c.upper())
2800
r = requests.Request("GET", url=url)
2802
assert normalize_percent_encode(p.url) == expected
2804
@pytest.mark.parametrize(
2807
b"http://*.google.com",
2809
"http://*.google.com",
2814
def test_preparing_bad_url(self, url):
2815
r = requests.Request("GET", url=url)
2816
with pytest.raises(requests.exceptions.InvalidURL):
2819
@pytest.mark.parametrize("url, exception", (("http://:1", InvalidURL),))
2820
def test_redirecting_to_bad_url(self, httpbin, url, exception):
2821
with pytest.raises(exception):
2822
requests.get(httpbin("redirect-to"), params={"url": url})
2824
@pytest.mark.parametrize(
2828
b"http+unix://%2Fvar%2Frun%2Fsocket/path%7E",
2829
"http+unix://%2Fvar%2Frun%2Fsocket/path~",
2832
"http+unix://%2Fvar%2Frun%2Fsocket/path%7E",
2833
"http+unix://%2Fvar%2Frun%2Fsocket/path~",
2836
b"mailto:user@example.org",
2837
"mailto:user@example.org",
2840
"mailto:user@example.org",
2841
"mailto:user@example.org",
2844
b"data:SSDimaUgUHl0aG9uIQ==",
2845
"data:SSDimaUgUHl0aG9uIQ==",
2849
def test_url_mutation(self, input, expected):
2851
This test validates that we correctly exclude some URLs from
2852
preparation, and that we handle others. Specifically, it tests that
2853
any URL whose scheme doesn't begin with "http" is left alone, and
2854
those whose scheme *does* begin with "http" are mutated.
2856
r = requests.Request("GET", url=input)
2858
assert p.url == expected
2854
those whose scheme *does* begin with "http" are mutated.
2856
r = requests.Request("GET", url=input)
2858
assert p.url == expected
2860
@pytest.mark.parametrize(
2861
"input, params, expected",
2864
b"http+unix://%2Fvar%2Frun%2Fsocket/path",
2866
"http+unix://%2Fvar%2Frun%2Fsocket/path?key=value",
2869
"http+unix://%2Fvar%2Frun%2Fsocket/path",
2871
"http+unix://%2Fvar%2Frun%2Fsocket/path?key=value",
2874
b"mailto:user@example.org",
2876
"mailto:user@example.org",
2879
"mailto:user@example.org",
2881
"mailto:user@example.org",
2885
def test_parameters_for_nonstandard_schemes(self, input, params, expected):
2887
Setting parameters for nonstandard schemes is allowed if those schemes
2888
begin with "http", and is forbidden otherwise.
2890
r = requests.Request("GET", url=input, params=params)
2892
assert p.url == expected
2894
def test_post_json_nan(self, httpbin):
2895
data = {"foo": float("nan")}
2896
with pytest.raises(requests.exceptions.InvalidJSONError):
2897
requests.post(httpbin("post"), json=data)
2894
def test_post_json_nan(self, httpbin):
2895
data = {"foo": float("nan")}
2896
with pytest.raises(requests.exceptions.InvalidJSONError):
2897
requests.post(httpbin("post"), json=data)
2899
def test_json_decode_compatibility(self, httpbin):
2900
r = requests.get(httpbin("bytes/20"))
2901
with pytest.raises(requests.exceptions.JSONDecodeError) as excinfo:
2903
assert isinstance(excinfo.value, RequestException)
2904
assert isinstance(excinfo.value, JSONDecodeError)
2905
assert r.text not in str(excinfo.value)
2907
def test_json_decode_persists_doc_attr(self, httpbin):
2908
r = requests.get(httpbin("bytes/20"))
2909
with pytest.raises(requests.exceptions.JSONDecodeError) as excinfo:
2911
assert excinfo.value.doc == r.text
2913
def test_status_code_425(self):
2914
r1 = requests.codes.get("TOO_EARLY")
2915
r2 = requests.codes.get("too_early")
2916
r3 = requests.codes.get("UNORDERED")
2917
r4 = requests.codes.get("unordered")
2918
r5 = requests.codes.get("UNORDERED_COLLECTION")
2919
r6 = requests.codes.get("unordered_collection")
2928
def test_different_connection_pool_for_tls_settings_verify_True(self):
2929
def response_handler(sock):
2930
consume_socket_content(sock, timeout=0.5)
2932
b"HTTP/1.1 200 OK\r\n"
2933
b"Content-Length: 18\r\n\r\n"
2934
b'\xff\xfe{\x00"\x00K0"\x00=\x00"\x00\xab0"\x00\r\n'
2932
b"HTTP/1.1 200 OK\r\n"
2933
b"Content-Length: 18\r\n\r\n"
2934
b'\xff\xfe{\x00"\x00K0"\x00=\x00"\x00\xab0"\x00\r\n'
2937
s = requests.Session()
2938
close_server = threading.Event()
2940
handler=response_handler,
2941
wait_to_close_event=close_server,
2942
requests_to_handle=3,
2943
cert_chain="tests/certs/expired/server/server.pem",
2944
keyfile="tests/certs/expired/server/server.key",
2947
with server as (host, port):
2948
url = f"https://{host}:{port}"
2949
r1 = s.get(url, verify=False)
2950
assert r1.status_code == 200
2952
# Cannot verify self-signed certificate
2953
with pytest.raises(requests.exceptions.SSLError):
2957
assert 2 == len(s.adapters["https://"].poolmanager.pools)
2959
def test_different_connection_pool_for_tls_settings_verify_bundle_expired_cert(
2962
def response_handler(sock):
2963
consume_socket_content(sock, timeout=0.5)
2965
b"HTTP/1.1 200 OK\r\n"
2966
b"Content-Length: 18\r\n\r\n"
2967
b'\xff\xfe{\x00"\x00K0"\x00=\x00"\x00\xab0"\x00\r\n'
2970
s = requests.Session()
2971
close_server = threading.Event()
2973
handler=response_handler,
2974
wait_to_close_event=close_server,
2975
requests_to_handle=3,
2976
cert_chain="tests/certs/expired/server/server.pem",
2977
keyfile="tests/certs/expired/server/server.key",
2974
wait_to_close_event=close_server,
2975
requests_to_handle=3,
2976
cert_chain="tests/certs/expired/server/server.pem",
2977
keyfile="tests/certs/expired/server/server.key",
2980
with server as (host, port):
2981
url = f"https://{host}:{port}"
2982
r1 = s.get(url, verify=False)
2983
assert r1.status_code == 200
2985
# Has right trust bundle, but certificate expired
2986
with pytest.raises(requests.exceptions.SSLError):
2987
s.get(url, verify="tests/certs/expired/ca/ca.crt")
2990
assert 2 == len(s.adapters["https://"].poolmanager.pools)
2992
def test_different_connection_pool_for_tls_settings_verify_bundle_unexpired_cert(
2995
def response_handler(sock):
2996
consume_socket_content(sock, timeout=0.5)
2998
b"HTTP/1.1 200 OK\r\n"
2999
b"Content-Length: 18\r\n\r\n"
3000
b'\xff\xfe{\x00"\x00K0"\x00=\x00"\x00\xab0"\x00\r\n'
3003
s = requests.Session()
3004
close_server = threading.Event()
3006
handler=response_handler,
3007
wait_to_close_event=close_server,
3008
requests_to_handle=3,
3009
cert_chain="tests/certs/valid/server/server.pem",
3010
keyfile="tests/certs/valid/server/server.key",
3007
wait_to_close_event=close_server,
3008
requests_to_handle=3,
3009
cert_chain="tests/certs/valid/server/server.pem",
3010
keyfile="tests/certs/valid/server/server.key",
3013
with server as (host, port):
3014
url = f"https://{host}:{port}"
3015
r1 = s.get(url, verify=False)
3016
assert r1.status_code == 200
3018
r2 = s.get(url, verify="tests/certs/valid/ca/ca.crt")
3019
assert r2.status_code == 200
3022
assert 2 == len(s.adapters["https://"].poolmanager.pools)
3024
def test_different_connection_pool_for_mtls_settings(self):
3027
def response_handler(sock):
3028
nonlocal client_cert
3029
client_cert = sock.getpeercert()
3030
consume_socket_content(sock, timeout=0.5)
3032
b"HTTP/1.1 200 OK\r\n"
3033
b"Content-Length: 18\r\n\r\n"
3034
b'\xff\xfe{\x00"\x00K0"\x00=\x00"\x00\xab0"\x00\r\n'
3037
s = requests.Session()
3038
close_server = threading.Event()
3040
handler=response_handler,
3041
wait_to_close_event=close_server,
3042
requests_to_handle=2,
3043
cert_chain="tests/certs/expired/server/server.pem",
3044
keyfile="tests/certs/expired/server/server.key",
3046
cacert="tests/certs/expired/ca/ca.crt",
3043
cert_chain="tests/certs/expired/server/server.pem",
3044
keyfile="tests/certs/expired/server/server.key",
3046
cacert="tests/certs/expired/ca/ca.crt",
3050
"tests/certs/mtls/client/client.pem",
3051
"tests/certs/mtls/client/client.key",
3053
with server as (host, port):
3054
url = f"https://{host}:{port}"
3055
r1 = s.get(url, verify=False, cert=cert)
3056
assert r1.status_code == 200
3057
with pytest.raises(requests.exceptions.SSLError):
3058
s.get(url, cert=cert)
3061
assert client_cert is not None
3064
def test_content_length_for_bytes_data(httpbin):
3065
data = "This is a string containing multi-byte UTF-8 ☃️"
3066
encoded_data = data.encode("utf-8")
3067
length = str(len(encoded_data))
3068
req = requests.Request("POST", httpbin("post"), data=encoded_data)
3071
assert p.headers["Content-Length"] == length
3076
reason="urllib3 2.x encodes all strings to utf-8, urllib3 1.x uses latin-1",
3078
def test_content_length_for_string_data_counts_bytes(httpbin):
3079
data = "This is a string containing multi-byte UTF-8 ☃️"
3080
length = str(len(data.encode("utf-8")))
3081
req = requests.Request("POST", httpbin("post"), data=data)
3084
assert p.headers["Content-Length"] == length
3080
length = str(len(data.encode("utf-8")))
3081
req = requests.Request("POST", httpbin("post"), data=data)
3084
assert p.headers["Content-Length"] == length
3087
def test_json_decode_errors_are_serializable_deserializable():
3088
json_decode_error = requests.exceptions.JSONDecodeError(
3090
'{"responseCode":["706"],"data":null}{"responseCode":["706"],"data":null}',
3093
deserialized_error = pickle.loads(pickle.dumps(json_decode_error))
3094
assert repr(json_decode_error) == repr(deserialized_error)