| 1 |
import copy |
| 2 |
import filecmp |
| 3 |
import os |
| 4 |
import tarfile |
| 5 |
import zipfile |
| 6 |
from collections import deque |
| 7 |
from io import BytesIO |
| 8 |
from unittest import mock |
| 9 |
|
| 10 |
import pytest |
| 11 |
|
| 12 |
from requests import compat |
| 13 |
from requests._internal_utils import unicode_is_ascii |
| 14 |
from requests.cookies import RequestsCookieJar |
| 15 |
from requests.structures import CaseInsensitiveDict |
| 16 |
from requests.utils import ( |
| 17 |
_parse_content_type_header, |
| 18 |
add_dict_to_cookiejar, |
| 19 |
address_in_network, |
| 20 |
dotted_netmask, |
| 21 |
extract_zipped_paths, |
| 22 |
get_auth_from_url, |
| 23 |
get_encoding_from_headers, |
| 24 |
get_encodings_from_content, |
| 25 |
get_environ_proxies, |
| 26 |
get_netrc_auth, |
| 27 |
guess_filename, |
| 28 |
guess_json_utf, |
| 29 |
is_ipv4_address, |
| 30 |
is_valid_cidr, |
| 31 |
iter_slices, |
| 32 |
parse_dict_header, |
| 33 |
parse_header_links, |
| 34 |
prepend_scheme_if_needed, |
| 35 |
requote_uri, |
| 36 |
select_proxy, |
| 37 |
set_environ, |
| 38 |
should_bypass_proxies, |
| 39 |
super_len, |
| 40 |
to_key_val_list, |
| 41 |
to_native_string, |
| 42 |
unquote_header_value, |
| 43 |
unquote_unreserved, |
| 44 |
urldefragauth, |
| 45 |
) |
| 46 |
|
| 47 |
from .compat import StringIO, cStringIO |
| 48 |
|
| 49 |
|
| 50 |
class TestSuperLen: |
| 51 |
@pytest.mark.parametrize( |
| 52 |
"stream, value", |
| 53 |
( |
| 54 |
(StringIO.StringIO, "Test"), |
| 55 |
(BytesIO, b"Test"), |
| 56 |
pytest.param( |
| 57 |
cStringIO, "Test", marks=pytest.mark.skipif("cStringIO is None") |
| 58 |
), |
| 59 |
), |
| 60 |
) |
| 61 |
def test_io_streams(self, stream, value): |
| 62 |
"""Ensures that we properly deal with different kinds of IO streams.""" |
| 63 |
assert super_len(stream()) == 0 |
| 64 |
assert super_len(stream(value)) == 4 |
| 65 |
|
| 66 |
def test_super_len_correctly_calculates_len_of_partially_read_file(self): |
| 67 |
"""Ensure that we handle partially consumed file like objects.""" |
| 68 |
s = StringIO.StringIO() |
| 69 |
s.write("foobarbogus") |
| 70 |
assert super_len(s) == 0 |
| 71 |
|
| 72 |
@pytest.mark.parametrize("error", [IOError, OSError]) |
| 73 |
def test_super_len_handles_files_raising_weird_errors_in_tell(self, error): |
| 74 |
"""If tell() raises errors, assume the cursor is at position zero.""" |
| 75 |
|
| 76 |
class BoomFile: |
| 77 |
def __len__(self): |
| 78 |
return 5 |
| 79 |
|
| 80 |
def tell(self): |
| 81 |
raise error() |
| 82 |
|
| 83 |
assert super_len(BoomFile()) == 0 |
| 84 |
|
| 85 |
@pytest.mark.parametrize("error", [IOError, OSError]) |
| 86 |
def test_super_len_tell_ioerror(self, error): |
| 87 |
"""Ensure that if tell gives an IOError super_len doesn't fail""" |
| 88 |
|
| 89 |
class NoLenBoomFile: |
| 90 |
def tell(self): |
| 91 |
raise error() |
| 92 |
|
| 93 |
def seek(self, offset, whence): |
| 94 |
pass |
| 95 |
|
| 96 |
assert super_len(NoLenBoomFile()) == 0 |
| 97 |
|
| 98 |
def test_string(self): |
| 99 |
assert super_len("Test") == 4 |
| 100 |
|
| 101 |
@pytest.mark.parametrize( |
| 102 |
"mode, warnings_num", |
| 103 |
( |
| 104 |
("r", 1), |
| 105 |
("rb", 0), |
| 106 |
), |
| 107 |
) |
| 108 |
def test_file(self, tmpdir, mode, warnings_num, recwarn): |
| 109 |
file_obj = tmpdir.join("test.txt") |
| 110 |
file_obj.write("Test") |
| 111 |
with file_obj.open(mode) as fd: |
| 112 |
assert super_len(fd) == 4 |
| 113 |
assert len(recwarn) == warnings_num |
| 114 |
|
| 115 |
def test_tarfile_member(self, tmpdir): |
| 116 |
file_obj = tmpdir.join("test.txt") |
| 117 |
file_obj.write("Test") |
| 118 |
|
| 119 |
tar_obj = str(tmpdir.join("test.tar")) |
| 120 |
with tarfile.open(tar_obj, "w") as tar: |
| 121 |
tar.add(str(file_obj), arcname="test.txt") |
| 122 |
|
| 123 |
with tarfile.open(tar_obj) as tar: |
| 124 |
member = tar.extractfile("test.txt") |
| 125 |
assert super_len(member) == 4 |
| 126 |
|
| 127 |
def test_super_len_with__len__(self): |
| 128 |
foo = [1, 2, 3, 4] |
| 129 |
len_foo = super_len(foo) |
| 130 |
assert len_foo == 4 |
| 131 |
|
| 132 |
def test_super_len_with_no__len__(self): |
| 133 |
class LenFile: |
| 134 |
def __init__(self): |
| 135 |
self.len = 5 |
| 136 |
|
| 137 |
assert super_len(LenFile()) == 5 |
| 138 |
|
| 139 |
def test_super_len_with_tell(self): |
| 140 |
foo = StringIO.StringIO("12345") |
| 141 |
assert super_len(foo) == 5 |
| 142 |
foo.read(2) |
| 143 |
assert super_len(foo) == 3 |
| 144 |
|
| 145 |
def test_super_len_with_fileno(self): |
| 146 |
with open(__file__, "rb") as f: |
| 147 |
length = super_len(f) |
| 148 |
file_data = f.read() |
| 149 |
assert length == len(file_data) |
| 150 |
|
| 151 |
def test_super_len_with_no_matches(self): |
| 152 |
"""Ensure that objects without any length methods default to 0""" |
| 153 |
assert super_len(object()) == 0 |
| 154 |
|
| 155 |
|
| 156 |
class TestGetNetrcAuth: |
| 157 |
def test_works(self, tmp_path, monkeypatch): |
| 158 |
netrc_path = tmp_path / ".netrc" |
| 159 |
monkeypatch.setenv("NETRC", str(netrc_path)) |
| 160 |
with open(netrc_path, "w") as f: |
| 161 |
f.write("machine example.com login aaaa password bbbb\n") |
| 162 |
auth = get_netrc_auth("http://example.com/thing") |
| 163 |
assert auth == ("aaaa", "bbbb") |
| 164 |
|
| 165 |
def test_not_vulnerable_to_bad_url_parsing(self, tmp_path, monkeypatch): |
| 166 |
netrc_path = tmp_path / ".netrc" |
| 167 |
monkeypatch.setenv("NETRC", str(netrc_path)) |
| 168 |
with open(netrc_path, "w") as f: |
| 169 |
f.write("machine example.com login aaaa password bbbb\n") |
| 170 |
auth = get_netrc_auth("http://example.com:@evil.com/'") |
| 171 |
assert auth is None |
| 172 |
|
| 173 |
def test_empty_default_credentials_ignored(self, tmp_path, monkeypatch): |
| 174 |
"""Empty default credentials should not be returned.""" |
| 175 |
netrc_path = tmp_path / ".netrc" |
| 176 |
monkeypatch.setenv("NETRC", str(netrc_path)) |
| 177 |
with open(netrc_path, "w") as f: |
| 178 |
f.write("machine example.com login user password pass\ndefault\n") |
| 179 |
|
| 180 |
auth = get_netrc_auth("http://httpbin.org/") |
| 181 |
assert auth is None |
| 182 |
|
| 183 |
|
| 184 |
class TestToKeyValList: |
| 185 |
@pytest.mark.parametrize( |
| 186 |
"value, expected", |
| 187 |
( |
| 188 |
([("key", "val")], [("key", "val")]), |
| 189 |
((("key", "val"),), [("key", "val")]), |
| 190 |
({"key": "val"}, [("key", "val")]), |
| 191 |
(None, None), |
| 192 |
), |
| 193 |
) |
| 194 |
def test_valid(self, value, expected): |
| 195 |
assert to_key_val_list(value) == expected |
| 196 |
|
| 197 |
def test_invalid(self): |
| 198 |
with pytest.raises(ValueError): |
| 199 |
to_key_val_list("string") |
| 200 |
|
| 201 |
|
| 202 |
class TestUnquoteHeaderValue: |
| 203 |
@pytest.mark.parametrize( |
| 204 |
"value, expected", |
| 205 |
( |
| 206 |
(None, None), |
| 207 |
("Test", "Test"), |
| 208 |
('"Test"', "Test"), |
| 209 |
('"Test\\\\"', "Test\\"), |
| 210 |
('"\\\\Comp\\Res"', "\\Comp\\Res"), |
| 211 |
), |
| 212 |
) |
| 213 |
def test_valid(self, value, expected): |
| 214 |
assert unquote_header_value(value) == expected |
| 215 |
|
| 216 |
def test_is_filename(self): |
| 217 |
assert unquote_header_value('"\\\\Comp\\Res"', True) == "\\\\Comp\\Res" |
| 218 |
|
| 219 |
|
| 220 |
class TestGetEnvironProxies: |
| 221 |
"""Ensures that IP addresses are correctly matches with ranges |
| 222 |
in no_proxy variable. |
| 223 |
""" |
| 224 |
|
| 225 |
@pytest.fixture(autouse=True, params=["no_proxy", "NO_PROXY"]) |
| 226 |
def no_proxy(self, request, monkeypatch): |
| 227 |
monkeypatch.setenv( |
| 228 |
request.param, "192.168.0.0/24,127.0.0.1,localhost.localdomain,172.16.1.1" |
| 229 |
) |
| 230 |
|
| 231 |
@pytest.mark.parametrize( |
| 232 |
"url", |
| 233 |
( |
| 234 |
"http://192.168.0.1:5000/", |
| 235 |
"http://192.168.0.1/", |
| 236 |
"http://172.16.1.1/", |
| 237 |
"http://172.16.1.1:5000/", |
| 238 |
"http://localhost.localdomain:5000/v1.0/", |
| 239 |
), |
| 240 |
) |
| 241 |
def test_bypass(self, url): |
| 242 |
assert get_environ_proxies(url, no_proxy=None) == {} |
| 243 |
|
| 244 |
@pytest.mark.parametrize( |
| 245 |
"url", |
| 246 |
( |
| 247 |
"http://192.168.1.1:5000/", |
| 248 |
"http://192.168.1.1/", |
| 249 |
"http://www.requests.com/", |
| 250 |
), |
| 251 |
) |
| 252 |
def test_not_bypass(self, url): |
| 253 |
assert get_environ_proxies(url, no_proxy=None) != {} |
| 254 |
|
| 255 |
@pytest.mark.parametrize( |
| 256 |
"url", |
| 257 |
( |
| 258 |
"http://192.168.1.1:5000/", |
| 259 |
"http://192.168.1.1/", |
| 260 |
"http://www.requests.com/", |
| 261 |
), |
| 262 |
) |
| 263 |
def test_bypass_no_proxy_keyword(self, url): |
| 264 |
no_proxy = "192.168.1.1,requests.com" |
| 265 |
assert get_environ_proxies(url, no_proxy=no_proxy) == {} |
| 266 |
|
| 267 |
@pytest.mark.parametrize( |
| 268 |
"url", |
| 269 |
( |
| 270 |
"http://192.168.0.1:5000/", |
| 271 |
"http://192.168.0.1/", |
| 272 |
"http://172.16.1.1/", |
| 273 |
"http://172.16.1.1:5000/", |
| 274 |
"http://localhost.localdomain:5000/v1.0/", |
| 275 |
), |
| 276 |
) |
| 277 |
def test_not_bypass_no_proxy_keyword(self, url, monkeypatch): |
| 278 |
# This is testing that the 'no_proxy' argument overrides the |
| 279 |
# environment variable 'no_proxy' |
| 280 |
monkeypatch.setenv("http_proxy", "http://proxy.example.com:3128/") |
| 281 |
no_proxy = "192.168.1.1,requests.com" |
| 282 |
assert get_environ_proxies(url, no_proxy=no_proxy) != {} |
| 283 |
|
| 284 |
|
| 285 |
class TestIsIPv4Address: |
| 286 |
def test_valid(self): |
| 287 |
assert is_ipv4_address("8.8.8.8") |
| 288 |
|
| 289 |
@pytest.mark.parametrize("value", ("8.8.8.8.8", "localhost.localdomain")) |
| 290 |
def test_invalid(self, value): |
| 291 |
assert not is_ipv4_address(value) |
| 292 |
|
| 293 |
|
| 294 |
class TestIsValidCIDR: |
| 295 |
def test_valid(self): |
| 296 |
assert is_valid_cidr("192.168.1.0/24") |
| 297 |
|
| 298 |
@pytest.mark.parametrize( |
| 299 |
"value", |
| 300 |
( |
| 301 |
"8.8.8.8", |
| 302 |
"192.168.1.0/a", |
| 303 |
"192.168.1.0/128", |
| 304 |
"192.168.1.0/-1", |
| 305 |
"192.168.1.999/24", |
| 306 |
), |
| 307 |
) |
| 308 |
def test_invalid(self, value): |
| 309 |
assert not is_valid_cidr(value) |
| 310 |
|
| 311 |
|
| 312 |
class TestAddressInNetwork: |
| 313 |
def test_valid(self): |
| 314 |
assert address_in_network("192.168.1.1", "192.168.1.0/24") |
| 315 |
|
| 316 |
def test_invalid(self): |
| 317 |
assert not address_in_network("172.16.0.1", "192.168.1.0/24") |
| 318 |
|
| 319 |
|
| 320 |
class TestGuessFilename: |
| 321 |
@pytest.mark.parametrize( |
| 322 |
"value", |
| 323 |
(1, type("Fake", (object,), {"name": 1})()), |
| 324 |
) |
| 325 |
def test_guess_filename_invalid(self, value): |
| 326 |
assert guess_filename(value) is None |
| 327 |
|
| 328 |
@pytest.mark.parametrize( |
| 329 |
"value, expected_type", |
| 330 |
( |
| 331 |
(b"value", compat.bytes), |
| 332 |
(b"value".decode("utf-8"), compat.str), |
| 333 |
), |
| 334 |
) |
| 335 |
def test_guess_filename_valid(self, value, expected_type): |
| 336 |
obj = type("Fake", (object,), {"name": value})() |
| 337 |
result = guess_filename(obj) |
| 338 |
assert result == value |
| 339 |
assert isinstance(result, expected_type) |
| 340 |
|
| 341 |
|
| 342 |
class TestExtractZippedPaths: |
| 343 |
@pytest.mark.parametrize( |
| 344 |
"path", |
| 345 |
( |
| 346 |
"/", |
| 347 |
__file__, |
| 348 |
pytest.__file__, |
| 349 |
"/etc/invalid/location", |
| 350 |
), |
| 351 |
) |
| 352 |
def test_unzipped_paths_unchanged(self, path): |
| 353 |
assert path == extract_zipped_paths(path) |
| 354 |
|
| 355 |
def test_zipped_paths_extracted(self, tmpdir): |
| 356 |
zipped_py = tmpdir.join("test.zip") |
| 357 |
with zipfile.ZipFile(zipped_py.strpath, "w") as f: |
| 358 |
f.write(__file__) |
| 359 |
|
| 360 |
_, name = os.path.splitdrive(__file__) |
| 361 |
zipped_path = os.path.join(zipped_py.strpath, name.lstrip(r"\/")) |
| 362 |
extracted_path = extract_zipped_paths(zipped_path) |
| 363 |
|
| 364 |
try: |
| 365 |
assert extracted_path != zipped_path |
| 366 |
assert os.path.exists(extracted_path) |
| 367 |
assert filecmp.cmp(extracted_path, __file__) |
| 368 |
finally: |
| 369 |
os.remove(extracted_path) |
| 370 |
|
| 371 |
def test_invalid_unc_path(self): |
| 372 |
path = r"\\localhost\invalid\location" |
| 373 |
assert extract_zipped_paths(path) == path |
| 374 |
|
| 375 |
|
| 376 |
class TestContentEncodingDetection: |
| 377 |
def test_none(self): |
| 378 |
encodings = get_encodings_from_content("") |
| 379 |
assert not len(encodings) |
| 380 |
|
| 381 |
@pytest.mark.parametrize( |
| 382 |
"content", |
| 383 |
( |
| 384 |
# HTML5 meta charset attribute |
| 385 |
'<meta charset="UTF-8">', |
| 386 |
# HTML4 pragma directive |
| 387 |
'<meta http-equiv="Content-type" content="text/html;charset=UTF-8">', |
| 388 |
# XHTML 1.x served with text/html MIME type |
| 389 |
'<meta http-equiv="Content-type" content="text/html;charset=UTF-8" />', |
| 390 |
# XHTML 1.x served as XML |
| 391 |
'<?xml version="1.0" encoding="UTF-8"?>', |
| 392 |
), |
| 393 |
) |
| 394 |
def test_pragmas(self, content): |
| 395 |
encodings = get_encodings_from_content(content) |
| 396 |
assert len(encodings) == 1 |
| 397 |
assert encodings[0] == "UTF-8" |
| 398 |
|
| 399 |
def test_precedence(self): |
| 400 |
content = """ |
| 401 |
<?xml version="1.0" encoding="XML"?> |
| 402 |
<meta charset="HTML5"> |
| 403 |
<meta http-equiv="Content-type" content="text/html;charset=HTML4" /> |
| 404 |
""".strip() |
| 405 |
assert get_encodings_from_content(content) == ["HTML5", "HTML4", "XML"] |
| 406 |
|
| 407 |
|
| 408 |
class TestGuessJSONUTF: |
| 409 |
@pytest.mark.parametrize( |
| 410 |
"encoding", |
| 411 |
( |
| 412 |
"utf-32", |
| 413 |
"utf-8-sig", |
| 414 |
"utf-16", |
| 415 |
"utf-8", |
| 416 |
"utf-16-be", |
| 417 |
"utf-16-le", |
| 418 |
"utf-32-be", |
| 419 |
"utf-32-le", |
| 420 |
), |
| 421 |
) |
| 422 |
def test_encoded(self, encoding): |
| 423 |
data = "{}".encode(encoding) |
| 424 |
assert guess_json_utf(data) == encoding |
| 425 |
|
| 426 |
def test_bad_utf_like_encoding(self): |
| 427 |
assert guess_json_utf(b"\x00\x00\x00\x00") is None |
| 428 |
|
| 429 |
@pytest.mark.parametrize( |
| 430 |
("encoding", "expected"), |
| 431 |
( |
| 432 |
("utf-16-be", "utf-16"), |
| 433 |
("utf-16-le", "utf-16"), |
| 434 |
("utf-32-be", "utf-32"), |
| 435 |
("utf-32-le", "utf-32"), |
| 436 |
), |
| 437 |
) |
| 438 |
def test_guess_by_bom(self, encoding, expected): |
| 439 |
data = "\ufeff{}".encode(encoding) |
| 440 |
assert guess_json_utf(data) == expected |
| 441 |
|
| 442 |
|
| 443 |
USER = PASSWORD = "%!*'();:@&=+$,/?#[] " |
| 444 |
ENCODED_USER = compat.quote(USER, "") |
| 445 |
ENCODED_PASSWORD = compat.quote(PASSWORD, "") |
| 446 |
|
| 447 |
|
| 448 |
@pytest.mark.parametrize( |
| 449 |
"url, auth", |
| 450 |
( |
| 451 |
( |
| 452 |
f"http://{ENCODED_USER}:{ENCODED_PASSWORD}@request.com/url.html#test", |
| 453 |
(USER, PASSWORD), |
| 454 |
), |
| 455 |
("http://user:[email protected]/path?query=yes", ("user", "pass")), |
| 456 |
( |
| 457 |
"http://user:pass%[email protected]/path?query=yes", |
| 458 |
("user", "pass pass"), |
| 459 |
), |
| 460 |
("http://user:pass [email protected]/path?query=yes", ("user", "pass pass")), |
| 461 |
( |
| 462 |
"http://user%25user:[email protected]/path?query=yes", |
| 463 |
("user%user", "pass"), |
| 464 |
), |
| 465 |
( |
| 466 |
"http://user:pass%[email protected]/path?query=yes", |
| 467 |
("user", "pass#pass"), |
| 468 |
), |
| 469 |
("http://complex.url.com/path?query=yes", ("", "")), |
| 470 |
), |
| 471 |
) |
| 472 |
def test_get_auth_from_url(url, auth): |
| 473 |
assert get_auth_from_url(url) == auth |
| 474 |
|
| 475 |
|
| 476 |
@pytest.mark.parametrize( |
| 477 |
"uri, expected", |
| 478 |
( |
| 479 |
( |
| 480 |
# Ensure requoting doesn't break expectations |
| 481 |
"http://example.com/fiz?buz=%25ppicture", |
| 482 |
"http://example.com/fiz?buz=%25ppicture", |
| 483 |
), |
| 484 |
( |
| 485 |
# Ensure we handle unquoted percent signs in redirects |
| 486 |
"http://example.com/fiz?buz=%ppicture", |
| 487 |
"http://example.com/fiz?buz=%25ppicture", |
| 488 |
), |
| 489 |
), |
| 490 |
) |
| 491 |
def test_requote_uri_with_unquoted_percents(uri, expected): |
| 492 |
"""See: https://github.com/psf/requests/issues/2356""" |
| 493 |
assert requote_uri(uri) == expected |
| 494 |
|
| 495 |
|
| 496 |
@pytest.mark.parametrize( |
| 497 |
"uri, expected", |
| 498 |
( |
| 499 |
( |
| 500 |
# Illegal bytes |
| 501 |
"http://example.com/?a=%--", |
| 502 |
"http://example.com/?a=%--", |
| 503 |
), |
| 504 |
( |
| 505 |
# Reserved characters |
| 506 |
"http://example.com/?a=%300", |
| 507 |
"http://example.com/?a=00", |
| 508 |
), |
| 509 |
), |
| 510 |
) |
| 511 |
def test_unquote_unreserved(uri, expected): |
| 512 |
assert unquote_unreserved(uri) == expected |
| 513 |
|
| 514 |
|
| 515 |
@pytest.mark.parametrize( |
| 516 |
"mask, expected", |
| 517 |
( |
| 518 |
(8, "255.0.0.0"), |
| 519 |
(24, "255.255.255.0"), |
| 520 |
(25, "255.255.255.128"), |
| 521 |
), |
| 522 |
) |
| 523 |
def test_dotted_netmask(mask, expected): |
| 524 |
assert dotted_netmask(mask) == expected |
| 525 |
|
| 526 |
|
| 527 |
http_proxies = { |
| 528 |
"http": "http://http.proxy", |
| 529 |
"http://some.host": "http://some.host.proxy", |
| 530 |
} |
| 531 |
all_proxies = { |
| 532 |
"all": "socks5://http.proxy", |
| 533 |
"all://some.host": "socks5://some.host.proxy", |
| 534 |
} |
| 535 |
mixed_proxies = { |
| 536 |
"http": "http://http.proxy", |
| 537 |
"http://some.host": "http://some.host.proxy", |
| 538 |
"all": "socks5://http.proxy", |
| 539 |
} |
| 540 |
|
| 541 |
|
| 542 |
@pytest.mark.parametrize( |
| 543 |
"url, expected, proxies", |
| 544 |
( |
| 545 |
("hTTp://u:[email protected]/path", "http://some.host.proxy", http_proxies), |
| 546 |
("hTTp://u:[email protected]/path", "http://http.proxy", http_proxies), |
| 547 |
("hTTp:///path", "http://http.proxy", http_proxies), |
| 548 |
("hTTps://Other.Host", None, http_proxies), |
| 549 |
("file:///etc/motd", None, http_proxies), |
| 550 |
("hTTp://u:[email protected]/path", "socks5://some.host.proxy", all_proxies), |
| 551 |
("hTTp://u:[email protected]/path", "socks5://http.proxy", all_proxies), |
| 552 |
("hTTp:///path", "socks5://http.proxy", all_proxies), |
| 553 |
("hTTps://Other.Host", "socks5://http.proxy", all_proxies), |
| 554 |
("http://u:[email protected]/path", "http://http.proxy", mixed_proxies), |
| 555 |
("http://u:[email protected]/path", "http://some.host.proxy", mixed_proxies), |
| 556 |
("https://u:[email protected]/path", "socks5://http.proxy", mixed_proxies), |
| 557 |
("https://u:[email protected]/path", "socks5://http.proxy", mixed_proxies), |
| 558 |
("https://", "socks5://http.proxy", mixed_proxies), |
| 559 |
# XXX: unsure whether this is reasonable behavior |
| 560 |
("file:///etc/motd", "socks5://http.proxy", all_proxies), |
| 561 |
), |
| 562 |
) |
| 563 |
def test_select_proxies(url, expected, proxies): |
| 564 |
"""Make sure we can select per-host proxies correctly.""" |
| 565 |
assert select_proxy(url, proxies) == expected |
| 566 |
|
| 567 |
|
| 568 |
@pytest.mark.parametrize( |
| 569 |
"value, expected", |
| 570 |
( |
| 571 |
('foo="is a fish", bar="as well"', {"foo": "is a fish", "bar": "as well"}), |
| 572 |
("key_without_value", {"key_without_value": None}), |
| 573 |
), |
| 574 |
) |
| 575 |
def test_parse_dict_header(value, expected): |
| 576 |
assert parse_dict_header(value) == expected |
| 577 |
|
| 578 |
|
| 579 |
@pytest.mark.parametrize( |
| 580 |
"value, expected", |
| 581 |
( |
| 582 |
("application/xml", ("application/xml", {})), |
| 583 |
( |
| 584 |
"application/json ; charset=utf-8", |
| 585 |
("application/json", {"charset": "utf-8"}), |
| 586 |
), |
| 587 |
( |
| 588 |
"application/json ; Charset=utf-8", |
| 589 |
("application/json", {"charset": "utf-8"}), |
| 590 |
), |
| 591 |
("text/plain", ("text/plain", {})), |
| 592 |
( |
| 593 |
"multipart/form-data; boundary = something ; boundary2='something_else' ; no_equals ", |
| 594 |
( |
| 595 |
"multipart/form-data", |
| 596 |
{ |
| 597 |
"boundary": "something", |
| 598 |
"boundary2": "something_else", |
| 599 |
}, |
| 600 |
), |
| 601 |
), |
| 602 |
( |
| 603 |
'multipart/form-data; boundary = something ; boundary2="something_else" ; no_equals ', |
| 604 |
( |
| 605 |
"multipart/form-data", |
| 606 |
{ |
| 607 |
"boundary": "something", |
| 608 |
"boundary2": "something_else", |
| 609 |
}, |
| 610 |
), |
| 611 |
), |
| 612 |
( |
| 613 |
"multipart/form-data; boundary = something ; 'boundary2=something_else' ; no_equals ", |
| 614 |
( |
| 615 |
"multipart/form-data", |
| 616 |
{ |
| 617 |
"boundary": "something", |
| 618 |
"boundary2": "something_else", |
| 619 |
}, |
| 620 |
), |
| 621 |
), |
| 622 |
( |
| 623 |
'multipart/form-data; boundary = something ; "boundary2=something_else" ; no_equals ', |
| 624 |
( |
| 625 |
"multipart/form-data", |
| 626 |
{ |
| 627 |
"boundary": "something", |
| 628 |
"boundary2": "something_else", |
| 629 |
}, |
| 630 |
), |
| 631 |
), |
| 632 |
("application/json ; ; ", ("application/json", {})), |
| 633 |
), |
| 634 |
) |
| 635 |
def test__parse_content_type_header(value, expected): |
| 636 |
assert _parse_content_type_header(value) == expected |
| 637 |
|
| 638 |
|
| 639 |
@pytest.mark.parametrize( |
| 640 |
"value, expected", |
| 641 |
( |
| 642 |
(CaseInsensitiveDict(), None), |
| 643 |
( |
| 644 |
CaseInsensitiveDict({"content-type": "application/json; charset=utf-8"}), |
| 645 |
"utf-8", |
| 646 |
), |
| 647 |
(CaseInsensitiveDict({"content-type": "text/plain"}), "ISO-8859-1"), |
| 648 |
( |
| 649 |
CaseInsensitiveDict({"content-type": "text/html; charset"}), |
| 650 |
"ISO-8859-1", |
| 651 |
), |
| 652 |
), |
| 653 |
) |
| 654 |
def test_get_encoding_from_headers(value, expected): |
| 655 |
assert get_encoding_from_headers(value) == expected |
| 656 |
|
| 657 |
|
| 658 |
@pytest.mark.parametrize( |
| 659 |
"value, length", |
| 660 |
( |
| 661 |
("", 0), |
| 662 |
("T", 1), |
| 663 |
("Test", 4), |
| 664 |
("Cont", 0), |
| 665 |
("Other", -5), |
| 666 |
("Content", None), |
| 667 |
), |
| 668 |
) |
| 669 |
def test_iter_slices(value, length): |
| 670 |
if length is None or (length <= 0 and len(value) > 0): |
| 671 |
# Reads all content at once |
| 672 |
assert len(list(iter_slices(value, length))) == 1 |
| 673 |
else: |
| 674 |
assert len(list(iter_slices(value, 1))) == length |
| 675 |
|
| 676 |
|
| 677 |
@pytest.mark.parametrize( |
| 678 |
"value, expected", |
| 679 |
( |
| 680 |
( |
| 681 |
'<http:/.../front.jpeg>; rel=front; type="image/jpeg"', |
| 682 |
[{"url": "http:/.../front.jpeg", "rel": "front", "type": "image/jpeg"}], |
| 683 |
), |
| 684 |
("<http:/.../front.jpeg>", [{"url": "http:/.../front.jpeg"}]), |
| 685 |
("<http:/.../front.jpeg>;", [{"url": "http:/.../front.jpeg"}]), |
| 686 |
( |
| 687 |
'<http:/.../front.jpeg>; type="image/jpeg",<http://.../back.jpeg>;', |
| 688 |
[ |
| 689 |
{"url": "http:/.../front.jpeg", "type": "image/jpeg"}, |
| 690 |
{"url": "http://.../back.jpeg"}, |
| 691 |
], |
| 692 |
), |
| 693 |
("", []), |
| 694 |
), |
| 695 |
) |
| 696 |
def test_parse_header_links(value, expected): |
| 697 |
assert parse_header_links(value) == expected |
| 698 |
|
| 699 |
|
| 700 |
@pytest.mark.parametrize( |
| 701 |
"value, expected", |
| 702 |
( |
| 703 |
("example.com/path", "http://example.com/path"), |
| 704 |
("//example.com/path", "http://example.com/path"), |
| 705 |
("example.com:80", "http://example.com:80"), |
| 706 |
( |
| 707 |
"http://user:[email protected]/path?query", |
| 708 |
"http://user:[email protected]/path?query", |
| 709 |
), |
| 710 |
("http://[email protected]/path?query", "http://[email protected]/path?query"), |
| 711 |
), |
| 712 |
) |
| 713 |
def test_prepend_scheme_if_needed(value, expected): |
| 714 |
assert prepend_scheme_if_needed(value, "http") == expected |
| 715 |
|
| 716 |
|
| 717 |
@pytest.mark.parametrize( |
| 718 |
"value, expected", |
| 719 |
( |
| 720 |
("T", "T"), |
| 721 |
(b"T", "T"), |
| 722 |
("T", "T"), |
| 723 |
), |
| 724 |
) |
| 725 |
def test_to_native_string(value, expected): |
| 726 |
assert to_native_string(value) == expected |
| 727 |
|
| 728 |
|
| 729 |
@pytest.mark.parametrize( |
| 730 |
"url, expected", |
| 731 |
( |
| 732 |
("http://u:[email protected]/path?a=1#test", "http://example.com/path?a=1"), |
| 733 |
("http://example.com/path", "http://example.com/path"), |
| 734 |
("//u:[email protected]/path", "//example.com/path"), |
| 735 |
("//example.com/path", "//example.com/path"), |
| 736 |
("example.com/path", "//example.com/path"), |
| 737 |
("scheme:u:[email protected]/path", "scheme://example.com/path"), |
| 738 |
), |
| 739 |
) |
| 740 |
def test_urldefragauth(url, expected): |
| 741 |
assert urldefragauth(url) == expected |
| 742 |
|
| 743 |
|
| 744 |
@pytest.mark.parametrize( |
| 745 |
"url, expected", |
| 746 |
( |
| 747 |
("http://192.168.0.1:5000/", True), |
| 748 |
("http://192.168.0.1/", True), |
| 749 |
("http://172.16.1.1/", True), |
| 750 |
("http://172.16.1.1:5000/", True), |
| 751 |
("http://localhost.localdomain:5000/v1.0/", True), |
| 752 |
("http://google.com:6000/", True), |
| 753 |
("http://172.16.1.12/", False), |
| 754 |
("http://172.16.1.12:5000/", False), |
| 755 |
("http://google.com:5000/v1.0/", False), |
| 756 |
("file:///some/path/on/disk", True), |
| 757 |
), |
| 758 |
) |
| 759 |
def test_should_bypass_proxies(url, expected, monkeypatch): |
| 760 |
"""Tests for function should_bypass_proxies to check if proxy |
| 761 |
can be bypassed or not |
| 762 |
""" |
| 763 |
monkeypatch.setenv( |
| 764 |
"no_proxy", |
| 765 |
"192.168.0.0/24,127.0.0.1,localhost.localdomain,172.16.1.1, google.com:6000", |
| 766 |
) |
| 767 |
monkeypatch.setenv( |
| 768 |
"NO_PROXY", |
| 769 |
"192.168.0.0/24,127.0.0.1,localhost.localdomain,172.16.1.1, google.com:6000", |
| 770 |
) |
| 771 |
assert should_bypass_proxies(url, no_proxy=None) == expected |
| 772 |
|
| 773 |
|
| 774 |
@pytest.mark.parametrize( |
| 775 |
"url, expected", |
| 776 |
( |
| 777 |
("http://172.16.1.1/", "172.16.1.1"), |
| 778 |
("http://172.16.1.1:5000/", "172.16.1.1"), |
| 779 |
("http://user:[email protected]", "172.16.1.1"), |
| 780 |
("http://user:[email protected]:5000", "172.16.1.1"), |
| 781 |
("http://hostname/", "hostname"), |
| 782 |
("http://hostname:5000/", "hostname"), |
| 783 |
("http://user:pass@hostname", "hostname"), |
| 784 |
("http://user:pass@hostname:5000", "hostname"), |
| 785 |
), |
| 786 |
) |
| 787 |
def test_should_bypass_proxies_pass_only_hostname(url, expected): |
| 788 |
"""The proxy_bypass function should be called with a hostname or IP without |
| 789 |
a port number or auth credentials. |
| 790 |
""" |
| 791 |
with mock.patch("requests.utils.proxy_bypass") as proxy_bypass: |
| 792 |
should_bypass_proxies(url, no_proxy=None) |
| 793 |
proxy_bypass.assert_called_once_with(expected) |
| 794 |
|
| 795 |
|
| 796 |
@pytest.mark.parametrize( |
| 797 |
"cookiejar", |
| 798 |
( |
| 799 |
compat.cookielib.CookieJar(), |
| 800 |
RequestsCookieJar(), |
| 801 |
), |
| 802 |
) |
| 803 |
def test_add_dict_to_cookiejar(cookiejar): |
| 804 |
"""Ensure add_dict_to_cookiejar works for |
| 805 |
non-RequestsCookieJar CookieJars |
| 806 |
""" |
| 807 |
cookiedict = {"test": "cookies", "good": "cookies"} |
| 808 |
cj = add_dict_to_cookiejar(cookiejar, cookiedict) |
| 809 |
cookies = {cookie.name: cookie.value for cookie in cj} |
| 810 |
assert cookiedict == cookies |
| 811 |
|
| 812 |
|
| 813 |
@pytest.mark.parametrize( |
| 814 |
"value, expected", |
| 815 |
( |
| 816 |
("test", True), |
| 817 |
("æíöû", False), |
| 818 |
("ジェーピーニック", False), |
| 819 |
), |
| 820 |
) |
| 821 |
def test_unicode_is_ascii(value, expected): |
| 822 |
assert unicode_is_ascii(value) is expected |
| 823 |
|
| 824 |
|
| 825 |
@pytest.mark.parametrize( |
| 826 |
"url, expected", |
| 827 |
( |
| 828 |
("http://192.168.0.1:5000/", True), |
| 829 |
("http://192.168.0.1/", True), |
| 830 |
("http://172.16.1.1/", True), |
| 831 |
("http://172.16.1.1:5000/", True), |
| 832 |
("http://localhost.localdomain:5000/v1.0/", True), |
| 833 |
("http://172.16.1.12/", False), |
| 834 |
("http://172.16.1.12:5000/", False), |
| 835 |
("http://google.com:5000/v1.0/", False), |
| 836 |
), |
| 837 |
) |
| 838 |
def test_should_bypass_proxies_no_proxy(url, expected, monkeypatch): |
| 839 |
"""Tests for function should_bypass_proxies to check if proxy |
| 840 |
can be bypassed or not using the 'no_proxy' argument |
| 841 |
""" |
| 842 |
no_proxy = "192.168.0.0/24,127.0.0.1,localhost.localdomain,172.16.1.1" |
| 843 |
# Test 'no_proxy' argument |
| 844 |
assert should_bypass_proxies(url, no_proxy=no_proxy) == expected |
| 845 |
|
| 846 |
|
| 847 |
@pytest.mark.parametrize( |
| 848 |
"url, expected", |
| 849 |
( |
| 850 |
("http://localhost/", True), |
| 851 |
("http://anotherdomain.com:8888/", True), |
| 852 |
("http://newdomain.com:1234/", True), |
| 853 |
("http://www.newdomain.com:1234/", True), |
| 854 |
("http://foo.d.o.t/", True), |
| 855 |
("http://d.o.t/", True), |
| 856 |
("http://prelocalhost/", False), |
| 857 |
("http://newdomain.com/", False), |
| 858 |
("http://newdomain.com:1235/", False), |
| 859 |
), |
| 860 |
) |
| 861 |
def test_should_bypass_proxies_no_proxy_domain_boundary(url, expected): |
| 862 |
"""Ensure no_proxy matching respects domain boundaries and does not |
| 863 |
greedily match domains that merely endswith the no_proxy entry. |
| 864 |
See CPython bpo-39057. |
| 865 |
""" |
| 866 |
no_proxy = "localhost, anotherdomain.com, newdomain.com:1234, .d.o.t" |
| 867 |
assert should_bypass_proxies(url, no_proxy=no_proxy) == expected |
| 868 |
|
| 869 |
|
| 870 |
@pytest.mark.skipif(os.name != "nt", reason="Test only on Windows") |
| 871 |
@pytest.mark.parametrize( |
| 872 |
"url, expected, override", |
| 873 |
( |
| 874 |
("http://192.168.0.1:5000/", True, None), |
| 875 |
("http://192.168.0.1/", True, None), |
| 876 |
("http://172.16.1.1/", True, None), |
| 877 |
("http://172.16.1.1:5000/", True, None), |
| 878 |
("http://localhost.localdomain:5000/v1.0/", True, None), |
| 879 |
("http://172.16.1.22/", False, None), |
| 880 |
("http://172.16.1.22:5000/", False, None), |
| 881 |
("http://google.com:5000/v1.0/", False, None), |
| 882 |
("http://mylocalhostname:5000/v1.0/", True, "<local>"), |
| 883 |
("http://192.168.0.1/", False, ""), |
| 884 |
), |
| 885 |
) |
| 886 |
def test_should_bypass_proxies_win_registry(url, expected, override, monkeypatch): |
| 887 |
"""Tests for function should_bypass_proxies to check if proxy |
| 888 |
can be bypassed or not with Windows registry settings |
| 889 |
""" |
| 890 |
if override is None: |
| 891 |
override = "192.168.*;127.0.0.1;localhost.localdomain;172.16.1.1" |
| 892 |
import winreg |
| 893 |
|
| 894 |
class RegHandle: |
| 895 |
def Close(self): |
| 896 |
pass |
| 897 |
|
| 898 |
ie_settings = RegHandle() |
| 899 |
proxyEnableValues = deque([1, "1"]) |
| 900 |
|
| 901 |
def OpenKey(key, subkey): |
| 902 |
return ie_settings |
| 903 |
|
| 904 |
def QueryValueEx(key, value_name): |
| 905 |
if key is ie_settings: |
| 906 |
if value_name == "ProxyEnable": |
| 907 |
# this could be a string (REG_SZ) or a 32-bit number (REG_DWORD) |
| 908 |
proxyEnableValues.rotate() |
| 909 |
return [proxyEnableValues[0]] |
| 910 |
elif value_name == "ProxyOverride": |
| 911 |
return [override] |
| 912 |
|
| 913 |
monkeypatch.setenv("http_proxy", "") |
| 914 |
monkeypatch.setenv("https_proxy", "") |
| 915 |
monkeypatch.setenv("ftp_proxy", "") |
| 916 |
monkeypatch.setenv("no_proxy", "") |
| 917 |
monkeypatch.setenv("NO_PROXY", "") |
| 918 |
monkeypatch.setattr(winreg, "OpenKey", OpenKey) |
| 919 |
monkeypatch.setattr(winreg, "QueryValueEx", QueryValueEx) |
| 920 |
assert should_bypass_proxies(url, None) == expected |
| 921 |
|
| 922 |
|
| 923 |
@pytest.mark.skipif(os.name != "nt", reason="Test only on Windows") |
| 924 |
def test_should_bypass_proxies_win_registry_bad_values(monkeypatch): |
| 925 |
"""Tests for function should_bypass_proxies to check if proxy |
| 926 |
can be bypassed or not with Windows invalid registry settings. |
| 927 |
""" |
| 928 |
import winreg |
| 929 |
|
| 930 |
class RegHandle: |
| 931 |
def Close(self): |
| 932 |
pass |
| 933 |
|
| 934 |
ie_settings = RegHandle() |
| 935 |
|
| 936 |
def OpenKey(key, subkey): |
| 937 |
return ie_settings |
| 938 |
|
| 939 |
def QueryValueEx(key, value_name): |
| 940 |
if key is ie_settings: |
| 941 |
if value_name == "ProxyEnable": |
| 942 |
# Invalid response; Should be an int or int-y value |
| 943 |
return [""] |
| 944 |
elif value_name == "ProxyOverride": |
| 945 |
return ["192.168.*;127.0.0.1;localhost.localdomain;172.16.1.1"] |
| 946 |
|
| 947 |
monkeypatch.setenv("http_proxy", "") |
| 948 |
monkeypatch.setenv("https_proxy", "") |
| 949 |
monkeypatch.setenv("no_proxy", "") |
| 950 |
monkeypatch.setenv("NO_PROXY", "") |
| 951 |
monkeypatch.setattr(winreg, "OpenKey", OpenKey) |
| 952 |
monkeypatch.setattr(winreg, "QueryValueEx", QueryValueEx) |
| 953 |
assert should_bypass_proxies("http://172.16.1.1/", None) is False |
| 954 |
|
| 955 |
|
| 956 |
@pytest.mark.parametrize( |
| 957 |
"env_name, value", |
| 958 |
( |
| 959 |
("no_proxy", "192.168.0.0/24,127.0.0.1,localhost.localdomain"), |
| 960 |
("no_proxy", None), |
| 961 |
("a_new_key", "192.168.0.0/24,127.0.0.1,localhost.localdomain"), |
| 962 |
("a_new_key", None), |
| 963 |
), |
| 964 |
) |
| 965 |
def test_set_environ(env_name, value): |
| 966 |
"""Tests set_environ will set environ values and will restore the environ.""" |
| 967 |
environ_copy = copy.deepcopy(os.environ) |
| 968 |
with set_environ(env_name, value): |
| 969 |
assert os.environ.get(env_name) == value |
| 970 |
|
| 971 |
assert os.environ == environ_copy |
| 972 |
|
| 973 |
|
| 974 |
def test_set_environ_raises_exception(): |
| 975 |
"""Tests set_environ will raise exceptions in context when the |
| 976 |
value parameter is None.""" |
| 977 |
with pytest.raises(Exception) as exception: |
| 978 |
with set_environ("test1", None): |
| 979 |
raise Exception("Expected exception") |
| 980 |
|
| 981 |
assert "Expected exception" in str(exception.value) |
| 982 |
|
| 983 |
|
| 984 |
@pytest.mark.skipif(os.name != "nt", reason="Test only on Windows") |
| 985 |
def test_should_bypass_proxies_win_registry_ProxyOverride_value(monkeypatch): |
| 986 |
"""Tests for function should_bypass_proxies to check if proxy |
| 987 |
can be bypassed or not with Windows ProxyOverride registry value ending with a semicolon. |
| 988 |
""" |
| 989 |
import winreg |
| 990 |
|
| 991 |
class RegHandle: |
| 992 |
def Close(self): |
| 993 |
pass |
| 994 |
|
| 995 |
ie_settings = RegHandle() |
| 996 |
|
| 997 |
def OpenKey(key, subkey): |
| 998 |
return ie_settings |
| 999 |
|
| 1000 |
def QueryValueEx(key, value_name): |
| 1001 |
if key is ie_settings: |
| 1002 |
if value_name == "ProxyEnable": |
| 1003 |
return [1] |
| 1004 |
elif value_name == "ProxyOverride": |
| 1005 |
return [ |
| 1006 |
"192.168.*;127.0.0.1;localhost.localdomain;172.16.1.1;<-loopback>;" |
| 1007 |
] |
| 1008 |
|
| 1009 |
monkeypatch.setenv("NO_PROXY", "") |
| 1010 |
monkeypatch.setenv("no_proxy", "") |
| 1011 |
monkeypatch.setattr(winreg, "OpenKey", OpenKey) |
| 1012 |
monkeypatch.setattr(winreg, "QueryValueEx", QueryValueEx) |
| 1013 |
assert should_bypass_proxies("http://example.com/", None) is False |