Left: | ||
Right: |
OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | |
3 * Copyright (C) 2006-2015 Eyeo GmbH | |
4 * | |
5 * Adblock Plus is free software: you can redistribute it and/or modify | |
6 * it under the terms of the GNU General Public License version 3 as | |
7 * published by the Free Software Foundation. | |
8 * | |
9 * Adblock Plus is distributed in the hope that it will be useful, | |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 * GNU General Public License for more details. | |
13 * | |
14 * You should have received a copy of the GNU General Public License | |
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | |
16 */ | |
17 | |
18 | |
19 (function() | |
20 { | |
21 var url = require("url"); | |
22 var getDecodedHostname = url.getDecodedHostname; | |
23 var extractHostFromFrame = url.extractHostFromFrame; | |
24 var stringifyURL = url.stringifyURL; | |
25 var isThirdParty = url.isThirdParty; | |
26 | |
27 module("URL/host tools"); | |
28 | |
29 test("Extracting hostname from URL", function() | |
30 { | |
31 equal(getDecodedHostname(new URL("http://example.com/foo")), "example.com", "with path"); | |
32 equal(getDecodedHostname(new URL("http://example.com/?foo=bar")), "example.c om", "with query"); | |
33 equal(getDecodedHostname(new URL("http://example.com/#top")), "example.com", "with hash"); | |
34 equal(getDecodedHostname(new URL("http://example.com/:8080")), "example.com" , "with port"); | |
Wladimir Palant
2015/02/09 12:54:29
Port number before slash please.
Sebastian Noack
2015/02/11 10:55:51
Done.
| |
35 equal(getDecodedHostname(new URL("http://user:password@example.com/")), "exa mple.com", "with auth credentials"); | |
36 equal(getDecodedHostname(new URL("http://xn--f-1gaa.com/")), "f\u00f6\u00f6. com", "with punycode"); | |
37 equal(getDecodedHostname(new URL("about:blank")), "", "about:blank"); | |
38 equal(getDecodedHostname(new URL("data:text/plain,foo")), "", "data: URL"); | |
Wladimir Palant
2015/02/09 12:54:29
Could you add back tests that you removed? In part
Sebastian Noack
2015/02/11 10:55:51
Not really removed them, since the function we tes
| |
39 }); | |
40 | |
41 test("Extracting hostname from frame", function() | |
42 { | |
43 function makeFrame(hierarchy) | |
44 { | |
45 var frame = null; | |
46 | |
47 for (var i = 0; i < hierarchy.length; i++) | |
48 frame = {parent: frame, url: new URL(hierarchy[i])}; | |
49 | |
50 return frame; | |
51 } | |
52 | |
53 equal(extractHostFromFrame(makeFrame(["http://example.com/"])), "example.com ", "single frame"); | |
54 equal(extractHostFromFrame(makeFrame(["http://example.com/", "http://example .org/"])), "example.org", "with parent frame"); | |
55 equal(extractHostFromFrame(makeFrame(["http://example.com/", "data:text/plai n,foo"])), "example.com", "data: URL, hostname in parent"); | |
56 equal(extractHostFromFrame(makeFrame(["http://example.com/", "about:blank", "about:blank"])), "example.com", "about:blank, hostname in ancestor"); | |
57 equal(extractHostFromFrame(makeFrame(["about:blank", "about:blank"])), "", " about:blank, no hostname"); | |
58 equal(extractHostFromFrame(makeFrame(["http://xn--f-1gaa.com/"])), "f\u00f6\ u00f6.com", "with punycode"); | |
59 }); | |
60 | |
61 test("Stringifying URLs", function() | |
62 { | |
63 equal(stringifyURL(new URL("http://example.com/foo")),"http://example.com/fo o", "includes path"); | |
64 equal(stringifyURL(new URL("http://example.com/?foo=bar")),"http://example.c om/?foo=bar", "includes query"); | |
65 equal(stringifyURL(new URL("http://example.com:8080/")),"http://example.com: 8080/", "includes port"); | |
66 equal(stringifyURL(new URL("http://example.com/#top")),"http://example.com/" , "stripped hash"); | |
67 equal(stringifyURL(new URL("http://user:password@example.com/")),"http://exa mple.com/", "stripped auth credentials"); | |
68 equal(stringifyURL(new URL("http://xn--f-1gaa.com/")),"http://f\u00f6\u00f6. com/", "decoded punycode"); | |
69 equal(stringifyURL(new URL("about:blank")), "about:blank", "about:blank"); | |
70 equal(stringifyURL(new URL("data:text/plain,foo")), "data:text/plain,foo", " data: URL"); | |
71 }); | |
72 | |
73 test("Third-party checks", function() | |
74 { | |
75 equal(isThirdParty(new URL("http://foo/"), "foo"), false, "same domain isn't third-party"); | |
76 equal(isThirdParty(new URL("http://foo/"), "bar"), true, "different domain i s third-party"); | |
77 equal(isThirdParty(new URL("http://foo.com/"), "foo.com"), false, "same doma in with TLD (.com) isn't third-party"); | |
78 equal(isThirdParty(new URL("http://foo.com/"), "bar.com"), true, "same TLD ( .com) but different domain is third-party"); | |
79 equal(isThirdParty(new URL("http://foo.example.com/"), "bar.example.com"), f alse, "same basedomain (example.com) isn't third-party"); | |
Wladimir Palant
2015/02/09 12:54:29
Please add back http://foo.com/, www.foo.com scena
Sebastian Noack
2015/02/11 10:55:51
Done.
| |
80 equal(isThirdParty(new URL("http://foo.uk/"), "bar.uk"), true, "same TLD (.u k) but different domain is third-party"); | |
81 equal(isThirdParty(new URL("http://foo.co.uk/"), "bar.co.uk"), true, "same T LD (.co.uk) but different domain is third-party"); | |
82 equal(isThirdParty(new URL("http://foo.example.co.uk/"), "bar.example.co.uk" ), false, "same basedomain (example.co.uk) isn't third-party"); | |
83 equal(isThirdParty(new URL("http://1.2.3.4/"), "1.2.3.4"), false, "same IP a ddress isn't third-party"); | |
84 equal(isThirdParty(new URL("http://1.1.1.1/"), "2.1.1.1"), true, "different IP address is third-party"); | |
85 equal(isThirdParty(new URL("http://xn--f-1gaa.com/"), "f\u00f6\u00f6.com"), false, "same IDN isn't third-party"); | |
86 }); | |
Wladimir Palant
2015/02/09 12:54:29
The original unit tests had a bunch more scenarios
Sebastian Noack
2015/02/11 10:55:51
Done.
| |
87 })(); | |
Wladimir Palant
2015/02/09 12:54:29
Nit: there is a reason why the original unit tests
Sebastian Noack
2015/02/11 10:55:51
The reason I didn't like that approach was that it
| |
OLD | NEW |