repositori / bagas / requests

requests public ready

https://github.com/psf/requests commit 3c7baaa 48 berkas · 374 potongan 2026-11-21T15:17:50Z
48berkas
36berkas python
374potongan
python 36 markdown 5 yaml 2 text 2 toml 1 css 1 html 1
tests/test_requests.pypython 3485 baris ditampilkan
1
"""Tests for Requests."""
2
3
import collections
4
import contextlib
5
import io
6
import json
7
import os
8
import pickle
9
import re
10
import tempfile
11
import threading
12
import warnings
13
from unittest import mock
14
15
import pytest
16
import urllib3
17
from urllib3.util import Timeout as Urllib3Timeout
18
19
import requests
20
from requests.adapters import HTTPAdapter
21
from requests.auth import HTTPDigestAuth, _basic_auth_str
22
from requests.compat import (
23
JSONDecodeError,
24
Morsel,
25
MutableMapping,
26
builtin_str,
27
cookielib,
28
getproxies,
29
is_urllib3_1,
30
urlparse,
31
)
32
from requests.cookies import cookiejar_from_dict, morsel_to_cookie
33
from requests.exceptions import (
34
ChunkedEncodingError,
35
ConnectionError,
36
ConnectTimeout,
37
ContentDecodingError,
38
InvalidHeader,
39
InvalidProxyURL,
40
InvalidSchema,
41
InvalidURL,
42
MissingSchema,
43
ProxyError,
44
ReadTimeout,
45
RequestException,
46
RetryError,
47
Timeout,
48
TooManyRedirects,
49
UnrewindableBodyError,
50
)
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
56
57
from . import SNIMissingWarning
58
from .compat import StringIO
59
from .testserver.server import TLSServer, consume_socket_content
60
from .utils import override_environ
56
57
from . import SNIMissingWarning
58
from .compat import StringIO
59
from .testserver.server import TLSServer, consume_socket_content
60
from .utils import override_environ
61
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"
65
66
# This is to avoid waiting the timeout of using TARPIT
67
INVALID_PROXY = "http://localhost:1"
68
69
try:
70
from ssl import SSLContext
71
72
del SSLContext
73
HAS_MODERN_SSL = True
74
except ImportError:
75
HAS_MODERN_SSL = False
76
77
try:
78
requests.pyopenssl
79
HAS_PYOPENSSL = True
80
except AttributeError:
81
HAS_PYOPENSSL = False
82
83
84
class TestRequests:
85
digest_auth_algo = ("MD5", "SHA-256", "SHA-512")
86
87
def test_entry_points(self):
88
requests.session
89
requests.session().get
90
requests.session().head
91
requests.get
92
requests.head
93
requests.put
94
requests.patch
95
requests.post
96
# Not really an entry point, but people rely on it.
97
from requests.packages.urllib3.poolmanager import PoolManager # noqa:F401
98
99
@pytest.mark.parametrize(
100
"exception, url",
101
(
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"),
109
),
110
)
111
def test_invalid_url(self, exception, url):
112
with pytest.raises(exception):
113
requests.get(url)
109
),
110
)
111
def test_invalid_url(self, exception, url):
112
with pytest.raises(exception):
113
requests.get(url)
114
115
def test_basic_building(self):
116
req = requests.Request()
117
req.url = "http://kennethreitz.org/"
118
req.data = {"life": "42"}
119
120
pr = req.prepare()
121
assert pr.url == req.url
122
assert pr.body == "life=42"
123
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
128
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"
133
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"
138
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"
144
145
def test_path_is_not_double_encoded(self):
146
request = requests.Request("GET", "http://0.0.0.0/get/test case").prepare()
147
148
assert request.path_url == "/get/test%20case"
149
150
@pytest.mark.parametrize(
151
"url, expected",
152
(
153
(
154
"http://example.com/path#fragment",
155
"http://example.com/path?a=b#fragment",
156
),
157
(
158
"http://example.com/path?key=value#fragment",
159
"http://example.com/path?key=value&a=b#fragment",
160
),
161
),
162
)
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
166
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))
170
)
171
session = requests.Session()
172
request = requests.Request(
173
"GET", "http://example.com/", params=param_ordered_dict
174
)
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
174
)
175
prep = session.prepare_request(request)
176
assert prep.url == "http://example.com/?z=1&a=1&k=1&d=1"
177
178
def test_params_bytes_are_encoded(self):
179
request = requests.Request(
180
"GET", "http://example.com", params=b"test=foo"
181
).prepare()
182
assert request.url == "http://example.com/?test=foo"
183
184
def test_binary_put(self):
185
request = requests.Request(
186
"PUT", "http://example.com", data="ööö".encode()
187
).prepare()
188
assert isinstance(request.body, bytes)
189
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/"
194
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}"
204
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()
204
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()
209
210
r = s.send(r.prepare())
211
212
assert r.status_code == 200
213
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
219
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]
227
228
def test_HTTP_307_ALLOW_REDIRECT_POST(self, httpbin):
229
r = requests.post(
230
httpbin("redirect-to"),
231
data="test",
232
params={"url": "post", "status_code": 307},
233
)
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"
238
239
def test_HTTP_307_ALLOW_REDIRECT_POST_WITH_SEEKABLE(self, httpbin):
240
byte_str = b"test"
241
r = requests.post(
242
httpbin("redirect-to"),
243
data=io.BytesIO(byte_str),
244
params={"url": "post", "status_code": 307},
245
)
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")
245
)
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")
250
251
def test_HTTP_302_TOO_MANY_REDIRECTS(self, httpbin):
252
try:
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
259
else:
260
pytest.fail("Expected redirect to raise TooManyRedirects but it did not")
261
262
def test_HTTP_302_TOO_MANY_REDIRECTS_WITH_PARAMS(self, httpbin):
263
s = requests.session()
264
s.max_redirects = 5
265
try:
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
272
else:
273
pytest.fail(
274
"Expected custom max number of redirects to be respected but was not"
275
)
276
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
283
284
def test_http_301_doesnt_change_head_to_get(self, httpbin):
285
r = requests.head(httpbin("status", "301"), allow_redirects=True)
286
print(r.content)
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
291
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
298
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
305
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
312
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
319
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)
326
327
# Mimic a redirect response
328
resp.status_code = 302
329
resp.headers["location"] = "get"
330
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
336
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
343
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")
347
resp.request = prep
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")
347
resp.request = prep
348
setattr(resp.raw, "release_conn", lambda *args: args)
349
350
# Mimic a redirect response
351
resp.status_code = 302
352
resp.headers["location"] = httpbin("get")
353
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
359
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)
363
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
367
368
def test_HTTP_200_OK_GET_WITH_PARAMS(self, httpbin):
369
heads = {"User-agent": "Mozilla/5.0"}
370
371
r = requests.get(httpbin("user-agent"), headers=heads)
372
373
assert heads["User-agent"] in r.text
374
assert r.status_code == 200
375
376
def test_HTTP_200_OK_GET_WITH_MIXED_PARAMS(self, httpbin):
377
heads = {"User-agent": "Mozilla/5.0"}
378
379
r = requests.get(
380
httpbin("get") + "?test=true", params={"q": "test"}, headers=heads
381
)
382
assert r.status_code == 200
378
379
r = requests.get(
380
httpbin("get") + "?test=true", params={"q": "test"}, headers=heads
381
)
382
assert r.status_code == 200
383
384
def test_set_cookie_on_301(self, httpbin):
385
s = requests.session()
386
url = httpbin("cookies/set?foo=bar")
387
s.get(url)
388
assert s.cookies["foo"] == "bar"
389
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"]
395
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"
400
s.get(
401
httpbin("response-headers"),
402
params={"Set-Cookie": "foo=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT"},
403
)
404
assert "foo" not in s.cookies
405
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"'
410
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"]
416
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"
424
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
429
assert not s.cookies
430
431
def test_generic_cookiejar_works(self, httpbin):
432
cj = cookielib.CookieJar()
433
cookiejar_from_dict({"foo": "bar"}, cj)
434
s = requests.session()
435
s.cookies = cj
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
441
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"
449
450
def test_cookielib_cookiejar_on_redirect(self, httpbin):
451
"""Tests resolve_redirect doesn't fail when merging cookies
452
with non-RequestsCookieJar cookiejar.
453
454
See GH #3579
455
"""
456
cj = cookiejar_from_dict({"foo": "bar"}, cookielib.CookieJar())
457
s = requests.Session()
458
s.cookies = cookiejar_from_dict({"cookie": "tasty"})
454
See GH #3579
455
"""
456
cj = cookiejar_from_dict({"foo": "bar"}, cookielib.CookieJar())
457
s = requests.Session()
458
s.cookies = cookiejar_from_dict({"cookie": "tasty"})
459
460
# Prepare request without using Session
461
req = requests.Request("GET", httpbin("headers"), cookies=cj)
462
prep_req = req.prepare()
463
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)
470
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)
475
476
cookies = {}
477
for c in resp.request._cookies:
478
cookies[c.name] = c.value
479
assert cookies["foo"] == "bar"
480
assert cookies["cookie"] == "tasty"
481
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
487
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)
495
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
503
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")
523
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)"}
527
528
r = requests.get(httpbin("user-agent"), headers=heads)
529
assert heads[key] in r.text
530
531
def test_HTTP_200_OK_HEAD(self, httpbin):
532
r = requests.head(httpbin("get"))
533
assert r.status_code == 200
534
535
def test_HTTP_200_OK_PUT(self, httpbin):
536
r = requests.put(httpbin("put"))
537
assert r.status_code == 200
538
539
def test_BASICAUTH_TUPLE_HTTP_200_OK_GET(self, httpbin):
540
auth = ("user", "pass")
541
url = httpbin("basic-auth", "user", "pass")
542
543
r = requests.get(url, auth=auth)
544
assert r.status_code == 200
545
546
r = requests.get(url)
547
assert r.status_code == 401
548
549
s = requests.session()
550
s.auth = auth
551
r = s.get(url)
552
assert r.status_code == 200
553
554
@pytest.mark.parametrize(
555
"username, password",
556
(
557
("user", "pass"),
558
("имя".encode(), "пароль".encode()),
559
(42, 42),
560
(None, None),
561
),
562
)
563
def test_set_basicauth(self, httpbin, username, password):
564
auth = (username, password)
565
url = httpbin("get")
561
),
562
)
563
def test_set_basicauth(self, httpbin, username, password):
564
auth = (username, password)
565
url = httpbin("get")
566
567
r = requests.Request("GET", url, auth=auth)
568
p = r.prepare()
569
570
assert p.headers["Authorization"] == _basic_auth_str(username, password)
571
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.
575
"""
576
auth = (b"\xc5\xafsername", b"test\xc6\xb6")
577
r = requests.Request("GET", "http://localhost", auth=auth)
578
p = r.prepare()
579
580
assert p.headers["Authorization"] == "Basic xa9zZXJuYW1lOnRlc3TGtg=="
581
582
@pytest.mark.parametrize(
583
"url, exception",
584
(
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),
591
),
592
)
593
def test_errors(self, url, exception):
594
with pytest.raises(exception):
595
requests.get(url, timeout=1)
596
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):
600
requests.get(
601
"http://localhost:1", proxies={"http": "non-resolvable-address"}
602
)
598
# any proxy related error (address resolution, no route to host, etc) should result in a ProxyError
599
with pytest.raises(ProxyError):
600
requests.get(
601
"http://localhost:1", proxies={"http": "non-resolvable-address"}
602
)
603
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"})
607
608
with pytest.raises(InvalidProxyURL):
609
requests.get(httpbin(), proxies={"http": "http://:8080"})
610
611
with pytest.raises(InvalidProxyURL):
612
requests.get(httpbin_secure(), proxies={"https": "https://"})
613
614
with pytest.raises(InvalidProxyURL):
615
requests.get(httpbin(), proxies={"http": "http:///example.com:8080"})
616
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())
623
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)
631
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")
637
print(url)
638
request = requests.Request("GET", url)
639
session.send(request.prepare())
640
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())
646
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())
652
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", {})
659
660
assert sent_headers.get("Proxy-Authorization") == proxy_auth_value
661
662
@pytest.mark.parametrize(
663
"url,has_proxy_auth",
664
(
665
("http://example.com", True),
666
("https://example.com", False),
667
),
668
)
669
def test_proxy_authorization_not_appended_to_https_request(
670
self, url, has_proxy_auth
671
):
672
session = requests.Session()
673
proxies = {
674
"http": "http://test:pass@localhost:8080",
675
"https": "http://test:pass@localhost:8090",
676
}
677
req = requests.Request("GET", url)
678
prep = req.prepare()
679
session.rebuild_proxies(prep, proxies)
680
681
assert ("Proxy-Authorization" in prep.headers) is has_proxy_auth
682
683
def test_basicauth_with_netrc(self, httpbin):
684
auth = ("user", "pass")
685
wrong_auth = ("wronguser", "wrongpass")
686
url = httpbin("basic-auth", "user", "pass")
687
688
old_auth = requests.sessions.get_netrc_auth
689
690
try:
691
692
def get_netrc_auth_mock(url):
693
return auth
694
695
requests.sessions.get_netrc_auth = get_netrc_auth_mock
696
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
696
697
# Should use netrc and work.
698
r = requests.get(url)
699
assert r.status_code == 200
700
701
# Given auth should override and fail.
702
r = requests.get(url, auth=wrong_auth)
703
assert r.status_code == 401
704
705
s = requests.session()
706
707
# Should use netrc and work.
708
r = s.get(url)
709
assert r.status_code == 200
710
711
# Given auth should override and fail.
712
s.auth = wrong_auth
713
r = s.get(url)
714
assert r.status_code == 401
715
finally:
716
requests.sessions.get_netrc_auth = old_auth
717
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}"
723
724
netrc_file = ""
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")
732
fp.close()
733
netrc_file = fp.name
734
735
old_netrc = os.environ.get("NETRC", "")
736
os.environ["NETRC"] = netrc_file
732
fp.close()
733
netrc_file = fp.name
734
735
old_netrc = os.environ.get("NETRC", "")
736
os.environ["NETRC"] = netrc_file
737
738
try:
739
# Should use netrc
740
# Make sure that we don't use the example.com credentials
741
# for the request
742
r = requests.get(url)
743
assert r.status_code == 200
744
finally:
745
os.environ["NETRC"] = old_netrc
746
os.unlink(netrc_file)
747
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")
752
753
r = requests.get(url, auth=auth)
754
assert r.status_code == 200
755
756
r = requests.get(url)
757
assert r.status_code == 401
758
print(r.headers["WWW-Authenticate"])
759
760
s = requests.session()
761
s.auth = HTTPDigestAuth("user", "pass")
762
r = s.get(url)
763
assert r.status_code == 200
764
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"
771
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"
771
772
r = requests.get(url, auth=auth)
773
assert r.status_code == 200
774
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"
782
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)
787
788
r = requests.get(url, auth=auth, stream=True)
789
assert r.raw.read() != b""
790
791
r = requests.get(url, auth=auth, stream=False)
792
assert r.raw.read() == b""
793
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)
798
799
r = requests.get(url, auth=auth)
800
assert r.status_code == 401
801
802
r = requests.get(url)
803
assert r.status_code == 401
804
805
s = requests.session()
806
s.auth = auth
807
r = s.get(url)
808
assert r.status_code == 401
804
805
s = requests.session()
806
s.auth = auth
807
r = s.get(url)
808
assert r.status_code == 401
809
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)
814
815
r = requests.get(url, auth=auth)
816
assert '"auth"' in r.request.headers["Authorization"]
817
818
def test_POSTBIN_GET_POST_FILES(self, httpbin):
819
url = httpbin("post")
820
requests.post(url).raise_for_status()
821
822
post1 = requests.post(url, data={"some": "data"})
823
assert post1.status_code == 200
824
825
with open("requirements-dev.txt") as f:
826
post2 = requests.post(url, files={"some": f})
827
assert post2.status_code == 200
828
829
post4 = requests.post(url, data='[{"some": "json"}]')
830
assert post4.status_code == 200
831
832
with pytest.raises(ValueError):
833
requests.post(url, files=["bad file data"])
834
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
840
841
def test_POSTBIN_SEEKED_OBJECT_WITH_NO_ITER(self, httpbin):
842
class TestStream:
843
def __init__(self, data):
844
self.data = data.encode()
845
self.length = len(self.data)
846
self.index = 0
842
class TestStream:
843
def __init__(self, data):
844
self.data = data.encode()
845
self.length = len(self.data)
846
self.index = 0
847
848
def __len__(self):
849
return self.length
850
851
def read(self, size=None):
852
if size:
853
ret = self.data[self.index : self.index + size]
854
self.index += size
855
else:
856
ret = self.data[self.index :]
857
self.index = self.length
858
return ret
859
860
def tell(self):
861
return self.index
862
863
def seek(self, offset, where=0):
864
if where == 0:
865
self.index = offset
866
elif where == 1:
867
self.index += offset
868
elif where == 2:
869
self.index = self.length + offset
870
871
test = TestStream("test")
872
post1 = requests.post(httpbin("post"), data=test)
873
assert post1.status_code == 200
874
assert post1.json()["data"] == "test"
875
876
test = TestStream("test")
877
test.seek(2)
878
post2 = requests.post(httpbin("post"), data=test)
879
assert post2.status_code == 200
880
assert post2.json()["data"] == "st"
881
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"
881
882
def test_POSTBIN_GET_POST_FILES_WITH_DATA(self, httpbin):
883
url = httpbin("post")
884
requests.post(url).raise_for_status()
885
886
post1 = requests.post(url, data={"some": "data"})
887
assert post1.status_code == 200
888
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
892
893
post4 = requests.post(url, data='[{"some": "json"}]')
894
assert post4.status_code == 200
895
896
with pytest.raises(ValueError):
897
requests.post(url, files=["bad file data"])
898
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)
903
904
def __delitem__(self, key):
905
del self.data[key]
906
907
def __getitem__(self, key):
908
return self.data[key]
909
910
def __setitem__(self, key, value):
911
self.data[key] = value
912
913
def __iter__(self):
914
return iter(self.data)
915
916
def __len__(self):
917
return len(self.data)
918
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"}
918
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"}
923
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})
929
930
def test_request_ok_set(self, httpbin):
931
r = requests.get(httpbin("status", "404"))
932
assert not r.ok
933
934
def test_status_raising(self, httpbin):
935
r = requests.get(httpbin("status", "404"))
936
with pytest.raises(requests.exceptions.HTTPError):
937
r.raise_for_status()
938
939
r = requests.get(httpbin("status", "500"))
940
assert not r.ok
941
942
def test_decompress_gzip(self, httpbin):
943
r = requests.get(httpbin("gzip"))
944
r.content.decode("ascii")
945
946
@pytest.mark.parametrize(
947
"url, params",
948
(
949
("/get", {"foo": "føø"}),
950
("/get", {"føø": "føø"}),
951
("/get", {"føø": "føø"}),
952
("/get", {"foo": "foo"}),
953
("ø", {"foo": "foo"}),
954
),
955
)
956
def test_unicode_get(self, httpbin, url, params):
957
requests.get(httpbin(url), params=params)
958
959
def test_unicode_header_name(self, httpbin):
960
requests.put(
961
httpbin("put"),
962
headers={"Content-Type": "application/octet-stream"},
963
data="\xff",
964
) # compat.str is unicode.
960
requests.put(
961
httpbin("put"),
962
headers={"Content-Type": "application/octet-stream"},
963
data="\xff",
964
) # compat.str is unicode.
965
966
def test_pyopenssl_redirect(self, httpbin_secure, httpbin_ca_bundle):
967
requests.get(httpbin_secure("status", "301"), verify=httpbin_ca_bundle)
968
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)
973
assert (
974
str(e.value)
975
== f"Could not find a suitable TLS CA certificate bundle, invalid path: {INVALID_PATH}"
976
)
977
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)
982
assert (
983
str(e.value)
984
== f"Could not find the TLS certificate file, invalid path: {INVALID_PATH}"
985
)
986
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}"
991
)
992
993
@pytest.mark.parametrize(
994
"env, expected",
995
(
996
({}, True),
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),
1002
(
1003
{
1004
"REQUESTS_CA_BUNDLE": "/some/path",
1005
"CURL_CA_BUNDLE": "/curl/path",
1006
},
1007
"/some/path",
1008
),
1009
(
1010
{
1011
"REQUESTS_CA_BUNDLE": "",
1012
"CURL_CA_BUNDLE": "/curl/path",
1013
},
1014
"/curl/path",
1015
),
1016
),
1017
)
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
1023
)
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
1023
)
1024
assert settings["verify"] == expected
1025
1026
def test_http_with_certificate(self, httpbin):
1027
r = requests.get(httpbin(), cert=".")
1028
assert r.status_code == 200
1029
1030
@pytest.mark.skipif(
1031
SNIMissingWarning is None,
1032
reason="urllib3 2.0 removed that warning and errors out instead",
1033
)
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",)
1039
else:
1040
warnings_expected = (
1041
"SNIMissingWarning",
1042
"InsecurePlatformWarning",
1043
"SubjectAltNameWarning",
1044
)
1045
1046
with pytest.warns() as warning_records:
1047
warnings.simplefilter("always")
1048
requests.get(f"https://localhost:{port}/", verify=ca_bundle)
1049
1050
warning_records = [
1051
item
1052
for item in warning_records
1053
if item.category.__name__ != "ResourceWarning"
1054
]
1055
1056
warnings_category = tuple(item.category.__name__ for item in warning_records)
1057
assert warnings_category == warnings_expected
1053
if item.category.__name__ != "ResourceWarning"
1054
]
1055
1056
warnings_category = tuple(item.category.__name__ for item in warning_records)
1057
assert warnings_category == warnings_expected
1058
1059
def test_certificate_failure(self, httpbin_secure):
1060
"""
1061
When underlying SSL problems occur, an SSLError is raised.
1062
"""
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"))
1067
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")
1072
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"])
1076
)
1077
prep = r.prepare()
1078
assert prep.body == "test=foo&test=baz"
1079
1080
def test_different_encodings_dont_break_post(self, httpbin):
1081
with open(__file__, "rb") as f:
1082
r = requests.post(
1083
httpbin("post"),
1084
data={"stuff": json.dumps({"a": 123})},
1085
params={"blah": "asdf1234"},
1086
files={"file": ("test_requests.py", f)},
1087
)
1088
assert r.status_code == 200
1084
data={"stuff": json.dumps({"a": 123})},
1085
params={"blah": "asdf1234"},
1086
files={"file": ("test_requests.py", f)},
1087
)
1088
assert r.status_code == 200
1089
1090
def test_post_named_tempfile(self, httpbin):
1091
with tempfile.NamedTemporaryFile(mode="w+") as f:
1092
f.write("named temp file contents\n")
1093
f.seek(0)
1094
r = requests.post(
1095
httpbin("post"),
1096
files={"file": f},
1097
)
1098
assert r.status_code == 200
1099
assert r.json()["files"]["file"] == "named temp file contents\n"
1100
1101
def test_post_getattr_proxy_read_only(self, httpbin):
1102
1103
class ReadProxy:
1104
def __init__(self):
1105
self._file = io.BytesIO(b"streamed body")
1106
1107
def __getattr__(self, name):
1108
if name == "__iter__":
1109
raise AttributeError(name)
1110
return getattr(self._file, name)
1111
1112
r = requests.post(httpbin("post"), data=ReadProxy())
1113
assert r.status_code == 200
1114
assert r.json()["data"] == "streamed body"
1115
1116
@pytest.mark.parametrize(
1117
"data",
1118
(
1119
{"stuff": "ëlïxr"},
1120
{"stuff": "ëlïxr".encode()},
1121
{"stuff": "elixr"},
1122
{"stuff": b"elixr"},
1123
),
1124
)
1125
def test_unicode_multipart_post(self, httpbin, data):
1126
with open(__file__, "rb") as f:
1127
r = requests.post(
1128
httpbin("post"),
1129
data=data,
1130
files={"file": ("test_requests.py", f)},
1131
)
1132
assert r.status_code == 200
1128
httpbin("post"),
1129
data=data,
1130
files={"file": ("test_requests.py", f)},
1131
)
1132
assert r.status_code == 200
1133
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(
1138
method="POST",
1139
url=httpbin("post"),
1140
data={b"stuff": "elixr"},
1141
files={"file": ("test_requests.py", f)},
1142
)
1143
prep = r.prepare()
1144
1145
assert b'name="stuff"' in prep.body
1146
assert b"name=\"b'stuff'\"" not in prep.body
1147
1148
def test_unicode_method_name(self, httpbin):
1149
with open(__file__, "rb") as f:
1150
files = {"file": f}
1151
r = requests.request(
1152
method="POST",
1153
url=httpbin("post"),
1154
files=files,
1155
)
1156
assert r.status_code == 200
1157
1158
def test_unicode_method_name_with_request_object(self, httpbin):
1159
s = requests.Session()
1160
with open(__file__, "rb") as f:
1161
files = {"file": 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"
1166
1167
resp = s.send(prep)
1168
assert resp.status_code == 200
1164
assert isinstance(prep.method, builtin_str)
1165
assert prep.method == "POST"
1166
1167
resp = s.send(prep)
1168
assert resp.status_code == 200
1169
1170
def test_non_prepared_request_error(self):
1171
s = requests.Session()
1172
req = requests.Request("POST", "/")
1173
1174
with pytest.raises(ValueError) as e:
1175
s.send(req)
1176
assert str(e.value) == "You can only send PreparedRequests."
1177
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})}
1182
files = {
1183
"file1": ("test_requests.py", f1),
1184
"file2": ("test_requests", f2, "text/py-content-type"),
1185
}
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
1189
1190
def test_hook_receives_request_arguments(self, httpbin):
1191
def hook(resp, **kwargs):
1192
assert resp is not None
1193
assert kwargs != {}
1194
1195
s = requests.Session()
1196
r = requests.Request("GET", httpbin(), hooks={"response": hook})
1197
prep = s.prepare_request(r)
1198
s.send(prep)
1199
1200
def test_session_hooks_are_used_with_no_request_hooks(self, httpbin):
1201
def hook(*args, **kwargs):
1202
pass
1198
s.send(prep)
1199
1200
def test_session_hooks_are_used_with_no_request_hooks(self, httpbin):
1201
def hook(*args, **kwargs):
1202
pass
1203
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]
1210
1211
def test_session_hooks_are_overridden_by_request_hooks(self, httpbin):
1212
def hook1(*args, **kwargs):
1213
pass
1214
1215
def hook2(*args, **kwargs):
1216
pass
1217
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]
1224
1225
def test_prepared_request_hook(self, httpbin):
1226
def hook(resp, **kwargs):
1227
resp.hook_working = True
1228
return resp
1229
1230
req = requests.Request("GET", httpbin(), hooks={"response": hook})
1231
prep = req.prepare()
1232
1233
s = requests.Session()
1234
s.proxies = getproxies()
1235
resp = s.send(prep)
1236
1237
assert hasattr(resp, "hook_working")
1238
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"
1243
return r
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"
1243
return r
1244
1245
req = requests.Request("GET", httpbin("headers"))
1246
assert not req.auth
1247
1248
s = requests.Session()
1249
s.auth = DummyAuth()
1250
1251
prep = s.prepare_request(req)
1252
resp = s.send(prep)
1253
1254
assert resp.json()["headers"]["Dummy-Auth-Test"] == "dummy-auth-test-ok"
1255
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/"
1261
1262
def test_request_with_bytestring_host(self, httpbin):
1263
s = requests.Session()
1264
resp = s.request(
1265
"GET",
1266
httpbin("cookies/set?cookie=value"),
1267
allow_redirects=False,
1268
headers={"Host": b"httpbin.org"},
1269
)
1270
assert resp.cookies.get("cookie") == "value"
1271
1272
def test_links(self):
1273
r = requests.Response()
1274
r.headers = {
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",
1282
"link": (
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>; "
1286
' rel="last"'
1287
),
1288
"server": "GitHub.com",
1289
"status": "200 OK",
1290
"vary": "Accept",
1291
"x-content-type-options": "nosniff",
1292
"x-github-media-type": "github.beta",
1293
"x-ratelimit-limit": "60",
1294
"x-ratelimit-remaining": "57",
1295
}
1296
assert r.links["next"]["rel"] == "next"
1292
"x-github-media-type": "github.beta",
1293
"x-ratelimit-limit": "60",
1294
"x-ratelimit-remaining": "57",
1295
}
1296
assert r.links["next"]["rel"] == "next"
1297
1298
def test_cookie_parameters(self):
1299
key = "some_cookie"
1300
value = "some_value"
1301
secure = True
1302
domain = "test.com"
1303
rest = {"HttpOnly": True}
1304
1305
jar = requests.cookies.RequestsCookieJar()
1306
jar.set(key, value, secure=secure, domain=domain, rest=rest)
1307
1308
assert len(jar) == 1
1309
assert "some_cookie" in jar
1310
1311
cookie = list(jar)[0]
1312
assert cookie.secure == secure
1313
assert cookie.domain == domain
1314
assert cookie._rest["HttpOnly"] == rest["HttpOnly"]
1315
1316
def test_cookie_as_dict_keeps_len(self):
1317
key = "some_cookie"
1318
value = "some_value"
1319
1320
key1 = "some_cookie1"
1321
value1 = "some_value1"
1322
1323
jar = requests.cookies.RequestsCookieJar()
1324
jar.set(key, value)
1325
jar.set(key1, value1)
1326
1327
d1 = dict(jar)
1328
d2 = dict(jar.iteritems())
1329
d3 = dict(jar.items())
1330
1331
assert len(jar) == 2
1332
assert len(d1) == 2
1333
assert len(d2) == 2
1334
assert len(d3) == 2
1335
1336
def test_cookie_as_dict_keeps_items(self):
1337
key = "some_cookie"
1338
value = "some_value"
1339
1340
key1 = "some_cookie1"
1341
value1 = "some_value1"
1342
1343
jar = requests.cookies.RequestsCookieJar()
1344
jar.set(key, value)
1345
jar.set(key1, value1)
1341
value1 = "some_value1"
1342
1343
jar = requests.cookies.RequestsCookieJar()
1344
jar.set(key, value)
1345
jar.set(key1, value1)
1346
1347
d1 = dict(jar)
1348
d2 = dict(jar.iteritems())
1349
d3 = dict(jar.items())
1350
1351
assert d1["some_cookie"] == "some_value"
1352
assert d2["some_cookie"] == "some_value"
1353
assert d3["some_cookie1"] == "some_value1"
1354
1355
def test_cookie_as_dict_keys(self):
1356
key = "some_cookie"
1357
value = "some_value"
1358
1359
key1 = "some_cookie1"
1360
value1 = "some_value1"
1361
1362
jar = requests.cookies.RequestsCookieJar()
1363
jar.set(key, value)
1364
jar.set(key1, value1)
1365
1366
keys = jar.keys()
1367
assert keys == list(keys)
1368
# make sure one can use keys multiple times
1369
assert list(keys) == list(keys)
1370
1371
def test_cookie_as_dict_values(self):
1372
key = "some_cookie"
1373
value = "some_value"
1374
1375
key1 = "some_cookie1"
1376
value1 = "some_value1"
1377
1378
jar = requests.cookies.RequestsCookieJar()
1379
jar.set(key, value)
1380
jar.set(key1, value1)
1381
1382
values = jar.values()
1383
assert values == list(values)
1384
# make sure one can use values multiple times
1385
assert list(values) == list(values)
1386
1387
def test_cookie_as_dict_items(self):
1388
key = "some_cookie"
1389
value = "some_value"
1390
1391
key1 = "some_cookie1"
1392
value1 = "some_value1"
1388
key = "some_cookie"
1389
value = "some_value"
1390
1391
key1 = "some_cookie1"
1392
value1 = "some_value1"
1393
1394
jar = requests.cookies.RequestsCookieJar()
1395
jar.set(key, value)
1396
jar.set(key1, value1)
1397
1398
items = jar.items()
1399
assert items == list(items)
1400
# make sure one can use items multiple times
1401
assert list(items) == list(items)
1402
1403
def test_cookie_duplicate_names_different_domains(self):
1404
key = "some_cookie"
1405
value = "some_value"
1406
domain1 = "test1.com"
1407
domain2 = "test2.com"
1408
1409
jar = requests.cookies.RequestsCookieJar()
1410
jar.set(key, value, domain=domain1)
1411
jar.set(key, value, domain=domain2)
1412
assert key in jar
1413
items = jar.items()
1414
assert len(items) == 2
1415
1416
# Verify that CookieConflictError is raised if domain is not specified
1417
with pytest.raises(requests.cookies.CookieConflictError):
1418
jar.get(key)
1419
1420
# Verify that CookieConflictError is not raised if domain is specified
1421
cookie = jar.get(key, domain=domain1)
1422
assert cookie == value
1423
1424
def test_cookie_duplicate_names_raises_cookie_conflict_error(self):
1425
key = "some_cookie"
1426
value = "some_value"
1427
path = "some_path"
1428
1429
jar = requests.cookies.RequestsCookieJar()
1430
jar.set(key, value, path=path)
1431
jar.set(key, value)
1432
with pytest.raises(requests.cookies.CookieConflictError):
1433
jar.get(key)
1429
jar = requests.cookies.RequestsCookieJar()
1430
jar.set(key, value, path=path)
1431
jar.set(key, value)
1432
with pytest.raises(requests.cookies.CookieConflictError):
1433
jar.get(key)
1434
1435
def test_cookie_policy_copy(self):
1436
class MyCookiePolicy(cookielib.DefaultCookiePolicy):
1437
pass
1438
1439
jar = requests.cookies.RequestsCookieJar()
1440
jar.set_policy(MyCookiePolicy())
1441
assert isinstance(jar.copy().get_policy(), MyCookiePolicy)
1442
1443
def test_time_elapsed_blank(self, httpbin):
1444
r = requests.get(httpbin("get"))
1445
td = r.elapsed
1446
total_seconds = (
1447
td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6
1448
) / 10**6
1449
assert total_seconds > 0.0
1450
1451
def test_empty_response_has_content_none(self):
1452
r = requests.Response()
1453
assert r.content is None
1454
1455
def test_response_is_iterable(self):
1456
r = requests.Response()
1457
io = StringIO.StringIO("abc")
1458
read_ = io.read
1459
1460
def read_mock(amt, decode_content=None):
1461
return read_(amt)
1462
1463
setattr(io, "read", read_mock)
1464
r.raw = io
1465
assert next(iter(r))
1466
io.close()
1467
1468
def test_response_decode_unicode(self):
1469
"""When called with decode_unicode, Response.iter_content should always
1470
return unicode.
1471
"""
1472
r = requests.Response()
1473
r._content_consumed = True
1474
r._content = b"the content"
1475
r.encoding = "ascii"
1471
"""
1472
r = requests.Response()
1473
r._content_consumed = True
1474
r._content = b"the content"
1475
r.encoding = "ascii"
1476
1477
chunks = r.iter_content(decode_unicode=True)
1478
assert all(isinstance(chunk, str) for chunk in chunks)
1479
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)
1486
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()
1492
r.status_code = 404
1493
r.encoding = None
1494
assert not r.ok # old behaviour - crashes here
1495
1496
def test_response_reason_unicode_fallback(self):
1497
# check raise_status falls back to ISO-8859-1
1498
r = requests.Response()
1499
r.url = "some url"
1500
reason = "Komponenttia ei löydy"
1501
r.reason = reason.encode("latin-1")
1502
r.status_code = 500
1503
r.encoding = None
1504
with pytest.raises(requests.exceptions.HTTPError) as e:
1505
r.raise_for_status()
1506
assert reason in e.value.args[0]
1507
1508
def test_response_chunk_size_type(self):
1509
"""Ensure that chunk_size is passed as None or an integer, otherwise
1510
raise a TypeError.
1511
"""
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)
1511
"""
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)
1516
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"]
1521
1522
r = requests.Response()
1523
r.raw = io.BytesIO(b"the content")
1524
with pytest.raises(TypeError):
1525
chunks = r.iter_content("1024")
1526
1527
@pytest.mark.parametrize(
1528
"exception, args, expected",
1529
(
1530
(urllib3.exceptions.ProtocolError, tuple(), ChunkedEncodingError),
1531
(urllib3.exceptions.DecodeError, tuple(), ContentDecodingError),
1532
(urllib3.exceptions.ReadTimeoutError, (None, "", ""), ConnectionError),
1533
(urllib3.exceptions.SSLError, tuple(), RequestsSSLError),
1534
),
1535
)
1536
def test_iter_content_wraps_exceptions(self, httpbin, exception, args, expected):
1537
r = requests.Response()
1538
r.raw = mock.Mock()
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)
1542
1543
with pytest.raises(expected):
1544
next(r.iter_content(1024))
1545
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))
1545
1546
def test_request_and_response_are_pickleable(self, httpbin):
1547
r = requests.get(httpbin("get"))
1548
1549
# verify we can pickle the original request
1550
assert pickle.loads(pickle.dumps(r.request))
1551
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
1557
1558
def test_prepared_request_is_pickleable(self, httpbin):
1559
p = requests.Request("GET", httpbin("get")).prepare()
1560
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
1566
1567
# Verify unpickled PreparedRequest sends properly
1568
s = requests.Session()
1569
resp = s.send(r)
1570
assert resp.status_code == 200
1571
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})
1575
p = r.prepare()
1576
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
1582
1583
# Verify unpickled PreparedRequest sends properly
1584
s = requests.Session()
1585
resp = s.send(r)
1586
assert resp.status_code == 200
1587
1588
def test_prepared_request_with_hook_is_pickleable(self, httpbin):
1589
r = requests.Request("GET", httpbin("get"), hooks=default_hooks())
1590
p = r.prepare()
1591
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
1598
1599
# Verify unpickled PreparedRequest sends properly
1600
s = requests.Session()
1601
resp = s.send(r)
1602
assert resp.status_code == 200
1603
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)
1608
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
1618
1619
def test_session_pickling(self, httpbin):
1620
r = requests.Request("GET", httpbin("get"))
1621
s = requests.Session()
1622
1623
s = pickle.loads(pickle.dumps(s))
1624
s.proxies = getproxies()
1625
1626
r = s.send(r.prepare())
1627
assert r.status_code == 200
1628
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"
1639
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()
1646
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())
1655
order = [
1656
"http://github.com/about/",
1657
"http://github.com",
1658
"http://github",
1659
"http://git",
1660
"https://",
1661
"http://",
1662
]
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())
1667
order = [
1668
"http://github.com/about/",
1669
"http://gittip.com/about/",
1670
"http://github.com",
1671
"http://gittip.com",
1672
"http://github",
1673
"http://gittip",
1674
"http://git",
1675
"https://",
1676
"http://",
1677
]
1678
assert order == list(s.adapters)
1679
s2 = requests.Session()
1680
s2.adapters = {"http://": HTTPAdapter()}
1675
"https://",
1676
"http://",
1677
]
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
1684
1685
def test_session_get_adapter_prefix_matching(self):
1686
prefix = "https://example.com"
1687
more_specific_prefix = prefix + "/some/path"
1688
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/"
1692
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)
1698
1699
assert s.get_adapter(url_matching_only_prefix) is prefix_adapter
1700
assert (
1701
s.get_adapter(url_matching_more_specific_prefix)
1702
is more_specific_prefix_adapter
1703
)
1704
assert s.get_adapter(url_not_matching_prefix) not in (
1705
prefix_adapter,
1706
more_specific_prefix_adapter,
1707
)
1708
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"
1707
)
1708
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"
1712
1713
s = requests.Session()
1714
my_adapter = HTTPAdapter()
1715
s.mount(mixed_case_prefix, my_adapter)
1716
1717
assert s.get_adapter(url_matching_prefix) is my_adapter
1718
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"
1723
)
1724
1725
s = requests.Session()
1726
my_adapter = HTTPAdapter()
1727
s.mount(mixed_case_prefix, my_adapter)
1728
1729
assert s.get_adapter(url_matching_prefix_with_different_case) is my_adapter
1730
1731
def test_session_get_adapter_prefix_with_trailing_slash(self):
1732
# from issue #6935
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"
1736
1737
s = requests.Session()
1738
adapter = HTTPAdapter()
1739
s.mount(prefix, adapter)
1740
1741
assert s.get_adapter(url_matching_prefix) is adapter
1742
assert s.get_adapter(url_not_matching_prefix) is not adapter
1743
1744
def test_session_get_adapter_prefix_without_trailing_slash(self):
1745
# from issue #6935
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):
1745
# from issue #6935
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"
1749
1750
s = requests.Session()
1751
adapter = HTTPAdapter()
1752
s.mount(prefix, adapter)
1753
1754
assert s.get_adapter(url_matching_prefix) is adapter
1755
assert s.get_adapter(url_extended_hostname) is adapter
1756
1757
def test_header_remove_is_case_insensitive(self, httpbin):
1758
# From issue #1321
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
1763
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"}
1769
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",
1775
)
1776
r = requests.Request("GET", url).prepare()
1777
assert r.url == url
1778
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)
1782
p = r.prepare()
1778
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)
1782
p = r.prepare()
1783
1784
# This is testing that they are builtin strings. A bit weird, but there
1785
# we go.
1786
assert "unicode" in p.headers.keys()
1787
assert "byte" in p.headers.keys()
1788
1789
def test_header_validation(self, httpbin):
1790
"""Ensure prepare_headers regex isn't flagging valid header contents."""
1791
valid_headers = {
1792
"foo": "bar baz qux",
1793
"bar": b"fbbq",
1794
"baz": "",
1795
"qux": "1",
1796
}
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]
1800
1801
@pytest.mark.parametrize(
1802
"invalid_header, key",
1803
(
1804
({"foo": 3}, "foo"),
1805
({"bar": {"foo": "bar"}}, "bar"),
1806
({"baz": ["foo", "bar"]}, "baz"),
1807
),
1808
)
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
1812
"""
1813
with pytest.raises(InvalidHeader) as excinfo:
1814
requests.get(httpbin("get"), headers=invalid_header)
1815
assert key in str(excinfo.value)
1816
1817
@pytest.mark.parametrize(
1818
"invalid_header",
1819
(
1820
{"foo": "bar\r\nbaz: qux"},
1821
{"foo": "bar\n\rbaz: qux"},
1822
{"foo": "bar\nbaz: qux"},
1823
{"foo": "bar\rbaz: qux"},
1824
{"fo\ro": "bar"},
1825
{"fo\r\no": "bar"},
1826
{"fo\n\ro": "bar"},
1827
{"fo\no": "bar"},
1828
{"foo": "bar\n"},
1829
{"foo\n": "bar"},
1830
{"foo": "bar\r\n"},
1831
{"foo": "\n"},
1832
{"foo": "\r\n"},
1833
),
1834
)
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.
1838
"""
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.
1838
"""
1839
with pytest.raises(InvalidHeader):
1840
requests.get(httpbin("get"), headers=invalid_header)
1841
1842
@pytest.mark.parametrize(
1843
"invalid_header",
1844
(
1845
{" foo": "bar"},
1846
{"\tfoo": "bar"},
1847
{" foo": "bar"},
1848
{"foo": " bar"},
1849
{"foo": " bar"},
1850
{"foo": "\tbar"},
1851
{" ": "bar"},
1852
),
1853
)
1854
def test_header_no_leading_space(self, httpbin, invalid_header):
1855
"""Ensure headers containing leading whitespace raise
1856
InvalidHeader Error before sending.
1857
"""
1858
with pytest.raises(InvalidHeader):
1859
requests.get(httpbin("get"), headers=invalid_header)
1860
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.
1865
"""
1866
1867
class MyString(str):
1868
pass
1869
1870
class MyBytes(bytes):
1871
pass
1872
1873
r_str = requests.get(httpbin("get"), headers={MyString("x-custom"): "myheader"})
1874
assert r_str.request.headers["x-custom"] == "myheader"
1875
1876
r_bytes = requests.get(
1877
httpbin("get"), headers={MyBytes(b"x-custom"): b"myheader"}
1878
)
1879
assert r_bytes.request.headers["x-custom"] == b"myheader"
1875
1876
r_bytes = requests.get(
1877
httpbin("get"), headers={MyBytes(b"x-custom"): b"myheader"}
1878
)
1879
assert r_bytes.request.headers["x-custom"] == b"myheader"
1880
1881
r_mixed = requests.get(
1882
httpbin("get"), headers={MyString("x-custom"): MyBytes(b"myheader")}
1883
)
1884
assert r_mixed.request.headers["x-custom"] == b"myheader"
1885
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)
1891
p = r.prepare()
1892
assert "multipart/form-data" in p.headers["Content-Type"]
1893
1894
def test_can_send_file_object_with_non_string_filename(self, httpbin):
1895
f = io.BytesIO()
1896
f.name = 2
1897
r = requests.Request("POST", httpbin("post"), files={"f": f})
1898
p = r.prepare()
1899
1900
assert "multipart/form-data" in p.headers["Content-Type"]
1901
1902
def test_autoset_header_values_are_native(self, httpbin):
1903
data = "this is a string"
1904
length = "16"
1905
req = requests.Request("POST", httpbin("post"), data=data)
1906
p = req.prepare()
1907
1908
assert p.headers["Content-Length"] == length
1909
1910
def test_nonhttp_schemes_dont_check_URLs(self):
1911
test_urls = (
1912
"data:image/gif;base64,R0lGODlhAQABAHAAACH5BAUAAAAALAAAAAABAAEAAAICRAEAOw==",
1913
"file:///etc/passwd",
1914
"magnet:?xt=urn:btih:be08f00302bc2d1d3cfa3af02024fa647a271431",
1915
)
1916
for test_url in test_urls:
1917
req = requests.Request("GET", test_url)
1918
preq = req.prepare()
1919
assert test_url == preq.url
1915
)
1916
for test_url in test_urls:
1917
req = requests.Request("GET", test_url)
1918
preq = req.prepare()
1919
assert test_url == preq.url
1920
1921
def test_auth_is_stripped_on_http_downgrade(
1922
self, httpbin, httpbin_secure, httpbin_ca_bundle
1923
):
1924
r = requests.get(
1925
httpbin_secure("redirect-to"),
1926
params={"url": httpbin("get")},
1927
auth=("user", "pass"),
1928
verify=httpbin_ca_bundle,
1929
)
1930
assert r.history[0].request.headers["Authorization"]
1931
assert "Authorization" not in r.request.headers
1932
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"]
1937
1938
assert h1 == h2
1939
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/"
1944
)
1945
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")
1949
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"
1954
)
1955
assert not s.should_strip_auth(
1956
"http://example.com:80/foo", "https://example.com/bar"
1957
)
1958
assert not s.should_strip_auth(
1959
"http://example.com/foo", "https://example.com:443/bar"
1960
)
1961
# Non-standard ports should trigger stripping
1962
assert s.should_strip_auth(
1963
"http://example.com:8080/foo", "https://example.com/bar"
1964
)
1965
assert s.should_strip_auth(
1966
"http://example.com/foo", "https://example.com:8443/bar"
1967
)
1963
"http://example.com:8080/foo", "https://example.com/bar"
1964
)
1965
assert s.should_strip_auth(
1966
"http://example.com/foo", "https://example.com:8443/bar"
1967
)
1968
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"
1973
)
1974
1975
@pytest.mark.parametrize(
1976
"old_uri, new_uri",
1977
(
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"),
1982
),
1983
)
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)
1987
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)
1993
1994
# read only the first eight bytes of the response body,
1995
# then follow the redirect
1996
r1.iter_content(8)
1997
r2 = next(rg)
1998
assert r2.is_redirect
1999
2000
# read all of the response via iter_content,
2001
# then follow the redirect
2002
for _ in r2.iter_content():
2003
pass
2004
r3 = next(rg)
2005
assert not r3.is_redirect
2001
# then follow the redirect
2002
for _ in r2.iter_content():
2003
pass
2004
r3 = next(rg)
2005
assert not r3.is_redirect
2006
2007
def test_prepare_body_position_non_stream(self):
2008
data = b"the data"
2009
prep = requests.Request("GET", "http://example.com", data=data).prepare()
2010
assert prep._body_position is None
2011
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"
2017
2018
# the data has all been read
2019
assert prep.body.read() == b""
2020
2021
# rewind it back
2022
requests.utils.rewind_body(prep)
2023
assert prep.body.read() == b"the data"
2024
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"
2031
2032
# the data has all been read
2033
assert prep.body.read() == b""
2034
2035
# rewind it back
2036
requests.utils.rewind_body(prep)
2037
assert prep.body.read() == b"data"
2038
2039
def test_rewind_body_no_seek(self):
2040
class BadFileObj:
2041
def __init__(self, data):
2042
self.data = data
2043
2044
def tell(self):
2045
return 0
2041
def __init__(self, data):
2042
self.data = data
2043
2044
def tell(self):
2045
return 0
2046
2047
def __iter__(self):
2048
return
2049
2050
data = BadFileObj("the data")
2051
prep = requests.Request("GET", "http://example.com", data=data).prepare()
2052
assert prep._body_position == 0
2053
2054
with pytest.raises(UnrewindableBodyError) as e:
2055
requests.utils.rewind_body(prep)
2056
2057
assert "Unable to rewind request body" in str(e)
2058
2059
def test_rewind_body_failed_seek(self):
2060
class BadFileObj:
2061
def __init__(self, data):
2062
self.data = data
2063
2064
def tell(self):
2065
return 0
2066
2067
def seek(self, pos, whence=0):
2068
raise OSError()
2069
2070
def __iter__(self):
2071
return
2072
2073
data = BadFileObj("the data")
2074
prep = requests.Request("GET", "http://example.com", data=data).prepare()
2075
assert prep._body_position == 0
2076
2077
with pytest.raises(UnrewindableBodyError) as e:
2078
requests.utils.rewind_body(prep)
2079
2080
assert "error occurred when rewinding request body" in str(e)
2081
2082
def test_rewind_body_failed_tell(self):
2083
class BadFileObj:
2084
def __init__(self, data):
2085
self.data = data
2086
2087
def tell(self):
2088
raise OSError()
2089
2090
def __iter__(self):
2091
return
2087
def tell(self):
2088
raise OSError()
2089
2090
def __iter__(self):
2091
return
2092
2093
data = BadFileObj("the data")
2094
prep = requests.Request("GET", "http://example.com", data=data).prepare()
2095
assert prep._body_position is not None
2096
2097
with pytest.raises(UnrewindableBodyError) as e:
2098
requests.utils.rewind_body(prep)
2099
2100
assert "Unable to rewind request body" in str(e)
2101
2102
def test_getattr_proxy_stream_follows_redirect(self, httpbin):
2103
"""Ensure stream wrappers that don't implement __iter__ directly are still detected."""
2104
2105
class AttrProxy:
2106
def __init__(self):
2107
self._file = io.BytesIO(b"data")
2108
2109
def __getattr__(self, name):
2110
return getattr(self._file, name)
2111
2112
r = requests.post(
2113
httpbin("redirect-to?url=/post&status_code=307"), data=AttrProxy()
2114
)
2115
assert r.json()["data"] == "data"
2116
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
2121
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
2127
return resp
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
2127
return resp
2128
2129
adapter.build_response = build_response
2130
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)
2135
s.get(url)
2136
2137
@pytest.mark.parametrize(
2138
"username, password, auth_str",
2139
(
2140
("test", "test", "Basic dGVzdDp0ZXN0"),
2141
(
2142
"имя".encode(),
2143
"пароль".encode(),
2144
"Basic 0LjQvNGPOtC/0LDRgNC+0LvRjA==",
2145
),
2146
),
2147
)
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
2152
2153
def test_requests_history_is_saved(self, httpbin):
2154
r = requests.get(httpbin("redirect/5"))
2155
total = r.history[-1].history
2156
i = 0
2157
for item in r.history:
2158
assert item.history == total[0:i]
2159
i += 1
2160
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"]
2166
2167
def test_json_param_post_should_not_override_data_param(self, httpbin):
2168
r = requests.Request(
2169
method="POST",
2170
url=httpbin("post"),
2171
data={"stuff": "elixr"},
2172
json={"music": "flute"},
2173
)
2174
prep = r.prepare()
2175
assert "stuff=elixr" == prep.body
2176
2177
def test_response_iter_lines(self, httpbin):
2178
r = requests.get(httpbin("stream/4"), stream=True)
2179
assert r.status_code == 200
2180
2181
it = r.iter_lines()
2182
next(it)
2183
assert len(list(it)) == 3
2184
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)
2188
2189
assert response.raw.closed
2190
2191
def test_unconsumed_session_response_closes_connection(self, httpbin):
2192
s = requests.session()
2193
2194
with contextlib.closing(s.get(httpbin("stream/4"), stream=True)) as response:
2195
pass
2196
2197
assert response._content_consumed is False
2198
assert response.raw.closed
2199
2200
@pytest.mark.xfail
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
2200
@pytest.mark.xfail
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
2205
2206
next(r.iter_lines())
2207
assert len(list(r.iter_lines())) == 3
2208
2209
def test_session_close_proxy_clear(self):
2210
proxies = {
2211
"one": mock.Mock(),
2212
"two": mock.Mock(),
2213
}
2214
session = requests.Session()
2215
with mock.patch.dict(session.adapters["http://"].proxy_manager, proxies):
2216
session.close()
2217
proxies["one"].clear.assert_called_once_with()
2218
proxies["two"].clear.assert_called_once_with()
2219
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"}
2224
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="}
2229
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
2233
r.status_code = 0
2234
r._content = False
2235
r._content_consumed = False
2236
2237
assert r.content is None
2238
with pytest.raises(ValueError):
2239
r.json()
2235
r._content_consumed = False
2236
2237
assert r.content is None
2238
with pytest.raises(ValueError):
2239
r.json()
2240
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`.
2244
"""
2245
resp = requests.Response()
2246
resp.raw = StringIO.StringIO("test")
2247
assert not resp.raw.closed
2248
resp.close()
2249
assert resp.raw.closed
2250
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.
2254
"""
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
2262
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.
2266
"""
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
2274
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.
2278
"""
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
2285
2286
def test_custom_redirect_mixin(self, httpbin):
2287
"""Tests a custom mixin to overwrite ``get_redirect_target``.
2288
2289
Ensures a subclassed ``requests.Session`` can handle a certain type of
2290
malformed redirect responses.
2291
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
2297
"""
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)
2303
urls_test = [
2304
url_redirect,
2305
url_redirect_malformed,
2306
url_final,
2307
]
2303
urls_test = [
2304
url_redirect,
2305
url_redirect_malformed,
2306
url_final,
2307
]
2308
2309
class CustomRedirectSession(requests.Session):
2310
def get_redirect_target(self, resp):
2311
# default behavior
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):
2317
return location
2318
return None
2319
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]
2329
2330
2331
class TestCaseInsensitiveDict:
2332
@pytest.mark.parametrize(
2333
"cid",
2334
(
2335
CaseInsensitiveDict({"Foo": "foo", "BAr": "bar"}),
2336
CaseInsensitiveDict([("Foo", "foo"), ("BAr", "bar")]),
2337
CaseInsensitiveDict(FOO="foo", BAr="bar"),
2338
),
2339
)
2340
def test_init(self, cid):
2341
assert len(cid) == 2
2342
assert "foo" in cid
2343
assert "bar" in cid
2344
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"]
2350
2351
def test_len(self):
2352
cid = CaseInsensitiveDict({"a": "a", "b": "b"})
2353
cid["A"] = "a"
2354
assert len(cid) == 2
2355
2356
def test_getitem(self):
2357
cid = CaseInsensitiveDict({"Spam": "blueval"})
2358
assert cid["spam"] == "blueval"
2359
assert cid["SPAM"] == "blueval"
2360
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"]
2371
2372
def test_delitem(self):
2373
cid = CaseInsensitiveDict()
2374
cid["Spam"] = "someval"
2375
del cid["sPam"]
2376
assert "spam" not in cid
2377
assert len(cid) == 0
2378
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
2387
2388
def test_get(self):
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"
2396
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"
2407
2408
def test_update_retains_unchanged(self):
2409
cid = CaseInsensitiveDict({"foo": "foo", "bar": "bar"})
2410
cid.update({"foo": "newfoo"})
2411
assert cid["bar"] == "bar"
2412
2413
def test_iter(self):
2414
cid = CaseInsensitiveDict({"Spam": "spam", "Eggs": "eggs"})
2415
keys = frozenset(["Spam", "Eggs"])
2416
assert frozenset(iter(cid)) == keys
2417
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()
2426
2427
def test_setdefault(self):
2428
cid = CaseInsensitiveDict({"Spam": "blueval"})
2429
assert cid.setdefault("spam", "notblueval") == "blueval"
2430
assert cid.setdefault("notspam", "notblueval") == "notblueval"
2426
2427
def test_setdefault(self):
2428
cid = CaseInsensitiveDict({"Spam": "blueval"})
2429
assert cid.setdefault("spam", "notblueval") == "blueval"
2430
assert cid.setdefault("notspam", "notblueval") == "notblueval"
2431
2432
def test_lower_items(self):
2433
cid = CaseInsensitiveDict(
2434
{
2435
"Accept": "application/json",
2436
"user-Agent": "requests",
2437
}
2438
)
2439
keyset = frozenset(lowerkey for lowerkey, v in cid.lower_items())
2440
lowerkeyset = frozenset(["accept", "user-agent"])
2441
assert keyset == lowerkeyset
2442
2443
def test_preserve_key_case(self):
2444
cid = CaseInsensitiveDict(
2445
{
2446
"Accept": "application/json",
2447
"user-Agent": "requests",
2448
}
2449
)
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
2454
2455
def test_preserve_last_key_case(self):
2456
cid = CaseInsensitiveDict(
2457
{
2458
"Accept": "application/json",
2459
"user-Agent": "requests",
2460
}
2461
)
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
2468
2469
def test_copy(self):
2470
cid = CaseInsensitiveDict(
2471
{
2472
"Accept": "application/json",
2473
"user-Agent": "requests",
2474
}
2475
)
2476
cid_copy = cid.copy()
2477
assert cid == cid_copy
2478
cid["changed"] = True
2479
assert cid != cid_copy
2480
2481
2482
class TestMorselToCookieExpires:
2483
"""Tests for morsel_to_cookie when morsel contains expires."""
2484
2485
def test_expires_valid_str(self):
2486
"""Test case where we convert expires from string time."""
2487
2488
morsel = Morsel()
2489
morsel["expires"] = "Thu, 01-Jan-1970 00:00:01 GMT"
2490
cookie = morsel_to_cookie(morsel)
2491
assert cookie.expires == 1
2492
2493
@pytest.mark.parametrize(
2494
"value, exception",
2495
(
2496
(100, TypeError),
2497
("woops", ValueError),
2498
),
2499
)
2500
def test_expires_invalid_int(self, value, exception):
2501
"""Test case where an invalid type is passed for expires."""
2502
morsel = Morsel()
2503
morsel["expires"] = value
2504
with pytest.raises(exception):
2505
morsel_to_cookie(morsel)
2506
2507
def test_expires_none(self):
2508
"""Test case where expires is None."""
2504
with pytest.raises(exception):
2505
morsel_to_cookie(morsel)
2506
2507
def test_expires_none(self):
2508
"""Test case where expires is None."""
2509
2510
morsel = Morsel()
2511
morsel["expires"] = None
2512
cookie = morsel_to_cookie(morsel)
2513
assert cookie.expires is None
2514
2515
2516
class TestMorselToCookieMaxAge:
2517
"""Tests for morsel_to_cookie when morsel contains max-age."""
2518
2519
def test_max_age_valid_int(self):
2520
"""Test case where a valid max age in seconds is passed."""
2521
2522
morsel = Morsel()
2523
morsel["max-age"] = 60
2524
cookie = morsel_to_cookie(morsel)
2525
assert isinstance(cookie.expires, int)
2526
2527
def test_max_age_invalid_str(self):
2528
"""Test case where a invalid max age is passed."""
2529
2530
morsel = Morsel()
2531
morsel["max-age"] = "woops"
2532
with pytest.raises(TypeError):
2533
morsel_to_cookie(morsel)
2534
2535
2536
class TestTimeout:
2537
def test_stream_timeout(self, httpbin):
2538
try:
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]
2542
2543
@pytest.mark.parametrize(
2544
"timeout, error_text",
2545
(
2546
((3, 4, 5), "(connect, read)"),
2547
("foo", "must be an int, float or None"),
2548
),
2549
)
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)
2549
)
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)
2554
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.
2558
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.
2564
"""
2565
r = requests.get(httpbin("get"), timeout=timeout)
2566
assert r.status_code == 200
2567
2568
@pytest.mark.parametrize(
2569
"timeout", ((None, 0.1), Urllib3Timeout(connect=None, read=0.1))
2570
)
2571
def test_read_timeout(self, httpbin, timeout):
2572
try:
2573
requests.get(httpbin("delay/10"), timeout=timeout)
2574
pytest.fail("The recv() request should time out.")
2575
except ReadTimeout:
2576
pass
2577
2578
@pytest.mark.parametrize(
2579
"timeout", ((0.1, None), Urllib3Timeout(connect=0.1, read=None))
2580
)
2581
def test_connect_timeout(self, timeout):
2582
try:
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)
2588
2589
@pytest.mark.parametrize(
2590
"timeout", ((0.1, 0.1), Urllib3Timeout(connect=0.1, read=0.1))
2591
)
2592
def test_total_timeout_connect(self, timeout):
2593
try:
2594
requests.get(TARPIT, timeout=timeout)
2595
pytest.fail("The connect() request should time out.")
2596
except ConnectTimeout:
2597
pass
2598
2599
def test_encoded_methods(self, httpbin):
2600
"""See: https://github.com/psf/requests/issues/2316"""
2601
r = requests.request(b"GET", httpbin("get"))
2602
assert r.ok
2603
2604
2605
SendCall = collections.namedtuple("SendCall", ("args", "kwargs"))
2606
2607
2608
class RedirectSession(SessionRedirectMixin):
2609
def __init__(self, order_of_redirects):
2610
self.redirects = order_of_redirects
2611
self.calls = []
2612
self.max_redirects = 30
2613
self.cookies = {}
2614
self.trust_env = False
2615
2616
def send(self, *args, **kwargs):
2617
self.calls.append(SendCall(args, kwargs))
2618
return self.build_response()
2619
2620
def build_response(self):
2621
request = self.calls[-1].args[0]
2622
r = requests.Response()
2623
r.url = request.url
2624
2625
try:
2626
r.status_code = int(self.redirects.pop(0))
2627
except IndexError:
2628
r.status_code = 200
2624
2625
try:
2626
r.status_code = int(self.redirects.pop(0))
2627
except IndexError:
2628
r.status_code = 200
2629
2630
r.headers = CaseInsensitiveDict({"Location": "/"})
2631
r.raw = self._build_raw()
2632
r.request = request
2633
return r
2634
2635
def _build_raw(self):
2636
string = StringIO.StringIO("")
2637
setattr(string, "release_conn", lambda *args: args)
2638
return string
2639
2640
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)
2647
2648
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 = {
2657
"stream": False,
2658
"verify": True,
2659
"cert": None,
2660
"timeout": None,
2661
"allow_redirects": False,
2662
"proxies": {},
2663
}
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
2663
}
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
2668
2669
2670
@pytest.mark.parametrize(
2671
"var,url,proxy",
2672
[
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"),
2677
],
2678
)
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)
2683
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
2690
2691
2692
@pytest.mark.parametrize(
2693
"data",
2694
(
2695
(("a", "b"), ("c", "d")),
2696
(("c", "d"), ("a", "b")),
2697
(("a", "b"), ("c", "d"), ("e", "f")),
2698
),
2699
)
2700
def test_data_argument_accepts_tuples(data):
2701
"""Ensure that the data argument will accept tuples of strings
2702
and properly encode them.
2703
"""
2704
p = PreparedRequest()
2705
p.prepare(
2706
method="GET", url="http://www.example.com", data=data, hooks=default_hooks()
2707
)
2708
assert p.body == urlencode(data)
2704
p = PreparedRequest()
2705
p.prepare(
2706
method="GET", url="http://www.example.com", data=data, hooks=default_hooks()
2707
)
2708
assert p.body == urlencode(data)
2709
2710
2711
@pytest.mark.parametrize(
2712
"kwargs",
2713
(
2714
None,
2715
{
2716
"method": "GET",
2717
"url": "http://www.example.com",
2718
"data": "foo=bar",
2719
"hooks": default_hooks(),
2720
},
2721
{
2722
"method": "GET",
2723
"url": "http://www.example.com",
2724
"data": "foo=bar",
2725
"hooks": default_hooks(),
2726
"cookies": {"foo": "bar"},
2727
},
2728
{"method": "GET", "url": "http://www.example.com/üniçø∂é"},
2729
),
2730
)
2731
def test_prepared_copy(kwargs):
2732
p = PreparedRequest()
2733
if kwargs:
2734
p.prepare(**kwargs)
2735
copy = p.copy()
2736
for attr in ("method", "url", "headers", "_cookies", "body", "hooks"):
2737
assert getattr(p, attr) == getattr(copy, attr)
2738
2739
2740
def test_urllib3_retries(httpbin):
2741
from urllib3.util import Retry
2742
2743
s = requests.Session()
2744
s.mount("http://", HTTPAdapter(max_retries=Retry(total=2, status_forcelist=[500])))
2745
2746
with pytest.raises(RetryError):
2747
s.get(httpbin("status/500"))
2748
2749
2750
def test_urllib3_pool_connection_closed(httpbin):
2751
s = requests.Session()
2752
s.mount("http://", HTTPAdapter(pool_connections=0, pool_maxsize=0))
2753
2754
try:
2755
s.get(httpbin("status/200"))
2756
except ConnectionError as e:
2757
assert "Pool is closed." in str(e)
2753
2754
try:
2755
s.get(httpbin("status/200"))
2756
except ConnectionError as e:
2757
assert "Pool is closed." in str(e)
2758
2759
2760
class TestPreparingURLs:
2761
@pytest.mark.parametrize(
2762
"url,expected",
2763
(
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"),
2769
(
2770
"http://straße.de/straße".encode(),
2771
"http://xn--strae-oqa.de/stra%C3%9Fe",
2772
),
2773
(
2774
"http://Königsgäßchen.de/straße",
2775
"http://xn--knigsgchen-b4a3dun.de/stra%C3%9Fe",
2776
),
2777
(
2778
"http://Königsgäßchen.de/straße".encode(),
2779
"http://xn--knigsgchen-b4a3dun.de/stra%C3%9Fe",
2780
),
2781
(b"http://xn--n3h.net/", "http://xn--n3h.net/"),
2782
(
2783
b"http://[1200:0000:ab00:1234:0000:2552:7777:1313]:12345/",
2784
"http://[1200:0000:ab00:1234:0000:2552:7777:1313]:12345/",
2785
),
2786
(
2787
"http://[1200:0000:ab00:1234:0000:2552:7777:1313]:12345/",
2788
"http://[1200:0000:ab00:1234:0000:2552:7777:1313]:12345/",
2789
),
2790
),
2791
)
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())
2798
return 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())
2798
return x
2799
2800
r = requests.Request("GET", url=url)
2801
p = r.prepare()
2802
assert normalize_percent_encode(p.url) == expected
2803
2804
@pytest.mark.parametrize(
2805
"url",
2806
(
2807
b"http://*.google.com",
2808
b"http://*",
2809
"http://*.google.com",
2810
"http://*",
2811
"http://☃.net/",
2812
),
2813
)
2814
def test_preparing_bad_url(self, url):
2815
r = requests.Request("GET", url=url)
2816
with pytest.raises(requests.exceptions.InvalidURL):
2817
r.prepare()
2818
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})
2823
2824
@pytest.mark.parametrize(
2825
"input, expected",
2826
(
2827
(
2828
b"http+unix://%2Fvar%2Frun%2Fsocket/path%7E",
2829
"http+unix://%2Fvar%2Frun%2Fsocket/path~",
2830
),
2831
(
2832
"http+unix://%2Fvar%2Frun%2Fsocket/path%7E",
2833
"http+unix://%2Fvar%2Frun%2Fsocket/path~",
2834
),
2835
(
2836
b"mailto:user@example.org",
2837
"mailto:user@example.org",
2838
),
2839
(
2840
"mailto:user@example.org",
2841
"mailto:user@example.org",
2842
),
2843
(
2844
b"data:SSDimaUgUHl0aG9uIQ==",
2845
"data:SSDimaUgUHl0aG9uIQ==",
2846
),
2847
),
2848
)
2849
def test_url_mutation(self, input, expected):
2850
"""
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.
2855
"""
2856
r = requests.Request("GET", url=input)
2857
p = r.prepare()
2858
assert p.url == expected
2854
those whose scheme *does* begin with "http" are mutated.
2855
"""
2856
r = requests.Request("GET", url=input)
2857
p = r.prepare()
2858
assert p.url == expected
2859
2860
@pytest.mark.parametrize(
2861
"input, params, expected",
2862
(
2863
(
2864
b"http+unix://%2Fvar%2Frun%2Fsocket/path",
2865
{"key": "value"},
2866
"http+unix://%2Fvar%2Frun%2Fsocket/path?key=value",
2867
),
2868
(
2869
"http+unix://%2Fvar%2Frun%2Fsocket/path",
2870
{"key": "value"},
2871
"http+unix://%2Fvar%2Frun%2Fsocket/path?key=value",
2872
),
2873
(
2874
b"mailto:user@example.org",
2875
{"key": "value"},
2876
"mailto:user@example.org",
2877
),
2878
(
2879
"mailto:user@example.org",
2880
{"key": "value"},
2881
"mailto:user@example.org",
2882
),
2883
),
2884
)
2885
def test_parameters_for_nonstandard_schemes(self, input, params, expected):
2886
"""
2887
Setting parameters for nonstandard schemes is allowed if those schemes
2888
begin with "http", and is forbidden otherwise.
2889
"""
2890
r = requests.Request("GET", url=input, params=params)
2891
p = r.prepare()
2892
assert p.url == expected
2893
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)
2893
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)
2898
2899
def test_json_decode_compatibility(self, httpbin):
2900
r = requests.get(httpbin("bytes/20"))
2901
with pytest.raises(requests.exceptions.JSONDecodeError) as excinfo:
2902
r.json()
2903
assert isinstance(excinfo.value, RequestException)
2904
assert isinstance(excinfo.value, JSONDecodeError)
2905
assert r.text not in str(excinfo.value)
2906
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:
2910
r.json()
2911
assert excinfo.value.doc == r.text
2912
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")
2920
2921
assert r1 == 425
2922
assert r2 == 425
2923
assert r3 == 425
2924
assert r4 == 425
2925
assert r5 == 425
2926
assert r6 == 425
2927
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)
2931
sock.send(
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'
2935
)
2931
sock.send(
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'
2935
)
2936
2937
s = requests.Session()
2938
close_server = threading.Event()
2939
server = TLSServer(
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",
2945
)
2946
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
2951
2952
# Cannot verify self-signed certificate
2953
with pytest.raises(requests.exceptions.SSLError):
2954
s.get(url)
2955
2956
close_server.set()
2957
assert 2 == len(s.adapters["https://"].poolmanager.pools)
2958
2959
def test_different_connection_pool_for_tls_settings_verify_bundle_expired_cert(
2960
self,
2961
):
2962
def response_handler(sock):
2963
consume_socket_content(sock, timeout=0.5)
2964
sock.send(
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'
2968
)
2969
2970
s = requests.Session()
2971
close_server = threading.Event()
2972
server = TLSServer(
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",
2978
)
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",
2978
)
2979
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
2984
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")
2988
2989
close_server.set()
2990
assert 2 == len(s.adapters["https://"].poolmanager.pools)
2991
2992
def test_different_connection_pool_for_tls_settings_verify_bundle_unexpired_cert(
2993
self,
2994
):
2995
def response_handler(sock):
2996
consume_socket_content(sock, timeout=0.5)
2997
sock.send(
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'
3001
)
3002
3003
s = requests.Session()
3004
close_server = threading.Event()
3005
server = TLSServer(
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",
3011
)
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",
3011
)
3012
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
3017
3018
r2 = s.get(url, verify="tests/certs/valid/ca/ca.crt")
3019
assert r2.status_code == 200
3020
3021
close_server.set()
3022
assert 2 == len(s.adapters["https://"].poolmanager.pools)
3023
3024
def test_different_connection_pool_for_mtls_settings(self):
3025
client_cert = None
3026
3027
def response_handler(sock):
3028
nonlocal client_cert
3029
client_cert = sock.getpeercert()
3030
consume_socket_content(sock, timeout=0.5)
3031
sock.send(
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'
3035
)
3036
3037
s = requests.Session()
3038
close_server = threading.Event()
3039
server = TLSServer(
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",
3045
mutual_tls=True,
3046
cacert="tests/certs/expired/ca/ca.crt",
3047
)
3043
cert_chain="tests/certs/expired/server/server.pem",
3044
keyfile="tests/certs/expired/server/server.key",
3045
mutual_tls=True,
3046
cacert="tests/certs/expired/ca/ca.crt",
3047
)
3048
3049
cert = (
3050
"tests/certs/mtls/client/client.pem",
3051
"tests/certs/mtls/client/client.key",
3052
)
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)
3059
close_server.set()
3060
3061
assert client_cert is not None
3062
3063
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)
3069
p = req.prepare()
3070
3071
assert p.headers["Content-Length"] == length
3072
3073
3074
@pytest.mark.skipif(
3075
is_urllib3_1,
3076
reason="urllib3 2.x encodes all strings to utf-8, urllib3 1.x uses latin-1",
3077
)
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)
3082
p = req.prepare()
3083
3084
assert p.headers["Content-Length"] == length
3080
length = str(len(data.encode("utf-8")))
3081
req = requests.Request("POST", httpbin("post"), data=data)
3082
p = req.prepare()
3083
3084
assert p.headers["Content-Length"] == length
3085
3086
3087
def test_json_decode_errors_are_serializable_deserializable():
3088
json_decode_error = requests.exceptions.JSONDecodeError(
3089
"Extra data",
3090
'{"responseCode":["706"],"data":null}{"responseCode":["706"],"data":null}',
3091
36,
3092
)
3093
deserialized_error = pickle.loads(pickle.dumps(json_decode_error))
3094
assert repr(json_decode_error) == repr(deserialized_error)