@@ -117,3 +117,69 @@ def test_json_method_doc(self):
117117
118118 def test_json_method_name (self ):
119119 self .assertEqual (self .mock .foo_json .__name__ , 'foo_json' )
120+
121+ class URLTest (unittest .TestCase ):
122+ def test_attributes (self ):
123+ url = utils .URL ('https://example.com/a/b/c?foo=bar&foo=barbar&bar=foo' )
124+ self .assertEqual (str (url ), 'https://example.com/a/b/c?foo=bar&foo=barbar&bar=foo' )
125+ self .assertEqual (repr (url ), "<URL 'https://example.com/a/b/c?foo=bar&foo=barbar&bar=foo'>" )
126+ self .assertEqual (url .scheme , 'https' )
127+ self .assertEqual (url .domain , 'example.com' )
128+ # webob.Response is wrong in environ_from_url(), here it should be example.com
129+ self .assertEqual (url .host , 'example.com:443' )
130+ # so netloc was implemented to replace it
131+ self .assertEqual (url .netloc , 'example.com' )
132+ self .assertEqual (url .host_url , 'https://example.com' )
133+ self .assertEqual (url .path , '/a/b/c' )
134+ self .assertEqual (url .path_url , 'https://example.com/a/b/c' )
135+ self .assertEqual (url .path_qs , '/a/b/c?foo=bar&foo=barbar&bar=foo' )
136+ self .assertEqual (list (url .params .items ()), [('foo' , 'bar' ), ('foo' , 'barbar' ), ('bar' , 'foo' )])
137+ self .assertEqual (url .params ['foo' ], 'barbar' )
138+ self .assertEqual (url .query_string , 'foo=bar&foo=barbar&bar=foo' )
139+
140+ def test_join (self ):
141+ url = utils .URL ('https://example.com/a/b/c?foo=bar&foo=barbar&bar=foo' )
142+ new_url = url .join ('x/y?foofo=bar' )
143+
144+ self .assertEqual (new_url , 'https://example.com/a/b/x/y?foofo=bar' )
145+
146+ def test_match (self ):
147+ url = utils .URL ('https://example.com/a/b/c?foo=bar&foo=barbar&bar=foo' )
148+ self .assertTrue (url .match ('https://' ))
149+ self .assertFalse (url .match ('http://' ))
150+ self .assertEqual (repr (url .match ('http://' )), 'Error -- scheme differs https != http' )
151+ self .assertTrue (url .match ('//example.com' ))
152+ self .assertFalse (url .match ('//a.example.com' ))
153+ self .assertEqual (repr (url .match ('//a.example.com' )), 'Error -- netloc differs example.com != a.example.com' )
154+ self .assertTrue (url .match ('https://example.com' ))
155+
156+ self .assertTrue (url .match ('/a/b/c' ))
157+ self .assertFalse (url .match ('/a/b/c/' ))
158+ self .assertEqual (repr (url .match ('/a/b/c/' )), 'Error -- path differs /a/b/c != /a/b/c/' )
159+ self .assertFalse (url .match ('/x' ))
160+
161+ self .assertTrue (url .match ('https://example.com/a/b/c' ))
162+ self .assertFalse (url .match ('https://example.com/a/b/c/' ))
163+ self .assertFalse (url .match ('https://example.com/x' ))
164+
165+ self .assertTrue (url .match (url ))
166+ self .assertTrue (url .match ('https://example.com/a/b/c?foo=bar&foo=barbar&bar=foo' ))
167+ self .assertTrue (url .match ('https://example.com/a/b/c?foo=bar' ))
168+ self .assertTrue (url .match ('https://example.com/a/b/c?foo=barbar' ))
169+ self .assertFalse (url .match ('https://example.com/a/b/c?foo=bar' , strict = True ))
170+ self .assertEqual (repr (url .match ('https://example.com/a/b/c?foo=bar&bar=foo' , strict = True )), 'Error -- ?foo=barbar was not expected.' )
171+
172+ # multiple errors
173+ self .assertEqual (repr (url .match ('https://example.com/a/b/c?foo=bar' , strict = True )), 'Error -- ?foo=barbar was not expected. ?bar=foo was not expected.' )
174+
175+ self .assertTrue (url .match ('?!foobar' ))
176+ self .assertTrue (url .match ('?bar=?' ))
177+ self .assertTrue (url .match ('?bar=?&foo=*' , strict = True ))
178+ self .assertEqual (repr (url .match ('?!foo' )), 'Error -- foo should be absent, but ?foo=bar&foo=barbar found.' )
179+ self .assertEqual (repr (url .match ('?foo=?' )), 'Error -- foo should have only one value but ?foo=bar&foo=barbar found.' )
180+ self .assertEqual (repr (url .match ('?foobar=?' )), 'Error -- foobar should have only one value but is absent.' )
181+
182+ self .assertEqual (repr (url .match ('?foo=barfoo' )), 'Error -- foo should have value \' barfoo\' but ?foo=bar&foo=barbar found.' )
183+ self .assertEqual (repr (url .match ('?foobar=barfoo' )), 'Error -- foobar should have value \' barfoo\' but is absent.' )
184+
185+
0 commit comments