1313# See the License for the specific language governing permissions and
1414# limitations under the License.
1515"""Module containing the tests for the URIBuilder object."""
16+ try :
17+ from urllib .parse import parse_qs
18+ except ImportError :
19+ from urlparse import parse_qs
20+
1621import pytest
1722
1823from rfc3986 import builder , uri_reference
@@ -191,6 +196,88 @@ def test_add_fragment():
191196 assert uribuilder .fragment == "section-2.5.1"
192197
193198
199+ @pytest .mark .parametrize (
200+ "uri, extend_with, expected_path" ,
201+ [
202+ ("https://api.github.com" , "/users" , "/users" ),
203+ ("https://api.github.com" , "/users/" , "/users/" ),
204+ ("https://api.github.com" , "users" , "/users" ),
205+ ("https://api.github.com" , "users/" , "/users/" ),
206+ ("" , "users/" , "/users/" ),
207+ ("" , "users" , "/users" ),
208+ ("?foo=bar" , "users" , "/users" ),
209+ (
210+ "https://api.github.com/users/" ,
211+ "sigmavirus24" ,
212+ "/users/sigmavirus24" ,
213+ ),
214+ (
215+ "https://api.github.com/users" ,
216+ "sigmavirus24" ,
217+ "/users/sigmavirus24" ,
218+ ),
219+ (
220+ "https://api.github.com/users" ,
221+ "/sigmavirus24" ,
222+ "/users/sigmavirus24" ,
223+ ),
224+ ],
225+ )
226+ def test_extend_path (uri , extend_with , expected_path ):
227+ """Verify the behaviour of extend_path."""
228+ uribuilder = (
229+ builder .URIBuilder ()
230+ .from_uri (uri_reference (uri ))
231+ .extend_path (extend_with )
232+ )
233+ assert uribuilder .path == expected_path
234+
235+
236+ @pytest .mark .parametrize (
237+ "uri, extend_with, expected_query" ,
238+ [
239+ (
240+ "https://github.com" ,
241+ [("a" , "b c" ), ("d" , "e&f" )],
242+ {"a" : ["b c" ], "d" : ["e&f" ]},
243+ ),
244+ (
245+ "https://github.com?a=0" ,
246+ [("a" , "b c" ), ("d" , "e&f" )],
247+ {"a" : ["0" , "b c" ], "d" : ["e&f" ]},
248+ ),
249+ (
250+ "https://github.com?a=0&e=f" ,
251+ [("a" , "b c" ), ("d" , "e&f" )],
252+ {"a" : ["0" , "b c" ], "e" : ["f" ], "d" : ["e&f" ]},
253+ ),
254+ (
255+ "https://github.com" ,
256+ {"a" : "b c" , "d" : "e&f" },
257+ {"a" : ["b c" ], "d" : ["e&f" ]},
258+ ),
259+ (
260+ "https://github.com?a=0" ,
261+ {"a" : "b c" , "d" : "e&f" },
262+ {"a" : ["0" , "b c" ], "d" : ["e&f" ]},
263+ ),
264+ (
265+ "https://github.com?a=0&e=f" ,
266+ {"a" : "b c" , "d" : "e&f" },
267+ {"a" : ["0" , "b c" ], "e" : ["f" ], "d" : ["e&f" ]},
268+ ),
269+ ],
270+ )
271+ def test_extend_query_with (uri , extend_with , expected_query ):
272+ """Verify the behaviour of extend_query_with."""
273+ uribuilder = (
274+ builder .URIBuilder ()
275+ .from_uri (uri_reference (uri ))
276+ .extend_query_with (extend_with )
277+ )
278+ assert parse_qs (uribuilder .query ) == expected_query
279+
280+
194281def test_finalize ():
195282 """Verify the whole thing."""
196283 uri = (
@@ -207,3 +294,20 @@ def test_finalize():
207294 "sigmavirus24/rfc3986"
208295 )
209296 assert expected == uri
297+
298+
299+ def test_geturl ():
300+ """Verify the short-cut to the URL."""
301+ uri = (
302+ builder .URIBuilder ()
303+ .add_scheme ("https" )
304+ .add_credentials ("sigmavirus24" , "not-my-re@l-password" )
305+ .add_host ("github.com" )
306+ .add_path ("sigmavirus24/rfc3986" )
307+ .geturl ()
308+ )
309+ expected = (
310+ "https://sigmavirus24:not-my-re%[email protected] /" 311+ "sigmavirus24/rfc3986"
312+ )
313+ assert expected == uri
0 commit comments