Coverage for tests/test_request.py: 100%

33 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-05-26 23:07 +0100

1import httpx 

2 

3 

4def byteiterator(buffer=b""): 

5 for num in buffer: # pragma: nocover 

6 yield chr(num) 

7 

8 

9def test_request(): 

10 r = httpx.Request("GET", "https://example.com") 

11 

12 assert repr(r) == "<Request [GET 'https://example.com']>" 

13 assert r.method == "GET" 

14 assert r.url == "https://example.com" 

15 assert r.headers == { 

16 "Host": "example.com" 

17 } 

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

19 

20def test_request_bytes(): 

21 content = b"Hello, world" 

22 r = httpx.Request("POST", "https://example.com", content=content) 

23 

24 assert repr(r) == "<Request [POST 'https://example.com']>" 

25 assert r.method == "POST" 

26 assert r.url == "https://example.com" 

27 assert r.headers == { 

28 "Host": "example.com", 

29 "Content-Length": "12", 

30 } 

31 assert r.stream == httpx.ByteStream(b"Hello, world") 

32 

33 

34def test_request_stream(): 

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

36 r = httpx.Request("POST", "https://example.com", content=stream) 

37 

38 assert repr(r) == "<Request [POST 'https://example.com']>" 

39 assert r.method == "POST" 

40 assert r.url == "https://example.com" 

41 assert r.headers == { 

42 "Host": "example.com", 

43 "Transfer-Encoding": "chunked", 

44 } 

45 assert r.stream is stream 

46 

47 

48def test_request_json(): 

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

50 r = httpx.Request("POST", "https://example.com", content=data) 

51 

52 assert repr(r) == "<Request [POST 'https://example.com']>" 

53 assert r.method == "POST" 

54 assert r.url == "https://example.com" 

55 assert r.headers == { 

56 "Host": "example.com", 

57 "Content-Length": "22", 

58 "Content-Type": "application/json", 

59 } 

60 assert r.stream == httpx.ByteStream(b'{"msg":"Hello, world"}')