summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/service-workers/service-worker/fetch-request-fallback.https.html
blob: a29f31d127bc7b7ce160cb7147d90db310d55d68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
<!DOCTYPE html>
<title>Service Worker: the fallback behavior of FetchEvent</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/get-host-info.sub.js"></script>
<script src="resources/test-helpers.sub.js"></script>
<script>
function get_fetched_urls(worker) {
  return new Promise(function(resolve) {
      var channel = new MessageChannel();
      channel.port1.onmessage = function(msg) { resolve(msg); };
      worker.postMessage({port: channel.port2}, [channel.port2]);
    });
}

function check_urls(worker, expected_requests) {
  return get_fetched_urls(worker)
    .then(function(msg) {
        var requests = msg.data.requests;
        assert_object_equals(requests, expected_requests);
    });
}

var path = new URL(".", window.location).pathname;
var SCOPE = 'resources/fetch-request-fallback-iframe.html';
var SCRIPT = 'resources/fetch-request-fallback-worker.js';
var host_info = get_host_info();
var BASE_URL = host_info['HTTPS_ORIGIN'] +
               path + 'resources/fetch-access-control.py?';
var BASE_PNG_URL = BASE_URL + 'PNGIMAGE&';
var OTHER_BASE_URL = host_info['HTTPS_REMOTE_ORIGIN'] +
                     path + 'resources/fetch-access-control.py?';
var OTHER_BASE_PNG_URL = OTHER_BASE_URL + 'PNGIMAGE&';
var REDIRECT_URL = host_info['HTTPS_ORIGIN'] +
                   path + 'resources/redirect.py?Redirect=';
var register;

promise_test(function(t) {
  var registration;
  var worker;

  register = service_worker_unregister_and_register(t, SCRIPT, SCOPE)
    .then(function(r) {
        registration = r;
        worker = registration.installing;
        return wait_for_state(t, worker, 'activated');
      })
    .then(function() { return with_iframe(SCOPE); })
    .then(function(frame) {
        // This test should not be considered complete until after the service
        // worker has been unregistered. Currently, `testharness.js` does not
        // support asynchronous global "tear down" logic, so this must be
        // expressed using a dedicated `promise_test`. Because the other
        // sub-tests in this file are declared synchronously, this test will be
        // the final test executed.
        promise_test(function(t) {
            t.add_cleanup(function() {
                frame.remove();
              });
            return registration.unregister();
          }, 'restore global state');

        return {frame: frame, worker: worker};
      });

    return register;
  }, 'initialize global state');

function promise_frame_test(body, desc) {
  promise_test(function(test) {
      return register.then(function(result) {
          return body(test, result.frame, result.worker);
        });
    }, desc);
}

promise_frame_test(function(t, frame, worker) {
      return check_urls(
          worker,
          [{
            url: host_info['HTTPS_ORIGIN'] + path + SCOPE,
            mode: 'navigate'
          }]);
  }, 'The SW must intercept the request for a main resource.');

promise_frame_test(function(t, frame, worker) {
    return frame.contentWindow.xhr(BASE_URL)
      .then(function() {
          return check_urls(
              worker,
              [{ url: BASE_URL, mode: 'cors' }]);
        });
  }, 'The SW must intercept the request of same origin XHR.');

promise_frame_test(function(t, frame, worker) {
    return promise_rejects_js(
        t,
        frame.contentWindow.Error,
        frame.contentWindow.xhr(OTHER_BASE_URL),
        'SW fallbacked CORS-unsupported other origin XHR should fail.')
      .then(function() {
          return check_urls(
              worker,
              [{ url: OTHER_BASE_URL, mode: 'cors' }]);
        });
  }, 'The SW must intercept the request of CORS-unsupported other origin XHR.');

promise_frame_test(function(t, frame, worker) {
    return frame.contentWindow.xhr(OTHER_BASE_URL + 'ACAOrigin=*')
      .then(function() {
          return check_urls(
              worker,
              [{ url: OTHER_BASE_URL + 'ACAOrigin=*', mode: 'cors' }]);
        })
  }, 'The SW must intercept the request of CORS-supported other origin XHR.');

promise_frame_test(function(t, frame, worker) {
    return frame.contentWindow.xhr(
                  REDIRECT_URL + encodeURIComponent(BASE_URL))
      .then(function() {
          return check_urls(
              worker,
              [{
                url: REDIRECT_URL + encodeURIComponent(BASE_URL),
                mode: 'cors'
              }]);
        });
  }, 'The SW must intercept only the first request of redirected XHR.');

promise_frame_test(function(t, frame, worker) {
    return promise_rejects_js(
        t,
        frame.contentWindow.Error,
        frame.contentWindow.xhr(
          REDIRECT_URL + encodeURIComponent(OTHER_BASE_URL)),
        'SW fallbacked XHR which is redirected to CORS-unsupported ' +
          'other origin should fail.')
      .then(function() {
          return check_urls(
              worker,
              [{
                url: REDIRECT_URL + encodeURIComponent(OTHER_BASE_URL),
                mode: 'cors'
              }]);
        });
  }, 'The SW must intercept only the first request for XHR which is' +
     ' redirected to CORS-unsupported other origin.');

