Coverage for tests/test_headers.py: 100%

69 statements  

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

1import httpx 

2import pytest 

3 

4 

5def test_headers_from_dict(): 

6 headers = httpx.Headers({ 

7 'Content-Length': '1024', 

8 'Content-Type': 'text/plain; charset=utf-8', 

9 }) 

10 assert headers['Content-Length'] == '1024' 

11 assert headers['Content-Type'] == 'text/plain; charset=utf-8' 

12 

13 

14def test_headers_from_list(): 

15 headers = httpx.Headers([ 

16 ('Location', 'https://www.example.com'), 

17 ('Set-Cookie', 'session_id=3498jj489jhb98jn'), 

18 ]) 

19 assert headers['Location'] == 'https://www.example.com' 

20 assert headers['Set-Cookie'] == 'session_id=3498jj489jhb98jn' 

21 

22 

23def test_header_keys(): 

24 h = httpx.Headers({"Accept": "*/*", "User-Agent": "python/httpx"}) 

25 assert list(h.keys()) == ["Accept", "User-Agent"] 

26 

27 

28def test_header_values(): 

29 h = httpx.Headers({"Accept": "*/*", "User-Agent": "python/httpx"}) 

30 assert list(h.values()) == ["*/*", "python/httpx"] 

31 

32 

33def test_header_items(): 

34 h = httpx.Headers({"Accept": "*/*", "User-Agent": "python/httpx"}) 

35 assert list(h.items()) == [("Accept", "*/*"), ("User-Agent", "python/httpx")] 

36 

37 

38def test_header_get(): 

39 h = httpx.Headers({"Accept": "*/*", "User-Agent": "python/httpx"}) 

40 assert h.get("User-Agent") == "python/httpx" 

41 assert h.get("user-agent") == "python/httpx" 

42 assert h.get("missing") is None 

43 

44 

45def test_header_copy_set(): 

46 h = httpx.Headers({"Expires": "0"}) 

47 h = h.copy_set("Expires", "Wed, 21 Oct 2015 07:28:00 GMT") 

48 assert h == httpx.Headers({"Expires": "Wed, 21 Oct 2015 07:28:00 GMT"}) 

49 

50 h = httpx.Headers({"Expires": "0"}) 

51 h = h.copy_set("expires", "Wed, 21 Oct 2015 07:28:00 GMT") 

52 assert h == httpx.Headers({"Expires": "Wed, 21 Oct 2015 07:28:00 GMT"}) 

53 

54 

55def test_header_copy_remove(): 

56 h = httpx.Headers({"Accept": "*/*"}) 

57 h = h.copy_remove("Accept") 

58 assert h == httpx.Headers({}) 

59 

60 h = httpx.Headers({"Accept": "*/*"}) 

61 h = h.copy_remove("accept") 

62 assert h == httpx.Headers({}) 

63 

64 

65def test_header_getitem(): 

66 h = httpx.Headers({"Accept": "*/*", "User-Agent": "python/httpx"}) 

67 assert h["User-Agent"] == "python/httpx" 

68 assert h["user-agent"] == "python/httpx" 

69 with pytest.raises(KeyError): 

70 h["missing"] 

71 

72 

73def test_header_contains(): 

74 h = httpx.Headers({"Accept": "*/*", "User-Agent": "python/httpx"}) 

75 assert "User-Agent" in h 

76 assert "user-agent" in h 

77 assert "missing" not in h 

78 

79 

80def test_header_bool(): 

81 h = httpx.Headers({"Accept": "*/*", "User-Agent": "python/httpx"}) 

82 assert bool(h) 

83 h = httpx.Headers() 

84 assert not bool(h) 

85 

86 

87def test_header_iter(): 

88 h = httpx.Headers({"Accept": "*/*", "User-Agent": "python/httpx"}) 

89 assert [k for k in h] == ["Accept", "User-Agent"] 

90 

91 

92def test_header_len(): 

93 h = httpx.Headers({"Accept": "*/*", "User-Agent": "python/httpx"}) 

94 assert len(h) == 2 

95 

96 

97def test_header_repr(): 

98 h = httpx.Headers({"Accept": "*/*", "User-Agent": "python/httpx"}) 

99 assert repr(h) == "<Headers {'Accept': '*/*', 'User-Agent': 'python/httpx'}>" 

100 

101 

102def test_header_invalid_name(): 

103 with pytest.raises(ValueError): 

104 httpx.Headers({"Accept\n": "*/*"}) 

105 

106 

107def test_header_invalid_value(): 

108 with pytest.raises(ValueError): 

109 httpx.Headers({"Accept": "*/*\n"})