Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add Url.abs_path and Url.abs_path_query

abs_path_query will be use to build the HTTP request.
  • Loading branch information...
commit fb160e9697478785e5546ee02360c9ea30502886 1 parent 224f21d
@ant0ine ant0ine authored franck cuny committed
Showing with 45 additions and 0 deletions.
  1. +15 −0 http/url.py
  2. +30 −0 tests/test_url.py
View
15 http/url.py
@@ -171,6 +171,21 @@ def __eq__(self, u):
__repr__ = __str__
+ @property
+ def abs_path(self):
+ """Return a string representing the absolute path.
+ ("/" if self.path is empty)"""
+ path = str(self.path)
+ if path == '':
+ path = '/'
+ return path
+
+ @property
+ def abs_path_query(self):
+ """Return the absolute path and query components as a single string.
+ The path and the query are separated by a "?" character."""
+ return self.abs_path + "?" + queryimplode(self.query)
+
path = property(lambda s: s._path, _path_set)
netloc = property(_netloc_get, _netloc_set)
is_absolute = property(lambda s: bool(s.host))
View
30 tests/test_url.py
@@ -132,3 +132,33 @@ def test_canonical(self):
u1 = Url("http://google.com")
u2 = Url("HTTP://google.com")
self.assertEqual(str(u1), str(u2))
+
+ def test_abs_path(self):
+ tests = [
+ {
+ "url": "http://example.org/foo",
+ "expected": "/foo",
+ },
+ {
+ "url": "http://example.org",
+ "expected": "/",
+ },
+ ]
+ for test in tests:
+ u = Url(test["url"])
+ self.assertEquals(u.abs_path, test["expected"])
+
+ def test_abs_path_query(self):
+ tests = [
+ {
+ "url": "http://example.org/foo?bar=1",
+ "expected": "/foo?bar=1",
+ },
+ {
+ "url": "http://example.org?bar=1",
+ "expected": "/?bar=1",
+ },
+ ]
+ for test in tests:
+ u = Url(test["url"])
+ self.assertEquals(u.abs_path_query, test["expected"])
Please sign in to comment.
Something went wrong with that request. Please try again.