Download tasks/data/forward/adaptedas__url_parser.2635bab/solution/golden.patch from Lego-X/ProgramGen: direct link, hf CLI and curl.
- Browser
- Download file 22.6 kB
-
https://huggingface.co/datasets/Lego-X/ProgramGen/resolve/main/tasks/data/forward/adaptedas__url_parser.2635bab/solution/golden.patch
- Command line
-
hf download hf://datasets/Lego-X/ProgramGen/tasks/data/forward/adaptedas__url_parser.2635bab/solution/golden.patch
-
curl -L -o golden.patch https://huggingface.co/datasets/Lego-X/ProgramGen/resolve/main/tasks/data/forward/adaptedas__url_parser.2635bab/solution/golden.patch
22.6 kB
| diff --git a/url_parser/__init__.py b/url_parser/__init__.py | |
| new file mode 100644 | |
| index 0000000000000000000000000000000000000000..876ffefadcf7828ab2576fa799a8f70cb4b5e54f | |
| --- /dev/null | |
| +++ b/url_parser/__init__.py | |
| +import re | |
| +import warnings | |
| +from collections import namedtuple | |
| + | |
| +from url_parser.public_suffix_list import PublicSuffixList | |
| + | |
| +UrlObject = namedtuple( | |
| + 'UrlObject', [ | |
| + 'protocol', | |
| + 'www', | |
| + 'sub_domain', | |
| + 'domain', | |
| + 'top_domain', | |
| + 'path', | |
| + 'dir', | |
| + 'file', | |
| + 'fragment', | |
| + 'query' | |
| + ]) | |
| + | |
| + | |
| +def _split_query_group(query_groups: list) -> dict: | |
| + result = dict() | |
| + | |
| + for query_group in query_groups: | |
| + query = query_group.split('=') | |
| + | |
| + if len(query) == 1: | |
| + result[query[0]] = None | |
| + continue | |
| + | |
| + result[query[0]] = query[1] | |
| + | |
| + return result | |
| + | |
| + | |
| +def _parse_url_with_top_domain(url, top_domain): | |
| + regex = r"^(?:(?P<protocol>[\w\d]+)(?:\:\/\/))?" \ | |
| + r"(?P<sub_domain>" \ | |
| + r"(?P<www>(?:www)?)(?:\.?)" \ | |
| + r"(?:(?:[\w\d-]+|\.)*?)?" \ | |
| + r")(?:\.?)" \ | |
| + r"(?P<domain>[^./]+(?=\.))\." \ | |
| + r"(?P<top_domain>" + re.escape(top_domain) + r"(?![^/?#]))" \ | |
| + r"(?P<path>" \ | |
| + r"(?P<dir>\/(?:[^/\r\n]+(?:/))+)?" \ | |
| + r"(?:\/?)(?P<file>[^?#\r\n]+)?" \ | |
| + r")?" \ | |
| + r"(?:\#(?P<fragment>[^#?\r\n]*))?" \ | |
| + r"(?:\?(?P<query>.*(?=$)))*$" | |
| + | |
| + dict_data = { | |
| + 'protocol': None, | |
| + 'www': None, | |
| + 'sub_domain': None, | |
| + 'domain': None, | |
| + 'top_domain': None, | |
| + 'path': None, | |
| + 'dir': None, | |
| + 'file': None, | |
| + 'fragment': None, | |
| + 'query': None, | |
| + } | |
| + | |
| + match = re.search(regex, url) | |
| + | |
| + dict_data['protocol'] = match.group('protocol') if match.group('protocol') else None | |
| + dict_data['www'] = match.group('www') if match.group('www') else None | |
| + dict_data['sub_domain'] = match.group('sub_domain') if match.group('sub_domain') else None | |
| + dict_data['domain'] = match.group('domain') | |
| + dict_data['top_domain'] = top_domain | |
| + dict_data['path'] = match.group('path') if match.group('path') else None | |
| + dict_data['dir'] = match.group('dir') if match.group('dir') else None | |
| + dict_data['file'] = match.group('file') if match.group('file') else None | |
| + dict_data['fragment'] = match.group('fragment') if match.group('fragment') else None | |
| + | |
| + query = match.group('query') if match.group('query') else None | |
| + | |
| + if query is not None: | |
| + query_groups = query.split('&') | |
| + query = _split_query_group(query_groups) | |
| + dict_data['query'] = query | |
| + | |
| + return dict_data | |
| + | |
| + | |
| +def _parse_url_with_public_suffix(url): | |
| + public_suffix = PublicSuffixList.get_list() | |
| + public_suffix.sort() | |
| + | |
| + domain_regex = r"(?:^|\/)(?P<domain>[^:/#?]+)(?:[/#?]|$)" | |
| + match = re.search(domain_regex, url) | |
| + domain = match.group('domain') | |
| + domain_parts = domain.split('.') | |
| + | |
| + top_domain = None | |
| + | |
| + for i in range(len(domain_parts)): | |
| + tail_gram = domain_parts[i:len(domain_parts)] | |
| + tail_gram = '.'.join(tail_gram) | |
| + | |
| + if tail_gram in public_suffix: | |
| + top_domain = tail_gram | |
| + break | |
| + | |
| + data = _parse_url_with_top_domain(url, top_domain) | |
| + | |
| + return data | |
| + | |
| + | |
| +def get_base_url(url: str) -> str: | |
| + url = get_url(url) | |
| + protocol = str(url.protocol) + '://' if url.protocol is not None else 'http://' | |
| + www = 'www.' if url.www is not None else '' | |
| + sub_domain = str(url.sub_domain) + '.' if url.sub_domain is not None and url.sub_domain != 'www.' else '' | |
| + return protocol + www + sub_domain + url.domain + '.' + url.top_domain | |
| + | |
| + | |
| +def get_url(url: str) -> UrlObject: | |
| + data = _parse_url_with_public_suffix(url) | |
| + | |
| + object_data = UrlObject( | |
| + protocol=data['protocol'], | |
| + www=data['www'], | |
| + sub_domain=data['sub_domain'], | |
| + domain=data['domain'], | |
| + top_domain=data['top_domain'], | |
| + path=data['path'], | |
| + dir=data['dir'], | |
| + file=data['file'], | |
| + fragment=data['fragment'], | |
| + query=data['query'], | |
| + ) | |
| + | |
| + return object_data | |
| + | |
| + | |
| +def parse_url(url: str) -> dict: | |
| + warnings.warn( | |
| + "parse_url is deprecated, use get_url instead", | |
| + DeprecationWarning | |
| + ) | |
| + | |
| + data = get_url(url) | |
| + return data._asdict() | |
| diff --git a/url_parser/public_suffix_list.py b/url_parser/public_suffix_list.py | |
| new file mode 100644 | |
| index 0000000000000000000000000000000000000000..0a2034c46be981467f394f4eb85988d5a9a2be2a | |
| --- /dev/null | |
| +++ b/url_parser/public_suffix_list.py | |
| +import os | |
| + | |
| + | |
| +class PublicSuffixList: | |
| + _public_suffix_list = None | |
| + | |
| + @staticmethod | |
| + def get_list(): | |
| + if PublicSuffixList._public_suffix_list is not None: | |
| + return PublicSuffixList._public_suffix_list | |
| + | |
| + public_suffix_list = [] | |
| + | |
| + dir_path = os.path.dirname(os.path.realpath(__file__)) | |
| + dat_file = f'{dir_path}/public_suffix_list.dat' | |
| + | |
| + with open(dat_file, encoding='utf-8') as file: | |
| + data = file.readlines() | |
| + | |
| + for line in data: | |
| + if line[0:2] == '//': | |
| + continue | |
| + | |
| + line = line.replace('\n', '') | |
| + line = line.replace('\r', '') | |
| + | |
| + if line == '': | |
| + continue | |
| + | |
| + public_suffix_list.append(line) | |
| + | |
| + PublicSuffixList._public_suffix_list = public_suffix_list | |
| + | |
| + return PublicSuffixList._public_suffix_list | |
| diff --git a/url_parser/tests/test_url_parser.py b/url_parser/tests/test_url_parser.py | |
| new file mode 100644 | |
| index 0000000000000000000000000000000000000000..b0f21426d55dff856903d65a579912249f34b63c | |
| --- /dev/null | |
| +++ b/url_parser/tests/test_url_parser.py | |
| +from unittest import TestCase | |
| + | |
| +import url_parser | |
| + | |
| + | |
| +class TestUrlParser(TestCase): | |
| + def test_parses_url_without_www(self): | |
| + url = 'example.com' | |
| + result = url_parser.parse_url(url) | |
| + self.assertEqual(result['domain'], 'example') | |
| + self.assertEqual(result['top_domain'], 'com') | |
| + | |
| + def test_parses_url_with_www(self): | |
| + url = 'www.example.com' | |
| + result = url_parser.parse_url(url) | |
| + self.assertEqual(result['domain'], 'example') | |
| + self.assertEqual(result['top_domain'], 'com') | |
| + | |
| + def test_returns_null_if_protocol_is_missing(self): | |
| + url = 'www.example.com' | |
| + result = url_parser.parse_url(url) | |
| + self.assertIsNone(result['protocol']) | |
| + | |
| + def test_returns_null_if_www_is_missing(self): | |
| + url = 'http://example.com' | |
| + result = url_parser.parse_url(url) | |
| + self.assertIsNone(result['www']) | |
| + | |
| + def test_removes_extra_dot_from_www(self): | |
| + url = 'http://www..example.com' | |
| + result = url_parser.parse_url(url) | |
| + has_dot = '.' in result['www'] | |
| + self.assertFalse(has_dot) | |
| + | |
| + def test_returns_null_if_sub_domain_is_missing(self): | |
| + url = 'http://example.com' | |
| + result = url_parser.parse_url(url) | |
| + self.assertIsNone(result['sub_domain']) | |
| + | |
| + def test_finds_sub_domain(self): | |
| + url = 'mysubdomain.example.com' | |
| + result = url_parser.parse_url(url) | |
| + self.assertEqual(result['sub_domain'], 'mysubdomain') | |
| + | |
| + def test_finds_multiple_subdomains(self): | |
| + url = 'my.subdomain.example.com' | |
| + result = url_parser.parse_url(url) | |
| + self.assertEqual(result['sub_domain'], 'my.subdomain') | |
| + | |
| + def test_finds_protocol(self): | |
| + url = 'http://mysubdomain.example.com' | |
| + result = url_parser.parse_url(url) | |
| + self.assertEqual(result['protocol'], 'http') | |
| + | |
| + url = 'https://mysubdomain.example.com' | |
| + result = url_parser.parse_url(url) | |
| + self.assertEqual(result['protocol'], 'https') | |
| + | |
| + url = 'ftp://mysubdomain.example.com' | |
| + result = url_parser.parse_url(url) | |
| + self.assertEqual(result['protocol'], 'ftp') | |
| + | |
| + def test_finds_dir(self): | |
| + url = 'http://mysubdomain.example.com/folder/' | |
| + result = url_parser.parse_url(url) | |
| + self.assertEqual(result['dir'], '/folder/') | |
| + | |
| + url = 'http://mysubdomain.example.com/multiple/folders/' | |
| + result = url_parser.parse_url(url) | |
| + self.assertEqual(result['dir'], '/multiple/folders/') | |
| + | |
| + url = 'http://mysubdomain.example.com/multiple/folders/with_a_file.js' | |
| + result = url_parser.parse_url(url) | |
| + self.assertEqual(result['dir'], '/multiple/folders/') | |
| + | |
| + def test_does_not_mistake_file_for_dir(self): | |
| + url = 'http://mysubdomain.example.com/folder/test' | |
| + result = url_parser.parse_url(url) | |
| + self.assertEqual(result['dir'], '/folder/') | |
| + self.assertNotEqual(result['dir'], '/folder/test') | |
| + | |
| + def test_finds_domain(self): | |
| + url = 'http://mysubdomain.example.com' | |
| + result = url_parser.parse_url(url) | |
| + self.assertEqual(result['domain'], 'example') | |
| + | |
| + def test_finds_top_domain(self): | |
| + url = 'http://mysubdomain.example.com' | |
| + result = url_parser.parse_url(url) | |
| + self.assertEqual(result['top_domain'], 'com') | |
| + | |
| + url = 'http://mysubdomain.example.co.uk' | |
| + result = url_parser.parse_url(url) | |
| + self.assertEqual(result['top_domain'], 'co.uk') | |
| + | |
| + def test_finds_file(self): | |
| + url = 'http://mysubdomain.example.com/cool.jpg' | |
| + result = url_parser.parse_url(url) | |
| + self.assertEqual(result['file'], 'cool.jpg') | |
| + | |
| + url = 'http://mysubdomain.example.com/directory/here/sample.mp4' | |
| + result = url_parser.parse_url(url) | |
| + self.assertEqual(result['file'], 'sample.mp4') | |
| + | |
| + def test_finds_path(self): | |
| + url = 'http://mysubdomain.example.com/path' | |
| + result = url_parser.parse_url(url) | |
| + self.assertEqual(result['path'], '/path') | |
| + | |
| + url = 'http://mysubdomain.example.com/this/is/the/path' | |
| + result = url_parser.parse_url(url) | |
| + self.assertEqual(result['path'], '/this/is/the/path') | |
| + | |
| + url = 'http://mysubdomain.example.com/path/with/file.js' | |
| + result = url_parser.parse_url(url) | |
| + self.assertEqual(result['path'], '/path/with/file.js') | |
| + | |
| + def test_finds_fragment(self): | |
| + url = 'http://mysubdomain.example.com#my_fragment' | |
| + result = url_parser.parse_url(url) | |
| + self.assertEqual(result['fragment'], 'my_fragment') | |
| + | |
| + url = 'http://mysubdomain.example.com/path/#my_fragment' | |
| + result = url_parser.parse_url(url) | |
| + self.assertEqual(result['fragment'], 'my_fragment') | |
| + | |
| + url = 'http://mysubdomain.example.com/path/file.js#my_fragment' | |
| + result = url_parser.parse_url(url) | |
| + self.assertEqual(result['fragment'], 'my_fragment') | |
| + | |
| + url = 'http://mysubdomain.example.com#my_fragment?myargs=test' | |
| + result = url_parser.parse_url(url) | |
| + self.assertEqual(result['fragment'], 'my_fragment') | |
| + | |
| + url = 'http://mysubdomain.example.com/test/path.js#my_fragment?myargs=test' | |
| + result = url_parser.parse_url(url) | |
| + self.assertEqual(result['fragment'], 'my_fragment') | |
| + | |
| + def test_finds_query(self): | |
| + url = 'http://mysubdomain.example.com?myquery=test' | |
| + result = url_parser.parse_url(url) | |
| + self.assertEqual(result['query']['myquery'], 'test') | |
| + | |
| + url = 'http://mysubdomain.example.com?myquery=test&one=two&test' | |
| + result = url_parser.parse_url(url) | |
| + self.assertEqual(result['query']['myquery'], 'test') | |
| + self.assertEqual(result['query']['one'], 'two') | |
| + self.assertIsNone(result['query']['test']) | |
| + | |
| + url = 'http://mysubdomain.example.com/file.js?myquery=test&one=two' | |
| + result = url_parser.parse_url(url) | |
| + self.assertEqual(result['query']['myquery'], 'test') | |
| + self.assertEqual(result['query']['one'], 'two') | |
| + | |
| + url = 'http://mysubdomain.example.com/path/and/file.js?myquery=test&one=two' | |
| + result = url_parser.parse_url(url) | |
| + self.assertEqual(result['query']['myquery'], 'test') | |
| + self.assertEqual(result['query']['one'], 'two') | |
| + | |
| + url = 'http://mysubdomain.example.com/path/?myquery=test&one=two' | |
| + result = url_parser.parse_url(url) | |
| + self.assertEqual(result['query']['myquery'], 'test') | |
| + self.assertEqual(result['query']['one'], 'two') | |
| + | |
| + def test_catastrophic_backtracking(self): | |
| + url = 'http://very_long-and-complixated_subdomaind-for-page.mywebpageishere.com/' | |
| + result = url_parser.parse_url(url) | |
| + self.assertEqual(result['top_domain'], 'com') | |
| + self.assertEqual(result['domain'], 'mywebpageishere') | |
| + self.assertEqual(result['sub_domain'], 'very_long-and-complixated_subdomaind-for-page') | |
| + | |
| + def test_domain_that_starts_with_same_letters_as_top_domain(self): | |
| + url = 'http://domains-stars-with-same-top-domain.nogo.no/' | |
| + result = url_parser.parse_url(url) | |
| + self.assertEqual(result['top_domain'], 'no') | |
| + self.assertEqual(result['domain'], 'nogo') | |
| + self.assertEqual(result['sub_domain'], 'domains-stars-with-same-top-domain') | |
| + | |
| + def test_domain_that_includes_a_top_domain_in_sub_domain(self): | |
| + url = 'http://test.com.hello.nogo.no/' | |
| + result = url_parser.parse_url(url) | |
| + self.assertEqual(result['top_domain'], 'no') | |
| + self.assertEqual(result['domain'], 'nogo') | |
| + self.assertEqual(result['sub_domain'], 'test.com.hello') | |
| + | |
| + def test_domain_that_includes_a_top_domain_in_query(self): | |
| + url = 'http://test.com.hello.nogo.no?my_query_domain=www.test.com' | |
| + result = url_parser.parse_url(url) | |
| + self.assertEqual(result['top_domain'], 'no') | |
| + self.assertEqual(result['domain'], 'nogo') | |
| + self.assertEqual(result['sub_domain'], 'test.com.hello') | |
| + self.assertEqual(result['query']['my_query_domain'], 'www.test.com') | |
| + | |
| + | |
| +class TestGetUrl(TestCase): | |
| + def test_parses_url_without_www(self): | |
| + url = 'example.com' | |
| + result = url_parser.get_url(url) | |
| + self.assertEqual(result.domain, 'example') | |
| + self.assertEqual(result.top_domain, 'com') | |
| + | |
| + def test_parses_url_with_www(self): | |
| + url = 'www.example.com' | |
| + result = url_parser.get_url(url) | |
| + self.assertEqual(result.domain, 'example') | |
| + self.assertEqual(result.top_domain, 'com') | |
| + | |
| + def test_returns_null_if_protocol_is_missing(self): | |
| + url = 'www.example.com' | |
| + result = url_parser.get_url(url) | |
| + self.assertIsNone(result.protocol) | |
| + | |
| + def test_returns_null_if_www_is_missing(self): | |
| + url = 'http://example.com' | |
| + result = url_parser.get_url(url) | |
| + self.assertIsNone(result.www) | |
| + | |
| + def test_removes_extra_dot_from_www(self): | |
| + url = 'http://www..example.com' | |
| + result = url_parser.get_url(url) | |
| + has_dot = '.' in result.www | |
| + self.assertFalse(has_dot) | |
| + | |
| + def test_returns_null_if_sub_domain_is_missing(self): | |
| + url = 'http://example.com' | |
| + result = url_parser.get_url(url) | |
| + self.assertIsNone(result.sub_domain) | |
| + | |
| + def test_finds_sub_domain(self): | |
| + url = 'mysubdomain.example.com' | |
| + result = url_parser.get_url(url) | |
| + self.assertEqual(result.sub_domain, 'mysubdomain') | |
| + | |
| + def test_finds_multiple_subdomains(self): | |
| + url = 'my.subdomain.example.com' | |
| + result = url_parser.get_url(url) | |
| + self.assertEqual(result.sub_domain, 'my.subdomain') | |
| + | |
| + def test_finds_protocol(self): | |
| + url = 'http://mysubdomain.example.com' | |
| + result = url_parser.get_url(url) | |
| + self.assertEqual(result.protocol, 'http') | |
| + | |
| + url = 'https://mysubdomain.example.com' | |
| + result = url_parser.get_url(url) | |
| + self.assertEqual(result.protocol, 'https') | |
| + | |
| + url = 'ftp://mysubdomain.example.com' | |
| + result = url_parser.get_url(url) | |
| + self.assertEqual(result.protocol, 'ftp') | |
| + | |
| + def test_finds_dir(self): | |
| + url = 'http://mysubdomain.example.com/folder/' | |
| + result = url_parser.get_url(url) | |
| + self.assertEqual(result.dir, '/folder/') | |
| + | |
| + url = 'http://mysubdomain.example.com/multiple/folders/' | |
| + result = url_parser.get_url(url) | |
| + self.assertEqual(result.dir, '/multiple/folders/') | |
| + | |
| + url = 'http://mysubdomain.example.com/multiple/folders/with_a_file.js' | |
| + result = url_parser.get_url(url) | |
| + self.assertEqual(result.dir, '/multiple/folders/') | |
| + | |
| + def test_does_not_mistake_file_for_dir(self): | |
| + url = 'http://mysubdomain.example.com/folder/test' | |
| + result = url_parser.get_url(url) | |
| + self.assertEqual(result.dir, '/folder/') | |
| + self.assertNotEqual(result.dir, '/folder/test') | |
| + | |
| + def test_finds_domain(self): | |
| + url = 'http://mysubdomain.example.com' | |
| + result = url_parser.get_url(url) | |
| + self.assertEqual(result.domain, 'example') | |
| + | |
| + def test_finds_top_domain(self): | |
| + url = 'http://mysubdomain.example.com' | |
| + result = url_parser.get_url(url) | |
| + self.assertEqual(result.top_domain, 'com') | |
| + | |
| + url = 'http://mysubdomain.example.co.uk' | |
| + result = url_parser.get_url(url) | |
| + self.assertEqual(result.top_domain, 'co.uk') | |
| + | |
| + def test_finds_file(self): | |
| + url = 'http://mysubdomain.example.com/cool.jpg' | |
| + result = url_parser.get_url(url) | |
| + self.assertEqual(result.file, 'cool.jpg') | |
| + | |
| + url = 'http://mysubdomain.example.com/directory/here/sample.mp4' | |
| + result = url_parser.get_url(url) | |
| + self.assertEqual(result.file, 'sample.mp4') | |
| + | |
| + def test_finds_path(self): | |
| + url = 'http://mysubdomain.example.com/path' | |
| + result = url_parser.get_url(url) | |
| + self.assertEqual(result.path, '/path') | |
| + | |
| + url = 'http://mysubdomain.example.com/this/is/the/path' | |
| + result = url_parser.get_url(url) | |
| + self.assertEqual(result.path, '/this/is/the/path') | |
| + | |
| + url = 'http://mysubdomain.example.com/path/with/file.js' | |
| + result = url_parser.get_url(url) | |
| + self.assertEqual(result.path, '/path/with/file.js') | |
| + | |
| + def test_finds_fragment(self): | |
| + url = 'http://mysubdomain.example.com#my_fragment' | |
| + result = url_parser.get_url(url) | |
| + self.assertEqual(result.fragment, 'my_fragment') | |
| + | |
| + url = 'http://mysubdomain.example.com/path/#my_fragment' | |
| + result = url_parser.get_url(url) | |
| + self.assertEqual(result.fragment, 'my_fragment') | |
| + | |
| + url = 'http://mysubdomain.example.com/path/file.js#my_fragment' | |
| + result = url_parser.get_url(url) | |
| + self.assertEqual(result.fragment, 'my_fragment') | |
| + | |
| + url = 'http://mysubdomain.example.com#my_fragment?myargs=test' | |
| + result = url_parser.get_url(url) | |
| + self.assertEqual(result.fragment, 'my_fragment') | |
| + | |
| + url = 'http://mysubdomain.example.com/test/path.js#my_fragment?myargs=test' | |
| + result = url_parser.get_url(url) | |
| + self.assertEqual(result.fragment, 'my_fragment') | |
| + | |
| + def test_finds_query(self): | |
| + url = 'http://mysubdomain.example.com?myquery=test' | |
| + result = url_parser.get_url(url) | |
| + self.assertEqual(result.query['myquery'], 'test') | |
| + | |
| + url = 'http://mysubdomain.example.com?myquery=test&one=two&test' | |
| + result = url_parser.get_url(url) | |
| + self.assertEqual(result.query['myquery'], 'test') | |
| + self.assertEqual(result.query['one'], 'two') | |
| + self.assertIsNone(result.query['test']) | |
| + | |
| + url = 'http://mysubdomain.example.com/file.js?myquery=test&one=two' | |
| + result = url_parser.get_url(url) | |
| + self.assertEqual(result.query['myquery'], 'test') | |
| + self.assertEqual(result.query['one'], 'two') | |
| + | |
| + url = 'http://mysubdomain.example.com/path/and/file.js?myquery=test&one=two' | |
| + result = url_parser.get_url(url) | |
| + self.assertEqual(result.query['myquery'], 'test') | |
| + self.assertEqual(result.query['one'], 'two') | |
| + | |
| + url = 'http://mysubdomain.example.com/path/?myquery=test&one=two' | |
| + result = url_parser.get_url(url) | |
| + self.assertEqual(result.query['myquery'], 'test') | |
| + self.assertEqual(result.query['one'], 'two') | |
| + | |
| + | |
| +class TestGetBasicUrl(TestCase): | |
| + def test_basic_url(self): | |
| + url = 'http://example.com' | |
| + result = url_parser.get_base_url(url) | |
| + self.assertEqual(result, 'http://example.com') | |
| + | |
| + url = 'https://example.com' | |
| + result = url_parser.get_base_url(url) | |
| + self.assertEqual(result, 'https://example.com') | |
| + | |
| + url = 'https://www.example.com' | |
| + result = url_parser.get_base_url(url) | |
| + self.assertEqual(result, 'https://www.example.com') | |
| + | |
| + url = 'example.com' | |
| + result = url_parser.get_base_url(url) | |
| + self.assertEqual(result, 'http://example.com') | |
| + | |
| + def test_sub_domain_basic_url(self): | |
| + url = 'http://mysubdomain.example.com' | |
| + result = url_parser.get_base_url(url) | |
| + self.assertEqual(result, 'http://mysubdomain.example.com') | |
| + | |
| + url = 'https://mysubdomain.example.com' | |
| + result = url_parser.get_base_url(url) | |
| + self.assertEqual(result, 'https://mysubdomain.example.com') | |
| + | |
| + url = 'ftp://mysubdomain.example.com' | |
| + result = url_parser.get_base_url(url) | |
| + self.assertEqual(result, 'ftp://mysubdomain.example.com') | |
| + | |
| + def test_path_url(self): | |
| + url = 'https://mysubdomain.example.com/path/to/wisdom' | |
| + result = url_parser.get_base_url(url) | |
| + self.assertEqual(result, 'https://mysubdomain.example.com') | |
| + | |
| + def test_long_url(self): | |
| + url = 'https://mysubdomain.example.com/path/to/wisdom?query=2&this=3' | |
| + result = url_parser.get_base_url(url) | |
| + self.assertEqual(result, 'https://mysubdomain.example.com') | |
| + | |
| + url = 'https://www.example.com/path/to/wisdom?query=2&this=3' | |
| + result = url_parser.get_base_url(url) | |
| + self.assertEqual(result, 'https://www.example.com') | |
| + | |
| + url = 'https://example.com/path/to/wisdom?query=2&this=3' | |
| + result = url_parser.get_base_url(url) | |
| + self.assertEqual(result, 'https://example.com') | |
| + | |
| + url = 'example.com/path/to/wisdom?query=2&this=3' | |
| + result = url_parser.get_base_url(url) | |
| + self.assertEqual(result, 'http://example.com') | |
| \ No newline at end of file | |