From 395f690f4a24331c4554e2169ac18a15955a4eab Mon Sep 17 00:00:00 2001 From: Allan Lei Date: Sun, 16 Jan 2022 07:08:40 +0800 Subject: [PATCH] Chaining `boltons.urlutils.URL.navigate()` results in `TypeError` (#298) * Add test cases for issue #158 * Fix TypeError w/issue #158 and PR #159 This makes only one more test pass (now only 3 failing) but at least prevents the `TypeError` when attempting to add a `list` and a `tuple`. * Fix: Expected results should be `https://host/b` * Provide parameters via `path` * Move test case after `test_navigate` Co-authored-by: Wil Cooley --- boltons/urlutils.py | 3 ++- tests/test_urlutils.py | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/boltons/urlutils.py b/boltons/urlutils.py index 6016e8ac..7462ed48 100644 --- a/boltons/urlutils.py +++ b/boltons/urlutils.py @@ -685,7 +685,8 @@ def navigate(self, dest): if dest.path.startswith(u'/'): # absolute path new_path_parts = list(dest.path_parts) else: # relative path - new_path_parts = self.path_parts[:-1] + dest.path_parts + new_path_parts = list(self.path_parts[:-1]) \ + + list(dest.path_parts) else: new_path_parts = list(self.path_parts) if not query_params: diff --git a/tests/test_urlutils.py b/tests/test_urlutils.py index a8e15396..2351899f 100644 --- a/tests/test_urlutils.py +++ b/tests/test_urlutils.py @@ -313,6 +313,25 @@ def test_navigate(): assert navd.to_text() == _dest_text +@pytest.mark.parametrize( + ('expected', 'base', 'paths'), [ + ('https://host/b', 'https://host', ('a', '/b', )), + ('https://host/b', 'https://host', ('a', 'b', )), + ('https://host/a/b', 'https://host', ('a/', 'b', )), + ('https://host/b', 'https://host', ('/a', 'b', )), + ('https://host/a/b', 'https://host/a/', (None, 'b', )), + ('https://host/b', 'https://host/a', (None, 'b', )), +]) +def test_chained_navigate(expected, base, paths): + """Chained :meth:`navigate` calls produces correct results.""" + url = URL(base) + + for path in paths: + url = url.navigate(path) + + assert expected == url.to_text() + + # TODO: RFC3986 6.2.3 (not just for query add, either) # def test_add_query(): # url = URL('http://www.example.com')