Coverage for tests/test_urls.py: 100%

1 import httpx

2 import pytest

3

4

5 def test_url():

6 url = httpx.URL('https://www.example.com/')

7 assert str(url) == "https://www.example.com/"

8

9

10 def test_url_repr():

11 url = httpx.URL('https://www.example.com/')

12 assert repr(url) == "<URL 'https://www.example.com/'>"

13

14

15 def test_url_params():

16 url = httpx.URL('https://www.example.com/', params={"a": "b", "c": "d"})

17 assert str(url) == "https://www.example.com/?a=b&c=d"

18

19

20 def test_url_normalisation():

21 url = httpx.URL('https://www.EXAMPLE.com:443/path/../main')

22 assert str(url) == 'https://www.example.com/main'

23

24

25 def test_url_relative():

26 url = httpx.URL('/README.md')

27 assert str(url) == '/README.md'

28

29

30 def test_url_escaping():

31 url = httpx.URL('https://example.com/path to here?search=🦋')

32 assert str(url) == 'https://example.com/path%20to%20here?search=%F0%9F%A6%8B'

33

34

35 def test_url_components():

36 url = httpx.URL(scheme="https", host="example.com", path="/")

37 assert str(url) == 'https://example.com/'

38

39

40# QueryParams

41

42 def test_queryparams():

43 params = httpx.QueryParams({"color": "black", "size": "medium"})

44 assert str(params) == 'color=black&size=medium'

45

46

47 def test_queryparams_repr():

48 params = httpx.QueryParams({"color": "black", "size": "medium"})

49 assert repr(params) == "<QueryParams 'color=black&size=medium'>"

50

51

52 def test_queryparams_list_of_values():

53 params = httpx.QueryParams({"filter": ["60GHz", "75GHz", "100GHz"]})

54 assert str(params) == 'filter=60GHz&filter=75GHz&filter=100GHz'

55

56

57 def test_queryparams_from_str():

58 params = httpx.QueryParams("color=black&size=medium")

59 assert str(params) == 'color=black&size=medium'

60

61

62 def test_queryparams_access():

63 params = httpx.QueryParams("sort_by=published&author=natalie")

64 assert params["sort_by"] == 'published'

65

66

67 def test_queryparams_escaping():

68 params = httpx.QueryParams({"email": "user@example.com", "search": "How HTTP works!"})

69 assert str(params) == 'email=user%40example.com&search=How+HTTP+works%21'

70

71

72 def test_queryparams_empty():

73 q = httpx.QueryParams({"a": ""})

74 assert str(q) == "a="

75

76 q = httpx.QueryParams("a=")

77 assert str(q) == "a="

78

79 q = httpx.QueryParams("a")

80 assert str(q) == "a="

81

82

83 def test_queryparams_set():

84 q = httpx.QueryParams("a=123")

85 q = q.copy_set("a", "456")

86 assert q == httpx.QueryParams("a=456")

87

88

89 def test_queryparams_append():

90 q = httpx.QueryParams("a=123")

91 q = q.copy_append("a", "456")

92 assert q == httpx.QueryParams("a=123&a=456")

93

94

95 def test_queryparams_remove():

96 q = httpx.QueryParams("a=123")

97 q = q.copy_remove("a")

98 assert q == httpx.QueryParams("")

99

100

101 def test_queryparams_merge():

102 q = httpx.QueryParams("a=123")

103 q = q.copy_update({"b": "456"})

104 assert q == httpx.QueryParams("a=123&b=456")

105 q = q.copy_update({"a": "000", "c": "789"})

106 assert q == httpx.QueryParams("a=000&b=456&c=789")

107

108

109 def test_queryparams_are_hashable():

110 params = (

111 httpx.QueryParams("a=123"),

112 httpx.QueryParams({"a": "123"}),

113 httpx.QueryParams("b=456"),

114 httpx.QueryParams({"b": "456"}),

115 )

116

117 assert len(set(params)) == 2

118

119

120@pytest.mark.parametrize(

121 "source",

122 [

123 "a=123&a=456&b=789",

124 {"a": ["123", "456"], "b": "789"},

125 {"a": ("123", "456"), "b": "789"},

126 [("a", "123"), ("a", "456"), ("b", "789")],

127 (("a", "123"), ("a", "456"), ("b", "789")),

128 ],

129)

130 def test_queryparams_misc(source):

131 q = httpx.QueryParams(source)

132 assert "a" in q

133 assert "A" not in q

134 assert "c" not in q

135 assert q["a"] == "123"

136 assert q.get("a") == "123"

137 assert q.get("nope", default=None) is None

138 assert q.get_list("a") == ["123", "456"]

139 assert bool(q)

140

141 assert list(q.keys()) == ["a", "b"]

142 assert list(q.values()) == ["123", "789"]

143 assert list(q.items()) == [("a", "123"), ("b", "789")]

144 assert len(q) == 2

145 assert list(q) == ["a", "b"]

146 assert dict(q) == {"a": "123", "b": "789"}

147 assert str(q) == "a=123&a=456&b=789"

148 assert httpx.QueryParams({"a": "123", "b": "456"}) == httpx.QueryParams(

149 [("a", "123"), ("b", "456")]

150 )

151 assert httpx.QueryParams({"a": "123", "b": "456"}) == httpx.QueryParams(

152 "a=123&b=456"

153 )

154 assert httpx.QueryParams({"a": "123", "b": "456"}) == httpx.QueryParams(

155 {"b": "456", "a": "123"}

156 )

157 assert httpx.QueryParams() == httpx.QueryParams({})

158 assert httpx.QueryParams([("a", "123"), ("a", "456")]) == httpx.QueryParams(

159 "a=123&a=456"

160 )

161 assert httpx.QueryParams({"a": "123", "b": "456"}) != "invalid"

162

163 q = httpx.QueryParams([("a", "123"), ("a", "456")])

164 assert httpx.QueryParams(q) == q