|
| 1 | +from plugins.link_announcer import url_re |
| 2 | + |
| 3 | +MATCHES = ( |
| 4 | + "http://foo.com/blah_blah", |
| 5 | + "http://foo.com/blah_blah/", |
| 6 | + "http://foo.com/blah_blah_(wikipedia)", |
| 7 | + "http://foo.com/blah_blah_(wikipedia)_(again)", |
| 8 | + "http://www.example.com/wpstyle/?p=364", |
| 9 | + "https://www.example.com/foo/?bar=baz&inga=42&quux", |
| 10 | + "http://userid:[email protected]:8080", |
| 11 | + "http://userid:[email protected]:8080/", |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | + "http://userid:[email protected]", |
| 17 | + "http://userid:[email protected]/", |
| 18 | + "http://142.42.1.1/", |
| 19 | + "http://142.42.1.1:8080/", |
| 20 | + "http://foo.com/blah_(wikipedia)#cite-1", |
| 21 | + "http://foo.com/blah_(wikipedia)_blah#cite-1", |
| 22 | + "http://foo.com/unicode_(✪)_in_parens", |
| 23 | + "http://foo.com/(something)?after=parens", |
| 24 | + "http://code.google.com/events/#&product=browser", |
| 25 | + "http://j.mp", |
| 26 | + "http://foo.bar/?q=Test%20URL-encoded%20stuff", |
| 27 | + "http://1337.net", |
| 28 | + "http://a.b-c.de", |
| 29 | + "http://223.255.255.254", |
| 30 | +) |
| 31 | + |
| 32 | +FAILS = ( |
| 33 | + "http://", |
| 34 | + "http://?", |
| 35 | + "http://??", |
| 36 | + "http://??/", |
| 37 | + "http://#", |
| 38 | + "http://##", |
| 39 | + "http://##/", |
| 40 | + "http://foo.bar?q=Spaces should be encoded", |
| 41 | + "//", |
| 42 | + "//a", |
| 43 | + "///a", |
| 44 | + "///", |
| 45 | + "http:///a", |
| 46 | + "foo.com", |
| 47 | + "rdar://1234", |
| 48 | + "h://test", |
| 49 | + "http:// shouldfail.com", |
| 50 | + ":// should fail", |
| 51 | + "http://foo.bar/foo(bar)baz quux", |
| 52 | + "ftps://foo.bar/", |
| 53 | +) |
| 54 | + |
| 55 | +SEARCH = ( |
| 56 | + ("[https://example.com]", "https://example.com"), |
| 57 | + ("<a hreh=\"https://example.com/test.page?#test\">", "https://example.com/test.page?#test"), |
| 58 | + ("<https://www.example.com/this.is.a.test/blah.txt?a=1#123>", "https://www.example.com/this.is.a.test/blah.txt?a=1#123"), |
| 59 | +) |
| 60 | + |
| 61 | + |
| 62 | +def test_urls(): |
| 63 | + for url in MATCHES: |
| 64 | + assert url_re.fullmatch(url), url |
| 65 | + |
| 66 | + for url in FAILS: |
| 67 | + match = url_re.fullmatch(url) |
| 68 | + assert not match, match.group() |
| 69 | + |
| 70 | + |
| 71 | +def test_search(): |
| 72 | + for text, out in SEARCH: |
| 73 | + match = url_re.search(text) |
| 74 | + assert match and match.group() == out |
0 commit comments