def test_default_ports(self): for i, o in [ ('http://*****:*****@www.example.com:80/index.html', 'http://*****:*****@www.example.com/index.html'), ('http://*****:*****@www.example.com:8080/index.html', 'http://*****:*****@www.example.com:8080/index.html'), ('http://*****:*****@www.example.com:443/index.html', 'http://*****:*****@www.example.com:443/index.html'), ('https://*****:*****@www.example.com:443/index.html', 'https://*****:*****@www.example.com/index.html'), ('https://*****:*****@www.example.com:442/index.html', 'https://*****:*****@www.example.com:442/index.html'), ('https://*****:*****@www.example.com:80/index.html', 'https://*****:*****@www.example.com:80/index.html'), ('ftp://*****:*****@www.example.com:21/file.txt', 'ftp://*****:*****@www.example.com/file.txt'), ('ftp://*****:*****@www.example.com:221/file.txt', 'ftp://*****:*****@www.example.com:221/file.txt'), ]: self.assertEqual(strip_url(i, strip_default_port=True, strip_credentials=False), o)
def test_default_ports_creds_off(self): for i, o in [ ('http://*****:*****@www.example.com:80/index.html?somekey=somevalue#section', 'http://www.example.com/index.html?somekey=somevalue'), ('http://*****:*****@www.example.com:8080/index.html#section', 'http://www.example.com:8080/index.html'), ('http://*****:*****@www.example.com:443/index.html?somekey=somevalue&someotherkey=sov#section', 'http://www.example.com:443/index.html?somekey=somevalue&someotherkey=sov'), ('https://*****:*****@www.example.com:443/index.html', 'https://www.example.com/index.html'), ('https://*****:*****@www.example.com:442/index.html', 'https://www.example.com:442/index.html'), ('https://*****:*****@www.example.com:80/index.html', 'https://www.example.com:80/index.html'), ('ftp://*****:*****@www.example.com:21/file.txt', 'ftp://www.example.com/file.txt'), ('ftp://*****:*****@www.example.com:221/file.txt', 'ftp://www.example.com:221/file.txt'), ]: self.assertEqual(strip_url(i), o)
def test_default_ports_keep(self): for i, o in [ ('http://*****:*****@www.example.com:80/index.html?somekey=somevalue&someotherkey=sov#section', 'http://*****:*****@www.example.com:80/index.html?somekey=somevalue&someotherkey=sov'), ('http://*****:*****@www.example.com:8080/index.html?somekey=somevalue&someotherkey=sov#section', 'http://*****:*****@www.example.com:8080/index.html?somekey=somevalue&someotherkey=sov'), ('http://*****:*****@www.example.com:443/index.html', 'http://*****:*****@www.example.com:443/index.html'), ('https://*****:*****@www.example.com:443/index.html', 'https://*****:*****@www.example.com:443/index.html'), ('https://*****:*****@www.example.com:442/index.html', 'https://*****:*****@www.example.com:442/index.html'), ('https://*****:*****@www.example.com:80/index.html', 'https://*****:*****@www.example.com:80/index.html'), ('ftp://*****:*****@www.example.com:21/file.txt', 'ftp://*****:*****@www.example.com:21/file.txt'), ('ftp://*****:*****@www.example.com:221/file.txt', 'ftp://*****:*****@www.example.com:221/file.txt'), ]: self.assertEqual(strip_url(i, strip_default_port=False, strip_credentials=False), o)
def test_path(self): for input_url, origin, output_url in [ ('http://www.example.com/', False, 'http://www.example.com/'), ('http://www.example.com', False, 'http://www.example.com'), ('http://www.example.com', True, 'http://www.example.com/'), ]: self.assertEqual(strip_url(input_url, origin_only=origin), output_url)
def test_credentials(self): for i, o in [ ('http://[email protected]/index.html?somekey=somevalue#section', 'http://www.example.com/index.html?somekey=somevalue'), ('https://username:@www.example.com/index.html?somekey=somevalue#section', 'https://www.example.com/index.html?somekey=somevalue'), ('ftp://*****:*****@www.example.com/index.html?somekey=somevalue#section', 'ftp://www.example.com/index.html?somekey=somevalue'), ]: self.assertEqual(strip_url(i, strip_credentials=True), o)
def test_origin_only(self): for i, o in [ ('http://*****:*****@www.example.com/index.html', 'http://www.example.com/'), ('http://*****:*****@www.example.com:80/foo/bar?query=value#somefrag', 'http://www.example.com/'), ('http://*****:*****@www.example.com:8008/foo/bar?query=value#somefrag', 'http://www.example.com:8008/'), ('https://*****:*****@www.example.com:443/index.html', 'https://www.example.com/'), ]: self.assertEqual(strip_url(i, origin_only=True), o)
def test_credentials_encoded_delims(self): for i, o in [ # user: "******" # password: none ('http://username%[email protected]/index.html?somekey=somevalue#section', 'http://www.example.com/index.html?somekey=somevalue'), # user: "******" # password: "" ('https://username%3Apass:@www.example.com/index.html?somekey=somevalue#section', 'https://www.example.com/index.html?somekey=somevalue'), # user: "******" # password: "******" ('ftp://*****:*****@www.example.com/index.html?somekey=somevalue#section', 'ftp://www.example.com/index.html?somekey=somevalue'), ]: self.assertEqual(strip_url(i, strip_credentials=True), o)
def strip_url(self, url, origin_only=False): """ https://www.w3.org/TR/referrer-policy/#strip-url If url is null, return no referrer. If url's scheme is a local scheme, then return no referrer. Set url's username to the empty string. Set url's password to null. Set url's fragment to null. If the origin-only flag is true, then: Set url's path to null. Set url's query to null. Return url. """ if not url: return None return strip_url(url, strip_credentials=True, strip_fragment=True, strip_default_port=True, origin_only=origin_only)
def test_noop(self): self.assertEqual(strip_url( 'http://www.example.com/index.html'), 'http://www.example.com/index.html')
def test_fragments(self): self.assertEqual(strip_url( 'http://www.example.com/index.html?somekey=somevalue#section', strip_fragment=False), 'http://www.example.com/index.html?somekey=somevalue#section')
def test_noop_query_string(self): self.assertEqual(strip_url( 'http://www.example.com/index.html?somekey=somevalue'), 'http://www.example.com/index.html?somekey=somevalue')