dom/base/test/test_url.html

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1
michael@0 2 <!DOCTYPE HTML>
michael@0 3 <html>
michael@0 4 <head>
michael@0 5 <meta charset="utf-8">
michael@0 6 <title>Test URL API</title>
michael@0 7 <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
michael@0 8 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
michael@0 9 </head>
michael@0 10 <body>
michael@0 11 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=887364">Mozilla Bug 887364</a>
michael@0 12 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=991471">Mozilla Bug 991471</a>
michael@0 13 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=996055">Mozilla Bug 996055</a>
michael@0 14 <p id="display"></p>
michael@0 15 <div id="content" style="display: none">
michael@0 16 <iframe name="x" id="x"></iframe>
michael@0 17 <iframe name="y" id="y"></iframe>
michael@0 18 </div>
michael@0 19 <pre id="test">
michael@0 20 </pre>
michael@0 21 <script type="application/javascript">
michael@0 22
michael@0 23 /** Test for Bug 887364 **/
michael@0 24 ok("URL" in window, "window.URL exists");
michael@0 25
michael@0 26 var tests = [
michael@0 27 { url: 'http://www.abc.com',
michael@0 28 base: undefined,
michael@0 29 error: false,
michael@0 30 href: 'http://www.abc.com/',
michael@0 31 origin: 'http://www.abc.com',
michael@0 32 protocol: 'http:',
michael@0 33 username: '',
michael@0 34 password: '',
michael@0 35 host: 'www.abc.com',
michael@0 36 hostname: 'www.abc.com',
michael@0 37 port: '',
michael@0 38 pathname: '/',
michael@0 39 search: '',
michael@0 40 hash: ''
michael@0 41 },
michael@0 42 { url: 'ftp://auser:apw@www.abc.com',
michael@0 43 base: undefined,
michael@0 44 error: false,
michael@0 45 href: 'ftp://auser:apw@www.abc.com/',
michael@0 46 origin: 'ftp://www.abc.com',
michael@0 47 protocol: 'ftp:',
michael@0 48 username: 'auser',
michael@0 49 password: 'apw',
michael@0 50 host: 'www.abc.com',
michael@0 51 hostname: 'www.abc.com',
michael@0 52 port: '',
michael@0 53 pathname: '/',
michael@0 54 search: '',
michael@0 55 hash: ''
michael@0 56 },
michael@0 57 { url: 'http://www.abc.com:90/apath/',
michael@0 58 base: undefined,
michael@0 59 error: false,
michael@0 60 href: 'http://www.abc.com:90/apath/',
michael@0 61 origin: 'http://www.abc.com:90',
michael@0 62 protocol: 'http:',
michael@0 63 username: '',
michael@0 64 password: '',
michael@0 65 host: 'www.abc.com:90',
michael@0 66 hostname: 'www.abc.com',
michael@0 67 port: '90',
michael@0 68 pathname: '/apath/',
michael@0 69 search: '',
michael@0 70 hash: ''
michael@0 71 },
michael@0 72 { url: 'http://www.abc.com/apath/afile.txt#ahash',
michael@0 73 base: undefined,
michael@0 74 error: false,
michael@0 75 href: 'http://www.abc.com/apath/afile.txt#ahash',
michael@0 76 origin: 'http://www.abc.com',
michael@0 77 protocol: 'http:',
michael@0 78 username: '',
michael@0 79 password: '',
michael@0 80 host: 'www.abc.com',
michael@0 81 hostname: 'www.abc.com',
michael@0 82 port: '',
michael@0 83 pathname: '/apath/afile.txt',
michael@0 84 search: '',
michael@0 85 hash: '#ahash'
michael@0 86 },
michael@0 87 { url: 'http://example.com/?test#hash',
michael@0 88 base: undefined,
michael@0 89 error: false,
michael@0 90 href: 'http://example.com/?test#hash',
michael@0 91 origin: 'http://example.com',
michael@0 92 protocol: 'http:',
michael@0 93 username: '',
michael@0 94 password: '',
michael@0 95 host: 'example.com',
michael@0 96 hostname: 'example.com',
michael@0 97 port: '',
michael@0 98 pathname: '/',
michael@0 99 search: '?test',
michael@0 100 hash: '#hash'
michael@0 101 },
michael@0 102 { url: 'http://example.com/?test',
michael@0 103 base: undefined,
michael@0 104 error: false,
michael@0 105 href: 'http://example.com/?test',
michael@0 106 origin: 'http://example.com',
michael@0 107 protocol: 'http:',
michael@0 108 username: '',
michael@0 109 password: '',
michael@0 110 host: 'example.com',
michael@0 111 hostname: 'example.com',
michael@0 112 port: '',
michael@0 113 pathname: '/',
michael@0 114 search: '?test',
michael@0 115 hash: ''
michael@0 116 },
michael@0 117 { url: 'http://example.com/carrot#question%3f',
michael@0 118 base: undefined,
michael@0 119 error: false,
michael@0 120 hash: '#question?'
michael@0 121 },
michael@0 122 { url: 'https://example.com:4443?',
michael@0 123 base: undefined,
michael@0 124 error: false,
michael@0 125 protocol: 'https:',
michael@0 126 port: '4443',
michael@0 127 pathname: '/',
michael@0 128 hash: '',
michael@0 129 search: ''
michael@0 130 },
michael@0 131 { url: 'http://www.abc.com/apath/afile.txt#ahash?asearch',
michael@0 132 base: undefined,
michael@0 133 error: false,
michael@0 134 href: 'http://www.abc.com/apath/afile.txt#ahash?asearch',
michael@0 135 protocol: 'http:',
michael@0 136 pathname: '/apath/afile.txt',
michael@0 137 hash: '#ahash?asearch',
michael@0 138 search: ''
michael@0 139 },
michael@0 140 { url: 'http://www.abc.com/apath/afile.txt?asearch#ahash',
michael@0 141 base: undefined,
michael@0 142 error: false,
michael@0 143 href: 'http://www.abc.com/apath/afile.txt?asearch#ahash',
michael@0 144 protocol: 'http:',
michael@0 145 pathname: '/apath/afile.txt',
michael@0 146 hash: '#ahash',
michael@0 147 search: '?asearch'
michael@0 148 },
michael@0 149 { url: 'http://abc.com/apath/afile.txt?#ahash',
michael@0 150 base: undefined,
michael@0 151 error: false,
michael@0 152 pathname: '/apath/afile.txt',
michael@0 153 hash: '#ahash',
michael@0 154 search: ''
michael@0 155 },
michael@0 156 { url: 'http://auser:apassword@www.abc.com:90/apath/afile.txt?asearch#ahash',
michael@0 157 base: undefined,
michael@0 158 error: false,
michael@0 159 protocol: 'http:',
michael@0 160 username: 'auser',
michael@0 161 password: 'apassword',
michael@0 162 host: 'www.abc.com:90',
michael@0 163 hostname: 'www.abc.com',
michael@0 164 port: '90',
michael@0 165 pathname: '/apath/afile.txt',
michael@0 166 hash: '#ahash',
michael@0 167 search: '?asearch',
michael@0 168 origin: 'http://www.abc.com:90'
michael@0 169 },
michael@0 170
michael@0 171 { url: '/foo#bar',
michael@0 172 base: 'www.test.org',
michael@0 173 error: true,
michael@0 174 },
michael@0 175 { url: '/foo#bar',
michael@0 176 base: null,
michael@0 177 error: true,
michael@0 178 },
michael@0 179 { url: '/foo#bar',
michael@0 180 base: 42,
michael@0 181 error: true,
michael@0 182 },
michael@0 183 { url: 'ftp://ftp.something.net',
michael@0 184 base: undefined,
michael@0 185 error: false,
michael@0 186 protocol: 'ftp:',
michael@0 187 },
michael@0 188 { url: 'file:///tmp/file',
michael@0 189 base: undefined,
michael@0 190 error: false,
michael@0 191 protocol: 'file:',
michael@0 192 },
michael@0 193 { url: 'gopher://gopher.something.net',
michael@0 194 base: undefined,
michael@0 195 error: false,
michael@0 196 protocol: 'gopher:',
michael@0 197 },
michael@0 198 { url: 'ws://ws.something.net',
michael@0 199 base: undefined,
michael@0 200 error: false,
michael@0 201 protocol: 'ws:',
michael@0 202 },
michael@0 203 { url: 'wss://ws.something.net',
michael@0 204 base: undefined,
michael@0 205 error: false,
michael@0 206 protocol: 'wss:',
michael@0 207 },
michael@0 208 { url: 'foo://foo.something.net',
michael@0 209 base: undefined,
michael@0 210 error: false,
michael@0 211 protocol: 'foo:',
michael@0 212 },
michael@0 213 ];
michael@0 214
michael@0 215 while(tests.length) {
michael@0 216 var test = tests.shift();
michael@0 217
michael@0 218 var error = false;
michael@0 219 var url;
michael@0 220 try {
michael@0 221 if (test.base) {
michael@0 222 url = new URL(test.url, test.base);
michael@0 223 } else {
michael@0 224 url = new URL(test.url);
michael@0 225 }
michael@0 226 } catch(e) {
michael@0 227 error = true;
michael@0 228 }
michael@0 229
michael@0 230 is(test.error, error, "Error creating URL");
michael@0 231 if (test.error) {
michael@0 232 continue;
michael@0 233 }
michael@0 234
michael@0 235 if ('href' in test) is(url.href, test.href, "href");
michael@0 236 if ('origin' in test) is(url.origin, test.origin, "origin");
michael@0 237 if ('protocol' in test) is(url.protocol, test.protocol, "protocol");
michael@0 238 if ('username' in test) is(url.username, test.username, "username");
michael@0 239 if ('password' in test) is(url.password, test.password, "password");
michael@0 240 if ('host' in test) is(url.host, test.host, "host");
michael@0 241 if ('hostname' in test) is(url.hostname, test.hostname, "hostname");
michael@0 242 if ('port' in test) is(url.port, test.port, "port");
michael@0 243 if ('pathname' in test) is(url.pathname, test.pathname, "pathname");
michael@0 244 if ('search' in test) is(url.search, test.search, "search");
michael@0 245 if ('hash' in test) is(url.hash, test.hash, "hash");
michael@0 246
michael@0 247 url = new URL('https://www.example.net/what#foo?bar');
michael@0 248 ok(url, "Url exists!");
michael@0 249
michael@0 250 if ('href' in test) url.href = test.href;
michael@0 251 if ('protocol' in test) url.protocol = test.protocol;
michael@0 252 if ('username' in test && test.username) url.username = test.username;
michael@0 253 if ('password' in test && test.password) url.password = test.password;
michael@0 254 if ('host' in test) url.host = test.host;
michael@0 255 if ('hostname' in test) url.hostname = test.hostname;
michael@0 256 if ('port' in test) url.port = test.port;
michael@0 257 if ('pathname' in test) url.pathname = test.pathname;
michael@0 258 if ('search' in test) url.search = test.search;
michael@0 259 if ('hash' in test) url.hash = test.hash;
michael@0 260
michael@0 261 if ('href' in test) is(url.href, test.href, "href");
michael@0 262 if ('origin' in test) is(url.origin, test.origin, "origin");
michael@0 263 if ('protocol' in test) is(url.protocol, test.protocol, "protocol");
michael@0 264 if ('username' in test) is(url.username, test.username, "username");
michael@0 265 if ('password' in test) is(url.password, test.password, "password");
michael@0 266 if ('host' in test) is(url.host, test.host, "host");
michael@0 267 if ('hostname' in test) is(test.hostname, url.hostname, "hostname");
michael@0 268 if ('port' in test) is(test.port, url.port, "port");
michael@0 269 if ('pathname' in test) is(test.pathname, url.pathname, "pathname");
michael@0 270 if ('search' in test) is(test.search, url.search, "search");
michael@0 271 if ('hash' in test) is(test.hash, url.hash, "hash");
michael@0 272
michael@0 273 if ('href' in test) is (test.href, url + '', 'stringify works');
michael@0 274 }
michael@0 275
michael@0 276 </script>
michael@0 277
michael@0 278 <script>
michael@0 279 /** Test for Bug 991471 **/
michael@0 280 var url = new URL("http://localhost/");
michael@0 281 url.hostname = "";
michael@0 282 url.username = "tttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt";
michael@0 283 url.hostname = "www.mozilla.org";
michael@0 284 url.username = "";
michael@0 285 url.hostname = "www.mozilla.org";
michael@0 286 is(url.href, "http://www.mozilla.org/", "No parsing error with empty host");
michael@0 287 </script>
michael@0 288
michael@0 289 <script>
michael@0 290 /** Test for Bug 996055 **/
michael@0 291 var url = new URL("http://localhost/");
michael@0 292 url.hostname = "";
michael@0 293 is(url.href, "http://localhost/", "Empty hostname is ignored");
michael@0 294 </script>
michael@0 295 </body>
michael@0 296 </html>

mercurial