test_responses.py
This file
Path
tests/models/test_responses.py
290 lines · 8.2 KiB · Python
Last changed by
Add additional flake8 plugins (#157)Can Sarıgölbefore adoption7e9110b
History of this fileTake it
Encoding detection in Response.json (#116)Yeray Diaz Diazbefore adoption5442006
lines 1–3why, one step back1import json2from unittest import mock3Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 4–5why, one step back4import pytest5Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 6why, one step back6import httpxRejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 7–8why, one step back78Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 9why, one step back9def streaming_body():Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 10–13why, one step back10 yield b"Hello, "11 yield b"world!"1213Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
lines 14–18why, one step back14async def async_streaming_body():15 yield b"Hello, "16 yield b"world!"1718Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
line 19why, one step back19def test_response():Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 20why, one step back20 response = httpx.Response(200, content=b"Hello, world!")Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 21–22why, one step back21 assert response.status_code == 20022 assert response.reason_phrase == "OK"Add response.text and response.encodingTom Christiebefore adoptiond8df61b
line 23why, one step back23 assert response.text == "Hello, world!"Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 24–25why, one step back2425Increase test coverageTom Christiebefore adoptione99ffbd
line 26why, one step back26def test_response_repr():Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 27why, one step back27 response = httpx.Response(200, content=b"Hello, world!")Fixed response repr. (#101)updatatodaybefore adoptionf9e3ad4
line 28why, one step back28 assert repr(response) == "<Response [200 OK]>"Increase test coverageTom Christiebefore adoptione99ffbd
lines 29–30why, one step back2930Add response.text and response.encodingTom Christiebefore adoptiond8df61b
line 31why, one step back31def test_response_content_type_encoding():Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36
lines 32–34why, one step back32 """33 Use the charset encoding in the Content-Type header if possible.34 """Add response.text and response.encodingTom Christiebefore adoptiond8df61b
line 35why, one step back35 headers = {"Content-Type": "text-plain; charset=latin-1"}Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36
line 36why, one step back36 content = "Latin 1: ÿ".encode("latin-1")Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 37why, one step back37 response = httpx.Response(200, content=content, headers=headers)Add response.text and response.encodingTom Christiebefore adoptiond8df61b
lines 38–42why, one step back38 assert response.text == "Latin 1: ÿ"39 assert response.encoding == "latin-1"404142def test_response_autodetect_encoding():Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36
lines 43–46why, one step back43 """44 Autodetect encoding if there is no charset info in a Content-Type header.45 """46 content = "おはようございます。".encode("EUC-JP")Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 47why, one step back47 response = httpx.Response(200, content=content)Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36
lines 48–49why, one step back48 assert response.text == "おはようございます。"49 assert response.encoding == "EUC-JP"Add response.text and response.encodingTom Christiebefore adoptiond8df61b
lines 50–51why, one step back5051Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36
lines 52–57why, one step back52def test_response_fallback_to_autodetect():53 """54 Fallback to autodetection if we get an invalid charset in the Content-Type header.55 """56 headers = {"Content-Type": "text-plain; charset=invalid-codec-name"}57 content = "おはようございます。".encode("EUC-JP")Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 58why, one step back58 response = httpx.Response(200, content=content, headers=headers)Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36
lines 59–62why, one step back59 assert response.text == "おはようございます。"60 assert response.encoding == "EUC-JP"6162Increase test coverageTom Christiebefore adoptione99ffbd
line 63why, one step back63def test_response_default_text_encoding():Media types with 'text/' should default to iso-8859-1Tom Christiebefore adoptiond7cf8bb
lines 64–69why, one step back64 """65 A media type of 'text/*' with no charset should default to ISO-8859-1.66 See: https://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7.167 """68 content = b"Hello, world!"69 headers = {"Content-Type": "text/plain"}Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 70why, one step back70 response = httpx.Response(200, content=content, headers=headers)Media types with 'text/' should default to iso-8859-1Tom Christiebefore adoptiond7cf8bb
lines 71–75why, one step back71 assert response.status_code == 20072 assert response.encoding == "iso-8859-1"73 assert response.text == "Hello, world!"7475Add response.text and response.encodingTom Christiebefore adoptiond8df61b
line 76why, one step back76def test_response_default_encoding():Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36
lines 77–79why, one step back77 """78 Default to utf-8 if all else fails.79 """Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 80why, one step back80 response = httpx.Response(200, content=b"")Add response.text and response.encodingTom Christiebefore adoptiond8df61b
lines 81–82why, one step back81 assert response.text == ""82 assert response.encoding == "utf-8"Increase test coverageTom Christiebefore adoptione99ffbd
lines 83–89why, one step back838485def test_response_non_text_encoding():86 """87 Default to apparent encoding for non-text content-type headers.88 """89 headers = {"Content-Type": "image/png"}Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 90why, one step back90 response = httpx.Response(200, content=b"xyz", headers=headers)Increase test coverageTom Christiebefore adoptione99ffbd
lines 91–92why, one step back91 assert response.text == "xyz"92 assert response.encoding == "ascii"Add response.text and response.encodingTom Christiebefore adoptiond8df61b
lines 93–94why, one step back9394Add test for explicitly setting the encoding to useTom Christiebefore adoptionfd9823e
line 95why, one step back95def test_response_set_explicit_encoding():Use rfc3986 for URL handlingTom Christiebefore adoption302fe93
lines 96–98why, one step back96 headers = {97 "Content-Type": "text-plain; charset=utf-8"98 } # Deliberately incorrect charsetRenaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 99why, one step back99 response = httpx.Response(Add test for explicitly setting the encoding to useTom Christiebefore adoptionfd9823e
lines 100–101why, one step back100 200, content="Latin 1: ÿ".encode("latin-1"), headers=headers101 )Use rfc3986 for URL handlingTom Christiebefore adoption302fe93
line 102why, one step back102 response.encoding = "latin-1"Add test for explicitly setting the encoding to useTom Christiebefore adoptionfd9823e
lines 103–106why, one step back103 assert response.text == "Latin 1: ÿ"104 assert response.encoding == "latin-1"105106Add response.text and response.encodingTom Christiebefore adoptiond8df61b
line 107why, one step back107def test_response_force_encoding():Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 108why, one step back108 response = httpx.Response(200, content="Snowman: ☃".encode("utf-8"))Add response.text and response.encodingTom Christiebefore adoptiond8df61b
lines 109–115why, one step back109 response.encoding = "iso-8859-1"110 assert response.status_code == 200111 assert response.reason_phrase == "OK"112 assert response.text == "Snowman: â\x98\x83"113 assert response.encoding == "iso-8859-1"114115Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 116why, one step back116def test_read_response():Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 117why, one step back117 response = httpx.Response(200, content=b"Hello, world!")Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 118–119why, one step back118119 assert response.status_code == 200Add response.text and response.encodingTom Christiebefore adoptiond8df61b
lines 120–121why, one step back120 assert response.text == "Hello, world!"121 assert response.encoding == "ascii"Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 122–123why, one step back122 assert response.is_closed123Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 124why, one step back124 content = response.read()Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
line 125why, one step back125Response.contentTom Christiebefore adoption4c55113
lines 126–127why, one step back126 assert content == b"Hello, world!"127 assert response.content == b"Hello, world!"Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
line 128why, one step back128 assert response.is_closedUse rfc3986 for URL handlingTom Christiebefore adoption302fe93
lines 129–130why, one step back129130Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 131why, one step back131def test_raw_interface():Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 132why, one step back132 response = httpx.Response(200, content=b"Hello, world!")Use rfc3986 for URL handlingTom Christiebefore adoption302fe93
lines 133–134why, one step back133134 raw = b""Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 135why, one step back135 for part in response.raw():Use rfc3986 for URL handlingTom Christiebefore adoption302fe93
lines 136–139why, one step back136 raw += part137 assert raw == b"Hello, world!"138139Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 140why, one step back140def test_stream_interface():Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 141why, one step back141 response = httpx.Response(200, content=b"Hello, world!")Use rfc3986 for URL handlingTom Christiebefore adoption302fe93
lines 142–143why, one step back142143 content = b""Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 144why, one step back144 for part in response.stream():Use rfc3986 for URL handlingTom Christiebefore adoption302fe93
lines 145–149why, one step back145 content += part146 assert content == b"Hello, world!"147148149@pytest.mark.asyncioSync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 150why, one step back150async def test_async_stream_interface():Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 151why, one step back151 response = httpx.AsyncResponse(200, content=b"Hello, world!")Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
lines 152–159why, one step back152153 content = b""154 async for part in response.stream():155 content += part156 assert content == b"Hello, world!"157158159def test_stream_interface_after_read():Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 160why, one step back160 response = httpx.Response(200, content=b"Hello, world!")Use rfc3986 for URL handlingTom Christiebefore adoption302fe93
line 161why, one step back161Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
lines 162–171why, one step back162 response.read()163164 content = b""165 for part in response.stream():166 content += part167 assert content == b"Hello, world!"168169170@pytest.mark.asyncio171async def test_async_stream_interface_after_read():Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 172why, one step back172 response = httpx.AsyncResponse(200, content=b"Hello, world!")Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 173why, one step back173Use rfc3986 for URL handlingTom Christiebefore adoption302fe93
lines 174–179why, one step back174 await response.read()175176 content = b""177 async for part in response.stream():178 content += part179 assert content == b"Hello, world!"Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 180–181why, one step back180181Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 182why, one step back182def test_streaming_response():Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 183why, one step back183 response = httpx.Response(200, content=streaming_body())Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 184–187why, one step back184185 assert response.status_code == 200186 assert not response.is_closed187Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 188why, one step back188 content = response.read()Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
line 189why, one step back189Response.contentTom Christiebefore adoption4c55113
lines 190–191why, one step back190 assert content == b"Hello, world!"191 assert response.content == b"Hello, world!"Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 192–195why, one step back192 assert response.is_closed193194195@pytest.mark.asyncioSync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 196why, one step back196async def test_async_streaming_response():Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 197why, one step back197 response = httpx.AsyncResponse(200, content=async_streaming_body())Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
lines 198–209why, one step back198199 assert response.status_code == 200200 assert not response.is_closed201202 content = await response.read()203204 assert content == b"Hello, world!"205 assert response.content == b"Hello, world!"206 assert response.is_closed207208209def test_cannot_read_after_stream_consumed():Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 210why, one step back210 response = httpx.Response(200, content=streaming_body())Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
line 211why, one step back211Response.contentTom Christiebefore adoption4c55113
line 212why, one step back212 content = b""Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
lines 213–215why, one step back213 for part in response.stream():214 content += part215Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 216why, one step back216 with pytest.raises(httpx.StreamConsumed):Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
lines 217–221why, one step back217 response.read()218219220@pytest.mark.asyncio221async def test_async_cannot_read_after_stream_consumed():Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 222why, one step back222 response = httpx.AsyncResponse(200, content=async_streaming_body())Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
lines 223–224why, one step back223224 content = b""Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
line 225why, one step back225 async for part in response.stream():Response.contentTom Christiebefore adoption4c55113
line 226why, one step back226 content += partRejig test and tighten up models APITom Christiebefore adoptiondea4d42
line 227why, one step back227Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 228why, one step back228 with pytest.raises(httpx.StreamConsumed):Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 229–231why, one step back229 await response.read()230231Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 232why, one step back232def test_cannot_read_after_response_closed():Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 233why, one step back233 response = httpx.Response(200, content=streaming_body())Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
lines 234–236why, one step back234235 response.close()236Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 237why, one step back237 with pytest.raises(httpx.ResponseClosed):Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
lines 238–242why, one step back238 response.read()239240241@pytest.mark.asyncio242async def test_async_cannot_read_after_response_closed():Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 243why, one step back243 response = httpx.AsyncResponse(200, content=async_streaming_body())Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 244–246why, one step back244245 await response.close()246Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 247why, one step back247 with pytest.raises(httpx.ResponseClosed):Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 248–251why, one step back248 await response.read()249250251def test_unknown_status_code():Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 252why, one step back252 response = httpx.Response(600)Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 253–254why, one step back253 assert response.status_code == 600254 assert response.reason_phrase == ""Add response.text and response.encodingTom Christiebefore adoptiond8df61b
line 255why, one step back255 assert response.text == ""Encoding detection in Response.json (#116)Yeray Diaz Diazbefore adoption5442006
lines 256–258why, one step back256257258def test_json_with_specified_encoding():Add additional flake8 plugins (#157)Can Sarıgölbefore adoption7e9110b
line 259why, one step back259 data = {"greeting": "hello", "recipient": "world"}Encoding detection in Response.json (#116)Yeray Diaz Diazbefore adoption5442006
lines 260–261why, one step back260 content = json.dumps(data).encode("utf-16")261 headers = {"Content-Type": "application/json, charset=utf-16"}Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 262why, one step back262 response = httpx.Response(200, content=content, headers=headers)Encoding detection in Response.json (#116)Yeray Diaz Diazbefore adoption5442006
lines 263–266why, one step back263 assert response.json() == data264265266def test_json_with_options():Add additional flake8 plugins (#157)Can Sarıgölbefore adoption7e9110b
line 267why, one step back267 data = {"greeting": "hello", "recipient": "world", "amount": 1}Encoding detection in Response.json (#116)Yeray Diaz Diazbefore adoption5442006
lines 268–269why, one step back268 content = json.dumps(data).encode("utf-16")269 headers = {"Content-Type": "application/json, charset=utf-16"}Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 270why, one step back270 response = httpx.Response(200, content=content, headers=headers)Encoding detection in Response.json (#116)Yeray Diaz Diazbefore adoption5442006
lines 271–274why, one step back271 assert response.json(parse_int=str)["amount"] == "1"272273274def test_json_without_specified_encoding():Add additional flake8 plugins (#157)Can Sarıgölbefore adoption7e9110b
line 275why, one step back275 data = {"greeting": "hello", "recipient": "world"}Encoding detection in Response.json (#116)Yeray Diaz Diazbefore adoption5442006
lines 276–277why, one step back276 content = json.dumps(data).encode("utf-32-be")277 headers = {"Content-Type": "application/json"}Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 278why, one step back278 response = httpx.Response(200, content=content, headers=headers)Encoding detection in Response.json (#116)Yeray Diaz Diazbefore adoption5442006
lines 279–282why, one step back279 assert response.json() == data280281282def test_json_without_specified_encoding_decode_error():Add additional flake8 plugins (#157)Can Sarıgölbefore adoption7e9110b
line 283why, one step back283 data = {"greeting": "hello", "recipient": "world"}Encoding detection in Response.json (#116)Yeray Diaz Diazbefore adoption5442006
lines 284–286why, one step back284 content = json.dumps(data).encode("utf-32-be")285 headers = {"Content-Type": "application/json"}286 # force incorrect guess from `guess_json_utf` to trigger errorRenaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
lines 287–288why, one step back287 with mock.patch("httpx.models.guess_json_utf", return_value="utf-32"):288 response = httpx.Response(200, content=content, headers=headers)Encoding detection in Response.json (#116)Yeray Diaz Diazbefore adoption5442006
lines 289–290why, one step back289 with pytest.raises(json.JSONDecodeError):290 response.json()