Coverage for tests/test_response.py: 100%

1 import httpx

2

3

4 def byteiterator(buffer=b""):

5 for num in buffer: # pragma: nocover

6 yield chr(num)

7

8

9 def test_response():

10 r = httpx.Response(200)

11

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"")

16

17

18 def test_response_204():

19 r = httpx.Response(204)

20

21 assert repr(r) == "<Response [204 No Content]>"

22 assert r.code == 204

23 assert r.headers == {}

24 assert r.stream == httpx.ByteStream(b"")

25

26

27 def test_response_bytes():

28 content = b"Hello, world"

29 r = httpx.Response(200, content=content)

30

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")

36

37

38 def test_response_stream():

39 stream = httpx.IterByteStream(byteiterator(b"Hello, world"))

40 r = httpx.Response(200, content=stream)

41

42 assert repr(r) == "<Response [200 OK]>"

43 assert r.headers == {

44 "Transfer-Encoding": "chunked",

45 }

46 assert r.stream is stream

47

48

49 def test_response_json():

50 data = httpx.JSON({"msg": "Hello, world"})

51 r = httpx.Response(200, content=data)

52

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"}')