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
Provenance
- Commit
de502a4- Tree
37c42a1- Author
Tom Christie <tom@tomchristie.com>- Committer
GitHub <noreply@github.com>- Parent
46b9282
2 files changed+209 added−59 removed
modified:tests/models/test_responses.py+160 added-43 removed
6 unmodified lines7 8 import httpx9 from httpx._content_streams import AsyncIteratorStream, IteratorStream10 11-REQUEST = httpx.Request("GET", "https://example.org")11 13-12 def streaming_body():13 yield b"Hello, "14 yield b"world!"15 3 unmodified lines19 yield b"world!"20 21 22 def 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+ )28 29 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_error36 37 38 def test_raise_for_status():39+ request = httpx.Request("GET", "https://example.org")40+41 # 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()44 45 # 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()49 50 # 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()54 55 # Calling .raise_for_status without setting a request instance is3 unmodified lines59 response.raise_for_status()60 61 62 def 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]>"68 69 70 def test_response_content_type_encoding():1 unmodified line72 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"83 84 1 unmodified line86 """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"96 97 2 unmodified lines100 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"111 112 3 unmodified lines116 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 line130 def 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"140 141 1 unmodified line143 """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"154 155 1 unmodified line157 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"168 169 170 def 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 line179 assert response.encoding == "iso-8859-1"180 181 182 def 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+ )187 188 assert response.status_code == 200189 assert response.text == "Hello, world!"190 assert response.encoding == "ascii"7 unmodified lines198 199 200 @pytest.mark.asyncio201 async 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+ )206 207 assert response.status_code == 200208 assert response.text == "Hello, world!"209 assert response.encoding == "ascii"7 unmodified lines217 218 219 def 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+ )225 226 raw = b""227 for part in response.iter_raw():228 raw += part2 unmodified lines231 232 @pytest.mark.asyncio233 async 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+ )239 240 raw = b""241 async for part in response.aiter_raw():242 raw += part243 assert raw == b"Hello, world!"244 245 246 def 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+ )251 252 content = b""253 for part in response.iter_bytes():254 content += part1 unmodified line256 257 258 @pytest.mark.asyncio259 async 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+ )264 265 content = b""266 async for part in response.aiter_bytes():267 content += part268 assert content == b"Hello, world!"269 270 271 def 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+ )276 277 content = ""278 for part in response.iter_text():279 content += part1 unmodified line281 282 283 @pytest.mark.asyncio284 async 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+ )289 290 content = ""291 async for part in response.aiter_text():292 content += part293 assert content == "Hello, world!"294 295 296 def 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+ )301 302 content = []303 for line in response.iter_lines():304 content.append(line)1 unmodified line306 307 308 @pytest.mark.asyncio309 async 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+ )314 315 content = []316 async for line in response.aiter_lines():317 content.append(line)1 unmodified line319 320 321 def 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+ )327 328 assert response.status_code == 200329 assert not response.is_closed330 6 unmodified lines337 338 @pytest.mark.asyncio339 async 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+ )345 346 assert response.status_code == 200347 assert not response.is_closed348 5 unmodified lines354 355 356 def 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+ )362 363 content = b""364 for part in response.iter_bytes():365 content += part4 unmodified lines370 371 @pytest.mark.asyncio372 async 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+ )378 379 content = b""380 async for part in response.aiter_bytes():381 content += part9 unmodified lines391 nonlocal is_closed392 is_closed = True393 394 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+ )399 400 response.close()401 assert is_closed402 11 unmodified lines414 415 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+ )422 423 await response.aclose()424 assert is_closed425 3 unmodified lines429 430 @pytest.mark.asyncio431 async 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+ )437 438 with pytest.raises(RuntimeError):439 response.elapsed440 441 442 def 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 line451 def 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() == data461 462 463 def 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"473 474 475 def 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() == data485 486 487 def test_json_without_specified_encoding_decode_error():2 unmodified lines490 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 lines526 ),527 ],528 )529 def 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 == expected536 537 538 @pytest.mark.parametrize("header_value", (b"deflate", b"gzip", b"br"))539 def 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+ )549+550 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+ )557 558 559 @pytest.mark.parametrize("header_value", (b"deflate", b"gzip", b"br"))560 def test_value_error_without_request(header_value):15 unmodified lines576 577 def test_set_request_after_init():578 response = httpx.Response(200, content=b"Hello, world!")579 464- response.request = REQUEST580+ response.request = httpx.Request("GET", "https://www.example.org")581 466- assert response.request == REQUEST582+ assert response.request.method == "GET"583+ assert response.request.url == "https://www.example.org"584 585 586 def test_cannot_access_unset_request():587 response = httpx.Response(200, content=b"Hello, world!")588 589 with pytest.raises(RuntimeError):473- assert response.request is not None590+ response.requestmodified:tests/test_decoders.py+49 added-16 removed
12 unmodified lines13 LineDecoder,14 TextDecoder,15 )16 17-REQUEST = httpx.Request("GET", "https://example.org")17 19-18 def test_deflate():19 """20 Deflate encoding may use either 'zlib' or 'deflate' in the wild.21 4 unmodified lines26 compressed_body = compressor.compress(body) + compressor.flush()27 28 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 == body35 36 7 unmodified lines44 compressed_body = zlib.compress(body)45 46 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 == body53 54 3 unmodified lines58 compressed_body = compressor.compress(body) + compressor.flush()59 60 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 == body67 68 2 unmodified lines71 compressed_body = brotli.compress(body)72 73 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 == body80 81 9 unmodified lines91 )92 93 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 == body100 101 2 unmodified lines104 compressed_body = brotli.compress(body)105 106 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 == body113 114 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 == body121 122 7 unmodified lines130 yield compressor.flush()131 132 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() == body141 142 143 @pytest.mark.parametrize("header_value", (b"deflate", b"gzip", b"br", b"identity"))144 def 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""152 153 154 @pytest.mark.parametrize(11 unmodified lines166 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)172 173+ with pytest.raises(ValueError):174+ httpx.Response(200, headers=headers, content=compressed_body)175 176+177 @pytest.mark.parametrize(178 ["data", "encoding"],179 [180 ((b"Hello,", b" world!"), "ascii"),18 unmodified lines199 yield chunk200 201 # 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)209 210 # 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)218 219 8 unmodified lines228 response = httpx.Response(229 200,230 headers=[(b"Content-Type", b"text/html; charset=shift-jis")],231 stream=stream,202- request=REQUEST,232 )233 234 await response.aread()235 assert "".join(response.text) == "トラベル"80 unmodified lines316 def test_invalid_content_encoding_header():317 headers = [(b"Content-Encoding", b"invalid-header")]318 body = b"test 123"319 291- 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