/testing/web-platform/tests/service-workers/service-worker/registration.https.html

https://github.com/rillian/firefox · HTML · 368 lines · 333 code · 35 blank · 0 comment · 0 complexity · 18ccc0667a577ddc03c5b12095352587 MD5 · raw file

  1. <!DOCTYPE html>
  2. <title>Service Worker: Registration</title>
  3. <script src="/resources/testharness.js"></script>
  4. <script src="resources/testharness-helpers.js"></script>
  5. <script src="/resources/testharnessreport.js"></script>
  6. <script src="resources/test-helpers.sub.js"></script>
  7. <script>
  8. promise_test(function(t) {
  9. var script = 'resources/registration-worker.js';
  10. var scope = 'resources/registration/normal';
  11. return navigator.serviceWorker.register(script, {scope: scope})
  12. .then(function(registration) {
  13. assert_true(registration instanceof ServiceWorkerRegistration,
  14. 'Successfully registered.');
  15. service_worker_unregister_and_done(t, scope);
  16. })
  17. }, 'Registering normal scope');
  18. promise_test(function(t) {
  19. var script = 'resources/registration-worker.js';
  20. var scope = 'resources/registration/scope-with-fragment#ref';
  21. return navigator.serviceWorker.register(script, {scope: scope})
  22. .then(function(registration) {
  23. assert_true(
  24. registration instanceof ServiceWorkerRegistration,
  25. 'Successfully registered.');
  26. assert_equals(
  27. registration.scope,
  28. normalizeURL('resources/registration/scope-with-fragment'),
  29. 'A fragment should be removed from scope')
  30. service_worker_unregister_and_done(t, scope);
  31. })
  32. }, 'Registering scope with fragment');
  33. promise_test(function(t) {
  34. var script = 'resources/registration-worker.js';
  35. var scope = 'resources/';
  36. return navigator.serviceWorker.register(script, {scope: scope})
  37. .then(function(registration) {
  38. assert_true(registration instanceof ServiceWorkerRegistration,
  39. 'Successfully registered.');
  40. service_worker_unregister_and_done(t, scope);
  41. })
  42. }, 'Registering same scope as the script directory');
  43. promise_test(function(t) {
  44. var script = 'resources/registration-worker.js';
  45. var scope = 'resources';
  46. return assert_promise_rejects(
  47. navigator.serviceWorker.register(script, {scope: scope}),
  48. 'SecurityError',
  49. 'Registering same scope as the script directory without the last ' +
  50. 'slash should fail with SecurityError.');
  51. }, 'Registering same scope as the script directory without the last slash');
  52. promise_test(function(t) {
  53. var script = 'resources/registration-worker.js';
  54. var scope = 'different-directory/';
  55. return assert_promise_rejects(
  56. navigator.serviceWorker.register(script, {scope: scope}),
  57. 'SecurityError',
  58. 'Registration scope outside the script directory should fail ' +
  59. 'with SecurityError.');
  60. }, 'Registration scope outside the script directory');
  61. promise_test(function(t) {
  62. var script = 'resources/registration-worker.js';
  63. var scope = 'http://example.com/';
  64. return assert_promise_rejects(
  65. navigator.serviceWorker.register(script, {scope: scope}),
  66. 'SecurityError',
  67. 'Registration scope outside domain should fail with SecurityError.');
  68. }, 'Registering scope outside domain');
  69. promise_test(function(t) {
  70. var script = 'http://example.com/worker.js';
  71. var scope = 'http://example.com/scope/';
  72. return assert_promise_rejects(
  73. navigator.serviceWorker.register(script, {scope: scope}),
  74. 'SecurityError',
  75. 'Registration script outside domain should fail with SecurityError.');
  76. }, 'Registering script outside domain');
  77. promise_test(function(t) {
  78. var script = 'resources/no-such-worker.js';
  79. var scope = 'resources/scope/no-such-worker';
  80. return assert_promise_rejects(
  81. navigator.serviceWorker.register(script, {scope: scope}),
  82. new TypeError(),
  83. 'Registration of non-existent script should fail.');
  84. }, 'Registering non-existent script');
  85. promise_test(function(t) {
  86. var script = 'resources/invalid-chunked-encoding.py';
  87. var scope = 'resources/scope/invalid-chunked-encoding/';
  88. return assert_promise_rejects(
  89. navigator.serviceWorker.register(script, {scope: scope}),
  90. new TypeError(),
  91. 'Registration of invalid chunked encoding script should fail.');
  92. }, 'Registering invalid chunked encoding script');
  93. promise_test(function(t) {
  94. var script = 'resources/invalid-chunked-encoding-with-flush.py';
  95. var scope = 'resources/scope/invalid-chunked-encoding-with-flush/';
  96. return assert_promise_rejects(
  97. navigator.serviceWorker.register(script, {scope: scope}),
  98. new TypeError(),
  99. 'Registration of invalid chunked encoding script should fail.');
  100. }, 'Registering invalid chunked encoding script with flush');
  101. promise_test(function(t) {
  102. var script = 'resources/mime-type-worker.py';
  103. var scope = 'resources/scope/no-mime-type-worker/';
  104. return assert_promise_rejects(
  105. navigator.serviceWorker.register(script, {scope: scope}),
  106. 'SecurityError',
  107. 'Registration of no MIME type script should fail.');
  108. }, 'Registering script with no MIME type');
  109. promise_test(function(t) {
  110. var script = 'resources/mime-type-worker.py?mime=text/plain';
  111. var scope = 'resources/scope/bad-mime-type-worker/';
  112. return assert_promise_rejects(
  113. navigator.serviceWorker.register(script, {scope: scope}),
  114. 'SecurityError',
  115. 'Registration of plain text script should fail.');
  116. }, 'Registering script with bad MIME type');
  117. promise_test(function(t) {
  118. var script = 'resources/redirect.py?Redirect=' +
  119. encodeURIComponent('/resources/registration-worker.js');
  120. var scope = 'resources/scope/redirect/';
  121. return assert_promise_rejects(
  122. navigator.serviceWorker.register(script, {scope: scope}),
  123. 'SecurityError',
  124. 'Registration of redirected script should fail.');
  125. }, 'Registering redirected script');
  126. promise_test(function(t) {
  127. var script = 'resources/malformed-worker.py?parse-error';
  128. var scope = 'resources/scope/parse-error';
  129. return assert_promise_rejects(
  130. navigator.serviceWorker.register(script, {scope: scope}),
  131. new TypeError(),
  132. 'Registration of script including parse error should fail.');
  133. }, 'Registering script including parse error');
  134. promise_test(function(t) {
  135. var script = 'resources/malformed-worker.py?undefined-error';
  136. var scope = 'resources/scope/undefined-error';
  137. return assert_promise_rejects(
  138. navigator.serviceWorker.register(script, {scope: scope}),
  139. new TypeError(),
  140. 'Registration of script including undefined error should fail.');
  141. }, 'Registering script including undefined error');
  142. promise_test(function(t) {
  143. var script = 'resources/malformed-worker.py?uncaught-exception';
  144. var scope = 'resources/scope/uncaught-exception';
  145. return assert_promise_rejects(
  146. navigator.serviceWorker.register(script, {scope: scope}),
  147. new TypeError(),
  148. 'Registration of script including uncaught exception should fail.');
  149. }, 'Registering script including uncaught exception');
  150. promise_test(function(t) {
  151. var script = 'resources/malformed-worker.py?caught-exception';
  152. var scope = 'resources/scope/caught-exception';
  153. return navigator.serviceWorker.register(script, {scope: scope})
  154. .then(function(registration) {
  155. assert_true(registration instanceof ServiceWorkerRegistration,
  156. 'Successfully registered.');
  157. service_worker_unregister_and_done(t, scope);
  158. })
  159. }, 'Registering script including caught exception');
  160. promise_test(function(t) {
  161. var script = 'resources/malformed-worker.py?import-malformed-script';
  162. var scope = 'resources/scope/import-malformed-script';
  163. return assert_promise_rejects(
  164. navigator.serviceWorker.register(script, {scope: scope}),
  165. new TypeError(),
  166. 'Registration of script importing malformed script should fail.');
  167. }, 'Registering script importing malformed script');
  168. promise_test(function(t) {
  169. var script = 'resources/malformed-worker.py?import-no-such-script';
  170. var scope = 'resources/scope/import-no-such-script';
  171. return assert_promise_rejects(
  172. navigator.serviceWorker.register(script, {scope: scope}),
  173. new TypeError(),
  174. 'Registration of script importing non-existent script should fail.');
  175. }, 'Registering script importing non-existent script');
  176. promise_test(function(t) {
  177. // URL-encoded full-width 'scope'.
  178. var name = '%ef%bd%93%ef%bd%83%ef%bd%8f%ef%bd%90%ef%bd%85';
  179. var script = 'resources/empty-worker.js';
  180. var scope = 'resources/' + name + '/escaped-multibyte-character-scope';
  181. return navigator.serviceWorker.register(script, {scope: scope})
  182. .then(function(registration) {
  183. assert_equals(
  184. registration.scope,
  185. normalizeURL(scope),
  186. 'URL-encoded multibyte characters should be available.');
  187. service_worker_unregister_and_done(t, scope);
  188. });
  189. }, 'Scope including URL-encoded multibyte characters');
  190. promise_test(function(t) {
  191. // Non-URL-encoded full-width "scope".
  192. var name = String.fromCodePoint(0xff53, 0xff43, 0xff4f, 0xff50, 0xff45);
  193. var script = 'resources/empty-worker.js';
  194. var scope = 'resources/' + name + '/non-escaped-multibyte-character-scope';
  195. return navigator.serviceWorker.register(script, {scope: scope})
  196. .then(function(registration) {
  197. assert_equals(
  198. registration.scope,
  199. normalizeURL(scope),
  200. 'Non-URL-encoded multibyte characters should be available.');
  201. service_worker_unregister_and_done(t, scope);
  202. });
  203. }, 'Scope including non-escaped multibyte characters');
  204. promise_test(function(t) {
  205. var script = 'resources%2fempty-worker.js';
  206. var scope = 'resources/scope/encoded-slash-in-script-url';
  207. return assert_promise_rejects(
  208. navigator.serviceWorker.register(script, {scope: scope}),
  209. new TypeError(),
  210. 'URL-encoded slash in the script URL should be rejected.');
  211. }, 'Script URL including URL-encoded slash');
  212. promise_test(function(t) {
  213. var script = 'resources/empty-worker.js';
  214. var scope = 'resources/scope%2fencoded-slash-in-scope';
  215. return assert_promise_rejects(
  216. navigator.serviceWorker.register(script, {scope: scope}),
  217. new TypeError(),
  218. 'URL-encoded slash in the scope should be rejected.');
  219. }, 'Scope including URL-encoded slash');
  220. promise_test(function(t) {
  221. var script = 'resources%5cempty-worker.js';
  222. var scope = 'resources/scope/encoded-slash-in-script-url';
  223. return assert_promise_rejects(
  224. navigator.serviceWorker.register(script, {scope: scope}),
  225. new TypeError(),
  226. 'URL-encoded backslash in the script URL should be rejected.');
  227. }, 'Script URL including URL-encoded backslash');
  228. promise_test(function(t) {
  229. var script = 'resources/empty-worker.js';
  230. var scope = 'resources/scope%5cencoded-slash-in-scope';
  231. return assert_promise_rejects(
  232. navigator.serviceWorker.register(script, {scope: scope}),
  233. new TypeError(),
  234. 'URL-encoded backslash in the scope should be rejected.');
  235. }, 'Scope including URL-encoded backslash');
  236. promise_test(function(t) {
  237. var script = 'resources/././empty-worker.js';
  238. var scope = 'resources/scope/parent-reference-in-script-url';
  239. return navigator.serviceWorker.register(script, {scope: scope})
  240. .then(function(registration) {
  241. assert_equals(
  242. registration.installing.scriptURL,
  243. normalizeURL('resources/empty-worker.js'),
  244. 'Script URL including self-reference should be normalized.');
  245. service_worker_unregister_and_done(t, scope);
  246. });
  247. }, 'Script URL including self-reference');
  248. promise_test(function(t) {
  249. var script = 'resources/empty-worker.js';
  250. var scope = 'resources/././scope/self-reference-in-scope';
  251. return navigator.serviceWorker.register(script, {scope: scope})
  252. .then(function(registration) {
  253. assert_equals(
  254. registration.scope,
  255. normalizeURL('resources/scope/self-reference-in-scope'),
  256. 'Scope including self-reference should be normalized.');
  257. service_worker_unregister_and_done(t, scope);
  258. });
  259. }, 'Scope including self-reference');
  260. promise_test(function(t) {
  261. var script = 'resources/../resources/empty-worker.js';
  262. var scope = 'resources/scope/parent-reference-in-script-url';
  263. return navigator.serviceWorker.register(script, {scope: scope})
  264. .then(function(registration) {
  265. assert_equals(
  266. registration.installing.scriptURL,
  267. normalizeURL('resources/empty-worker.js'),
  268. 'Script URL including parent-reference should be normalized.');
  269. service_worker_unregister_and_done(t, scope);
  270. });
  271. }, 'Script URL including parent-reference');
  272. promise_test(function(t) {
  273. var script = 'resources/empty-worker.js';
  274. var scope = 'resources/../resources/scope/parent-reference-in-scope';
  275. return navigator.serviceWorker.register(script, {scope: scope})
  276. .then(function(registration) {
  277. assert_equals(
  278. registration.scope,
  279. normalizeURL('resources/scope/parent-reference-in-scope'),
  280. 'Scope including parent-reference should be normalized.');
  281. service_worker_unregister_and_done(t, scope);
  282. });
  283. }, 'Scope including parent-reference');
  284. promise_test(function(t) {
  285. var script = 'resources/empty-worker.js';
  286. var scope = 'resources/../scope/parent-reference-in-scope';
  287. return assert_promise_rejects(
  288. navigator.serviceWorker.register(script, {scope: scope}),
  289. 'SecurityError',
  290. 'Scope not under the script directory should be rejected.');
  291. }, 'Scope including parent-reference and not under the script directory');
  292. promise_test(function(t) {
  293. var script = 'resources////empty-worker.js';
  294. var scope = 'resources/scope/consecutive-slashes-in-script-url';
  295. return assert_promise_rejects(
  296. navigator.serviceWorker.register(script, {scope: scope}),
  297. 'SecurityError',
  298. 'Consecutive slashes in the script url should not be unified.');
  299. }, 'Script URL including consecutive slashes');
  300. promise_test(function(t) {
  301. var script = 'resources/empty-worker.js';
  302. var scope = 'resources/scope////consecutive-slashes-in-scope';
  303. return navigator.serviceWorker.register(script, {scope: scope})
  304. .then(function(registration) {
  305. // Although consecutive slashes in the scope are not unified, the
  306. // scope is under the script directory and registration should
  307. // succeed.
  308. assert_equals(
  309. registration.scope,
  310. normalizeURL(scope),
  311. 'Should successfully be registered.');
  312. service_worker_unregister_and_done(t, scope);
  313. })
  314. }, 'Scope including consecutive slashes');
  315. promise_test(function(t) {
  316. var script = 'filesystem:' + normalizeURL('resources/empty-worker.js');
  317. var scope = 'resources/scope/filesystem-script-url';
  318. return assert_promise_rejects(
  319. navigator.serviceWorker.register(script, {scope: scope}),
  320. 'SecurityError',
  321. 'Registering a script which has same-origin filesystem: URL should ' +
  322. 'fail with SecurityError.');
  323. }, 'Script URL is same-origin filesystem: URL');
  324. promise_test(function(t) {
  325. var script = 'resources/empty-worker.js';
  326. var scope = 'filesystem:' + normalizeURL('resources/scope/filesystem-scope-url');
  327. return assert_promise_rejects(
  328. navigator.serviceWorker.register(script, {scope: scope}),
  329. 'SecurityError',
  330. 'Registering with the scope that has same-origin filesystem: URL ' +
  331. 'should fail with SecurityError.');
  332. }, 'Scope URL is same-origin filesystem: URL');
  333. </script>