class URLObjectRelativeTest(unittest.TestCase): def setUp(self): self.url = URL("https://github.com/zacharyvoase/urlblocks?spam=eggs#foo") def test_relative_with_scheme_returns_the_given_URL(self): assert self.url.relative('http://example.com/abc') == 'http://example.com/abc' def test_relative_with_netloc_returns_the_given_URL_but_preserves_scheme(self): assert self.url.relative('//example.com/abc') == 'https://example.com/abc' def test_relative_with_path_replaces_path_and_removes_query_string_and_fragment(self): assert self.url.relative('another-project') == 'https://github.com/zacharyvoase/another-project' assert self.url.relative('.') == 'https://github.com/zacharyvoase/' assert self.url.relative('/dvxhouse/intessa') == 'https://github.com/dvxhouse/intessa' assert self.url.relative('/dvxhouse/intessa') == 'https://github.com/dvxhouse/intessa' def test_relative_with_empty_string_removes_fragment_but_preserves_query(self): # The empty string is treated as a path meaning 'the current location'. assert self.url.relative('') == self.url.without_fragment() def test_relative_with_query_string_removes_fragment(self): assert self.url.relative('?name=value') == self.url.without_fragment().with_query('name=value') def test_relative_with_fragment_removes_nothing(self): assert self.url.relative('#foobar') == self.url.with_fragment('foobar') def test_compound_relative_urls(self): assert self.url.relative('//example.com/a/b') == 'https://example.com/a/b' assert self.url.relative('//example.com/a/b#bar') == 'https://example.com/a/b#bar' assert self.url.relative('//example.com/a/b?c=d#bar') == 'https://example.com/a/b?c=d#bar' assert self.url.relative('/a/b?c=d#bar') == 'https://github.com/a/b?c=d#bar' assert self.url.relative('?c=d#bar') == 'https://github.com/zacharyvoase/urlblocks?c=d#bar' assert self.url.relative('#bar') == 'https://github.com/zacharyvoase/urlblocks?spam=eggs#bar'