Coverage for tests/test_response.py: 100%
32 statements
« prev ^ index » next coverage.py v7.6.12, created at 2025-05-26 23:07 +0100
« prev ^ index » next coverage.py v7.6.12, created at 2025-05-26 23:07 +0100
1import httpx
4def byteiterator(buffer=b""):
5 for num in buffer: # pragma: nocover
6 yield chr(num)
9def test_response():
10 r = httpx.Response(200)
12 assert repr(r) == "<Response [200 OK]>"
13 assert r.code == 200
14 assert r.headers == {'Content-Length': '0'}
15 assert r.stream == httpx.ByteStream(b"")
18def test_response_204():
19 r = httpx.Response(204)
21 assert repr(r) == "<Response [204 No Content]>"
22 assert r.code == 204
23 assert r.headers == {}
24 assert r.stream == httpx.ByteStream(b"")
27def test_response_bytes():
28 content = b"Hello, world"
29 r = httpx.Response(200, content=content)
31 assert repr(r) == "<Response [200 OK]>"
32 assert r.headers == {
33 "Content-Length": "12",
34 }
35 assert r.stream == httpx.ByteStream(b"Hello, world")
38def test_response_stream():
39 stream = httpx.IterByteStream(byteiterator(b"Hello, world"))
40 r = httpx.Response(200, content=stream)
42 assert repr(r) == "<Response [200 OK]>"
43 assert r.headers == {
44 "Transfer-Encoding": "chunked",
45 }
46 assert r.stream is stream
49def test_response_json():
50 data = httpx.JSON({"msg": "Hello, world"})
51 r = httpx.Response(200, content=data)
53 assert repr(r) == "<Response [200 OK]>"
54 assert r.headers == {
55 "Content-Length": "22",
56 "Content-Type": "application/json",
57 }
58 assert r.stream == httpx.ByteStream(b'{"msg":"Hello, world"}')