Skip to content
Myr

Drop Response(..., request=...) style in test cases. (#1243)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* Drop Response(..., request=...) style in test cases except where required * Lowercase variable name

authored by Tom Christie · committed by GitHub · parent 46b9282 · Browse files · 2 files+209 added−59 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+209 added−59 removed

  • tests:+209 added−59 removed

2 files+209 added−59 removed

modified:tests/models/test_responses.py+160 added-43 removed
6 unmodified lines
78import httpx9from httpx._content_streams import AsyncIteratorStream, IteratorStream1011REQUEST = httpx.Request("GET", "https://example.org")111312def streaming_body():13 yield b"Hello, "14 yield b"world!"15
3 unmodified lines
19 yield b"world!"202122def test_response():25 response = httpx.Response(200, content=b"Hello, world!", request=REQUEST)23 response = httpx.Response(24 200,25 content=b"Hello, world!",26 request=httpx.Request("GET", "https://example.org"),27 )2829 assert response.status_code == 20030 assert response.reason_phrase == "OK"31 assert response.text == "Hello, world!"30 assert response.request is REQUEST32 assert response.request.method == "GET"33 assert response.request.url == "https://example.org"34 assert response.elapsed >= datetime.timedelta(0)35 assert not response.is_error363738def test_raise_for_status():39 request = httpx.Request("GET", "https://example.org")4041 # 2xx status codes are not an error.37 response = httpx.Response(200, request=REQUEST)42 response = httpx.Response(200, request=request)43 response.raise_for_status()4445 # 4xx status codes are a client error.41 response = httpx.Response(403, request=REQUEST)46 response = httpx.Response(403, request=request)47 with pytest.raises(httpx.HTTPStatusError):48 response.raise_for_status()4950 # 5xx status codes are a server error.46 response = httpx.Response(500, request=REQUEST)51 response = httpx.Response(500, request=request)52 with pytest.raises(httpx.HTTPStatusError):53 response.raise_for_status()5455 # Calling .raise_for_status without setting a request instance is
3 unmodified lines
59 response.raise_for_status()606162def test_response_repr():58 response = httpx.Response(200, content=b"Hello, world!", request=REQUEST)63 response = httpx.Response(64 200,65 content=b"Hello, world!",66 )67 assert repr(response) == "<Response [200 OK]>"686970def test_response_content_type_encoding():
1 unmodified line
72 Use the charset encoding in the Content-Type header if possible.73 """74 headers = {"Content-Type": "text-plain; charset=latin-1"}75 content = "Latin 1: ÿ".encode("latin-1")68 response = httpx.Response(200, content=content, headers=headers, request=REQUEST)76 response = httpx.Response(77 200,78 content=content,79 headers=headers,80 )81 assert response.text == "Latin 1: ÿ"82 assert response.encoding == "latin-1"8384
1 unmodified line
86 """87 Autodetect encoding if there is no charset info in a Content-Type header.88 """89 content = "おはようございます。".encode("EUC-JP")78 response = httpx.Response(200, content=content, request=REQUEST)90 response = httpx.Response(91 200,92 content=content,93 )94 assert response.text == "おはようございます。"95 assert response.encoding == "EUC-JP"9697
2 unmodified lines
100 Fallback to autodetection if we get an invalid charset in the Content-Type header.101 """102 headers = {"Content-Type": "text-plain; charset=invalid-codec-name"}103 content = "おはようございます。".encode("EUC-JP")89 response = httpx.Response(200, content=content, headers=headers, request=REQUEST)104 response = httpx.Response(105 200,106 content=content,107 headers=headers,108 )109 assert response.text == "おはようございます。"110 assert response.encoding == "EUC-JP"111112
3 unmodified lines
116 See: https://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7.1117 """118 content = b"Hello, world!"119 headers = {"Content-Type": "text/plain"}101 response = httpx.Response(200, content=content, headers=headers, request=REQUEST)120 response = httpx.Response(121 200,122 content=content,123 headers=headers,124 )125 assert response.status_code == 200126 assert response.encoding == "iso-8859-1"127 assert response.text == "Hello, world!"128
1 unmodified line
130def test_response_default_encoding():131 """132 Default to utf-8 if all else fails.133 """111 response = httpx.Response(200, content=b"", request=REQUEST)134 response = httpx.Response(135 200,136 content=b"",137 )138 assert response.text == ""139 assert response.encoding == "utf-8"140141
1 unmodified line
143 """144 Default to apparent encoding for non-text content-type headers.145 """146 headers = {"Content-Type": "image/png"}121 response = httpx.Response(200, content=b"xyz", headers=headers, request=REQUEST)147 response = httpx.Response(148 200,149 content=b"xyz",150 headers=headers,151 )152 assert response.text == "xyz"153 assert response.encoding == "ascii"154155
1 unmodified line
157 headers = {158 "Content-Type": "text-plain; charset=utf-8"159 } # Deliberately incorrect charset160 response = httpx.Response(131 200, content="Latin 1: ÿ".encode("latin-1"), headers=headers, request=REQUEST161 200,162 content="Latin 1: ÿ".encode("latin-1"),163 headers=headers,164 )165 response.encoding = "latin-1"166 assert response.text == "Latin 1: ÿ"167 assert response.encoding == "latin-1"168169170def test_response_force_encoding():171 response = httpx.Response(140 200, content="Snowman: ☃".encode("utf-8"), request=REQUEST172 200,173 content="Snowman: ☃".encode("utf-8"),174 )175 response.encoding = "iso-8859-1"176 assert response.status_code == 200177 assert response.reason_phrase == "OK"
1 unmodified line
179 assert response.encoding == "iso-8859-1"180181182def test_read():150 response = httpx.Response(200, content=b"Hello, world!", request=REQUEST)183 response = httpx.Response(184 200,185 content=b"Hello, world!",186 )187188 assert response.status_code == 200189 assert response.text == "Hello, world!"190 assert response.encoding == "ascii"
7 unmodified lines
198199200@pytest.mark.asyncio201async def test_aread():166 response = httpx.Response(200, content=b"Hello, world!", request=REQUEST)202 response = httpx.Response(203 200,204 content=b"Hello, world!",205 )206207 assert response.status_code == 200208 assert response.text == "Hello, world!"209 assert response.encoding == "ascii"
7 unmodified lines
217218219def test_iter_raw():220 stream = IteratorStream(iterator=streaming_body())182 response = httpx.Response(200, stream=stream, request=REQUEST)221 response = httpx.Response(222 200,223 stream=stream,224 )225226 raw = b""227 for part in response.iter_raw():228 raw += part
2 unmodified lines
231232@pytest.mark.asyncio233async def test_aiter_raw():234 stream = AsyncIteratorStream(aiterator=async_streaming_body())193 response = httpx.Response(200, stream=stream, request=REQUEST)235 response = httpx.Response(236 200,237 stream=stream,238 )239240 raw = b""241 async for part in response.aiter_raw():242 raw += part243 assert raw == b"Hello, world!"244245246def test_iter_bytes():202 response = httpx.Response(200, content=b"Hello, world!", request=REQUEST)247 response = httpx.Response(248 200,249 content=b"Hello, world!",250 )251252 content = b""253 for part in response.iter_bytes():254 content += part
1 unmodified line
256257258@pytest.mark.asyncio259async def test_aiter_bytes():212 response = httpx.Response(200, content=b"Hello, world!", request=REQUEST)260 response = httpx.Response(261 200,262 content=b"Hello, world!",263 )264265 content = b""266 async for part in response.aiter_bytes():267 content += part268 assert content == b"Hello, world!"269270271def test_iter_text():221 response = httpx.Response(200, content=b"Hello, world!", request=REQUEST)272 response = httpx.Response(273 200,274 content=b"Hello, world!",275 )276277 content = ""278 for part in response.iter_text():279 content += part
1 unmodified line
281282283@pytest.mark.asyncio284async def test_aiter_text():231 response = httpx.Response(200, content=b"Hello, world!", request=REQUEST)285 response = httpx.Response(286 200,287 content=b"Hello, world!",288 )289290 content = ""291 async for part in response.aiter_text():292 content += part293 assert content == "Hello, world!"294295296def test_iter_lines():240 response = httpx.Response(200, content=b"Hello,\nworld!", request=REQUEST)297 response = httpx.Response(298 200,299 content=b"Hello,\nworld!",300 )301302 content = []303 for line in response.iter_lines():304 content.append(line)
1 unmodified line
306307308@pytest.mark.asyncio309async def test_aiter_lines():250 response = httpx.Response(200, content=b"Hello,\nworld!", request=REQUEST)310 response = httpx.Response(311 200,312 content=b"Hello,\nworld!",313 )314315 content = []316 async for line in response.aiter_lines():317 content.append(line)
1 unmodified line
319320321def test_sync_streaming_response():322 stream = IteratorStream(iterator=streaming_body())260 response = httpx.Response(200, stream=stream, request=REQUEST)323 response = httpx.Response(324 200,325 stream=stream,326 )327328 assert response.status_code == 200329 assert not response.is_closed330
6 unmodified lines
337338@pytest.mark.asyncio339async def test_async_streaming_response():340 stream = AsyncIteratorStream(aiterator=async_streaming_body())275 response = httpx.Response(200, stream=stream, request=REQUEST)341 response = httpx.Response(342 200,343 stream=stream,344 )345346 assert response.status_code == 200347 assert not response.is_closed348
5 unmodified lines
354355356def test_cannot_read_after_stream_consumed():357 stream = IteratorStream(iterator=streaming_body())289 response = httpx.Response(200, stream=stream, request=REQUEST)358 response = httpx.Response(359 200,360 stream=stream,361 )362363 content = b""364 for part in response.iter_bytes():365 content += part
4 unmodified lines
370371@pytest.mark.asyncio372async def test_cannot_aread_after_stream_consumed():373 stream = AsyncIteratorStream(aiterator=async_streaming_body())302 response = httpx.Response(200, stream=stream, request=REQUEST)374 response = httpx.Response(375 200,376 stream=stream,377 )378379 content = b""380 async for part in response.aiter_bytes():381 content += part
9 unmodified lines
391 nonlocal is_closed392 is_closed = True393394 stream = IteratorStream(iterator=streaming_body(), close_func=close_func)320 response = httpx.Response(200, stream=stream, request=REQUEST)395 response = httpx.Response(396 200,397 stream=stream,398 )399400 response.close()401 assert is_closed402
11 unmodified lines
414415 stream = AsyncIteratorStream(416 aiterator=async_streaming_body(), close_func=close_func417 )340 response = httpx.Response(200, stream=stream, request=REQUEST)418 response = httpx.Response(419 200,420 stream=stream,421 )422423 await response.aclose()424 assert is_closed425
3 unmodified lines
429430@pytest.mark.asyncio431async def test_elapsed_not_available_until_closed():432 stream = AsyncIteratorStream(aiterator=async_streaming_body())352 response = httpx.Response(200, stream=stream, request=REQUEST)433 response = httpx.Response(434 200,435 stream=stream,436 )437438 with pytest.raises(RuntimeError):439 response.elapsed440441442def test_unknown_status_code():359 response = httpx.Response(600, request=REQUEST)443 response = httpx.Response(444 600,445 )446 assert response.status_code == 600447 assert response.reason_phrase == ""448 assert response.text == ""449
1 unmodified line
451def test_json_with_specified_encoding():452 data = {"greeting": "hello", "recipient": "world"}453 content = json.dumps(data).encode("utf-16")454 headers = {"Content-Type": "application/json, charset=utf-16"}369 response = httpx.Response(200, content=content, headers=headers, request=REQUEST)455 response = httpx.Response(456 200,457 content=content,458 headers=headers,459 )460 assert response.json() == data461462463def test_json_with_options():464 data = {"greeting": "hello", "recipient": "world", "amount": 1}465 content = json.dumps(data).encode("utf-16")466 headers = {"Content-Type": "application/json, charset=utf-16"}377 response = httpx.Response(200, content=content, headers=headers, request=REQUEST)467 response = httpx.Response(468 200,469 content=content,470 headers=headers,471 )472 assert response.json(parse_int=str)["amount"] == "1"473474475def test_json_without_specified_encoding():476 data = {"greeting": "hello", "recipient": "world"}477 content = json.dumps(data).encode("utf-32-be")478 headers = {"Content-Type": "application/json"}385 response = httpx.Response(200, content=content, headers=headers, request=REQUEST)479 response = httpx.Response(480 200,481 content=content,482 headers=headers,483 )484 assert response.json() == data485486487def test_json_without_specified_encoding_decode_error():
2 unmodified lines
490 headers = {"Content-Type": "application/json"}491 # force incorrect guess from `guess_json_utf` to trigger error492 with mock.patch("httpx._models.guess_json_utf", return_value="utf-32"):493 response = httpx.Response(396 200, content=content, headers=headers, request=REQUEST494 200,495 content=content,496 headers=headers,497 )498 with pytest.raises(json.decoder.JSONDecodeError):499 response.json()500
25 unmodified lines
526 ),527 ],528)529def test_link_headers(headers, expected):430 response = httpx.Response(200, content=None, headers=headers, request=REQUEST)530 response = httpx.Response(531 200,532 content=None,533 headers=headers,534 )535 assert response.links == expected536537538@pytest.mark.parametrize("header_value", (b"deflate", b"gzip", b"br"))539def test_decode_error_with_request(header_value):540 headers = [(b"Content-Encoding", header_value)]541 body = b"test 123"542 compressed_body = brotli.compress(body)[3:]543 with pytest.raises(ValueError):544 httpx.Response(545 200,546 headers=headers,547 content=compressed_body,548 )549550 with pytest.raises(httpx.DecodingError):440 httpx.Response(200, headers=headers, content=compressed_body, request=REQUEST)551 httpx.Response(552 200,553 headers=headers,554 content=compressed_body,555 request=httpx.Request("GET", "https://www.example.org/"),556 )557558559@pytest.mark.parametrize("header_value", (b"deflate", b"gzip", b"br"))560def test_value_error_without_request(header_value):
15 unmodified lines
576577def test_set_request_after_init():578 response = httpx.Response(200, content=b"Hello, world!")579464 response.request = REQUEST580 response.request = httpx.Request("GET", "https://www.example.org")581466 assert response.request == REQUEST582 assert response.request.method == "GET"583 assert response.request.url == "https://www.example.org"584585586def test_cannot_access_unset_request():587 response = httpx.Response(200, content=b"Hello, world!")588589 with pytest.raises(RuntimeError):473 assert response.request is not None590 response.request
modified:tests/test_decoders.py+49 added-16 removed
12 unmodified lines
13 LineDecoder,14 TextDecoder,15)1617REQUEST = httpx.Request("GET", "https://example.org")171918def test_deflate():19 """20 Deflate encoding may use either 'zlib' or 'deflate' in the wild.21
4 unmodified lines
26 compressed_body = compressor.compress(body) + compressor.flush()2728 headers = [(b"Content-Encoding", b"deflate")]29 response = httpx.Response(32 200, headers=headers, content=compressed_body, request=REQUEST30 200,31 headers=headers,32 content=compressed_body,33 )34 assert response.content == body3536
7 unmodified lines
44 compressed_body = zlib.compress(body)4546 headers = [(b"Content-Encoding", b"deflate")]47 response = httpx.Response(48 200, headers=headers, content=compressed_body, request=REQUEST48 200,49 headers=headers,50 content=compressed_body,51 )52 assert response.content == body5354
3 unmodified lines
58 compressed_body = compressor.compress(body) + compressor.flush()5960 headers = [(b"Content-Encoding", b"gzip")]61 response = httpx.Response(60 200, headers=headers, content=compressed_body, request=REQUEST62 200,63 headers=headers,64 content=compressed_body,65 )66 assert response.content == body6768
2 unmodified lines
71 compressed_body = brotli.compress(body)7273 headers = [(b"Content-Encoding", b"br")]74 response = httpx.Response(71 200, headers=headers, content=compressed_body, request=REQUEST75 200,76 headers=headers,77 content=compressed_body,78 )79 assert response.content == body8081
9 unmodified lines
91 )9293 headers = [(b"Content-Encoding", b"deflate, gzip")]94 response = httpx.Response(89 200, headers=headers, content=compressed_body, request=REQUEST95 200,96 headers=headers,97 content=compressed_body,98 )99 assert response.content == body100101
2 unmodified lines
104 compressed_body = brotli.compress(body)105106 headers = [(b"Content-Encoding", b"br, identity")]107 response = httpx.Response(100 200, headers=headers, content=compressed_body, request=REQUEST108 200,109 headers=headers,110 content=compressed_body,111 )112 assert response.content == body113114 headers = [(b"Content-Encoding", b"identity, br")]115 response = httpx.Response(106 200, headers=headers, content=compressed_body, request=REQUEST116 200,117 headers=headers,118 content=compressed_body,119 )120 assert response.content == body121122
7 unmodified lines
130 yield compressor.flush()131132 headers = [(b"Content-Encoding", b"gzip")]133 stream = AsyncIteratorStream(aiterator=compress(body))122 response = httpx.Response(200, headers=headers, stream=stream, request=REQUEST)134 response = httpx.Response(135 200,136 headers=headers,137 stream=stream,138 )139 assert not hasattr(response, "body")140 assert await response.aread() == body141142143@pytest.mark.parametrize("header_value", (b"deflate", b"gzip", b"br", b"identity"))144def test_empty_content(header_value):145 headers = [(b"Content-Encoding", header_value)]130 response = httpx.Response(200, headers=headers, content=b"", request=REQUEST)146 response = httpx.Response(147 200,148 headers=headers,149 content=b"",150 )151 assert response.content == b""152153154@pytest.mark.parametrize(
11 unmodified lines
166 headers = [(b"Content-Encoding", header_value)]167 body = b"test 123"168 compressed_body = brotli.compress(body)[3:]169 with pytest.raises(httpx.DecodingError):170 request = httpx.Request("GET", "https://example.org")150 httpx.Response(200, headers=headers, content=compressed_body, request=REQUEST)171 httpx.Response(200, headers=headers, content=compressed_body, request=request)172173 with pytest.raises(ValueError):174 httpx.Response(200, headers=headers, content=compressed_body)175176177@pytest.mark.parametrize(178 ["data", "encoding"],179 [180 ((b"Hello,", b" world!"), "ascii"),
18 unmodified lines
199 yield chunk200201 # Accessing `.text` on a read response.202 stream = AsyncIteratorStream(aiterator=iterator())179 response = httpx.Response(200, stream=stream, request=REQUEST)203 response = httpx.Response(204 200,205 stream=stream,206 )207 await response.aread()208 assert response.text == (b"".join(data)).decode(encoding)209210 # Streaming `.aiter_text` iteratively.211 stream = AsyncIteratorStream(aiterator=iterator())185 response = httpx.Response(200, stream=stream, request=REQUEST)212 response = httpx.Response(213 200,214 stream=stream,215 )216 text = "".join([part async for part in response.aiter_text()])217 assert text == (b"".join(data)).decode(encoding)218219
8 unmodified lines
228 response = httpx.Response(229 200,230 headers=[(b"Content-Type", b"text/html; charset=shift-jis")],231 stream=stream,202 request=REQUEST,232 )233234 await response.aread()235 assert "".join(response.text) == "トラベル"
80 unmodified lines
316def test_invalid_content_encoding_header():317 headers = [(b"Content-Encoding", b"invalid-header")]318 body = b"test 123"319291 response = httpx.Response(200, headers=headers, content=body, request=REQUEST)320 response = httpx.Response(321 200,322 headers=headers,323 content=body,324 )325 assert response.content == body