summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/service-workers/service-worker/navigation-redirect.https.html
blob: a87de56931638d0939e406aa95146699e5a63603 (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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
<!DOCTYPE html>
<title>Service Worker: Navigation redirection</title>
<meta name="timeout" content="long">
<!-- default variant tests document.location and intercepted URLs -->
<meta name="variant" content="?default">
<!-- client variant tests the Clients API (resultingClientId and Client.url) -->
<meta name="variant" content="?client">

<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>
<body>
<script>
const host_info = get_host_info();

// This test registers three Service Workers at SCOPE1, SCOPE2 and
// OTHER_ORIGIN_SCOPE. And checks the redirected page's URL and the requests
// which are intercepted by Service Worker while loading redirect page.
const BASE_URL = host_info['HTTPS_ORIGIN'] + base_path();
const OTHER_BASE_URL = host_info['HTTPS_REMOTE_ORIGIN'] + base_path();

const SCOPE1 = BASE_URL + 'resources/navigation-redirect-scope1.py?';
const SCOPE2 = BASE_URL + 'resources/navigation-redirect-scope2.py?';
const OUT_SCOPE = BASE_URL + 'resources/navigation-redirect-out-scope.py?';
const SCRIPT = 'resources/redirect-worker.js';

const OTHER_ORIGIN_IFRAME_URL =
      OTHER_BASE_URL + 'resources/navigation-redirect-other-origin.html';
const OTHER_ORIGIN_SCOPE =
      OTHER_BASE_URL + 'resources/navigation-redirect-scope1.py?';
const OTHER_ORIGIN_OUT_SCOPE =
      OTHER_BASE_URL + 'resources/navigation-redirect-out-scope.py?';

let registrations;
let workers;
let other_origin_frame;
let message_resolvers = {};
let next_message_id = 0;

promise_test(async t  => {
  // In this frame we register a service worker at OTHER_ORIGIN_SCOPE.
  // And will use this frame to communicate with the worker.
  other_origin_frame = await with_iframe(OTHER_ORIGIN_IFRAME_URL);

  // Register same-origin service workers.
  registrations = await Promise.all([
      service_worker_unregister_and_register(t, SCRIPT, SCOPE1),
      service_worker_unregister_and_register(t, SCRIPT, SCOPE2)]);

  // Wait for all workers to activate.
  workers = registrations.map(get_effective_worker);
  return Promise.all([
      wait_for_state(t, workers[0], 'activated'),
      wait_for_state(t, workers[1], 'activated'),
      // This promise will resolve when |wait_for_worker_promise|
      // in OTHER_ORIGIN_IFRAME_URL resolves.
      send_to_iframe(other_origin_frame, {command: 'wait_for_worker'})]);
}, 'initialize global state');

function get_effective_worker(registration) {
  if (registration.active)
    return registration.active;
  if (registration.waiting)
    return registration.waiting;
  if (registration.installing)
    return registration.installing;
}

async function check_all_intercepted_urls(expected_urls) {
  const urls = [];
  urls.push(await get_intercepted_urls(workers[0]));
  urls.push(await get_intercepted_urls(workers[1]));
  // Gets the request URLs which are intercepted by OTHER_ORIGIN_SCOPE's
  // SW. This promise will resolve when get_request_infos() in
  // OTHER_ORIGIN_IFRAME_URL resolves.
  const request_infos = await send_to_iframe(other_origin_frame,
                                             {command: 'get_request_infos'});
  urls.push(request_infos.map(info => { return info.url; }));

  assert_object_equals(urls, expected_urls, 'Intercepted URLs should match.');
}

// Checks |clients| returned from a worker. Only the client matching
// |expected_final_client_tag| should be found. Returns true if a client was
// found. Note that the final client is not necessarily found by this worker,
// if the client is cross-origin.
//
// |clients| is an object like:
// {x: {found: true, id: id1, url: url1}, b: {found: false}}
function check_clients(clients,
                       expected_id,
                       expected_url,
                       expected_final_client_tag,
                       worker_name) {
  let found = false;
  Object.keys(clients).forEach(key => {
    const info = clients[key];
    if (info.found) {
      assert_true(!!expected_final_client_tag,
                  `${worker_name} client tag exists`);
      assert_equals(key, expected_final_client_tag,
                    `${worker_name} client tag matches`);
      assert_equals(info.id, expected_id, `${worker_name} client id`);
      assert_equals(info.url, expected_url, `${worker_name} client url`);
      found = true;
    }
  });
  return found;
}

function check_resulting_client_ids(infos, expected_infos, actual_ids, worker) {
  assert_equals(infos.length, expected_infos.length,
                `request length for ${worker}`);
  for (var i = 0; i < infos.length; i++) {
    const tag = expected_infos[i].resultingClientIdTag;
    const url = expected_infos[i].url;
    const actual_id = infos[i].resultingClientId;
    const expected_id = actual_ids[tag];
    assert_equals(typeof(actual_id), 'string',
                  `resultingClientId for ${url} request to ${worker}`);
    if (expected_id) {
      assert_equals(actual_id, expected_id,
                    `resultingClientId for ${url} request to ${worker}`);
    } else {
      actual_ids[tag] = actual_id;
    }
  }
}

// Creates an iframe and navigates to |url|, which is expected to start a chain
// of redirects.
// - |expected_last_url| is the expected window.location after the
//   navigation.
//
// - |expected_request_infos| is the expected requests that the service workers
//   were dispatched fetch events for. The format is:
//   [
//     [
//       // Requests received by workers[0].
//       {url: url1, resultingClientIdTag: 'a'},
//       {url: url2, resultingClientIdTag: 'a'}
//     ],
//     [
//       // Requests received by workers[1].
//       {url: url3, resultingClientIdTag: 'a'}
//     ],
//     [
//       // Requests received by the cross-origin worker.
//       {url: url4, resultingClientIdTag: 'x'}
//       {url: url5, resultingClientIdTag: 'x'}
//     ]
//   ]
//   Here, |url| is |event.request.url| and |resultingClientIdTag| represents
//   |event.resultingClientId|. Since the actual client ids are not known
//   beforehand, the expectation isn't the literal expected value, but all equal
//   tags must map to the same actual id.
//
// - |expected_final_client_tag| is the resultingClientIdTag that is
//   expected to map to the created client's id. This is null if there
//   is no such tag, which can happen when the final request was a cross-origin
//   redirect to out-scope, so no worker received a fetch event whose
//   resultingClientId is the id of the resulting client.
//
// In the example above:
// - workers[0] receives two requests with the same resultingClientId.
// - workers[1] receives one request also with that resultingClientId.
// - The cross-origin worker receives two requests with the same
//   resultingClientId which differs from the previous one.
// - Assuming |expected_final_client_tag| is 'x', then the created
//   client has the id seen by the cross-origin worker above.
function redirect_test(url,
                       expected_last_url,
                       expected_request_infos,
                       expected_final_client_tag,
                       test_name) {
  promise_test(async t => {
    const frame = await with_iframe(url);
    t.add_cleanup(() => { frame.remove(); });

    // Switch on variant.
    if (document.location.search == '?client') {
      return client_variant_test(url, expected_last_url, expected_request_infos,
                                 expected_final_client_tag, test_name);
    }

    return default_variant_test(url, expected_last_url, expected_request_infos,
                                frame, test_name);
  }, test_name);
}

// The default variant tests the request interception chain and
// resulting document.location.
async function default_variant_test(url,
                                    expected_last_url,
                                    expected_request_infos,
                                    frame,
                                    test_name) {
  const expected_intercepted_urls = expected_request_infos.map(
      requests_for_worker => {
    return requests_for_worker.map(info => {
      return info.url;
    });
  });
  await check_all_intercepted_urls(expected_intercepted_urls);
  const last_url = await send_to_iframe(frame, 'getLocation');
  assert_equals(last_url, expected_last_url, 'Last URL should match.');
}

// The "client" variant tests the Clients API using resultingClientId.
async function client_variant_test(url,
                                   expected_last_url,
                                   expected_request_infos,
                                   expected_final_client_tag,
                                   test_name) {
  // Request infos is an array like:
  // [
  //   [{url: url1, resultingClientIdTag: tag1}],
  //   [{url: url2, resultingClientIdTag: tag2}],
  //   [{url: url3: resultingClientIdTag: tag3}]
  // ]
  const requestInfos = await get_all_request_infos();

  // We check the actual infos against the expected ones, and learn the
  // actual ids as we go.
  const actual_ids = {};
  check_resulting_client_ids(requestInfos[0],
                             expected_request_infos[0],
                             actual_ids,
                             'worker0');
  check_resulting_client_ids(requestInfos[1],
                             expected_request_infos[1],
                             actual_ids,
                             'worker1');
  check_resulting_client_ids(requestInfos[2],
                             expected_request_infos[2],
                             actual_ids,
                             'crossOriginWorker');

  // Now |actual_ids| maps tag to actual id:
  // {x: id1, b: id2, c: id3}
  // Ask each worker to try to resolve the actual ids to clients.
  // Only |expected_final_client_tag| should resolve to a client.
  const client_infos = await get_all_clients(actual_ids);

  // Client infos is an object like:
  // {
  //   worker0: {x: {found: true, id: id1, url: url1}, b: {found: false}},
  //   worker1: {x: {found: true, id: id1, url: url1}},
  //   crossOriginWorker: {x: {found: false}}, {b: {found: false}}
  // }
  //
  // Now check each client info. check_clients() verifies each info: only
  // |expected_final_client_tag| should ever be found and the found client
  // should have the expected url and id. A wrinkle is that not all workers
  // will find the client, if they are cross-origin to the client. This
  // means check_clients() trivially passes if no clients are found. So
  // additionally check that at least one worker found the client (|found|),
  // if that was expected (|expect_found|).
  let found = false;
  const expect_found = !!expected_final_client_tag;
  const expected_id = actual_ids[expected_final_client_tag];
  found = check_clients(client_infos.worker0,
                        expected_id,
                        expected_last_url,
                        expected_final_client_tag,
                        'worker0');
  found = check_clients(client_infos.worker1,
                        expected_id,
                        expected_last_url,
                        expected_final_client_tag,
                        'worker1') || found;
  found = check_clients(client_infos.crossOriginWorker,
                        expected_id,
                        expected_last_url,
                        expected_final_client_tag,
                        'crossOriginWorker') || found;
  assert_equals(found, expect_found, 'client found');

  if (!expect_found) {
    // TODO(falken): Ask the other origin frame if it has a client of the
    // expected URL.
  }
}

window.addEventListener('message', on_message, false);

function on_message(e) {
  if (e.origin != host_info['HTTPS_REMOTE_ORIGIN'] &&
      e.origin != host_info['HTTPS_ORIGIN'] ) {
    console.error('invalid origin: ' + e.origin);
    return;
  }
  var resolve = message_resolvers[e.data.id];
  delete message_resolvers[e.data.id];
  resolve(e.data.result);
}

function send_to_iframe(frame, message) {
  var message_id = next_message_id++;
  return new Promise(resolve => {
    message_resolvers[message_id] = resolve;
    frame.contentWindow.postMessage(
        {id: message_id, message},
        '*');
  });
}

async function get_all_clients(actual_ids) {
  const client_infos = {};
  client_infos['worker0'] = await get_clients(workers[0], actual_ids);
  client_infos['worker1'] = await get_clients(workers[1], actual_ids);
  client_infos['crossOriginWorker'] =
      await send_to_iframe(other_origin_frame,
                           {command: 'get_clients', actual_ids});
  return client_infos;
}

function get_clients(worker, actual_ids) {
  return new Promise(resolve => {
    var channel = new MessageChannel();
    channel.port1.onmessage = (msg) => {
      resolve(msg.data.clients);
    };
    worker.postMessage({command: 'getClients', actual_ids, port: channel.port2},
                       [channel.port2]);
  });
}

// Returns an array of the URLs that |worker| received fetch events for:
//   [url1, url2]
async function get_intercepted_urls(worker) {
  const infos = await get_request_infos(worker);
  return infos.map(info => { return info.url; });
}

// Returns the requests that |worker| received fetch events for. The return
// value is an array of format:
// [
//   {url: url1, resultingClientId: id},
//   {url: url2, resultingClientId: id}
// ]
function get_request_infos(worker) {
  return new Promise(resolve => {
    var channel = new MessageChannel();
    channel.port1.onmessage = (msg) => {
      resolve(msg.data.requestInfos);
    };
    worker.postMessage({command: 'getRequestInfos', port: channel.port2},
                       [channel.port2]);
  });
}

// Returns an array of the requests the workers received fetch events for:
// [
//   // Requests from workers[0].
//   [
//     {url: url1, resultingClientIdTag: tag1},
//     {url: url2, resultingClientIdTag: tag1}
//   ],
//
//   // Requests from workers[1].
//   [{url: url3, resultingClientIdTag: tag2}],
//
//   // Requests from the cross-origin worker.
//   []
// ]
async function get_all_request_infos()  {
  const request_infos = [];
  request_infos.push(await get_request_infos(workers[0]));
  request_infos.push(await get_request_infos(workers[1]));
  request_infos.push(await send_to_iframe(other_origin_frame,
                                          {command: 'get_request_infos'}));
  return request_infos;
}

let url;
let url1;
let url2;

// Normal redirect (from out-scope to in-scope).
url = SCOPE1;
redirect_test(
    OUT_SCOPE + 'url=' + encodeURIComponent(url),
    url,
    [[{url, resultingClientIdTag: 'x'}], [], []],
    'x',
    'Normal redirect to same-origin scope.');


url = SCOPE1 + '#ref';
redirect_test(
    OUT_SCOPE + 'url=' + encodeURIComponent(SCOPE1) + '#ref',
    url,
    [[{url, resultingClientIdTag: 'x'}], [], []],
    'x',
    'Normal redirect to same-origin scope with a hash fragment.');

url = SCOPE1 + '#ref2';
redirect_test(
    OUT_SCOPE + 'url=' + encodeURIComponent(url) + '#ref',
    url,
    [[{url, resultingClientIdTag: 'x'}], [], []],
    'x',
    'Normal redirect to same-origin scope with different hash fragments.');

url = OTHER_ORIGIN_SCOPE;
redirect_test(
    OUT_SCOPE + 'url=' + encodeURIComponent(url),
    url,
    [[], [], [{url, resultingClientIdTag: 'x'}]],
    'x',
    'Normal redirect to other-origin scope.');

// SW fallbacked redirect. SW doesn't handle the fetch request.
url = SCOPE1 + 'url=' + encodeURIComponent(OUT_SCOPE);
redirect_test(
    url,
    OUT_SCOPE,
    [[{url, resultingClientIdTag: 'x'}], [], []],
    'x',
    'SW-fallbacked redirect to same-origin out-scope.');

url1 = SCOPE1 + 'url=' + encodeURIComponent(SCOPE1);
url2 = SCOPE1;
redirect_test(
    url1,
    url2,
    [
      [
        {url: url1, resultingClientIdTag: 'x'},
        {url: url2, resultingClientIdTag: 'x'}
      ],
      [],
      []
    ],
    'x',
    'SW-fallbacked redirect to same-origin same-scope.');

url1 = SCOPE1 + 'url=' + encodeURIComponent(SCOPE1) + '#ref';
url2 = SCOPE1 + '#ref';
redirect_test(
    url1,
    url2,
    [
      [
        {url: url1, resultingClientIdTag: 'x'},
        {url: url2, resultingClientIdTag: 'x'}
      ],
      [],
      []
    ],
    'x',
    'SW-fallbacked redirect to same-origin same-scope with a hash fragment.');

url1 = SCOPE1 + 'url=' + encodeURIComponent(SCOPE1 + '#ref2') + '#ref';
url2 = SCOPE1 + '#ref2';
redirect_test(
    url1,
    url2,
    [
      [
        {url: url1, resultingClientIdTag: 'x'},
        {url: url2, resultingClientIdTag: 'x'}
      ],
      [],
      []
    ],
    'x',
    'SW-fallbacked redirect to same-origin same-scope with different hash ' +
    'fragments.');

url1 = SCOPE1 + 'url=' + encodeURIComponent(SCOPE2);
url2 = SCOPE2;
redirect_test(
    url1,
    url2,
    [
      [{url: url1, resultingClientIdTag: 'x'}],
      [{url: url2, resultingClientIdTag: 'x'}],
      []
    ],
    'x',
    'SW-fallbacked redirect to same-origin other-scope.');

url1 = SCOPE1 + 'url=' + encodeURIComponent(OTHER_ORIGIN_OUT_SCOPE);
url2 = OTHER_ORIGIN_OUT_SCOPE;
redirect_test(
    url1,
    url2,
    [[{url: url1, resultingClientIdTag: 'a'}], [], []],
    null,
    'SW-fallbacked redirect to other-origin out-scope.');

url1 = SCOPE1 + 'url=' + encodeURIComponent(OTHER_ORIGIN_SCOPE);
url2 = OTHER_ORIGIN_SCOPE;
redirect_test(
    url1,
    url2,
    [
      [{url: url1, resultingClientIdTag: 'a'}],
      [],
      [{url: url2, resultingClientIdTag: 'x'}]
    ],
    'x',
    'SW-fallbacked redirect to other-origin in-scope.');


url3 = SCOPE1;
url2 = OTHER_ORIGIN_SCOPE + 'url=' + encodeURIComponent(url3);
url1 = SCOPE1 + 'url=' + encodeURIComponent(url2);
redirect_test(
    url1,
    url3,
    [
      [
        {url: url1, resultingClientIdTag: 'a'},
        {url: url3, resultingClientIdTag: 'x'}
      ],
      [],
      [{url: url2, resultingClientIdTag: 'b'}]
    ],
    'x',
    'SW-fallbacked redirect to other-origin and back to same-origin.');

// SW generated redirect.
// SW: event.respondWith(Response.redirect(params['url']));
url1 = SCOPE1 + 'sw=gen&url=' + encodeURIComponent(OUT_SCOPE);
url2 = OUT_SCOPE;
redirect_test(
    url1,
    url2,
    [[{url: url1, resultingClientIdTag: 'x'}], [], []],
    'x',
    'SW-generated redirect to same-origin out-scope.');

url1 = SCOPE1 + 'sw=gen&url=' + encodeURIComponent(OUT_SCOPE) + '#ref';
url2 = OUT_SCOPE + '#ref';
redirect_test(
    url1,
    url2,
    [[{url: url1, resultingClientIdTag: 'x'}], [], []],
    'x',
    'SW-generated redirect to same-origin out-scope with a hash fragment.');

url1 = SCOPE1 + 'sw=gen&url=' + encodeURIComponent(OUT_SCOPE + '#ref2') + '#ref';
url2 = OUT_SCOPE + '#ref2';
redirect_test(
    url1,
    url2,
    [[{url: url1, resultingClientIdTag: 'x'}], [], []],
    'x',
    'SW-generated redirect to same-origin out-scope with different hash ' +
    'fragments.');

url1 = SCOPE1 + 'sw=gen&url=' + encodeURIComponent(SCOPE1);
url2 = SCOPE1;
redirect_test(
    url1,
    url2,
    [
      [
        {url: url1, resultingClientIdTag: 'x'},
        {url: url2, resultingClientIdTag: 'x'}
      ],
      [],
      []
    ],
    'x',
    'SW-generated redirect to same-origin same-scope.');

url1 = SCOPE1 + 'sw=gen&url=' + encodeURIComponent(SCOPE2);
url2 = SCOPE2;
redirect_test(
    url1,
    url2,
    [
      [{url: url1, resultingClientIdTag: 'x'}],
      [{url: url2, resultingClientIdTag: 'x'}],
      []
    ],
    'x',
    'SW-generated redirect to same-origin other-scope.');

url1 = SCOPE1 + 'sw=gen&url=' + encodeURIComponent(OTHER_ORIGIN_OUT_SCOPE);
url2 = OTHER_ORIGIN_OUT_SCOPE;
redirect_test(
    url1,
    url2,
    [[{url: url1, resultingClientIdTag: 'a'}], [], []],
    null,
    'SW-generated redirect to other-origin out-scope.');

url1 = SCOPE1 + 'sw=gen&url=' + encodeURIComponent(OTHER_ORIGIN_SCOPE);
url2 = OTHER_ORIGIN_SCOPE;
redirect_test(
    url1,
    url2,
    [
      [{url: url1, resultingClientIdTag: 'a'}],
      [],
      [{url: url2, resultingClientIdTag: 'x'}]
    ],
    'x',
    'SW-generated redirect to other-origin in-scope.');


// SW fetched redirect.
// SW: event.respondWith(fetch(event.request));
url1 = SCOPE1 + 'sw=fetch&url=' + encodeURIComponent(OUT_SCOPE)
url2 = OUT_SCOPE;
redirect_test(
    url1,
    url2,
    [[{url: url1, resultingClientIdTag: 'x'}], [], []],
    'x',
    'SW-fetched redirect to same-origin out-scope.');

url1 = SCOPE1 + 'sw=fetch&url=' + encodeURIComponent(SCOPE1);
url2 = SCOPE1;
redirect_test(
    url1,
    url2,
    [
      [
        {url: url1, resultingClientIdTag: 'x'},
        {url: url2, resultingClientIdTag: 'x'}
      ],
      [],
      []
    ],
    'x',
    'SW-fetched redirect to same-origin same-scope.');

url1 = SCOPE1 + 'sw=fetch&url=' + encodeURIComponent(SCOPE2);
url2 = SCOPE2;
redirect_test(
    url1,
    url2,
    [
      [{url: url1, resultingClientIdTag: 'x'}],
      [{url: url2, resultingClientIdTag: 'x'}],
      []
    ],
    'x',
    'SW-fetched redirect to same-origin other-scope.');

url1 = SCOPE1 + 'sw=fetch&url=' + encodeURIComponent(OTHER_ORIGIN_OUT_SCOPE);
url2 = OTHER_ORIGIN_OUT_SCOPE;
redirect_test(
    url1,
    url2,
    [[{url: url1, resultingClientIdTag: 'a'}], [], []],
    null,
    'SW-fetched redirect to other-origin out-scope.');

url1 = SCOPE1 + 'sw=fetch&url=' + encodeURIComponent(OTHER_ORIGIN_SCOPE);
url2 = OTHER_ORIGIN_SCOPE;
redirect_test(
    url1,
    url2,
    [
      [{url: url1, resultingClientIdTag: 'a'}],
      [],
      [{url: url2, resultingClientIdTag: 'x'}]
    ],
    'x',
    'SW-fetched redirect to other-origin in-scope.');


// SW responds with a fetch from a different url.
// SW: event.respondWith(fetch(params['url']));
url2 = SCOPE1;
url1 = SCOPE1 + 'sw=fetch-url&url=' + encodeURIComponent(url2);
redirect_test(
    url1,
    url1,
    [
      [
        {url: url1, resultingClientIdTag: 'x'}
      ],
      [],
      []
    ],
    'x',
    'SW-fetched response from different URL, same-origin same-scope.');


// Opaque redirect.
// SW: event.respondWith(fetch(
//         new Request(event.request.url, {redirect: 'manual'})));
url1 = SCOPE1 + 'sw=manual&url=' + encodeURIComponent(OUT_SCOPE);
url2 = OUT_SCOPE;
redirect_test(
    url1,
    url2,
    [[{url: url1, resultingClientIdTag: 'x'}], [], []],
    'x',
    'Redirect to same-origin out-scope with opaque redirect response.');

url1 = SCOPE1 + 'sw=manual&url=' + encodeURIComponent(SCOPE1);
url2 = SCOPE1;
redirect_test(
    url1,
    url2,
    [
      [
        {url: url1, resultingClientIdTag: 'x'},
        {url: url2, resultingClientIdTag: 'x'}
      ],
      [],
      []
    ],
    'x',
    'Redirect to same-origin same-scope with opaque redirect response.');

url1 = SCOPE1 + 'sw=manual&url=' + encodeURIComponent(SCOPE2);
url2 = SCOPE2;
redirect_test(
    url1,
    url2,
    [
      [{url: url1, resultingClientIdTag: 'x'}],
      [{url: url2, resultingClientIdTag: 'x'}],
      []
    ],
    'x',
    'Redirect to same-origin other-scope with opaque redirect response.');

url1 = SCOPE1 + 'sw=manual&url=' + encodeURIComponent(OTHER_ORIGIN_OUT_SCOPE);
url2 = OTHER_ORIGIN_OUT_SCOPE;
redirect_test(
    url1,
    url2,
    [[{url: url1, resultingClientIdTag: 'a'}], [], []],
    null,
    'Redirect to other-origin out-scope with opaque redirect response.');

url1 = SCOPE1 + 'sw=manual&url=' + encodeURIComponent(OTHER_ORIGIN_SCOPE);
url2 = OTHER_ORIGIN_SCOPE;
redirect_test(
    url1,
    url2,
    [
      [{url: url1, resultingClientIdTag: 'a'}],
      [],
      [{url: url2, resultingClientIdTag: 'x'}]
    ],
    'x',
    'Redirect to other-origin in-scope with opaque redirect response.');

url= SCOPE1 + 'sw=manual&noLocationRedirect';
redirect_test(
    url, url, [[{url, resultingClientIdTag: 'x'}], [], []],
    'x',
    'No location redirect response.');


// Opaque redirect passed through Cache.
// SW responds with an opaque redirectresponse from the Cache API.
url1 = SCOPE1 + 'sw=manualThroughCache&url=' + encodeURIComponent(OUT_SCOPE);
url2 = OUT_SCOPE;
redirect_test(
    url1,
    url2,
    [[{url: url1, resultingClientIdTag: 'x'}], [], []],
    'x',
    'Redirect to same-origin out-scope with opaque redirect response which ' +
    'is passed through Cache.');

url1 = SCOPE1 + 'sw=manualThroughCache&url=' + encodeURIComponent(SCOPE1);
url2 = SCOPE1;
redirect_test(
    url1,
    url2,
    [
      [
        {url: url1, resultingClientIdTag: 'x'},
        {url: url2, resultingClientIdTag: 'x'}
      ],
      [],
      []
    ],
    'x',
    'Redirect to same-origin same-scope with opaque redirect response which ' +
    'is passed through Cache.');

url1 = SCOPE1 + 'sw=manualThroughCache&url=' + encodeURIComponent(SCOPE2);
url2 = SCOPE2;
redirect_test(
    url1,
    url2,
    [
      [{url: url1, resultingClientIdTag: 'x'}],
      [{url: url2, resultingClientIdTag: 'x'}],
      []
    ],
    'x',
    'Redirect to same-origin other-scope with opaque redirect response which ' +
    'is passed through Cache.');

url1 = SCOPE1 + 'sw=manualThroughCache&url=' +
       encodeURIComponent(OTHER_ORIGIN_OUT_SCOPE);
url2 = OTHER_ORIGIN_OUT_SCOPE;
redirect_test(
    url1,
    url2,
    [[{url: url1, resultingClientIdTag: 'a'}], [], []],
    null,
    'Redirect to other-origin out-scope with opaque redirect response which ' +
    'is passed through Cache.');

url1 = SCOPE1 + 'sw=manualThroughCache&url=' +
       encodeURIComponent(OTHER_ORIGIN_SCOPE);
url2 = OTHER_ORIGIN_SCOPE;
redirect_test(
    url1,
    url2,
    [
      [{url: url1, resultingClientIdTag: 'a'}],
      [],
      [{url: url2, resultingClientIdTag: 'x'}],
    ],
    'x',
    'Redirect to other-origin in-scope with opaque redirect response which ' +
    'is passed through Cache.');

url = SCOPE1 + 'sw=manualThroughCache&noLocationRedirect';
redirect_test(
    url,
    url,
    [[{url, resultingClientIdTag: 'x'}], [], []],
    'x',
    'No location redirect response via Cache.');

// Clean up the test environment. This promise_test() needs to be the last one.
promise_test(async t => {
  registrations.forEach(async registration => {
    if (registration)
      await registration.unregister();
  });
  await send_to_iframe(other_origin_frame, {command: 'unregister'});
  other_origin_frame.remove();
}, 'clean up global state');
</script>
</body>