1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
|
import json
import pickle
import typing
import chardet
import pytest
import httpx
class StreamingBody:
def __iter__(self):
yield b"Hello, "
yield b"world!"
def streaming_body() -> typing.Iterator[bytes]:
yield b"Hello, "
yield b"world!"
async def async_streaming_body() -> typing.AsyncIterator[bytes]:
yield b"Hello, "
yield b"world!"
def autodetect(content):
return chardet.detect(content).get("encoding")
def test_response():
response = httpx.Response(
200,
content=b"Hello, world!",
request=httpx.Request("GET", "https://example.org"),
)
assert response.status_code == 200
assert response.reason_phrase == "OK"
assert response.text == "Hello, world!"
assert response.request.method == "GET"
assert response.request.url == "https://example.org"
assert not response.is_error
def test_response_content():
response = httpx.Response(200, content="Hello, world!")
assert response.status_code == 200
assert response.reason_phrase == "OK"
assert response.text == "Hello, world!"
assert response.headers == {"Content-Length": "13"}
def test_response_text():
response = httpx.Response(200, text="Hello, world!")
assert response.status_code == 200
assert response.reason_phrase == "OK"
assert response.text == "Hello, world!"
assert response.headers == {
"Content-Length": "13",
"Content-Type": "text/plain; charset=utf-8",
}
def test_response_html():
response = httpx.Response(200, html="<html><body>Hello, world!</html></body>")
assert response.status_code == 200
assert response.reason_phrase == "OK"
assert response.text == "<html><body>Hello, world!</html></body>"
assert response.headers == {
"Content-Length": "39",
"Content-Type": "text/html; charset=utf-8",
}
def test_response_json():
response = httpx.Response(200, json={"hello": "world"})
assert response.status_code == 200
assert response.reason_phrase == "OK"
assert str(response.json()) == "{'hello': 'world'}"
assert response.headers == {
"Content-Length": "17",
"Content-Type": "application/json",
}
def test_raise_for_status():
request = httpx.Request("GET", "https://example.org")
# 2xx status codes are not an error.
response = httpx.Response(200, request=request)
response.raise_for_status()
# 1xx status codes are informational responses.
response = httpx.Response(101, request=request)
assert response.is_informational
with pytest.raises(httpx.HTTPStatusError) as exc_info:
response.raise_for_status()
assert str(exc_info.value) == (
"Informational response '101 Switching Protocols' for url 'https://example.org'\n"
"For more information check: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/101"
)
# 3xx status codes are redirections.
headers = {"location": "https://other.org"}
response = httpx.Response(303, headers=headers, request=request)
assert response.is_redirect
with pytest.raises(httpx.HTTPStatusError) as exc_info:
response.raise_for_status()
assert str(exc_info.value) == (
"Redirect response '303 See Other' for url 'https://example.org'\n"
"Redirect location: 'https://other.org'\n"
"For more information check: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/303"
)
# 4xx status codes are a client error.
response = httpx.Response(403, request=request)
assert response.is_client_error
assert response.is_error
with pytest.raises(httpx.HTTPStatusError) as exc_info:
response.raise_for_status()
assert str(exc_info.value) == (
"Client error '403 Forbidden' for url 'https://example.org'\n"
"For more information check: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/403"
)
# 5xx status codes are a server error.
response = httpx.Response(500, request=request)
assert response.is_server_error
assert response.is_error
with pytest.raises(httpx.HTTPStatusError) as exc_info:
response.raise_for_status()
assert str(exc_info.value) == (
"Server error '500 Internal Server Error' for url 'https://example.org'\n"
"For more information check: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500"
)
# Calling .raise_for_status without setting a request instance is
# not valid. Should raise a runtime error.
response = httpx.Response(200)
with pytest.raises(RuntimeError):
response.raise_for_status()
def test_response_repr():
response = httpx.Response(
200,
content=b"Hello, world!",
)
assert repr(response) == "<Response [200 OK]>"
def test_response_content_type_encoding():
"""
Use the charset encoding in the Content-Type header if possible.
"""
headers = {"Content-Type": "text-plain; charset=latin-1"}
content = "Latin 1: ÿ".encode("latin-1")
response = httpx.Response(
200,
content=content,
headers=headers,
)
assert response.text == "Latin 1: ÿ"
assert response.encoding == "latin-1"
def test_response_default_to_utf8_encoding():
"""
Default to utf-8 encoding if there is no Content-Type header.
"""
content = "おはようございます。".encode("utf-8")
response = httpx.Response(
200,
content=content,
)
assert response.text == "おはようございます。"
assert response.encoding == "utf-8"
def test_response_fallback_to_utf8_encoding():
"""
Fallback to utf-8 if we get an invalid charset in the Content-Type header.
"""
headers = {"Content-Type": "text-plain; charset=invalid-codec-name"}
content = "おはようございます。".encode("utf-8")
response = httpx.Response(
200,
content=content,
headers=headers,
)
assert response.text == "おはようございます。"
assert response.encoding == "utf-8"
def test_response_no_charset_with_ascii_content():
"""
A response with ascii encoded content should decode correctly,
even with no charset specified.
"""
content = b"Hello, world!"
headers = {"Content-Type": "text/plain"}
response = httpx.Response(
200,
content=content,
headers=headers,
)
assert response.status_code == 200
assert response.encoding == "utf-8"
assert response.text == "Hello, world!"
def test_response_no_charset_with_utf8_content():
"""
A response with UTF-8 encoded content should decode correctly,
even with no charset specified.
"""
content = "Unicode Snowman: ☃".encode("utf-8")
headers = {"Content-Type": "text/plain"}
response = httpx.Response(
200,
content=content,
headers=headers,
)
assert response.text == "Unicode Snowman: ☃"
assert response.encoding == "utf-8"
def test_response_no_charset_with_iso_8859_1_content():
"""
A response with ISO 8859-1 encoded content should decode correctly,
even with no charset specified, if autodetect is enabled.
"""
content = "Accented: Österreich abcdefghijklmnopqrstuzwxyz".encode("iso-8859-1")
headers = {"Content-Type": "text/plain"}
response = httpx.Response(
200, content=content, headers=headers, default_encoding=autodetect
)
assert response.text == "Accented: Österreich abcdefghijklmnopqrstuzwxyz"
assert response.charset_encoding is None
def test_response_no_charset_with_cp_1252_content():
"""
A response with Windows 1252 encoded content should decode correctly,
even with no charset specified, if autodetect is enabled.
"""
content = "Euro Currency: € abcdefghijklmnopqrstuzwxyz".encode("cp1252")
headers = {"Content-Type": "text/plain"}
response = httpx.Response(
200, content=content, headers=headers, default_encoding=autodetect
)
assert response.text == "Euro Currency: € abcdefghijklmnopqrstuzwxyz"
assert response.charset_encoding is None
def test_response_non_text_encoding():
"""
Default to attempting utf-8 encoding for non-text content-type headers.
"""
headers = {"Content-Type": "image/png"}
response = httpx.Response(
200,
content=b"xyz",
headers=headers,
)
assert response.text == "xyz"
assert response.encoding == "utf-8"
def test_response_set_explicit_encoding():
headers = {
"Content-Type": "text-plain; charset=utf-8"
} # Deliberately incorrect charset
response = httpx.Response(
200,
content="Latin 1: ÿ".encode("latin-1"),
headers=headers,
)
response.encoding = "latin-1"
assert response.text == "Latin 1: ÿ"
assert response.encoding == "latin-1"
def test_response_force_encoding():
response = httpx.Response(
200,
content="Snowman: ☃".encode("utf-8"),
)
response.encoding = "iso-8859-1"
assert response.status_code == 200
assert response.reason_phrase == "OK"
assert response.text == "Snowman: â\x98\x83"
assert response.encoding == "iso-8859-1"
def test_response_force_encoding_after_text_accessed():
response = httpx.Response(
200,
content=b"Hello, world!",
)
assert response.status_code == 200
assert response.reason_phrase == "OK"
assert response.text == "Hello, world!"
assert response.encoding == "utf-8"
with pytest.raises(ValueError):
response.encoding = "UTF8"
with pytest.raises(ValueError):
response.encoding = "iso-8859-1"
def test_read():
response = httpx.Response(
200,
content=b"Hello, world!",
)
assert response.status_code == 200
assert response.text == "Hello, world!"
assert response.encoding == "utf-8"
assert response.is_closed
content = response.read()
assert content == b"Hello, world!"
assert response.content == b"Hello, world!"
assert response.is_closed
def test_empty_read():
response = httpx.Response(200)
assert response.status_code == 200
assert response.text == ""
assert response.encoding == "utf-8"
assert response.is_closed
content = response.read()
assert content == b""
assert response.content == b""
assert response.is_closed
@pytest.mark.anyio
async def test_aread():
response = httpx.Response(
200,
content=b"Hello, world!",
)
assert response.status_code == 200
assert response.text == "Hello, world!"
assert response.encoding == "utf-8"
assert response.is_closed
content = await response.aread()
assert content == b"Hello, world!"
assert response.content == b"Hello, world!"
assert response.is_closed
@pytest.mark.anyio
async def test_empty_aread():
response = httpx.Response(200)
assert response.status_code == 200
assert response.text == ""
assert response.encoding == "utf-8"
assert response.is_closed
content = await response.aread()
assert content == b""
assert response.content == b""
assert response.is_closed
def test_iter_raw():
response = httpx.Response(
200,
content=streaming_body(),
)
raw = b""
for part in response.iter_raw():
raw += part
assert raw == b"Hello, world!"
def test_iter_raw_with_chunksize():
response = httpx.Response(200, content=streaming_body())
parts = list(response.iter_raw(chunk_size=5))
assert parts == [b"Hello", b", wor", b"ld!"]
response = httpx.Response(200, content=streaming_body())
parts = list(response.iter_raw(chunk_size=7))
assert parts == [b"Hello, ", b"world!"]
response = httpx.Response(200, content=streaming_body())
parts = list(response.iter_raw(chunk_size=13))
assert parts == [b"Hello, world!"]
response = httpx.Response(200, content=streaming_body())
parts = list(response.iter_raw(chunk_size=20))
assert parts == [b"Hello, world!"]
def test_iter_raw_doesnt_return_empty_chunks():
def streaming_body_with_empty_chunks() -> typing.Iterator[bytes]:
yield b"Hello, "
yield b""
yield b"world!"
yield b""
response = httpx.Response(200, content=streaming_body_with_empty_chunks())
parts = list(response.iter_raw())
assert parts == [b"Hello, ", b"world!"]
def test_iter_raw_on_iterable():
response = httpx.Response(
200,
content=StreamingBody(),
)
raw = b""
for part in response.iter_raw():
raw += part
assert raw == b"Hello, world!"
def test_iter_raw_on_async():
response = httpx.Response(
200,
content=async_streaming_body(),
)
with pytest.raises(RuntimeError):
list(response.iter_raw())
def test_close_on_async():
response = httpx.Response(
200,
content=async_streaming_body(),
)
with pytest.raises(RuntimeError):
response.close()
def test_iter_raw_increments_updates_counter():
response = httpx.Response(200, content=streaming_body())
num_downloaded = response.num_bytes_downloaded
for part in response.iter_raw():
assert len(part) == (response.num_bytes_downloaded - num_downloaded)
num_downloaded = response.num_bytes_downloaded
@pytest.mark.anyio
async def test_aiter_raw():
response = httpx.Response(200, content=async_streaming_body())
raw = b""
async for part in response.aiter_raw():
raw += part
assert raw == b"Hello, world!"
@pytest.mark.anyio
async def test_aiter_raw_with_chunksize():
response = httpx.Response(200, content=async_streaming_body())
parts = [part async for part in response.aiter_raw(chunk_size=5)]
assert parts == [b"Hello", b", wor", b"ld!"]
response = httpx.Response(200, content=async_streaming_body())
parts = [part async for part in response.aiter_raw(chunk_size=13)]
assert parts == [b"Hello, world!"]
response = httpx.Response(200, content=async_streaming_body())
parts = [part async for part in response.aiter_raw(chunk_size=20)]
assert parts == [b"Hello, world!"]
@pytest.mark.anyio
async def test_aiter_raw_on_sync():
response = httpx.Response(
200,
content=streaming_body(),
)
with pytest.raises(RuntimeError):
[part async for part in response.aiter_raw()]
@pytest.mark.anyio
async def test_aclose_on_sync():
response = httpx.Response(
200,
content=streaming_body(),
)
with pytest.raises(RuntimeError):
await response.aclose()
@pytest.mark.anyio
async def test_aiter_raw_increments_updates_counter():
response = httpx.Response(200, content=async_streaming_body())
num_downloaded = response.num_bytes_downloaded
async for part in response.aiter_raw():
assert len(part) == (response.num_bytes_downloaded - num_downloaded)
num_downloaded = response.num_bytes_downloaded
def test_iter_bytes():
response = httpx.Response(200, content=b"Hello, world!")
content = b""
for part in response.iter_bytes():
content += part
assert content == b"Hello, world!"
def test_iter_bytes_with_chunk_size():
response = httpx.Response(200, content=streaming_body())
parts = list(response.iter_bytes(chunk_size=5))
assert parts == [b"Hello", b", wor", b"ld!"]
response = httpx.Response(200, content=streaming_body())
parts = list(response.iter_bytes(chunk_size=13))
assert parts == [b"Hello, world!"]
response = httpx.Response(200, content=streaming_body())
parts = list(response.iter_bytes(chunk_size=20))
assert parts == [b"Hello, world!"]
def test_iter_bytes_with_empty_response():
response = httpx.Response(200, content=b"")
parts = list(response.iter_bytes())
assert parts == []
def test_iter_bytes_doesnt_return_empty_chunks():
def streaming_body_with_empty_chunks() -> typing.Iterator[bytes]:
yield b"Hello, "
yield b""
yield b"world!"
yield b""
response = httpx.Response(200, content=streaming_body_with_empty_chunks())
parts = list(response.iter_bytes())
assert parts == [b"Hello, ", b"world!"]
@pytest.mark.anyio
async def test_aiter_bytes():
response = httpx.Response(
200,
content=b"Hello, world!",
)
content = b""
async for part in response.aiter_bytes():
content += part
assert content == b"Hello, world!"
@pytest.mark.anyio
async def test_aiter_bytes_with_chunk_size():
response = httpx.Response(200, content=async_streaming_body())
parts = [part async for part in response.aiter_bytes(chunk_size=5)]
assert parts == [b"Hello", b", wor", b"ld!"]
response = httpx.Response(200, content=async_streaming_body())
parts = [part async for part in response.aiter_bytes(chunk_size=13)]
assert parts == [b"Hello, world!"]
response = httpx.Response(200, content=async_streaming_body())
parts = [part async for part in response.aiter_bytes(chunk_size=20)]
assert parts == [b"Hello, world!"]
def test_iter_text():
response = httpx.Response(
200,
content=b"Hello, world!",
)
content = ""
for part in response.iter_text():
content += part
assert content == "Hello, world!"
def test_iter_text_with_chunk_size():
response = httpx.Response(200, content=b"Hello, world!")
parts = list(response.iter_text(chunk_size=5))
assert parts == ["Hello", ", wor", "ld!"]
response = httpx.Response(200, content=b"Hello, world!!")
parts = list(response.iter_text(chunk_size=7))
assert parts == ["Hello, ", "world!!"]
response = httpx.Response(200, content=b"Hello, world!")
parts = list(response.iter_text(chunk_size=7))
assert parts == ["Hello, ", "world!"]
response = httpx.Response(200, content=b"Hello, world!")
parts = list(response.iter_text(chunk_size=13))
assert parts == ["Hello, world!"]
response = httpx.Response(200, content=b"Hello, world!")
parts = list(response.iter_text(chunk_size=20))
assert parts == ["Hello, world!"]
@pytest.mark.anyio
async def test_aiter_text():
response = httpx.Response(
200,
content=b"Hello, world!",
)
content = ""
async for part in response.aiter_text():
content += part
assert content == "Hello, world!"
@pytest.mark.anyio
async def test_aiter_text_with_chunk_size():
response = httpx.Response(200, content=b"Hello, world!")
parts = [part async for part in response.aiter_text(chunk_size=5)]
assert parts == ["Hello", ", wor", "ld!"]
response = httpx.Response(200, content=b"Hello, world!")
parts = [part async for part in response.aiter_text(chunk_size=13)]
assert parts == ["Hello, world!"]
response = httpx.Response(200, content=b"Hello, world!")
parts = [part async for part in response.aiter_text(chunk_size=20)]
assert parts == ["Hello, world!"]
def test_iter_lines():
response = httpx.Response(
200,
content=b"Hello,\nworld!",
)
content = list(response.iter_lines())
assert content == ["Hello,", "world!"]
@pytest.mark.anyio
async def test_aiter_lines():
response = httpx.Response(
200,
content=b"Hello,\nworld!",
)
content = []
async for line in response.aiter_lines():
content.append(line)
assert content == ["Hello,", "world!"]
def test_sync_streaming_response():
response = httpx.Response(
200,
content=streaming_body(),
)
assert response.status_code == 200
assert not response.is_closed
content = response.read()
assert content == b"Hello, world!"
assert response.content == b"Hello, world!"
assert response.is_closed
@pytest.mark.anyio
async def test_async_streaming_response():
response = httpx.Response(
200,
content=async_streaming_body(),
)
assert response.status_code == 200
assert not response.is_closed
content = await response.aread()
assert content == b"Hello, world!"
assert response.content == b"Hello, world!"
assert response.is_closed
def test_cannot_read_after_stream_consumed():
response = httpx.Response(
200,
content=streaming_body(),
)
content = b""
for part in response.iter_bytes():
content += part
with pytest.raises(httpx.StreamConsumed):
response.read()
@pytest.mark.anyio
async def test_cannot_aread_after_stream_consumed():
response = httpx.Response(
200,
content=async_streaming_body(),
)
content = b""
async for part in response.aiter_bytes():
content += part
with pytest.raises(httpx.StreamConsumed):
await response.aread()
def test_cannot_read_after_response_closed():
response = httpx.Response(
200,
content=streaming_body(),
)
response.close()
with pytest.raises(httpx.StreamClosed):
response.read()
@pytest.mark.anyio
async def test_cannot_aread_after_response_closed():
response = httpx.Response(
200,
content=async_streaming_body(),
)
await response.aclose()
with pytest.raises(httpx.StreamClosed):
await response.aread()
@pytest.mark.anyio
async def test_elapsed_not_available_until_closed():
response = httpx.Response(
200,
content=async_streaming_body(),
)
with pytest.raises(RuntimeError):
response.elapsed # noqa: B018
def test_unknown_status_code():
response = httpx.Response(
600,
)
assert response.status_code == 600
assert response.reason_phrase == ""
assert response.text == ""
def test_json_with_specified_encoding():
data = {"greeting": "hello", "recipient": "world"}
content = json.dumps(data).encode("utf-16")
headers = {"Content-Type": "application/json, charset=utf-16"}
response = httpx.Response(
200,
content=content,
headers=headers,
)
assert response.json() == data
def test_json_with_options():
data = {"greeting": "hello", "recipient": "world", "amount": 1}
content = json.dumps(data).encode("utf-16")
headers = {"Content-Type": "application/json, charset=utf-16"}
response = httpx.Response(
200,
content=content,
headers=headers,
)
assert response.json(parse_int=str)["amount"] == "1"
@pytest.mark.parametrize(
"encoding",
[
"utf-8",
"utf-8-sig",
"utf-16",
"utf-16-be",
"utf-16-le",
"utf-32",
"utf-32-be",
"utf-32-le",
],
)
def test_json_without_specified_charset(encoding):
data = {"greeting": "hello", "recipient": "world"}
content = json.dumps(data).encode(encoding)
headers = {"Content-Type": "application/json"}
response = httpx.Response(
200,
content=content,
headers=headers,
)
assert response.json() == data
@pytest.mark.parametrize(
"encoding",
[
"utf-8",
"utf-8-sig",
"utf-16",
"utf-16-be",
"utf-16-le",
"utf-32",
"utf-32-be",
"utf-32-le",
],
)
def test_json_with_specified_charset(encoding):
data = {"greeting": "hello", "recipient": "world"}
content = json.dumps(data).encode(encoding)
headers = {"Content-Type": f"application/json; charset={encoding}"}
response = httpx.Response(
200,
content=content,
headers=headers,
)
assert response.json() == data
@pytest.mark.parametrize(
"headers, expected",
[
(
{"Link": "<https://example.com>; rel='preload'"},
{"preload": {"rel": "preload", "url": "https://example.com"}},
),
(
{"Link": '</hub>; rel="hub", </resource>; rel="self"'},
{
"hub": {"url": "/hub", "rel": "hub"},
"self": {"url": "/resource", "rel": "self"},
},
),
],
)
def test_link_headers(headers, expected):
response = httpx.Response(
200,
content=None,
headers=headers,
)
assert response.links == expected
@pytest.mark.parametrize("header_value", (b"deflate", b"gzip", b"br"))
def test_decode_error_with_request(header_value):
headers = [(b"Content-Encoding", header_value)]
broken_compressed_body = b"xxxxxxxxxxxxxx"
with pytest.raises(httpx.DecodingError):
httpx.Response(
200,
headers=headers,
content=broken_compressed_body,
)
with pytest.raises(httpx.DecodingError):
httpx.Response(
200,
headers=headers,
content=broken_compressed_body,
request=httpx.Request("GET", "https://www.example.org/"),
)
@pytest.mark.parametrize("header_value", (b"deflate", b"gzip", b"br"))
def test_value_error_without_request(header_value):
headers = [(b"Content-Encoding", header_value)]
broken_compressed_body = b"xxxxxxxxxxxxxx"
with pytest.raises(httpx.DecodingError):
httpx.Response(200, headers=headers, content=broken_compressed_body)
def test_response_with_unset_request():
response = httpx.Response(200, content=b"Hello, world!")
assert response.status_code == 200
assert response.reason_phrase == "OK"
assert response.text == "Hello, world!"
assert not response.is_error
def test_set_request_after_init():
response = httpx.Response(200, content=b"Hello, world!")
response.request = httpx.Request("GET", "https://www.example.org")
assert response.request.method == "GET"
assert response.request.url == "https://www.example.org"
def test_cannot_access_unset_request():
response = httpx.Response(200, content=b"Hello, world!")
with pytest.raises(RuntimeError):
response.request # noqa: B018
def test_generator_with_transfer_encoding_header():
def content() -> typing.Iterator[bytes]:
yield b"test 123" # pragma: no cover
response = httpx.Response(200, content=content())
assert response.headers == {"Transfer-Encoding": "chunked"}
def test_generator_with_content_length_header():
def content() -> typing.Iterator[bytes]:
yield b"test 123" # pragma: no cover
headers = {"Content-Length": "8"}
response = httpx.Response(200, content=content(), headers=headers)
assert response.headers == {"Content-Length": "8"}
def test_response_picklable():
response = httpx.Response(
200,
content=b"Hello, world!",
request=httpx.Request("GET", "https://example.org"),
)
pickle_response = pickle.loads(pickle.dumps(response))
assert pickle_response.is_closed is True
assert pickle_response.is_stream_consumed is True
assert pickle_response.next_request is None
assert pickle_response.stream is not None
assert pickle_response.content == b"Hello, world!"
assert pickle_response.status_code == 200
assert pickle_response.request.url == response.request.url
assert pickle_response.extensions == {}
assert pickle_response.history == []
@pytest.mark.anyio
async def test_response_async_streaming_picklable():
response = httpx.Response(200, content=async_streaming_body())
pickle_response = pickle.loads(pickle.dumps(response))
with pytest.raises(httpx.ResponseNotRead):
pickle_response.content # noqa: B018
with pytest.raises(httpx.StreamClosed):
await pickle_response.aread()
assert pickle_response.is_stream_consumed is False
assert pickle_response.num_bytes_downloaded == 0
assert pickle_response.headers == {"Transfer-Encoding": "chunked"}
response = httpx.Response(200, content=async_streaming_body())
await response.aread()
pickle_response = pickle.loads(pickle.dumps(response))
assert pickle_response.is_stream_consumed is True
assert pickle_response.content == b"Hello, world!"
assert pickle_response.num_bytes_downloaded == 13
def test_response_decode_text_using_autodetect():
# Ensure that a 'default_encoding="autodetect"' on the response allows for
# encoding autodetection to be used when no "Content-Type: text/plain; charset=..."
# info is present.
#
# Here we have some french text encoded with ISO-8859-1, rather than UTF-8.
text = (
"Non-seulement Despréaux ne se trompait pas, mais de tous les écrivains "
"que la France a produits, sans excepter Voltaire lui-même, imprégné de "
"l'esprit anglais par son séjour à Londres, c'est incontestablement "
"Molière ou Poquelin qui reproduit avec l'exactitude la plus vive et la "
"plus complète le fond du génie français."
)
content = text.encode("ISO-8859-1")
response = httpx.Response(200, content=content, default_encoding=autodetect)
assert response.status_code == 200
assert response.reason_phrase == "OK"
assert response.encoding == "ISO-8859-1"
assert response.text == text
def test_response_decode_text_using_explicit_encoding():
# Ensure that a 'default_encoding="..."' on the response is used for text decoding
# when no "Content-Type: text/plain; charset=..."" info is present.
#
# Here we have some french text encoded with Windows-1252, rather than UTF-8.
# https://en.wikipedia.org/wiki/Windows-1252
text = (
"Non-seulement Despréaux ne se trompait pas, mais de tous les écrivains "
"que la France a produits, sans excepter Voltaire lui-même, imprégné de "
"l'esprit anglais par son séjour à Londres, c'est incontestablement "
"Molière ou Poquelin qui reproduit avec l'exactitude la plus vive et la "
"plus complète le fond du génie français."
)
content = text.encode("cp1252")
response = httpx.Response(200, content=content, default_encoding="cp1252")
assert response.status_code == 200
assert response.reason_phrase == "OK"
assert response.encoding == "cp1252"
assert response.text == text
|