1 const StandardURL
= Components
.Constructor("@mozilla.org/network/standard-url;1",
4 const nsIStandardURL
= Components
.interfaces
.nsIStandardURL
;
6 function symmetricEquality(expect
, a
, b
)
8 /* Use if/else instead of |do_check_eq(expect, a.spec == b.spec)| so
9 that we get the specs output on the console if the check fails.
12 /* Check all the sub-pieces too, since that can help with
13 debugging cases when equals() returns something unexpected */
14 /* We don't check port in the loop, because it can be defaulted in
16 ["spec", "prePath", "scheme", "userPass", "username", "password",
17 "hostPort", "host", "path", "filePath", "param", "query",
18 "ref", "directory", "fileName", "fileBaseName", "fileExtension"]
20 dump("Testing '"+ prop
+ "'\n");
21 do_check_eq(a
[prop
], b
[prop
]);
24 do_check_neq(a
.spec
, b
.spec
);
26 do_check_eq(expect
, a
.equals(b
));
27 do_check_eq(expect
, b
.equals(a
));
30 function stringToURL(str
) {
31 return (new StandardURL(nsIStandardURL
.URLTYPE_AUTHORITY
, 80,
33 .QueryInterface(Components
.interfaces
.nsIURL
);
36 function pairToURLs(pair
) {
37 do_check_eq(pair
.length
, 2);
38 return pair
.map(stringToURL
);
41 function test_setEmptyPath()
45 ["http://example.com", "http://example.com/tests/dom/tests"],
46 ["http://example.com:80", "http://example.com/tests/dom/tests"],
47 ["http://example.com:80/", "http://example.com/tests/dom/test"],
48 ["http://example.com/", "http://example.com/tests/dom/tests"],
49 ["http://example.com/a", "http://example.com/tests/dom/tests"],
50 ["http://example.com:80/a", "http://example.com/tests/dom/tests"],
53 for each (var [provided
, target
] in pairs
)
55 symmetricEquality(false, target
, provided
);
60 do_check_eq(provided
.spec
, target
.spec
);
61 symmetricEquality(true, target
, provided
);
65 function test_setQuery()
69 ["http://example.com", "http://example.com/?foo"],
70 ["http://example.com/bar", "http://example.com/bar?foo"],
71 ["http://example.com#bar", "http://example.com/?foo#bar"],
72 ["http://example.com/#bar", "http://example.com/?foo#bar"],
73 ["http://example.com/?longerthanfoo#bar", "http://example.com/?foo#bar"],
74 ["http://example.com/?longerthanfoo", "http://example.com/?foo"],
75 /* And one that's nonempty but shorter than "foo" */
76 ["http://example.com/?f#bar", "http://example.com/?foo#bar"],
77 ["http://example.com/?f", "http://example.com/?foo"],
80 for each (var [provided
, target
] in pairs
) {
81 symmetricEquality(false, provided
, target
);
83 provided
.query
= "foo";
85 do_check_eq(provided
.spec
, target
.spec
);
86 symmetricEquality(true, provided
, target
);
90 ["http://example.com/#", "http://example.com/?foo#bar"].map(stringToURL
);
91 symmetricEquality(false, provided
, target
);
92 provided
.query
= "foo";
93 symmetricEquality(false, provided
, target
);
95 var newProvided
= Components
.classes
["@mozilla.org/network/io-service;1"]
96 .getService(Components
.interfaces
.nsIIOService
)
97 .newURI("#bar", null, provided
)
98 .QueryInterface(Components
.interfaces
.nsIURL
);
100 do_check_eq(newProvided
.spec
, target
.spec
);
101 symmetricEquality(true, newProvided
, target
);
105 function test_setRef()
109 ["http://example.com", "", "http://example.com/"],
110 ["http://example.com:80", "", "http://example.com:80/"],
111 ["http://example.com:80/", "", "http://example.com:80/"],
112 ["http://example.com/", "", "http://example.com/"],
113 ["http://example.com/a", "", "http://example.com/a"],
114 ["http://example.com:80/a", "", "http://example.com:80/a"],
116 ["http://example.com", "x", "http://example.com/#x"],
117 ["http://example.com:80", "x", "http://example.com:80/#x"],
118 ["http://example.com:80/", "x", "http://example.com:80/#x"],
119 ["http://example.com/", "x", "http://example.com/#x"],
120 ["http://example.com/a", "x", "http://example.com/a#x"],
121 ["http://example.com:80/a", "x", "http://example.com:80/a#x"],
123 ["http://example.com", "xx", "http://example.com/#xx"],
124 ["http://example.com:80", "xx", "http://example.com:80/#xx"],
125 ["http://example.com:80/", "xx", "http://example.com:80/#xx"],
126 ["http://example.com/", "xx", "http://example.com/#xx"],
127 ["http://example.com/a", "xx", "http://example.com/a#xx"],
128 ["http://example.com:80/a", "xx", "http://example.com:80/a#xx"],
130 ["http://example.com", "xxxxxxxxxxxxxx", "http://example.com/#xxxxxxxxxxxxxx"],
131 ["http://example.com:80", "xxxxxxxxxxxxxx", "http://example.com:80/#xxxxxxxxxxxxxx"],
132 ["http://example.com:80/", "xxxxxxxxxxxxxx", "http://example.com:80/#xxxxxxxxxxxxxx"],
133 ["http://example.com/", "xxxxxxxxxxxxxx", "http://example.com/#xxxxxxxxxxxxxx"],
134 ["http://example.com/a", "xxxxxxxxxxxxxx", "http://example.com/a#xxxxxxxxxxxxxx"],
135 ["http://example.com:80/a", "xxxxxxxxxxxxxx", "http://example.com:80/a#xxxxxxxxxxxxxx"],
138 for each (var [before
, ref
, result
] in tests
)
140 /* Test1: starting with empty ref */
141 var a
= stringToURL(before
);
143 var b
= stringToURL(result
);
145 do_check_eq(a
.spec
, b
.spec
);
146 do_check_eq(ref
, b
.ref
);
147 symmetricEquality(true, a
, b
);
149 /* Test2: starting with non-empty */
151 var c
= stringToURL(before
);
153 symmetricEquality(true, a
, c
);
155 /* Test3: reset the ref */
157 symmetricEquality(true, a
, stringToURL(before
));
159 /* Test4: verify again after reset */
161 symmetricEquality(true, a
, b
);