MCPcopy
hub / github.com/encode/httpx / test_text_decoder_with_autodetect

Function test_text_decoder_with_autodetect

tests/test_decoders.py:244–263  ·  view source on GitHub ↗
(data, encoding)

Source from the content-addressed store, hash-verified

242)
243@pytest.mark.anyio
244async def test_text_decoder_with_autodetect(data, encoding):
245 async def iterator() -> typing.AsyncIterator[bytes]:
246 nonlocal data
247 for chunk in data:
248 yield chunk
249
250 def autodetect(content):
251 return chardet.detect(content).get("encoding")
252
253 # Accessing `.text` on a read response.
254 response = httpx.Response(200, content=iterator(), default_encoding=autodetect)
255 await response.aread()
256 assert response.text == (b"".join(data)).decode(encoding)
257
258 # Streaming `.aiter_text` iteratively.
259 # Note that if we streamed the text *without* having read it first, then
260 # we won't get a `charset_normalizer` guess, and will instead always rely
261 # on utf-8 if no charset is specified.
262 text = "".join([part async for part in response.aiter_text()])
263 assert text == (b"".join(data)).decode(encoding)
264
265
266@pytest.mark.anyio

Callers

nothing calls this directly

Calls 5

areadMethod · 0.95
aiter_textMethod · 0.95
iteratorFunction · 0.85
joinMethod · 0.80
decodeMethod · 0.45

Tested by

no test coverage detected