def test_fragmentEquality(self): """ An URL created with the empty string for a fragment compares equal to an URL created with C{None} for a fragment. """ self.assertEqual(url.URL('http://localhost:1234/#'), url.URL('http://localhost:1234/'))
def test_parent(self): urlpath = url.URL(theurl) self.assertEquals("http://www.foo.com:80/a/nice/path", urlpath.parent()) self.assertEquals( url.URL('http://localhost/').parent(), 'http://localhost') self.assertRaises(IndexError, url.URL('http://localhost').parent)
def test_equality(self): urlpath = url.URL(theurl) self.failUnlessEqual(urlpath, url.URL(theurl)) self.failIfEqual( urlpath, url.URL( 'ftp://www.anotherinvaliddomain.com/foo/bar/baz/?zot=21&zut'))
def test_secure(self): self.assertEquals( url.URL('http://localhost/').secure(), 'https://localhost/') self.assertEquals( url.URL('http://localhost/').secure(True), 'https://localhost/') self.assertEquals( url.URL('https://localhost/').secure(), 'https://localhost/') self.assertEquals( url.URL('https://localhost/').secure(False), 'http://localhost/') self.assertEquals( url.URL('http://localhost/').secure(False), 'http://localhost/') self.assertEquals( url.URL('http://localhost/foo').secure(), 'https://localhost/foo') self.assertEquals( url.URL('http://localhost/foo?bar=1').secure(), 'https://localhost/foo?bar=1') self.assertEquals( url.URL('http://localhost/').secure(port=443), 'https://localhost/') self.assertEquals( url.URL('http://localhost:8080/').secure(port=8443), 'https://localhost:8443/') self.assertEquals( url.URL('https://localhost:8443/').secure(False, 8080), 'http://localhost:8080/')
def test_path_url(self): """ Test that URL.path is, itself, a URL. """ self.assertTrue( isinstance(url.URL('http://localhost/a/b').path, url.URL)) self.assertEquals( url.URL('http://localhost/a/b').path.child('c'), '/a/b/c')
def test_sibling(self): urlpath = url.URL(theurl) self.assertEquals("http://www.foo.com:80/a/nice/path/sister", urlpath.sibling('sister')) # use an url without trailing '/' to check child removal theurl2 = "http://www.foo.com:80/a/nice/path?zot=23&zut" urlpath = url.URL(theurl2) self.assertEquals("http://www.foo.com:80/a/nice/sister", urlpath.sibling('sister'))
def test_add_noquery(self): # from_string is a different code path, test them both self.assertEquals( "http://www.foo.com:80/a/nice/path/?foo=bar", url.URL("http://www.foo.com:80/a/nice/path/").add_query( "foo", "bar")) self.assertEquals( "http://www.foo.com?foo=bar", url.URL("http://www.foo.com").add_query("foo", "bar"))
def test_path(self): """ L{URL.path} should be a C{str} giving the I{path} portion of the URL only. Certain bytes should not be quoted. """ urlpath = url.URL("http://example.com/foo/bar?baz=quux#foobar") self.assertEqual(urlpath.path, "/foo/bar") urlpath = url.URL("http://example.com/foo%2Fbar?baz=quux#foobar") self.assertEqual(urlpath.path, "/foo%2Fbar") urlpath = url.URL("http://example.com/-_.!*'()?baz=quux#foo") self.assertEqual(urlpath.path, "/-_.!*'()")
def test_parseEqualInParamValue(self): S = 'http://localhost/?=x=x=x' u = url.URL(S) self.failUnless(u.query == '=x=x=x') self.failUnless(u.query_list == [('', 'x=x=x')]) self.failUnless(u == S) self.failUnless(u.clone() == S) S = 'http://localhost/?foo=x=x=x&bar=y' u = url.URL(S) self.failUnless(u.query == 'foo=x=x=x&bar=y') self.failUnless(u.query_list == [('foo', 'x=x=x'), ('bar', 'y')]) self.failUnless(u == S) self.failUnless(u.clone() == S)
def test_childs(self): self.assertEquals( url.URL('http://localhost/foo').child('bar'), 'http://localhost/foo/bar') self.assertEquals( url.URL('http://localhost/foo').child('bar', 'woo'), 'http://localhost/foo/bar/woo') self.assertEquals( url.URL('http://localhost/').child('foo', 'bar'), 'http://localhost/foo/bar') self.assertEquals( url.URL('http://localhost').child('foo', 'bar'), 'http://localhost/foo/bar')
def setUp(self): self.host_url = url.URL("http://localhost:1234") self.application_url = self.host_url.child('app') self.url = self.application_url.child('resource').add_query( 'foo', 'bar') self.request = http.Request.blank('/resource?foo=bar', base_url=self.application_url) self.url_accessor = url.URLAccessor(self.request)
def test_fragment(self): urlpath = url.URL(theurl) self.assertEquals( "http://www.foo.com:80/a/nice/path/?zot=23&zut#hiboy", urlpath.anchor("hiboy")) self.assertEquals("http://www.foo.com:80/a/nice/path/?zot=23&zut", urlpath.anchor()) self.assertEquals("http://www.foo.com:80/a/nice/path/?zot=23&zut", urlpath.anchor(''))
def test_properties(self): u = url.URL("http://localhost:1234/a/b/c?d&e=f#g") self.assertEquals(u.scheme, 'http') self.assertEquals(u.netloc, 'localhost:1234') self.assertEquals(u.path, '/a/b/c') self.assertEquals(u.path_segments, ['a', 'b', 'c']) self.assertEquals(u.query, 'd&e=f') self.assertEquals(u.query_list, [('d', None), ('e', 'f')]) self.assertEquals(u.fragment, 'g')
def test_child(self): urlpath = url.URL(theurl) self.assertEquals("http://www.foo.com:80/a/nice/path/gong", urlpath.child('gong')) self.assertEquals("http://www.foo.com:80/a/nice/path/gong%2F", urlpath.child('gong/')) self.assertEquals("http://www.foo.com:80/a/nice/path/gong%2Fdouble", urlpath.child('gong/double')) self.assertEquals("http://www.foo.com:80/a/nice/path/gong%2Fdouble%2F", urlpath.child('gong/double/'))
def test_strangeSegs(self): base = 'http://localhost/' tests = ( (r'/foo/', '%2Ffoo%2F'), (r'c:\foo\bar bar', 'c%3A%5Cfoo%5Cbar%20bar'), (r'&<>', '%26%3C%3E'), (u'!"\N{POUND SIGN}$%^&*()_+'.encode('utf-8'), '!%22%C2%A3%24%25%5E%26*()_%2B'), ) for test, result in tests: u = url.URL(base).child(test) self.assertEquals(u, base + result)
def test_cloneUnchanged(self): """ Verify that L{url.URL.cloneURL} doesn't change any of the arguments it is passed. """ urlpath = url.URL('https://x:1/y?z=1#A') self.assertEqual( urlpath.clone(scheme=urlpath.scheme, netloc=urlpath.netloc, path=urlpath.path, query=urlpath.query, fragment=urlpath.fragment), urlpath)
def test_replace_query(self): urlpath = url.URL(theurl) self.assertEquals("http://www.foo.com:80/a/nice/path/?zot=32&zut", urlpath.replace_query("zot", 32)) # replace name without value with name/value and vice-versa self.assertEquals( "http://www.foo.com:80/a/nice/path/?zot&zut=itworked", urlpath.replace_query("zot").replace_query("zut", "itworked")) # Q: what happens when the query has two values and we replace? # A: we replace both values with a single one self.assertEquals( "http://www.foo.com:80/a/nice/path/?zot=32&zut", urlpath.add_query("zot", "xxx").replace_query("zot", 32))
def test_clear_queries(self): urlpath = url.URL(theurl) self.assertEquals("http://www.foo.com:80/a/nice/path/?zut", urlpath.clear_queries("zot")) self.assertEquals("http://www.foo.com:80/a/nice/path/?zot=23", urlpath.clear_queries("zut")) # something stranger, query with two values, both should get cleared self.assertEquals("http://www.foo.com:80/a/nice/path/?zut", urlpath.add_query("zot", 1971).clear_queries("zot")) # two ways to clear the whole query self.assertEquals("http://www.foo.com:80/a/nice/path/", urlpath.clear_queries("zut").clear_queries("zot")) self.assertEquals("http://www.foo.com:80/a/nice/path/", urlpath.clear_queries())
def test_click(self): urlpath = url.URL(theurl) # a null uri should be valid (return here) self.assertEquals("http://www.foo.com:80/a/nice/path/?zot=23&zut", urlpath.click("")) # a simple relative path remove the query self.assertEquals("http://www.foo.com:80/a/nice/path/click", urlpath.click("click")) # an absolute path replace path and query self.assertEquals("http://www.foo.com:80/click", urlpath.click("/click")) # replace just the query self.assertEquals("http://www.foo.com:80/a/nice/path/?burp", urlpath.click("?burp")) # from a url with no query clicking a url with a query, # the query should be handled properly u = url.URL('http://www.foo.com:80/me/noquery') self.failUnlessEqual('http://www.foo.com:80/me/17?spam=158', u.click('/me/17?spam=158')) # Clicking on a fragment URL keeps all other parts of the current URL. self.assertEquals( url.URL("http://www.foo.com:80/click?a=b").click('#frag'), "http://www.foo.com:80/click?a=b#frag") # Clicking on a fragment URL discards the current fragment. self.assertEquals( url.URL("http://www.foo.com:80/click#foo").click('#bar'), "http://www.foo.com:80/click#bar") # Check that everything from the path onward is removed when the click link # has no path. u = url.URL('http://localhost/foo?abc=def') self.failUnlessEqual(u.click('http://www.python.org'), 'http://www.python.org')
def test_weird_path_segments(self): class Resource(resource.Resource): def __init__(self, segment): self.segment = segment def resource_child(self, request, segments): return self.__class__(segments[0]), segments[1:] def __call__(self, request): return http.ok([('Content-Type', 'text/plain')], self.segment.encode('utf-8')) A = app.RestishApp(Resource('')) assert webtest.TestApp(A).get('/').body == '' assert webtest.TestApp(A).get('/foo').body == 'foo' assert webtest.TestApp(A).get( url.URL('/').child( '*****@*****.**').path).body == '*****@*****.**'
def test_add_query(self): urlpath = url.URL(theurl) self.assertEquals("http://www.foo.com:80/a/nice/path/?zot=23&zut&burp", urlpath.add_query("burp")) self.assertEquals( "http://www.foo.com:80/a/nice/path/?zot=23&zut&burp=xxx", urlpath.add_query("burp", "xxx")) self.assertEquals( "http://www.foo.com:80/a/nice/path/?zot=23&zut&burp=xxx&zing", urlpath.add_query("burp", "xxx").add_query("zing")) # note the inversion! self.assertEquals( "http://www.foo.com:80/a/nice/path/?zot=23&zut&zing&burp=xxx", urlpath.add_query("zing").add_query("burp", "xxx")) # note the two values for the same name self.assertEquals( "http://www.foo.com:80/a/nice/path/?zot=23&zut&burp=xxx&zot=32", urlpath.add_query("burp", "xxx").add_query("zot", 32))
def render_page(self, request, form): C = _store(request) form = category_form(C, self.path, self.referenced_type, request) with C.session() as S: facet_docs = S.docs_by_type(self.model_type) facet_docs = list(facet_docs) assert len(facet_docs) == 1 facet = list(facet_docs)[0] root_url = url.URL('/admin/categories').child(self.path) tree = build_tree(facet, root_url, self.category_path) categories = filter_categories(facet, self.path, self.category_path) form.defaults = {'category': categories} return { 'categories': categories, 'form': form, 'tree': tree, 'facet': self.facet }
def test_roundtrip(self): tests = ( "http://localhost", "http://localhost/", "http://localhost/foo", "http://localhost/foo/", "http://localhost/foo!!bar/", "http://localhost/foo%20bar/", "http://localhost/foo%2Fbar/", "http://localhost/foo?n", "http://localhost/foo?n=v", "http://localhost/foo?n=%2Fa%2Fb", "http://example.com/foo!%40%24bar?b!%40z=123", "http://localhost/asd?a=asd%20sdf%2F345", "http://localhost/#%7F", ) for test in tests: result = url.URL(test) self.assertEquals(test, result)
def test_clickCollapse(self): tests = [ ['http://localhost/', '.', 'http://localhost/'], ['http://localhost/', '..', 'http://localhost/'], ['http://localhost/a/b/c', '.', 'http://localhost/a/b/'], ['http://localhost/a/b/c', '..', 'http://localhost/a/'], ['http://localhost/a/b/c', './d/e', 'http://localhost/a/b/d/e'], ['http://localhost/a/b/c', '../d/e', 'http://localhost/a/d/e'], ['http://localhost/a/b/c', '/./d/e', 'http://localhost/d/e'], ['http://localhost/a/b/c', '/../d/e', 'http://localhost/d/e'], [ 'http://localhost/a/b/c/', '../../d/e/', 'http://localhost/a/d/e/' ], ['http://localhost/a/./c', '../d/e', 'http://localhost/d/e'], ['http://localhost/a/./c/', '../d/e', 'http://localhost/a/d/e'], [ 'http://localhost/a/b/c/d', './e/../f/../g', 'http://localhost/a/b/c/g' ], ['http://localhost/a/b/c', 'd//e', 'http://localhost/a/b/d//e'], ] for start, click, result in tests: self.assertEquals(url.URL(start).click(click), result)
def test_rmq(self): """Check rmq is an alias for remove_query""" u = url.URL("http://localhost:1234/path?p1=foo&p2=bar") assert u.rmq("p1") == "http://localhost:1234/path?p2=bar"
def test_q(self): """Check q is an alias for replace_query""" u = url.URL("http://localhost:1234/path?p=foo") assert u.q("p", "bar") == "http://localhost:1234/path?p=bar"
def test_path_qs(self): path_qs = url.URL('http://localhost:1234/foo?a=b#c').path_qs assert isinstance(path_qs, url.URL) assert path_qs == '/foo?a=b#c'
def test_ne_apples_vs_oranges(self): u = url.URL('http://localhost/') self.failUnless(u != 42, "URL must differ from a number.") self.failUnless(u != object(), "URL must be differ from an object.")
def test_ne_different(self): u1 = url.URL('http://localhost/a') u2 = url.URL('http://localhost/b') self.failUnless(u1 != u2, "%r == %r" % (u1, u2))
def test_ne_similar(self): u1 = url.URL('http://localhost/') u2 = url.URL('http://localhost/') self.failIf(u1 != u2, "%r == %r" % (u1, u2))