urlApi_worker.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. function ok(a, msg) {
  2. dump("OK: " + !!a + " => " + a + " " + msg + "\n");
  3. postMessage({type: 'status', status: !!a, msg: a + ": " + msg });
  4. }
  5. function is(a, b, msg) {
  6. dump("IS: " + (a===b) + " => " + a + " | " + b + " " + msg + "\n");
  7. postMessage({type: 'status', status: a === b, msg: a + " === " + b + ": " + msg });
  8. }
  9. onmessage = function() {
  10. status = false;
  11. try {
  12. if ((URL instanceof Object)) {
  13. status = true;
  14. }
  15. } catch(e) {
  16. }
  17. var tests = [
  18. { url: 'http://www.abc.com',
  19. base: undefined,
  20. error: false,
  21. href: 'http://www.abc.com/',
  22. origin: 'http://www.abc.com',
  23. protocol: 'http:',
  24. username: '',
  25. password: '',
  26. host: 'www.abc.com',
  27. hostname: 'www.abc.com',
  28. port: '',
  29. pathname: '/',
  30. search: '',
  31. hash: ''
  32. },
  33. { url: 'ftp://auser:apw@www.abc.com',
  34. base: undefined,
  35. error: false,
  36. href: 'ftp://auser:apw@www.abc.com/',
  37. origin: 'ftp://www.abc.com',
  38. protocol: 'ftp:',
  39. username: 'auser',
  40. password: 'apw',
  41. host: 'www.abc.com',
  42. hostname: 'www.abc.com',
  43. port: '',
  44. pathname: '/',
  45. search: '',
  46. hash: ''
  47. },
  48. { url: 'http://www.abc.com:90/apath/',
  49. base: undefined,
  50. error: false,
  51. href: 'http://www.abc.com:90/apath/',
  52. origin: 'http://www.abc.com:90',
  53. protocol: 'http:',
  54. username: '',
  55. password: '',
  56. host: 'www.abc.com:90',
  57. hostname: 'www.abc.com',
  58. port: '90',
  59. pathname: '/apath/',
  60. search: '',
  61. hash: ''
  62. },
  63. { url: 'http://www.abc.com/apath/afile.txt#ahash',
  64. base: undefined,
  65. error: false,
  66. href: 'http://www.abc.com/apath/afile.txt#ahash',
  67. origin: 'http://www.abc.com',
  68. protocol: 'http:',
  69. username: '',
  70. password: '',
  71. host: 'www.abc.com',
  72. hostname: 'www.abc.com',
  73. port: '',
  74. pathname: '/apath/afile.txt',
  75. search: '',
  76. hash: '#ahash'
  77. },
  78. { url: 'http://example.com/?test#hash',
  79. base: undefined,
  80. error: false,
  81. href: 'http://example.com/?test#hash',
  82. origin: 'http://example.com',
  83. protocol: 'http:',
  84. username: '',
  85. password: '',
  86. host: 'example.com',
  87. hostname: 'example.com',
  88. port: '',
  89. pathname: '/',
  90. search: '?test',
  91. hash: '#hash'
  92. },
  93. { url: 'http://example.com/?test',
  94. base: undefined,
  95. error: false,
  96. href: 'http://example.com/?test',
  97. origin: 'http://example.com',
  98. protocol: 'http:',
  99. username: '',
  100. password: '',
  101. host: 'example.com',
  102. hostname: 'example.com',
  103. port: '',
  104. pathname: '/',
  105. search: '?test',
  106. hash: ''
  107. },
  108. { url: 'http://example.com/carrot#question%3f',
  109. base: undefined,
  110. error: false,
  111. hash: '#question%3f'
  112. },
  113. { url: 'https://example.com:4443?',
  114. base: undefined,
  115. error: false,
  116. protocol: 'https:',
  117. port: '4443',
  118. pathname: '/',
  119. hash: '',
  120. search: ''
  121. },
  122. { url: 'http://www.abc.com/apath/afile.txt#ahash?asearch',
  123. base: undefined,
  124. error: false,
  125. href: 'http://www.abc.com/apath/afile.txt#ahash?asearch',
  126. protocol: 'http:',
  127. pathname: '/apath/afile.txt',
  128. hash: '#ahash?asearch',
  129. search: ''
  130. },
  131. { url: 'http://www.abc.com/apath/afile.txt?asearch#ahash',
  132. base: undefined,
  133. error: false,
  134. href: 'http://www.abc.com/apath/afile.txt?asearch#ahash',
  135. protocol: 'http:',
  136. pathname: '/apath/afile.txt',
  137. hash: '#ahash',
  138. search: '?asearch'
  139. },
  140. { url: 'http://abc.com/apath/afile.txt?#ahash',
  141. base: undefined,
  142. error: false,
  143. pathname: '/apath/afile.txt',
  144. hash: '#ahash',
  145. search: ''
  146. },
  147. { url: 'http://auser:apassword@www.abc.com:90/apath/afile.txt?asearch#ahash',
  148. base: undefined,
  149. error: false,
  150. protocol: 'http:',
  151. username: 'auser',
  152. password: 'apassword',
  153. host: 'www.abc.com:90',
  154. hostname: 'www.abc.com',
  155. port: '90',
  156. pathname: '/apath/afile.txt',
  157. hash: '#ahash',
  158. search: '?asearch',
  159. origin: 'http://www.abc.com:90'
  160. },
  161. { url: '/foo#bar',
  162. base: 'www.test.org',
  163. error: true,
  164. },
  165. { url: '/foo#bar',
  166. base: null,
  167. error: true,
  168. },
  169. { url: '/foo#bar',
  170. base: 42,
  171. error: true,
  172. },
  173. { url: 'ftp://ftp.something.net',
  174. base: undefined,
  175. error: false,
  176. protocol: 'ftp:',
  177. },
  178. { url: 'file:///tmp/file',
  179. base: undefined,
  180. error: false,
  181. protocol: 'file:',
  182. },
  183. { url: 'gopher://gopher.something.net',
  184. base: undefined,
  185. error: false,
  186. protocol: 'gopher:',
  187. },
  188. { url: 'ws://ws.something.net',
  189. base: undefined,
  190. error: false,
  191. protocol: 'ws:',
  192. },
  193. { url: 'wss://ws.something.net',
  194. base: undefined,
  195. error: false,
  196. protocol: 'wss:',
  197. },
  198. { url: 'foo://foo.something.net',
  199. base: undefined,
  200. error: false,
  201. protocol: 'foo:',
  202. },
  203. ];
  204. while(tests.length) {
  205. var test = tests.shift();
  206. var error = false;
  207. var url;
  208. try {
  209. if (test.base) {
  210. url = new URL(test.url, test.base);
  211. } else {
  212. url = new URL(test.url);
  213. }
  214. } catch(e) {
  215. error = true;
  216. }
  217. is(test.error, error, "Error creating URL");
  218. if (test.error) {
  219. continue;
  220. }
  221. if ('href' in test) is(url.href, test.href, "href");
  222. if ('origin' in test) is(url.origin, test.origin, "origin");
  223. if ('protocol' in test) is(url.protocol, test.protocol, "protocol");
  224. if ('username' in test) is(url.username, test.username, "username");
  225. if ('password' in test) is(url.password, test.password, "password");
  226. if ('host' in test) is(url.host, test.host, "host");
  227. if ('hostname' in test) is(url.hostname, test.hostname, "hostname");
  228. if ('port' in test) is(url.port, test.port, "port");
  229. if ('pathname' in test) is(url.pathname, test.pathname, "pathname");
  230. if ('search' in test) is(url.search, test.search, "search");
  231. if ('hash' in test) is(url.hash, test.hash, "hash");
  232. url = new URL('https://www.example.net/what#foo?bar');
  233. ok(url, "Url exists!");
  234. if ('href' in test) url.href = test.href;
  235. if ('protocol' in test) url.protocol = test.protocol;
  236. if ('username' in test && test.username) url.username = test.username;
  237. if ('password' in test && test.password) url.password = test.password;
  238. if ('host' in test) url.host = test.host;
  239. if ('hostname' in test) url.hostname = test.hostname;
  240. if ('port' in test) url.port = test.port;
  241. if ('pathname' in test) url.pathname = test.pathname;
  242. if ('search' in test) url.search = test.search;
  243. if ('hash' in test) url.hash = test.hash;
  244. if ('href' in test) is(url.href, test.href, "href");
  245. if ('origin' in test) is(url.origin, test.origin, "origin");
  246. if ('protocol' in test) is(url.protocol, test.protocol, "protocol");
  247. if ('username' in test) is(url.username, test.username, "username");
  248. if ('password' in test) is(url.password, test.password, "password");
  249. if ('host' in test) is(url.host, test.host, "host");
  250. if ('hostname' in test) is(test.hostname, url.hostname, "hostname");
  251. if ('port' in test) is(test.port, url.port, "port");
  252. if ('pathname' in test) is(test.pathname, url.pathname, "pathname");
  253. if ('search' in test) is(test.search, url.search, "search");
  254. if ('hash' in test) is(test.hash, url.hash, "hash");
  255. if ('href' in test) is (test.href, url + '', 'stringify works');
  256. }
  257. postMessage({type: 'finish' });
  258. }