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_lowlevel.pypython 484 baris ditampilkan
1
import threading
2
3
import pytest
4
5
import requests
6
from requests.compat import JSONDecodeError
7
from tests.testserver.server import Server, consume_socket_content
8
9
from .utils import override_environ
10
11
12
def echo_response_handler(sock):
13
"""Simple handler that will take request and echo it back to requester."""
14
request_content = consume_socket_content(sock, timeout=0.5)
15
16
text_200 = (
17
b"HTTP/1.1 200 OK\r\n"
18
b"Content-Length: %d\r\n\r\n"
19
b"%s"
20
) % (len(request_content), request_content)
21
sock.send(text_200)
22
23
24
def test_chunked_upload():
25
"""can safely send generators"""
26
close_server = threading.Event()
27
server = Server.basic_response_server(wait_to_close_event=close_server)
28
data = iter([b"a", b"b", b"c"])
29
30
with server as (host, port):
31
url = f"http://{host}:{port}/"
32
r = requests.post(url, data=data, stream=True)
33
close_server.set() # release server block
34
35
assert r.status_code == 200
36
assert r.request.headers["Transfer-Encoding"] == "chunked"
37
38
39
def test_chunked_encoding_error():
40
"""get a ChunkedEncodingError if the server returns a bad response"""
41
42
def incomplete_chunked_response_handler(sock):
43
request_content = consume_socket_content(sock, timeout=0.5)
44
45
# The server never ends the request and doesn't provide any valid chunks
46
sock.send(
47
b"HTTP/1.1 200 OK\r\n"
48
b"Transfer-Encoding: chunked\r\n"
49
)
45
# The server never ends the request and doesn't provide any valid chunks
46
sock.send(
47
b"HTTP/1.1 200 OK\r\n"
48
b"Transfer-Encoding: chunked\r\n"
49
)
50
51
return request_content
52
53
close_server = threading.Event()
54
server = Server(incomplete_chunked_response_handler)
55
56
with server as (host, port):
57
url = f"http://{host}:{port}/"
58
with pytest.raises(requests.exceptions.ChunkedEncodingError):
59
requests.get(url)
60
close_server.set() # release server block
61
62
63
def test_chunked_upload_uses_only_specified_host_header():
64
"""Ensure we use only the specified Host header for chunked requests."""
65
close_server = threading.Event()
66
server = Server(echo_response_handler, wait_to_close_event=close_server)
67
68
data = iter([b"a", b"b", b"c"])
69
custom_host = "sample-host"
70
71
with server as (host, port):
72
url = f"http://{host}:{port}/"
73
r = requests.post(url, data=data, headers={"Host": custom_host}, stream=True)
74
close_server.set() # release server block
75
76
expected_header = b"Host: %s\r\n" % custom_host.encode("utf-8")
77
assert expected_header in r.content
78
assert r.content.count(b"Host: ") == 1
79
80
81
def test_chunked_upload_doesnt_skip_host_header():
82
"""Ensure we don't omit all Host headers with chunked requests."""
83
close_server = threading.Event()
84
server = Server(echo_response_handler, wait_to_close_event=close_server)
80
81
def test_chunked_upload_doesnt_skip_host_header():
82
"""Ensure we don't omit all Host headers with chunked requests."""
83
close_server = threading.Event()
84
server = Server(echo_response_handler, wait_to_close_event=close_server)
85
86
data = iter([b"a", b"b", b"c"])
87
88
with server as (host, port):
89
expected_host = f"{host}:{port}"
90
url = f"http://{host}:{port}/"
91
r = requests.post(url, data=data, stream=True)
92
close_server.set() # release server block
93
94
expected_header = b"Host: %s\r\n" % expected_host.encode("utf-8")
95
assert expected_header in r.content
96
assert r.content.count(b"Host: ") == 1
97
98
99
def test_conflicting_content_lengths():
100
"""Ensure we correctly throw an InvalidHeader error if multiple
101
conflicting Content-Length headers are returned.
102
"""
103
104
def multiple_content_length_response_handler(sock):
105
request_content = consume_socket_content(sock, timeout=0.5)
106
response = (
107
b"HTTP/1.1 200 OK\r\n"
108
b"Content-Type: text/plain\r\n"
109
b"Content-Length: 16\r\n"
110
b"Content-Length: 32\r\n\r\n"
111
b"-- Bad Actor -- Original Content\r\n"
112
)
113
sock.send(response)
114
115
return request_content
116
117
close_server = threading.Event()
118
server = Server(multiple_content_length_response_handler)
119
120
with server as (host, port):
121
url = f"http://{host}:{port}/"
122
with pytest.raises(requests.exceptions.InvalidHeader):
123
requests.get(url)
124
close_server.set()
120
with server as (host, port):
121
url = f"http://{host}:{port}/"
122
with pytest.raises(requests.exceptions.InvalidHeader):
123
requests.get(url)
124
close_server.set()
125
126
127
def test_digestauth_401_count_reset_on_redirect():
128
"""Ensure we correctly reset num_401_calls after a successful digest auth,
129
followed by a 302 redirect to another digest auth prompt.
130
131
See https://github.com/psf/requests/issues/1979.
132
"""
133
text_401 = (b'HTTP/1.1 401 UNAUTHORIZED\r\n'
134
b'Content-Length: 0\r\n'
135
b'WWW-Authenticate: Digest nonce="6bf5d6e4da1ce66918800195d6b9130d"'
136
b', opaque="372825293d1c26955496c80ed6426e9e", '
137
b'realm="me@kennethreitz.com", qop=auth\r\n\r\n')
138
139
text_302 = (b'HTTP/1.1 302 FOUND\r\n'
140
b'Content-Length: 0\r\n'
141
b'Location: /\r\n\r\n')
142
143
text_200 = (b'HTTP/1.1 200 OK\r\n'
144
b'Content-Length: 0\r\n\r\n')
145
146
expected_digest = (b'Authorization: Digest username="user", '
147
b'realm="me@kennethreitz.com", '
148
b'nonce="6bf5d6e4da1ce66918800195d6b9130d", uri="/"')
149
150
auth = requests.auth.HTTPDigestAuth('user', 'pass')
151
152
def digest_response_handler(sock):
153
# Respond to initial GET with a challenge.
154
request_content = consume_socket_content(sock, timeout=0.5)
155
assert request_content.startswith(b"GET / HTTP/1.1")
156
sock.send(text_401)
152
def digest_response_handler(sock):
153
# Respond to initial GET with a challenge.
154
request_content = consume_socket_content(sock, timeout=0.5)
155
assert request_content.startswith(b"GET / HTTP/1.1")
156
sock.send(text_401)
157
158
# Verify we receive an Authorization header in response, then redirect.
159
request_content = consume_socket_content(sock, timeout=0.5)
160
assert expected_digest in request_content
161
sock.send(text_302)
162
163
# Verify Authorization isn't sent to the redirected host,
164
# then send another challenge.
165
request_content = consume_socket_content(sock, timeout=0.5)
166
assert b'Authorization:' not in request_content
167
sock.send(text_401)
168
169
# Verify Authorization is sent correctly again, and return 200 OK.
170
request_content = consume_socket_content(sock, timeout=0.5)
171
assert expected_digest in request_content
172
sock.send(text_200)
173
174
return request_content
175
176
close_server = threading.Event()
177
server = Server(digest_response_handler, wait_to_close_event=close_server)
178
179
with server as (host, port):
180
url = f'http://{host}:{port}/'
181
r = requests.get(url, auth=auth)
182
# Verify server succeeded in authenticating.
183
assert r.status_code == 200
184
# Verify Authorization was sent in final request.
185
assert 'Authorization' in r.request.headers
186
assert r.request.headers['Authorization'].startswith('Digest ')
187
# Verify redirect happened as we expected.
188
assert r.history[0].status_code == 302
189
close_server.set()
185
assert 'Authorization' in r.request.headers
186
assert r.request.headers['Authorization'].startswith('Digest ')
187
# Verify redirect happened as we expected.
188
assert r.history[0].status_code == 302
189
close_server.set()
190
191
192
def test_digestauth_401_only_sent_once():
193
"""Ensure we correctly respond to a 401 challenge once, and then
194
stop responding if challenged again.
195
"""
196
text_401 = (b'HTTP/1.1 401 UNAUTHORIZED\r\n'
197
b'Content-Length: 0\r\n'
198
b'WWW-Authenticate: Digest nonce="6bf5d6e4da1ce66918800195d6b9130d"'
199
b', opaque="372825293d1c26955496c80ed6426e9e", '
200
b'realm="me@kennethreitz.com", qop=auth\r\n\r\n')
201
202
expected_digest = (b'Authorization: Digest username="user", '
203
b'realm="me@kennethreitz.com", '
204
b'nonce="6bf5d6e4da1ce66918800195d6b9130d", uri="/"')
205
206
auth = requests.auth.HTTPDigestAuth('user', 'pass')
207
208
def digest_failed_response_handler(sock):
209
# Respond to initial GET with a challenge.
210
request_content = consume_socket_content(sock, timeout=0.5)
211
assert request_content.startswith(b"GET / HTTP/1.1")
212
sock.send(text_401)
213
214
# Verify we receive an Authorization header in response, then
215
# challenge again.
216
request_content = consume_socket_content(sock, timeout=0.5)
217
assert expected_digest in request_content
218
sock.send(text_401)
214
# Verify we receive an Authorization header in response, then
215
# challenge again.
216
request_content = consume_socket_content(sock, timeout=0.5)
217
assert expected_digest in request_content
218
sock.send(text_401)
219
220
# Verify the client didn't respond to second challenge.
221
request_content = consume_socket_content(sock, timeout=0.5)
222
assert request_content == b''
223
224
return request_content
225
226
close_server = threading.Event()
227
server = Server(digest_failed_response_handler, wait_to_close_event=close_server)
228
229
with server as (host, port):
230
url = f'http://{host}:{port}/'
231
r = requests.get(url, auth=auth)
232
# Verify server didn't authenticate us.
233
assert r.status_code == 401
234
assert r.history[0].status_code == 401
235
close_server.set()
236
237
238
def test_digestauth_only_on_4xx():
239
"""Ensure we only send digestauth on 4xx challenges.
240
241
See https://github.com/psf/requests/issues/3772.
242
"""
243
text_200_chal = (b'HTTP/1.1 200 OK\r\n'
244
b'Content-Length: 0\r\n'
245
b'WWW-Authenticate: Digest nonce="6bf5d6e4da1ce66918800195d6b9130d"'
246
b', opaque="372825293d1c26955496c80ed6426e9e", '
247
b'realm="me@kennethreitz.com", qop=auth\r\n\r\n')
248
249
auth = requests.auth.HTTPDigestAuth('user', 'pass')
250
251
def digest_response_handler(sock):
252
# Respond to GET with a 200 containing www-authenticate header.
253
request_content = consume_socket_content(sock, timeout=0.5)
254
assert request_content.startswith(b"GET / HTTP/1.1")
255
sock.send(text_200_chal)
251
def digest_response_handler(sock):
252
# Respond to GET with a 200 containing www-authenticate header.
253
request_content = consume_socket_content(sock, timeout=0.5)
254
assert request_content.startswith(b"GET / HTTP/1.1")
255
sock.send(text_200_chal)
256
257
# Verify the client didn't respond with auth.
258
request_content = consume_socket_content(sock, timeout=0.5)
259
assert request_content == b''
260
261
return request_content
262
263
close_server = threading.Event()
264
server = Server(digest_response_handler, wait_to_close_event=close_server)
265
266
with server as (host, port):
267
url = f'http://{host}:{port}/'
268
r = requests.get(url, auth=auth)
269
# Verify server didn't receive auth from us.
270
assert r.status_code == 200
271
assert len(r.history) == 0
272
close_server.set()
273
274
275
_schemes_by_var_prefix = [
276
('http', ['http']),
277
('https', ['https']),
278
('all', ['http', 'https']),
279
]
280
281
_proxy_combos = []
282
for prefix, schemes in _schemes_by_var_prefix:
283
for scheme in schemes:
284
_proxy_combos.append((f"{prefix}_proxy", scheme))
285
286
_proxy_combos += [(var.upper(), scheme) for var, scheme in _proxy_combos]
287
288
289
@pytest.mark.parametrize("var,scheme", _proxy_combos)
290
def test_use_proxy_from_environment(httpbin, var, scheme):
291
url = f"{scheme}://httpbin.org"
292
fake_proxy = Server() # do nothing with the requests; just close the socket
293
with fake_proxy as (host, port):
294
proxy_url = f"socks5://{host}:{port}"
295
kwargs = {var: proxy_url}
296
with override_environ(**kwargs):
297
# fake proxy's lack of response will cause a ConnectionError
298
with pytest.raises(requests.exceptions.ConnectionError):
299
requests.get(url)
295
kwargs = {var: proxy_url}
296
with override_environ(**kwargs):
297
# fake proxy's lack of response will cause a ConnectionError
298
with pytest.raises(requests.exceptions.ConnectionError):
299
requests.get(url)
300
301
# the fake proxy received a request
302
assert len(fake_proxy.handler_results) == 1
303
304
# it had actual content (not checking for SOCKS protocol for now)
305
assert len(fake_proxy.handler_results[0]) > 0
306
307
308
def test_redirect_rfc1808_to_non_ascii_location():
309
path = 'š'
310
expected_path = b'%C5%A1'
311
redirect_request = [] # stores the second request to the server
312
313
def redirect_resp_handler(sock):
314
consume_socket_content(sock, timeout=0.5)
315
location = f'//{host}:{port}/{path}'
316
sock.send(
317
(
318
b'HTTP/1.1 301 Moved Permanently\r\n'
319
b'Content-Length: 0\r\n'
320
b'Location: %s\r\n'
321
b'\r\n'
322
) % location.encode('utf8')
323
)
324
redirect_request.append(consume_socket_content(sock, timeout=0.5))
325
sock.send(b'HTTP/1.1 200 OK\r\n\r\n')
326
327
close_server = threading.Event()
328
server = Server(redirect_resp_handler, wait_to_close_event=close_server)
329
330
with server as (host, port):
331
url = f'http://{host}:{port}'
332
r = requests.get(url=url, allow_redirects=True)
333
assert r.status_code == 200
334
assert len(r.history) == 1
335
assert r.history[0].status_code == 301
336
assert redirect_request[0].startswith(b'GET /' + expected_path + b' HTTP/1.1')
337
assert r.url == '{}/{}'.format(url, expected_path.decode('ascii'))
333
assert r.status_code == 200
334
assert len(r.history) == 1
335
assert r.history[0].status_code == 301
336
assert redirect_request[0].startswith(b'GET /' + expected_path + b' HTTP/1.1')
337
assert r.url == '{}/{}'.format(url, expected_path.decode('ascii'))
338
339
close_server.set()
340
341
342
def test_fragment_not_sent_with_request():
343
"""Verify that the fragment portion of a URI isn't sent to the server."""
344
close_server = threading.Event()
345
server = Server(echo_response_handler, wait_to_close_event=close_server)
346
347
with server as (host, port):
348
url = f'http://{host}:{port}/path/to/thing/#view=edit&token=hunter2'
349
r = requests.get(url)
350
raw_request = r.content
351
352
assert r.status_code == 200
353
headers, body = raw_request.split(b'\r\n\r\n', 1)
354
status_line, headers = headers.split(b'\r\n', 1)
355
356
assert status_line == b'GET /path/to/thing/ HTTP/1.1'
357
for frag in (b'view', b'edit', b'token', b'hunter2'):
358
assert frag not in headers
359
assert frag not in body
360
361
close_server.set()
362
363
364
def test_fragment_update_on_redirect():
365
"""Verify we only append previous fragment if one doesn't exist on new
366
location. If a new fragment is encountered in a Location header, it should
367
be added to all subsequent requests.
368
"""
369
370
def response_handler(sock):
371
consume_socket_content(sock, timeout=0.5)
372
sock.send(
373
b'HTTP/1.1 302 FOUND\r\n'
374
b'Content-Length: 0\r\n'
375
b'Location: /get#relevant-section\r\n\r\n'
376
)
377
consume_socket_content(sock, timeout=0.5)
378
sock.send(
379
b'HTTP/1.1 302 FOUND\r\n'
380
b'Content-Length: 0\r\n'
381
b'Location: /final-url/\r\n\r\n'
382
)
383
consume_socket_content(sock, timeout=0.5)
384
sock.send(
385
b'HTTP/1.1 200 OK\r\n\r\n'
386
)
382
)
383
consume_socket_content(sock, timeout=0.5)
384
sock.send(
385
b'HTTP/1.1 200 OK\r\n\r\n'
386
)
387
388
close_server = threading.Event()
389
server = Server(response_handler, wait_to_close_event=close_server)
390
391
with server as (host, port):
392
url = f'http://{host}:{port}/path/to/thing/#view=edit&token=hunter2'
393
r = requests.get(url)
394
395
assert r.status_code == 200
396
assert len(r.history) == 2
397
assert r.history[0].request.url == url
398
399
# Verify we haven't overwritten the location with our previous fragment.
400
assert r.history[1].request.url == f'http://{host}:{port}/get#relevant-section'
401
# Verify previous fragment is used and not the original.
402
assert r.url == f'http://{host}:{port}/final-url/#relevant-section'
403
404
close_server.set()
405
406
407
def test_json_decode_compatibility_for_alt_utf_encodings():
408
409
def response_handler(sock):
410
consume_socket_content(sock, timeout=0.5)
411
sock.send(
412
b'HTTP/1.1 200 OK\r\n'
413
b'Content-Length: 18\r\n\r\n'
414
b'\xff\xfe{\x00"\x00K0"\x00=\x00"\x00\xab0"\x00\r\n'
415
)
416
417
close_server = threading.Event()
418
server = Server(response_handler, wait_to_close_event=close_server)
419
420
with server as (host, port):
421
url = f'http://{host}:{port}/'
422
r = requests.get(url)
423
r.encoding = None
424
with pytest.raises(requests.exceptions.JSONDecodeError) as excinfo:
419
420
with server as (host, port):
421
url = f'http://{host}:{port}/'
422
r = requests.get(url)
423
r.encoding = None
424
with pytest.raises(requests.exceptions.JSONDecodeError) as excinfo:
425
r.json()
426
assert isinstance(excinfo.value, requests.exceptions.RequestException)
427
assert isinstance(excinfo.value, JSONDecodeError)
428
assert r.text not in str(excinfo.value)