@@ -117,6 +117,44 @@ def test_with_query_list_int():
117117 assert str (url .with_query ([("a" , 1 )])) == "http://example.com/?a=1"
118118
119119
120+ @pytest .mark .parametrize (
121+ ("query" , "expected" ),
122+ [
123+ pytest .param ({"a" : []}, "" , id = "empty list" ),
124+ pytest .param ({"a" : ()}, "" , id = "empty tuple" ),
125+ pytest .param ({"a" : [1 ]}, "/?a=1" , id = "single list" ),
126+ pytest .param ({"a" : (1 ,)}, "/?a=1" , id = "single tuple" ),
127+ pytest .param ({"a" : [1 , 2 ]}, "/?a=1&a=2" , id = "list" ),
128+ pytest .param ({"a" : (1 , 2 )}, "/?a=1&a=2" , id = "tuple" ),
129+ pytest .param ({"a[]" : [1 , 2 ]}, "/?a%5B%5D=1&a%5B%5D=2" , id = "key with braces" ),
130+ pytest .param ({"&" : [1 , 2 ]}, "/?%26=1&%26=2" , id = "quote key" ),
131+ pytest .param ({"a" : ["1" , 2 ]}, "/?a=1&a=2" , id = "mixed types" ),
132+ pytest .param ({"&" : ["=" , 2 ]}, "/?%26=%3D&%26=2" , id = "quote key and value" ),
133+ pytest .param ({"a" : 1 , "b" : [2 , 3 ]}, "/?a=1&b=2&b=3" , id = "single then list" ),
134+ pytest .param ({"a" : [1 , 2 ], "b" : 3 }, "/?a=1&a=2&b=3" , id = "list then single" ),
135+ pytest .param ({"a" : ["1&a=2" , 3 ]}, "/?a=1%26a%3D2&a=3" , id = "ampersand then int" ),
136+ pytest .param ({"a" : [1 , "2&a=3" ]}, "/?a=1&a=2%26a%3D3" , id = "int then ampersand" ),
137+ ],
138+ )
139+ def test_with_query_sequence (query , expected ):
140+ url = URL ("http://example.com" )
141+ expected = "http://example.com{expected}" .format_map (locals ())
142+ assert str (url .with_query (query )) == expected
143+
144+
145+ @pytest .mark .parametrize (
146+ "query" ,
147+ [
148+ pytest .param ({"a" : [[1 ]]}, id = "nested" ),
149+ pytest .param ([("a" , [1 , 2 ])], id = "tuple list" ),
150+ ],
151+ )
152+ def test_with_query_sequence_invalid_use (query ):
153+ url = URL ("http://example.com" )
154+ with pytest .raises (TypeError , match = "Invalid variable type" ):
155+ url .with_query (query )
156+
157+
120158def test_with_query_non_str ():
121159 url = URL ("http://example.com" )
122160 with pytest .raises (TypeError ):
@@ -196,16 +234,27 @@ def test_with_query_memoryview():
196234 url .with_query (memoryview (b"123" ))
197235
198236
199- def test_with_query_params ():
200- url = URL ("http://example.com/get" )
201- url2 = url .with_query ([("key" , "1;2;3" )])
202- assert str (url2 ) == "http://example.com/get?key=1%3B2%3B3"
203-
204-
205- def test_with_query_params2 ():
237+ @pytest .mark .parametrize (
238+ ("query" , "expected" ),
239+ [
240+ pytest .param ([("key" , "1;2;3" )], "?key=1%3B2%3B3" , id = "tuple list semicolon" ),
241+ pytest .param ({"key" : "1;2;3" }, "?key=1%3B2%3B3" , id = "mapping semicolon" ),
242+ pytest .param ([("key" , "1&a=2" )], "?key=1%26a%3D2" , id = "tuple list ampersand" ),
243+ pytest .param ({"key" : "1&a=2" }, "?key=1%26a%3D2" , id = "mapping ampersand" ),
244+ pytest .param ([("&" , "=" )], "?%26=%3D" , id = "tuple list quote key" ),
245+ pytest .param ({"&" : "=" }, "?%26=%3D" , id = "mapping quote key" ),
246+ pytest .param ([("a[]" , "3" )], "?a%5B%5D=3" , id = "quote one key braces" ,),
247+ pytest .param (
248+ [("a[]" , "3" ), ("a[]" , "4" )],
249+ "?a%5B%5D=3&a%5B%5D=4" ,
250+ id = "quote many key braces" ,
251+ ),
252+ ],
253+ )
254+ def test_with_query_params (query , expected ):
206255 url = URL ("http://example.com/get" )
207- url2 = url .with_query ({ "key" : "1;2;3" } )
208- assert str (url2 ) == "http://example.com/get?key=1%3B2%3B3"
256+ url2 = url .with_query (query )
257+ assert str (url2 ) == ( "http://example.com/get" + expected )
209258
210259
211260def test_with_query_only ():
0 commit comments