Coverage for tests/test_content.py: 100%

78 statements  

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

1import httpx 

2import pytest 

3 

4 

5# HTML 

6 

7def test_html(): 

8 html = httpx.HTML("<html><body>Hello, world</body></html>") 

9 

10 stream, content_type = html.encode() 

11 assert stream == httpx.ByteStream(b'<html><body>Hello, world</body></html>') 

12 assert content_type == "text/html; charset='utf-8'" 

13 

14 

15# Text 

16 

17def test_text(): 

18 text = httpx.Text("Hello, world") 

19 

20 stream, content_type = text.encode() 

21 assert stream == httpx.ByteStream(b'Hello, world') 

22 assert content_type == "text/plain; charset='utf-8'" 

23 

24 

25# JSON 

26 

27def test_json(): 

28 data = httpx.JSON({'data': 123}) 

29 

30 stream, content_type = data.encode() 

31 assert stream == httpx.ByteStream(b'{"data":123}') 

32 assert content_type == "application/json" 

33 

34 

35# Form 

36 

37def test_form(): 

38 f = httpx.Form([("a", "123"), ("a", "456"), ("b", "789")]) 

39 

40 # Accessors 

41 assert "a" in f 

42 assert "A" not in f 

43 assert "c" not in f 

44 assert f["a"] == "123" 

45 assert f.get("a") == "123" 

46 assert f.get("nope", default=None) is None 

47 

48 # Standard dict operations 

49 assert list(f.keys()) == ["a", "b"] 

50 assert list(f.values()) == ["123", "789"] 

51 assert list(f.items()) == [("a", "123"), ("b", "789")] 

52 assert list(f) == ["a", "b"] 

53 assert dict(f) == {"a": "123", "b": "789"} 

54 

55 assert len(f) == 2 

56 assert bool(f) 

57 assert hash(f) 

58 

59 # Comparison 

60 assert f == httpx.Form([("a", "123"), ("a", "456"), ("b", "789")]) 

61 

62 # Multi-dict interface 

63 assert f.get_list("a") == ["123", "456"] 

64 assert list(f.multi_items()) == [("a", "123"), ("a", "456"), ("b", "789")] 

65 assert f.multi_dict() == {"a": ["123", "456"], "b": ["789"]} 

66 

67 # Update 

68 assert f.copy_set("a", "abc") == httpx.Form([("a", "abc"), ("b", "789")]) 

69 assert f.copy_append("a", "abc") == httpx.Form([("a", "123"), ("a", "456"), ("a", "abc"), ("b", "789")]) 

70 assert f.copy_remove("a") == httpx.Form([("b", "789")]) 

71 

72 assert str(f) == "a=123&a=456&b=789" 

73 

74 

75def test_empty_form(): 

76 f = httpx.Form() 

77 assert f.multi_dict() == {} 

78 

79 

80def test_form_from_dict(): 

81 f = httpx.Form({ 

82 "a": ["123", "456"], 

83 "b": "789" 

84 }) 

85 assert f.multi_dict() == { 

86 "a": ["123", "456"], 

87 "b": ["789"] 

88 } 

89 

90 

91def test_form_encode(): 

92 form = httpx.Form({'email': 'address@example.com'}) 

93 assert form['email'] == "address@example.com" 

94 

95 stream, content_type = form.encode() 

96 assert stream == httpx.ByteStream(b"email=address%40example.com") 

97 assert content_type == "application/x-www-form-urlencoded" 

98 

99 

100# Files 

101 

102def test_files(): 

103 f = httpx.Files([ 

104 ("a", httpx.File("123.json")), 

105 ("a", httpx.File("456.json")), 

106 ("b", httpx.File("789.json")) 

107 ]) 

108 

109 # Accessors 

110 assert "a" in f 

111 assert "A" not in f 

112 assert "c" not in f 

113 assert f["a"] == httpx.File("123.json") 

114 assert f.get("a") == httpx.File("123.json") 

115 assert f.get("nope", default=None) is None 

116 

117 # Standard dict operations 

118 assert list(f.keys()) == ["a", "b"] 

119 assert list(f.values()) == [httpx.File("123.json"), httpx.File("789.json")] 

120 assert list(f.items()) == [("a", httpx.File("123.json")), ("b", httpx.File("789.json"))] 

121 assert list(f) == ["a", "b"] 

122 assert dict(f) == {"a": httpx.File("123.json"), "b": httpx.File("789.json")} 

123 

124 assert len(f) == 2 

125 assert bool(f) 

126 

127 # Comparison 

128 assert f == httpx.Files([ 

129 ("a", httpx.File("123.json")), 

130 ("a", httpx.File("456.json")), 

131 ("b", httpx.File("789.json")), 

132 ]) 

133 

134 # Multi-dict interface 

135 assert f.get_list("a") == [ 

136 httpx.File("123.json"), 

137 httpx.File("456.json"), 

138 ] 

139 assert list(f.multi_items()) == [ 

140 ("a", httpx.File("123.json")), 

141 ("a", httpx.File("456.json")), 

142 ("b", httpx.File("789.json")), 

143 ] 

144 assert f.multi_dict() == { 

145 "a": [ 

146 httpx.File("123.json"), 

147 httpx.File("456.json"), 

148 ], 

149 "b": [ 

150 httpx.File("789.json"), 

151 ] 

152 } 

153 

154 

155def test_empty_files(): 

156 f = httpx.Files() 

157 assert f.multi_dict() == {} 

158 

159 

160def test_files_from_dict(): 

161 f = httpx.Files({ 

162 "a": [ 

163 httpx.File("123.json"), 

164 httpx.File("456.json"), 

165 ], 

166 "b": httpx.File("789.json") 

167 }) 

168 assert f.multi_dict() == { 

169 "a": [ 

170 httpx.File("123.json"), 

171 httpx.File("456.json"), 

172 ], 

173 "b": [ 

174 httpx.File("789.json"), 

175 ] 

176 } 

177 

178 

179 

180# def test_multipart(tmpfile): 

181# multipart = httpx.MultiPart(form={'name': '...'}, files={'upload': httpx.File(tmpfile)}) 

182# assert multipart.form['name'] == "..." 

183# assert multipart.files['upload'].name == 'upload.txt' 

184