diff --git a/Lib/test/test_urllib2.py b/Lib/test/test_urllib2.py index 9db23e6ce04bd5..680b7d619f5fef 100644 --- a/Lib/test/test_urllib2.py +++ b/Lib/test/test_urllib2.py @@ -145,6 +145,8 @@ def test_password_manager(self): mgr = urllib.request.HTTPPasswordMgr() add = mgr.add_password find_user_pass = mgr.find_user_password + is_suburi = mgr.is_suburi + reduce_uri = mgr.reduce_uri add("Some Realm", "http://example.com/", "joe", "password") add("Some Realm", "http://example.com/ni", "ni", "ni") @@ -211,6 +213,25 @@ def test_password_manager(self): self.assertEqual(find_user_pass("Some Realm", "e.example.com:3128"), ('5', 'e')) + # is_suburi + + true = self.assertTrue + false = self.assertFalse + true(is_suburi(reduce_uri('http://example.com'), + reduce_uri('http://example.com/sub_dir'))) + true(is_suburi(reduce_uri('http://example.com/sub_dir'), + reduce_uri('http://example.com/sub_dir/sub_dir'))) + false(is_suburi(reduce_uri('http://example.com/sub_dir/sub_dir'), + reduce_uri('http://example.com/sub_dir'))) + false(is_suburi(reduce_uri('http://example.com/second_dir'), + reduce_uri('http://example.com/first_dir/second_dir'))) + false(is_suburi(reduce_uri('http://example.com/sub_dir'), + reduce_uri('http://example.com/sub'))) + false(is_suburi(reduce_uri('http://example.com/sub_dir'), + reduce_uri('http://exmaple.com/sub_dir_diff'))) + false(is_suburi(reduce_uri('http://example.com/sub_dir_diff'), + reduce_uri('http://exmaple.com/sub_dir_second_diff'))) + def test_password_manager_default_port(self): """ The point to note here is that we can't guess the default port if diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py index e5febe61f556d3..227485d336c3f6 100644 --- a/Lib/urllib/request.py +++ b/Lib/urllib/request.py @@ -881,15 +881,15 @@ def reduce_uri(self, uri, default_port=True): return authority, path def is_suburi(self, base, test): - """Check if test is below base in a URI tree + """Check if test is equal or below base in a URI tree Both args must be URIs in reduced form. """ if base == test: return True - if base[0] != test[0]: + if base[0] != test[0] or len(test[1]) < len(base[1]): return False - common = posixpath.commonprefix((base[1], test[1])) + common = posixpath.commonpath((base[1], test[1])) if len(common) == len(base[1]): return True return False diff --git a/Misc/ACKS b/Misc/ACKS index 211455b4dfc3c2..b8c901407acf75 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -552,6 +552,7 @@ Stefan Franke Martin Franklin Kent Frazier Bruce Frederiksen +Yair Frid Jason Fried Robin Friedrich Bradley Froehle diff --git a/Misc/NEWS.d/next/Library/2021-01-09-22-36-01.bpo-42766.6ybdUr.rst b/Misc/NEWS.d/next/Library/2021-01-09-22-36-01.bpo-42766.6ybdUr.rst new file mode 100644 index 00000000000000..fe22bbb1035085 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2021-01-09-22-36-01.bpo-42766.6ybdUr.rst @@ -0,0 +1,3 @@ +Fixed 2 bugs in urllib.request.HTTPPasswordMgr.is_suburi, added some tests +for it. :func:`urllib.request.HTTPPasswordMgr.is_suburi`. Patch by Yair +Frid.`