urlApi_worker.js (8524B)
1 /* eslint-env worker */ 2 3 function ok(a, msg) { 4 dump("OK: " + !!a + " => " + a + " " + msg + "\n"); 5 postMessage({ type: "status", status: !!a, msg: a + ": " + msg }); 6 } 7 8 function is(a, b, msg) { 9 dump("IS: " + (a === b) + " => " + a + " | " + b + " " + msg + "\n"); 10 postMessage({ 11 type: "status", 12 status: a === b, 13 msg: a + " === " + b + ": " + msg, 14 }); 15 } 16 17 // eslint-disable-next-line complexity 18 onmessage = function () { 19 let status = false; 20 try { 21 if (URL instanceof Object) { 22 status = true; 23 } 24 } catch (e) {} 25 26 ok(status, "URL in workers \\o/"); 27 28 var tests = [ 29 { 30 url: "http://www.abc.com", 31 base: undefined, 32 error: false, 33 href: "http://www.abc.com/", 34 origin: "http://www.abc.com", 35 protocol: "http:", 36 username: "", 37 password: "", 38 host: "www.abc.com", 39 hostname: "www.abc.com", 40 port: "", 41 pathname: "/", 42 search: "", 43 hash: "", 44 }, 45 { 46 url: "ftp://auser:apw@www.abc.com", 47 base: undefined, 48 error: false, 49 href: "ftp://auser:apw@www.abc.com/", 50 origin: "ftp://www.abc.com", 51 protocol: "ftp:", 52 username: "auser", 53 password: "apw", 54 host: "www.abc.com", 55 hostname: "www.abc.com", 56 port: "", 57 pathname: "/", 58 search: "", 59 hash: "", 60 }, 61 { 62 url: "http://www.abc.com:90/apath/", 63 base: undefined, 64 error: false, 65 href: "http://www.abc.com:90/apath/", 66 origin: "http://www.abc.com:90", 67 protocol: "http:", 68 username: "", 69 password: "", 70 host: "www.abc.com:90", 71 hostname: "www.abc.com", 72 port: "90", 73 pathname: "/apath/", 74 search: "", 75 hash: "", 76 }, 77 { 78 url: "http://www.abc.com/apath/afile.txt#ahash", 79 base: undefined, 80 error: false, 81 href: "http://www.abc.com/apath/afile.txt#ahash", 82 origin: "http://www.abc.com", 83 protocol: "http:", 84 username: "", 85 password: "", 86 host: "www.abc.com", 87 hostname: "www.abc.com", 88 port: "", 89 pathname: "/apath/afile.txt", 90 search: "", 91 hash: "#ahash", 92 }, 93 { 94 url: "http://example.com/?test#hash", 95 base: undefined, 96 error: false, 97 href: "http://example.com/?test#hash", 98 origin: "http://example.com", 99 protocol: "http:", 100 username: "", 101 password: "", 102 host: "example.com", 103 hostname: "example.com", 104 port: "", 105 pathname: "/", 106 search: "?test", 107 hash: "#hash", 108 }, 109 { 110 url: "http://example.com/?test", 111 base: undefined, 112 error: false, 113 href: "http://example.com/?test", 114 origin: "http://example.com", 115 protocol: "http:", 116 username: "", 117 password: "", 118 host: "example.com", 119 hostname: "example.com", 120 port: "", 121 pathname: "/", 122 search: "?test", 123 hash: "", 124 }, 125 { 126 url: "http://example.com/carrot#question%3f", 127 base: undefined, 128 error: false, 129 hash: "#question%3f", 130 }, 131 { 132 url: "https://example.com:4443?", 133 base: undefined, 134 error: false, 135 protocol: "https:", 136 port: "4443", 137 pathname: "/", 138 hash: "", 139 search: "", 140 }, 141 { 142 url: "http://www.abc.com/apath/afile.txt#ahash?asearch", 143 base: undefined, 144 error: false, 145 href: "http://www.abc.com/apath/afile.txt#ahash?asearch", 146 protocol: "http:", 147 pathname: "/apath/afile.txt", 148 hash: "#ahash?asearch", 149 search: "", 150 }, 151 { 152 url: "http://www.abc.com/apath/afile.txt?asearch#ahash", 153 base: undefined, 154 error: false, 155 href: "http://www.abc.com/apath/afile.txt?asearch#ahash", 156 protocol: "http:", 157 pathname: "/apath/afile.txt", 158 hash: "#ahash", 159 search: "?asearch", 160 }, 161 { 162 url: "http://abc.com/apath/afile.txt?#ahash", 163 base: undefined, 164 error: false, 165 pathname: "/apath/afile.txt", 166 hash: "#ahash", 167 search: "", 168 }, 169 { 170 url: "http://auser:apassword@www.abc.com:90/apath/afile.txt?asearch#ahash", 171 base: undefined, 172 error: false, 173 protocol: "http:", 174 username: "auser", 175 password: "apassword", 176 host: "www.abc.com:90", 177 hostname: "www.abc.com", 178 port: "90", 179 pathname: "/apath/afile.txt", 180 hash: "#ahash", 181 search: "?asearch", 182 origin: "http://www.abc.com:90", 183 }, 184 185 { url: "/foo#bar", base: "www.test.org", error: true }, 186 { url: "/foo#bar", base: null, error: true }, 187 { url: "/foo#bar", base: 42, error: true }, 188 { 189 url: "ftp://ftp.something.net", 190 base: undefined, 191 error: false, 192 protocol: "ftp:", 193 }, 194 { 195 url: "file:///tmp/file", 196 base: undefined, 197 error: false, 198 protocol: "file:", 199 }, 200 { 201 url: "gopher://gopher.something.net", 202 base: undefined, 203 error: false, 204 protocol: "gopher:", 205 expectedChangedProtocol: "https:", 206 }, 207 { 208 url: "ws://ws.something.net", 209 base: undefined, 210 error: false, 211 protocol: "ws:", 212 }, 213 { 214 url: "wss://ws.something.net", 215 base: undefined, 216 error: false, 217 protocol: "wss:", 218 }, 219 { 220 url: "foo://foo.something.net", 221 base: undefined, 222 error: false, 223 protocol: "foo:", 224 expectedChangedProtocol: "https:", 225 }, 226 ]; 227 228 while (tests.length) { 229 var test = tests.shift(); 230 231 var error = false; 232 var url; 233 try { 234 if (test.base) { 235 url = new URL(test.url, test.base); 236 } else { 237 url = new URL(test.url); 238 } 239 } catch (e) { 240 error = true; 241 } 242 243 is(test.error, error, "Error creating URL"); 244 if (test.error) { 245 continue; 246 } 247 248 if ("href" in test) { 249 is(url.href, test.href, "href"); 250 } 251 if ("origin" in test) { 252 is(url.origin, test.origin, "origin"); 253 } 254 if ("protocol" in test) { 255 is(url.protocol, test.protocol, "protocol"); 256 } 257 if ("username" in test) { 258 is(url.username, test.username, "username"); 259 } 260 if ("password" in test) { 261 is(url.password, test.password, "password"); 262 } 263 if ("host" in test) { 264 is(url.host, test.host, "host"); 265 } 266 if ("hostname" in test) { 267 is(url.hostname, test.hostname, "hostname"); 268 } 269 if ("port" in test) { 270 is(url.port, test.port, "port"); 271 } 272 if ("pathname" in test) { 273 is(url.pathname, test.pathname, "pathname"); 274 } 275 if ("search" in test) { 276 is(url.search, test.search, "search"); 277 } 278 if ("hash" in test) { 279 is(url.hash, test.hash, "hash"); 280 } 281 282 url = new URL("https://www.example.net/what#foo?bar"); 283 ok(url, "Url exists!"); 284 285 if ("href" in test) { 286 url.href = test.href; 287 } 288 if ("protocol" in test) { 289 url.protocol = test.protocol; 290 } 291 if ("username" in test && test.username) { 292 url.username = test.username; 293 } 294 if ("password" in test && test.password) { 295 url.password = test.password; 296 } 297 if ("host" in test) { 298 url.host = test.host; 299 } 300 if ("hostname" in test) { 301 url.hostname = test.hostname; 302 } 303 if ("port" in test) { 304 url.port = test.port; 305 } 306 if ("pathname" in test) { 307 url.pathname = test.pathname; 308 } 309 if ("search" in test) { 310 url.search = test.search; 311 } 312 if ("hash" in test) { 313 url.hash = test.hash; 314 } 315 316 if ("href" in test) { 317 is(url.href, test.href, "href"); 318 } 319 if ("origin" in test) { 320 is(url.origin, test.origin, "origin"); 321 } 322 if ("expectedChangedProtocol" in test) { 323 is(url.protocol, test.expectedChangedProtocol, "protocol"); 324 } else if ("protocol" in test) { 325 is(url.protocol, test.protocol, "protocol"); 326 } 327 if ("username" in test) { 328 is(url.username, test.username, "username"); 329 } 330 if ("password" in test) { 331 is(url.password, test.password, "password"); 332 } 333 if ("host" in test) { 334 is(url.host, test.host, "host"); 335 } 336 if ("hostname" in test) { 337 is(test.hostname, url.hostname, "hostname"); 338 } 339 if ("port" in test) { 340 is(test.port, url.port, "port"); 341 } 342 if ("pathname" in test) { 343 is(test.pathname, url.pathname, "pathname"); 344 } 345 if ("search" in test) { 346 is(test.search, url.search, "search"); 347 } 348 if ("hash" in test) { 349 is(test.hash, url.hash, "hash"); 350 } 351 352 if ("href" in test) { 353 is(test.href, url + "", "stringify works"); 354 } 355 } 356 357 postMessage({ type: "finish" }); 358 };