2 <!-- This file tests JavaScript error messages of some of the
3 failure cases from http/tests/serviceworker/registration.html. It
4 should not be upstreamed to web platform tests since error messages
5 are not defined by the specification. -->
6 <title>Service Worker: Error messages for register()
</title>
7 <script src=
"../../resources/testharness.js"></script>
8 <script src=
"../../resources/testharnessreport.js"></script>
11 function assert_register_fails(script
, scope
, message
) {
12 return navigator
.serviceWorker
.getRegistration(scope
)
13 .then(function(registration
) {
15 return registration
.unregister();
16 return Promise
.resolve();
19 return navigator
.serviceWorker
.register(script
, { scope
: scope
});
22 function() { assert_unreached('register should fail'); },
23 function(error
) { assert_equals(error
.message
, message
); });
26 promise_test(function(t
) {
27 var script
= '../resources/registration-worker.js';
28 var scope
= '../resources';
29 var message
= 'Failed to register a ServiceWorker: The path of the ' +
30 'provided scope (\'/serviceworker/resources\') is not under the max ' +
31 'scope allowed (\'/serviceworker/resources/\'). Adjust the scope, ' +
32 'move the Service Worker script, or use the Service-Worker-Allowed ' +
33 'HTTP header to allow the scope.';
34 return assert_register_fails(script
, scope
, message
);
35 }, 'Registering same scope as the script directory without the last slash');
37 promise_test(function(t
) {
38 var script
= '../resources/registration-worker.js';
39 var scope
= '../different-directory/';
40 var message
= 'Failed to register a ServiceWorker: The path of the ' +
41 'provided scope (\'/serviceworker/different-directory/\') is not ' +
42 'under the max scope allowed (\'/serviceworker/resources/\'). ' +
43 'Adjust the scope, move the Service Worker script, or use the ' +
44 'Service-Worker-Allowed HTTP header to allow the scope.';
45 return assert_register_fails(script
, scope
, message
);
46 }, 'Registration scope outside the script directory');
48 promise_test(function(t
) {
49 var script
= '../resources/registration-worker.js';
50 var scope
= 'http://example.com';
51 var message
= 'Failed to get a ServiceWorkerRegistration: The origin of ' +
52 'the provided documentURL (\'http://example.com\') does not match ' +
53 'the current origin (\'' + window
.location
.origin
+ '\').';
54 return assert_register_fails(script
, scope
, message
);
55 }, 'Registration scope outside domain');
57 promise_test(function(t
) {
58 var script
= 'http://example.com/worker.js';
59 var scope
= 'http://example.com/scope/';
60 var message
= 'Failed to get a ServiceWorkerRegistration: The origin of ' +
61 'the provided documentURL (\'http://example.com\') does not match ' +
62 'the current origin (\'' + window
.location
.origin
+ '\').';
63 return assert_register_fails(script
, scope
, message
);
64 }, 'Registering script outside domain');
66 promise_test(function(t
) {
67 var script
= '../resources/no-such-worker.js';
68 var scope
= '../resources/scope/no-such-worker';
69 var message
= 'Failed to register a ServiceWorker: A bad HTTP response ' +
70 'code (404) was received when fetching the script.';
71 return assert_register_fails(script
, scope
, message
);
72 }, 'Registering non-existent script');
74 promise_test(function(t
) {
75 var script
= '../resources/invalid-chunked-encoding.php';
76 var scope
= '../resources/scope/invalid-chunked-encoding/';
77 var message
= 'Failed to register a ServiceWorker: An unknown error ' +
78 'occurred when fetching the script.';
79 return assert_register_fails(script
, scope
, message
);
80 }, 'Registering invalid chunked encoding script');
82 promise_test(function(t
) {
83 var script
= '../resources/mime-type-worker.php';
84 var scope
= '../resources/scope/no-mime-type-worker/';
85 var message
= 'Failed to register a ServiceWorker: The script does not ' +
87 return assert_register_fails(script
, scope
, message
);
88 }, 'Registering script with no MIME type');
90 promise_test(function(t
) {
91 var script
= '../resources/mime-type-worker.php?mime=text/plain';
92 var scope
= '../resources/scope/bad-mime-type-worker/';
93 var message
= 'Failed to register a ServiceWorker: The script has an ' +
94 'unsupported MIME type (\'text/plain\').';
95 return assert_register_fails(script
, scope
, message
);
96 }, 'Registering script with bad MIME type');
98 promise_test(function(t
) {
99 var script
= '../resources/redirect.php?Redirect=' +
100 encodeURIComponent('..//resources/registration-worker.js');
101 var scope
= '../resources/scope/redirect/';
102 var message
= 'Failed to register a ServiceWorker: The script resource ' +
103 'is behind a redirect, which is disallowed.';
104 return assert_register_fails(script
, scope
, message
);
105 }, 'Registering redirected script');
107 promise_test(function(t
) {
108 var script
= '../resources/malformed-worker.php?parse-error';
109 var scope
= '../resources/scope/parse-error';
110 var message
= 'Failed to register a ServiceWorker: ServiceWorker script ' +
112 return assert_register_fails(script
, scope
, message
);
113 }, 'Registering script including parse error');
115 promise_test(function(t
) {
116 var script
= '../resources/malformed-worker.php?undefined-error';
117 var scope
= '../resources/scope/undefined-error';
118 var message
= 'Failed to register a ServiceWorker: ServiceWorker script ' +
120 return assert_register_fails(script
, scope
, message
);
121 }, 'Registering script including undefined error');
123 promise_test(function(t
) {
124 var script
= '../resources/malformed-worker.php?uncaught-exception';
125 var scope
= '../resources/scope/uncaught-exception';
126 var message
= 'Failed to register a ServiceWorker: ServiceWorker script ' +
128 return assert_register_fails(script
, scope
, message
);
129 }, 'Registering script including uncaught exception');
131 promise_test(function(t
) {
132 var script
= '../resources/malformed-worker.php?import-malformed-script';
133 var scope
= '../resources/scope/import-malformed-script';
134 var message
= 'Failed to register a ServiceWorker: ServiceWorker script ' +
136 return assert_register_fails(script
, scope
, message
);
137 }, 'Registering script importing malformed script');
139 promise_test(function(t
) {
140 var script
= '../resources/malformed-worker.php?import-no-such-script';
141 var scope
= '../resources/scope/import-no-such-script';
142 var message
= 'Failed to register a ServiceWorker: ServiceWorker script ' +
144 return assert_register_fails(script
, scope
, message
);
145 }, 'Registering script importing non-existent script');
147 promise_test(function(t
) {
148 var script
= '../resources%2fempty-worker.js';
149 var scope
= '../resources/scope/encoded-slash-in-script-url';
150 var message
= 'Failed to register a ServiceWorker: The provided scope ' +
151 '(\'' + window
.location
.origin
+ '/serviceworker/resources/scope/' +
152 'encoded-slash-in-script-url\') or scriptURL ' +
153 '(\'' + window
.location
.origin
+ '/serviceworker/' +
154 'resources%2fempty-worker.js\') ' +
155 'includes a disallowed escape character.';
156 return assert_register_fails(script
, scope
, message
);
157 }, 'Script URL including URL-encoded slash');