promise_frame_test(function(t, frame, worker) {
  return frame.contentWindow.xhr(
                  REDIRECT_URL +
                  encodeURIComponent(OTHER_BASE_URL + 'ACAOrigin=*'))
      .then(function() {
          return check_urls(
              worker,
              [{
                url: REDIRECT_URL +
                     encodeURIComponent(OTHER_BASE_URL + 'ACAOrigin=*'),
                mode: 'cors'
              }]);
        });
  }, 'The SW must intercept only the first request for XHR which is ' +
     'redirected to CORS-supported other origin.');

promise_frame_test(function(t, frame, worker) {
  return frame.contentWindow.load_image(BASE_PNG_URL, '')
      .then(function() {
          return check_urls(
              worker,
              [{ url: BASE_PNG_URL, mode: 'no-cors' }]);
        });
  }, 'The SW must intercept the request for image.');

promise_frame_test(function(t, frame, worker) {
  return frame.contentWindow.load_image(OTHER_BASE_PNG_URL, '')
      .then(function() {
          return check_urls(
              worker,
              [{ url: OTHER_BASE_PNG_URL, mode: 'no-cors' }]);
        });
  }, 'The SW must intercept the request for other origin image.');

promise_frame_test(function(t, frame, worker) {
  return promise_rejects_js(
        t,
        frame.contentWindow.Error,
        frame.contentWindow.load_image(OTHER_BASE_PNG_URL, 'anonymous'),
        'SW fallbacked CORS-unsupported other origin image request ' +
          'should fail.')
      .then(function() {
          return check_urls(
              worker,
              [{ url: OTHER_BASE_PNG_URL, mode: 'cors' }]);
        })
  }, 'The SW must intercept the request for CORS-unsupported other ' +
     'origin image.');

promise_frame_test(function(t, frame, worker) {
  return frame.contentWindow.load_image(
                  OTHER_BASE_PNG_URL + 'ACAOrigin=*', 'anonymous')
      .then(function() {
          return check_urls(
              worker,
              [{ url: OTHER_BASE_PNG_URL + 'ACAOrigin=*', mode: 'cors' }]);
        });
  }, 'The SW must intercept the request for CORS-supported other ' +
     'origin image.');

promise_frame_test(function(t, frame, worker) {
  return frame.contentWindow.load_image(
                  REDIRECT_URL + encodeURIComponent(BASE_PNG_URL), '')
      .catch(function() {
          assert_unreached(
              'SW fallbacked redirected image request should succeed.');
        })
      .then(function() {
          return check_urls(
              worker,
              [{
                url: REDIRECT_URL + encodeURIComponent(BASE_PNG_URL),
                mode: 'no-cors'
              }]);
        });
  }, 'The SW must intercept only the first request for redirected ' +
     'image resource.');

promise_frame_test(function(t, frame, worker) {
  return frame.contentWindow.load_image(
                  REDIRECT_URL + encodeURIComponent(OTHER_BASE_PNG_URL), '')
      .catch(function() {
          assert_unreached(
              'SW fallbacked image request which is redirected to ' +
              'other origin should succeed.');
        })
      .then(function() {
          return check_urls(
              worker,
              [{
                url: REDIRECT_URL + encodeURIComponent(OTHER_BASE_PNG_URL),
                mode: 'no-cors'
              }]);
        })
  }, 'The SW must intercept only the first request for image ' +
     'resource which is redirected to other origin.');

promise_frame_test(function(t, frame, worker) {
  return promise_rejects_js(
        t,
        frame.contentWindow.Error,
        frame.contentWindow.load_image(
            REDIRECT_URL + encodeURIComponent(OTHER_BASE_PNG_URL),
            'anonymous'),
        'SW fallbacked image request which is redirected to ' +
          'CORS-unsupported other origin should fail.')
      .then(function() {
          return check_urls(
              worker,
              [{
                url: REDIRECT_URL + encodeURIComponent(OTHER_BASE_PNG_URL),
                mode: 'cors'
              }]);
        });
  }, 'The SW must intercept only the first request for image ' +
     'resource which is redirected to CORS-unsupported other origin.');

promise_frame_test(function(t, frame, worker) {
    return frame.contentWindow.load_image(
        REDIRECT_URL +
          encodeURIComponent(OTHER_BASE_PNG_URL + 'ACAOrigin=*'),
        'anonymous')
      .then(function() {
          return check_urls(
              worker,
              [{
                url: REDIRECT_URL +
                     encodeURIComponent(OTHER_BASE_PNG_URL + 'ACAOrigin=*'),
                mode: 'cors'
              }]);
        });
  }, 'The SW must intercept only the first request for image ' +
     'resource which is redirected to CORS-supported other origin.');
</script>