File size: 22,585 Bytes
d086025
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
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
@@ -0,0 +1,145 @@
+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
@@ -0,0 +1,34 @@
+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
@@ -0,0 +1,406 @@
+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