summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/service-workers/service-worker/fetch-event.https.html
blob: ce53f3c9bff3f7b4bc1377891adfdfd6a322f2d5 (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
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<meta name=timeout content=long>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/get-host-info.sub.js"></script>
<script src="/common/utils.js"></script>
<script src="resources/test-helpers.sub.js"></script>
<body>
<script>
var worker = 'resources/fetch-event-test-worker.js';
function wait(ms) {
    return new Promise(resolve => step_timeout(resolve, ms));
}

promise_test(async t => {
    const scope = 'resources/';
    const registration =
        await service_worker_unregister_and_register(t, worker, scope);
    await wait_for_state(t, registration.installing, 'activated');

    // This will happen after all other tests
    promise_test(t => {
        return registration.unregister();
      }, 'restore global state');
  }, 'global setup');

promise_test(t => {
    const page_url = 'resources/simple.html?headers';
    return with_iframe(page_url)
      .then(function(frame) {
          t.add_cleanup(() => { frame.remove(); });
          const headers = JSON.parse(frame.contentDocument.body.textContent);
          const header_names = {};
          for (const [name, value] of headers) {
            header_names[name] = true;
          }

          assert_true(
            header_names.hasOwnProperty('accept'),
            'request includes "Accept" header as inserted by Fetch'
          );
        });
  }, 'Service Worker headers in the request of a fetch event');

promise_test(t => {
    const page_url = 'resources/simple.html?string';
    return with_iframe(page_url)
      .then(function(frame) {
          t.add_cleanup(() => { frame.remove(); });
          assert_equals(
            frame.contentDocument.body.textContent,
            'Test string',
            'Service Worker should respond to fetch with a test string');
          assert_equals(
            frame.contentDocument.contentType,
            'text/plain',
            'The content type of the response created with a string should be text/plain');
          assert_equals(
            frame.contentDocument.characterSet,
            'UTF-8',
            'The character set of the response created with a string should be UTF-8');
        });
  }, 'Service Worker responds to fetch event with string');

promise_test(t => {
    const page_url = 'resources/simple.html?string';
    var frame;
    return with_iframe(page_url)
      .then(function(f) {
        frame = f;
        t.add_cleanup(() => { frame.remove(); });
        return frame.contentWindow.fetch(page_url + "#foo")
      })
      .then(function(response) { return response.text() })
      .then(function(text) {
          assert_equals(
            text,
            'Test string',
            'Service Worker should respond to fetch with a test string');
        });
  }, 'Service Worker responds to fetch event using request fragment with string');

promise_test(t => {
    const page_url = 'resources/simple.html?blob';
    return with_iframe(page_url)
      .then(frame => {
          t.add_cleanup(() => { frame.remove(); });
          assert_equals(
            frame.contentDocument.body.textContent,
            'Test blob',
            'Service Worker should respond to fetch with a test string');
        });
  }, 'Service Worker responds to fetch event with blob body');

promise_test(t => {
    const page_url = 'resources/simple.html?referrer';
      return with_iframe(page_url)
      .then(frame => {
          t.add_cleanup(() => { frame.remove(); });
          assert_equals(
            frame.contentDocument.body.textContent,
            'Referrer: ' + document.location.href,
            'Service Worker should respond to fetch with the referrer URL');
        });
  }, 'Service Worker responds to fetch event with the referrer URL');

promise_test(t => {
    const page_url = 'resources/simple.html?clientId';
    var frame;
    return with_iframe(page_url)
      .then(function(f) {
          frame = f;
          t.add_cleanup(() => { frame.remove(); });
          assert_equals(
            frame.contentDocument.body.textContent,
            'Client ID Not Found',
            'Service Worker should respond to fetch with a client id');
          return frame.contentWindow.fetch('resources/other.html?clientId');
        })
      .then(function(response) { return response.text(); })
      .then(function(response_text) {
          assert_equals(
            response_text.substr(0, 15),
            'Client ID Found',
            'Service Worker should respond to fetch with an existing client id');
        });
  }, 'Service Worker responds to fetch event with an existing client id');

promise_test(t => {
    const page_url = 'resources/simple.html?resultingClientId';
    const expected_found = 'Resulting Client ID Found';
    const expected_not_found = 'Resulting Client ID Not Found';
    return with_iframe(page_url)
      .then(function(frame) {
          t.add_cleanup(() => { frame.remove(); });
          assert_equals(
            frame.contentDocument.body.textContent.substr(0, expected_found.length),
            expected_found,
            'Service Worker should respond with an existing resulting client id for non-subresource requests');
          return frame.contentWindow.fetch('resources/other.html?resultingClientId');
        })
      .then(function(response) { return response.text(); })
      .then(function(response_text) {
          assert_equals(
            response_text.substr(0),
            expected_not_found,
            'Service Worker should respond with an empty resulting client id for subresource requests');
        });
  }, 'Service Worker responds to fetch event with the correct resulting client id');

promise_test(t => {
    const page_url = 'resources/simple.html?ignore';
    return with_iframe(page_url)
      .then(function(frame) {
          t.add_cleanup(() => { frame.remove(); });
          assert_equals(frame.contentDocument.body.textContent,
                        'Here\'s a simple html file.\n',
                        'Response should come from fallback to native fetch');
        });
  }, 'Service Worker does not respond to fetch event');

promise_test(t => {
    const page_url = 'resources/simple.html?null';
    return with_iframe(page_url)
      .then(function(frame) {
          t.add_cleanup(() => { frame.remove(); });
          assert_equals(frame.contentDocument.body.textContent,
                        '',
                        'Response should be the empty string');
        });
  }, 'Service Worker responds to fetch event with null response body');

promise_test(t => {
    const page_url = 'resources/simple.html?fetch';
    return with_iframe(page_url)
      .then(function(frame) {
          t.add_cleanup(() => { frame.remove(); });
          assert_equals(frame.contentDocument.body.textContent,
                        'Here\'s an other html file.\n',
                        'Response should come from fetched other file');
        });
  }, 'Service Worker fetches other file in fetch event');

// Creates a form and an iframe and does a form submission that navigates the
// frame to |action_url|. Returns the frame after navigation.
function submit_form(action_url) {
  return new Promise(resolve => {
      const frame = document.createElement('iframe');
      frame.name = 'post-frame';
      document.body.appendChild(frame);
      const form = document.createElement('form');
      form.target = frame.name;
      form.action = action_url;
      form.method = 'post';
      const input1 = document.createElement('input');
      input1.type = 'text';
      input1.value = 'testValue1';
      input1.name = 'testName1'
      form.appendChild(input1);
      const input2 = document.createElement('input');
      input2.type = 'text';
      input2.value = 'testValue2';
      input2.name = 'testName2'
      form.appendChild(input2);
      document.body.appendChild(form);
      frame.onload = function() {
        form.remove();
        resolve(frame);
      };
      form.submit();
    });
}

promise_test(t => {
    const page_url = 'resources/simple.html?form-post';
    return submit_form(page_url)
      .then(frame => {
          t.add_cleanup(() => { frame.remove(); });
          assert_equals(frame.contentDocument.body.textContent,
                        'POST:application/x-www-form-urlencoded:' +
                        'testName1=testValue1&testName2=testValue2');
        });
  }, 'Service Worker responds to fetch event with POST form');

promise_test(t => {
    // Add '?ignore' so the service worker falls back to network.
    const page_url = 'resources/echo-content.py?ignore';
    return submit_form(page_url)
      .then(frame => {
          t.add_cleanup(() => { frame.remove(); });
          assert_equals(frame.contentDocument.body.textContent,
                        'testName1=testValue1&testName2=testValue2');
        });
  }, 'Service Worker falls back to network in fetch event with POST form');

promise_test(t => {
    const page_url = 'resources/simple.html?multiple-respond-with';
    return with_iframe(page_url)
      .then(frame => {
          t.add_cleanup(() => { frame.remove(); });
          assert_equals(
            frame.contentDocument.body.textContent,
            '(0)(1)[InvalidStateError](2)[InvalidStateError]',
            'Multiple calls of respondWith must throw InvalidStateErrors.');
        });
  }, 'Multiple calls of respondWith must throw InvalidStateErrors');

promise_test(t => {
    const page_url = 'resources/simple.html?used-check';
    var first_frame;
    return with_iframe(page_url)
      .then(function(frame) {
          assert_equals(frame.contentDocument.body.textContent,
                        'Here\'s an other html file.\n',
                        'Response should come from fetched other file');
          first_frame = frame;
          t.add_cleanup(() => { first_frame.remove(); });
          return with_iframe(page_url);
        })
      .then(function(frame) {
          t.add_cleanup(() => { frame.remove(); });
          // When we access to the page_url in the second time, the content of the
          // response is generated inside the ServiceWorker. The body contains
          // the value of bodyUsed of the first response which is already
          // consumed by FetchEvent.respondWith method.
          assert_equals(
            frame.contentDocument.body.textContent,
            'bodyUsed: true',
            'event.respondWith must set the used flag.');
        });
  }, 'Service Worker event.respondWith must set the used flag');

promise_test(t => {
    const page_url = 'resources/simple.html?fragment-check';
    var fragment = '#/some/fragment';
    var first_frame;
    return with_iframe(page_url + fragment)
      .then(function(frame) {
          t.add_cleanup(() => { frame.remove(); });
          assert_equals(
            frame.contentDocument.body.textContent,
            'Fragment Found :' + fragment,
            'Service worker should expose URL fragments in request.');
        });
  }, 'Service Worker should expose FetchEvent URL fragments.');

promise_test(t => {
    const page_url = 'resources/simple.html?cache';
    var frame;
    var cacheTypes = [
      undefined, 'default', 'no-store', 'reload', 'no-cache', 'force-cache', 'only-if-cached'
    ];
    return with_iframe(page_url)
      .then(function(f) {
          frame = f;
          t.add_cleanup(() => { frame.remove(); });
          assert_equals(frame.contentWindow.document.body.textContent, 'default');
          var tests = cacheTypes.map(function(type) {
            return new Promise(function(resolve, reject) {
                var init = {cache: type};
                if (type === 'only-if-cached') {
                  // For privacy reasons, for the time being, only-if-cached
                  // requires the mode to be same-origin.
                  init.mode = 'same-origin';
                }
                return frame.contentWindow.fetch(page_url + '=' + type, init)
                  .then(function(response) { return response.text(); })
                  .then(function(response_text) {
                      var expected = (type === undefined) ? 'default' : type;
                      assert_equals(response_text, expected,
                                    'Service Worker should respond to fetch with the correct type');
                    })
                  .then(resolve)
                  .catch(reject);
              });
          });
          return Promise.all(tests);
        })
      .then(function() {
          return new Promise(function(resolve, reject) {
            frame.addEventListener('load', function onLoad() {
              frame.removeEventListener('load', onLoad);
              try {
                assert_equals(frame.contentWindow.document.body.textContent,
                              'no-cache');
                resolve();
              } catch (e) {
                reject(e);
              }
            });
            frame.contentWindow.location.reload();
          });
        });
  }, 'Service Worker responds to fetch event with the correct cache types');

promise_test(t => {
    const page_url = 'resources/simple.html?eventsource';
    var frame;

    function test_eventsource(opts) {
      return new Promise(function(resolve, reject) {
        var eventSource = new frame.contentWindow.EventSource(page_url, opts);
        eventSource.addEventListener('message', function(msg) {
          eventSource.close();
          try {
            var data = JSON.parse(msg.data);
            assert_equals(data.mode, 'cors',
                          'EventSource should make CORS requests.');
            assert_equals(data.cache, 'no-store',
                          'EventSource should bypass the http cache.');
            var expectedCredentials = opts.withCredentials ? 'include'
                                                           : 'same-origin';
            assert_equals(data.credentials, expectedCredentials,
                          'EventSource should pass correct credentials mode.');
            resolve();
          } catch (e) {
            reject(e);
          }
        });
        eventSource.addEventListener('error', function(e) {
          eventSource.close();
          reject('The EventSource fired an error event.');
        });
      });
    }

    return with_iframe(page_url)
      .then(function(f) {
          frame = f;
          t.add_cleanup(() => { frame.remove(); });
          return test_eventsource({ withCredentials: false });
        })
      .then(function() {
          return test_eventsource({ withCredentials: true });
        });
  }, 'Service Worker should intercept EventSource');

promise_test(t => {
    const page_url = 'resources/simple.html?integrity';
    var frame;
    var integrity_metadata = 'gs0nqru8KbsrIt5YToQqS9fYao4GQJXtcId610g7cCU=';

    return with_iframe(page_url)
      .then(function(f) {
          frame = f;
          t.add_cleanup(() => { frame.remove(); });
          // A request has associated integrity metadata (a string).
          // Unless stated otherwise, it is the empty string.
          assert_equals(
            frame.contentDocument.body.textContent, '');

          return frame.contentWindow.fetch(page_url, {'integrity': integrity_metadata});
        })
      .then(response => {
          return response.text();
        })
      .then(response_text => {
          assert_equals(response_text, integrity_metadata, 'integrity');
        });
  }, 'Service Worker responds to fetch event with the correct integrity_metadata');

// Test that the service worker can read FetchEvent#body when it is a string.
// It responds with request body it read.
promise_test(t => {
    // Set page_url to "?ignore" so the service worker falls back to network
    // for the main resource request, and add a suffix to avoid colliding
    // with other tests.
    const page_url = 'resources/simple.html?ignore-for-request-body-string';
    let frame;

    return with_iframe(page_url)
      .then(f => {
          frame = f;
          t.add_cleanup(() => { frame.remove(); });
          return frame.contentWindow.fetch('simple.html?request-body', {
              method: 'POST',
              body: 'i am the request body'
            });
        })
      .then(response => {
          return response.text();
        })
      .then(response_text => {
          assert_equals(response_text, 'i am the request body');
        });
  }, 'FetchEvent#body is a string');

// Test that the service worker can read FetchEvent#body when it is made from
// a ReadableStream. It responds with request body it read.
promise_test(async t => {
    const rs = new ReadableStream({start(c) {
      c.enqueue('i a');
      c.enqueue('m the request');
      step_timeout(t.step_func(() => {
        c.enqueue(' body');
        c.close();
      }, 10));
    }});

    // Set page_url to "?ignore" so the service worker falls back to network
    // for the main resource request, and add a suffix to avoid colliding
    // with other tests.
    const page_url = `resources/simple.html?ignore&id=${token()}`;

    const frame = await with_iframe(page_url);
    t.add_cleanup(() => { frame.remove(); });
    const res = await frame.contentWindow.fetch('simple.html?request-body', {
      method: 'POST',
      body: rs.pipeThrough(new TextEncoderStream()),
      duplex: 'half',
    });
    assert_equals(await res.text(), 'i am the request body');
  }, 'FetchEvent#body is a ReadableStream');

// Test that the request body is sent to network upon network fallback,
// for a string body.
promise_test(t => {
    // Set page_url to "?ignore" so the service worker falls back to network
    // for the main resource request, and add a suffix to avoid colliding
    // with other tests.
    const page_url = 'resources/?ignore-for-request-body-fallback-string';
    let frame;

    return with_iframe(page_url)
      .then(f => {
          frame = f;
          t.add_cleanup(() => { frame.remove(); });
          // Add "?ignore" so the service worker falls back to echo-content.py.
          const echo_url = '/fetch/api/resources/echo-content.py?ignore';
          return frame.contentWindow.fetch(echo_url, {
              method: 'POST',
              body: 'i am the request body'
            });
        })
      .then(response => {
          return response.text();
        })
      .then(response_text => {
          assert_equals(
              response_text,
              'i am the request body',
              'the network fallback request should include the request body');
        });
  }, 'FetchEvent#body is a string and is passed to network fallback');

// Test that the request body is sent to network upon network fallback,
// for a ReadableStream body.
promise_test(async t => {
    const rs = new ReadableStream({start(c) {
      c.enqueue('i a');
      c.enqueue('m the request');
      t.step_timeout(t.step_func(() => {
        c.enqueue(' body');
        c.close();
      }, 10));
    }});
    // Set page_url to "?ignore" so the service worker falls back to network
    // for the main resource request, and add a suffix to avoid colliding
    // with other tests.
    const page_url = 'resources/?ignore-for-request-body-fallback-string';
    const frame = await with_iframe(page_url);
    t.add_cleanup(() => { frame.remove(); });
    // Add "?ignore" so the service worker falls back to echo-content.py.
    const echo_url = '/fetch/api/resources/echo-content.py?ignore';
    const w = frame.contentWindow;
    await promise_rejects_js(t, w.TypeError,  w.fetch(echo_url, {
        method: 'POST',
        body: rs
    }));
  }, 'FetchEvent#body is a none Uint8Array ReadableStream and is passed to a service worker');

// Test that the request body is sent to network upon network fallback even when
// the request body is used in the service worker, for a string body.
promise_test(async t => {
    // Set page_url to "?ignore" so the service worker falls back to network
    // for the main resource request, and add a suffix to avoid colliding
    // with other tests.
    const page_url = 'resources/?ignore-for-request-body-fallback-string';

    const frame = await with_iframe(page_url);
    t.add_cleanup(() => { frame.remove(); });
    // Add "?use-and-ignore" so the service worker falls back to echo-content.py.
    const echo_url = '/fetch/api/resources/echo-content.py?use-and-ignore';
    const response = await frame.contentWindow.fetch(echo_url, {
        method: 'POST',
        body: 'i am the request body'
    });
    const text = await response.text();
    assert_equals(
        text,
        'i am the request body',
        'the network fallback request should include the request body');
  }, 'FetchEvent#body is a string, used and passed to network fallback');

// Test that the request body is sent to network upon network fallback even when
// the request body is used by clone() in the service worker, for a string body.
promise_test(async t => {
    // Set page_url to "?ignore" so the service worker falls back to network
    // for the main resource request, and add a suffix to avoid colliding
    // with other tests.
    const page_url = 'resources/?ignore-for-request-body-fallback-string';

    const frame = await with_iframe(page_url);
    t.add_cleanup(() => { frame.remove(); });
    // Add "?clone-and-ignore" so the service worker falls back to
    // echo-content.py.
    const echo_url = '/fetch/api/resources/echo-content.py?clone-and-ignore';
    const response = await frame.contentWindow.fetch(echo_url, {
        method: 'POST',
        body: 'i am the request body'
    });
    const text = await response.text();
    assert_equals(
        text,
        'i am the request body',
        'the network fallback request should include the request body');
  }, 'FetchEvent#body is a string, cloned and passed to network fallback');

// Test that the service worker can read FetchEvent#body when it is a blob.
// It responds with request body it read.
promise_test(t => {
    // Set page_url to "?ignore" so the service worker falls back to network
    // for the main resource request, and add a suffix to avoid colliding
    // with other tests.
    const page_url = 'resources/simple.html?ignore-for-request-body-blob';
    let frame;

    return with_iframe(page_url)
      .then(f => {
          frame = f;
          t.add_cleanup(() => { frame.remove(); });
          const blob = new Blob(['it\'s me the blob', ' ', 'and more blob!']);
          return frame.contentWindow.fetch('simple.html?request-body', {
              method: 'POST',
              body: blob
            });
        })
      .then(response => {
          return response.text();
        })
      .then(response_text => {
          assert_equals(response_text, 'it\'s me the blob and more blob!');
        });
  }, 'FetchEvent#body is a blob');

// Test that the request body is sent to network upon network fallback,
// for a blob body.
promise_test(t => {
    // Set page_url to "?ignore" so the service worker falls back to network
    // for the main resource request, and add a suffix to avoid colliding
    // with other tests.
    const page_url = 'resources/simple.html?ignore-for-request-body-fallback-blob';
    let frame;

    return with_iframe(page_url)
      .then(f => {
          frame = f;
          t.add_cleanup(() => { frame.remove(); });
          const blob = new Blob(['it\'s me the blob', ' ', 'and more blob!']);
          // Add "?ignore" so the service worker falls back to echo-content.py.
          const echo_url = '/fetch/api/resources/echo-content.py?ignore';
          return frame.contentWindow.fetch(echo_url, {
              method: 'POST',
              body: blob
            });
        })
      .then(response => {
          return response.text();
        })
      .then(response_text => {
          assert_equals(
              response_text,
              'it\'s me the blob and more blob!',
              'the network fallback request should include the request body');
        });
  }, 'FetchEvent#body is a blob and is passed to network fallback');

promise_test(async (t) => {
    const page_url = 'resources/simple.html?keepalive';
    const frame = await with_iframe(page_url);
    t.add_cleanup(() => { frame.remove(); });
    assert_equals(frame.contentDocument.body.textContent, 'false');
    const response = await frame.contentWindow.fetch(page_url, {keepalive: true});
    const text = await response.text();
    assert_equals(text, 'true');
  }, 'Service Worker responds to fetch event with the correct keepalive value');

promise_test(async (t) => {
    const page_url = 'resources/simple.html?isReloadNavigation';
    const frame = await with_iframe(page_url);
    t.add_cleanup(() => { frame.remove(); });
    assert_equals(frame.contentDocument.body.textContent,
                  'method = GET, isReloadNavigation = false');
    await new Promise((resolve) => {
      frame.addEventListener('load', resolve);
      frame.contentWindow.location.reload();
    });
    assert_equals(frame.contentDocument.body.textContent,
                  'method = GET, isReloadNavigation = true');
  }, 'FetchEvent#request.isReloadNavigation is true (location.reload())');

promise_test(async (t) => {
    const page_url = 'resources/simple.html?isReloadNavigation';
    const frame = await with_iframe(page_url);
    t.add_cleanup(() => { frame.remove(); });
    assert_equals(frame.contentDocument.body.textContent,
                  'method = GET, isReloadNavigation = false');
    await new Promise((resolve) => {
      frame.addEventListener('load', resolve);
      frame.contentWindow.history.go(0);
    });
    assert_equals(frame.contentDocument.body.textContent,
                  'method = GET, isReloadNavigation = true');
  }, 'FetchEvent#request.isReloadNavigation is true (history.go(0))');

promise_test(async (t) => {
    const page_url = 'resources/simple.html?isReloadNavigation';
    const frame = await with_iframe(page_url);
    t.add_cleanup(() => { frame.remove(); });
    assert_equals(frame.contentDocument.body.textContent,
                  'method = GET, isReloadNavigation = false');
    await new Promise((resolve) => {
      frame.addEventListener('load', resolve);
      const form = frame.contentDocument.createElement('form');
      form.method = 'POST';
      form.name = 'form';
      form.action = new Request(page_url).url;
      frame.contentDocument.body.appendChild(form);
      form.submit();
    });
    assert_equals(frame.contentDocument.body.textContent,
                  'method = POST, isReloadNavigation = false');
    await new Promise((resolve) => {
      frame.addEventListener('load', resolve);
      frame.contentWindow.location.reload();
    });
    assert_equals(frame.contentDocument.body.textContent,
                  'method = POST, isReloadNavigation = true');
  }, 'FetchEvent#request.isReloadNavigation is true (POST + location.reload())');

promise_test(async (t) => {
    const page_url = 'resources/simple.html?isReloadNavigation';
    const anotherUrl = new Request('resources/simple.html').url;
    let frame = await with_iframe(page_url);
    t.add_cleanup(() => { frame.remove(); });
    assert_equals(frame.contentDocument.body.textContent,
                  'method = GET, isReloadNavigation = false');
    // Use step_timeout(0) to ensure the history entry is created for Blink
    // and WebKit. See https://bugs.webkit.org/show_bug.cgi?id=42861.
    await wait(0);
    await new Promise((resolve) => {
      frame.addEventListener('load', resolve);
      frame.src = anotherUrl;
    });
    assert_equals(frame.contentDocument.body.textContent, "Here's a simple html file.\n");
    await new Promise((resolve) => {
      frame.addEventListener('load', resolve);
      frame.contentWindow.history.go(-1);
    });
    assert_equals(frame.contentDocument.body.textContent,
                  'method = GET, isReloadNavigation = false');
    await new Promise((resolve) => {
      frame.addEventListener('load', resolve);
      frame.contentWindow.history.go(0);
    });
    assert_equals(frame.contentDocument.body.textContent,
                  'method = GET, isReloadNavigation = true');
    await new Promise((resolve) => {
      frame.addEventListener('load', resolve);
      frame.contentWindow.history.go(1);
    });
    assert_equals(frame.contentDocument.body.textContent, "Here's a simple html file.\n");
  }, 'FetchEvent#request.isReloadNavigation is true (with history traversal)');

promise_test(async (t) => {
    const page_url = 'resources/simple.html?isHistoryNavigation';
    const anotherUrl = new Request('resources/simple.html?ignore').url;
    const frame = await with_iframe(page_url);
    t.add_cleanup(() => { frame.remove(); });
    assert_equals(frame.contentDocument.body.textContent,
                  'method = GET, isHistoryNavigation = false');
    // Use step_timeout(0) to ensure the history entry is created for Blink
    // and WebKit. See https://bugs.webkit.org/show_bug.cgi?id=42861.
    await wait(0);
    await new Promise((resolve) => {
      frame.addEventListener('load', resolve);
      frame.src = anotherUrl;
    });
    assert_equals(frame.contentDocument.body.textContent, "Here's a simple html file.\n");
    await new Promise((resolve) => {
      frame.addEventListener('load', resolve);
      frame.contentWindow.history.go(-1);
    });
    assert_equals(frame.contentDocument.body.textContent,
                  'method = GET, isHistoryNavigation = true');
  }, 'FetchEvent#request.isHistoryNavigation is true (with history.go(-1))');

promise_test(async (t) => {
    const page_url = 'resources/simple.html?isHistoryNavigation';
    const anotherUrl = new Request('resources/simple.html?ignore').url;
    const frame = await with_iframe(anotherUrl);
    t.add_cleanup(() => { frame.remove(); });
    assert_equals(frame.contentDocument.body.textContent, "Here's a simple html file.\n");
    // Use step_timeout(0) to ensure the history entry is created for Blink
    // and WebKit. See https://bugs.webkit.org/show_bug.cgi?id=42861.
    await wait(0);
    await new Promise((resolve) => {
      frame.addEventListener('load', resolve);
      frame.src = page_url;
    });
    assert_equals(frame.contentDocument.body.textContent,
                  'method = GET, isHistoryNavigation = false');
    await new Promise((resolve) => {
      frame.addEventListener('load', resolve);
      frame.contentWindow.history.go(-1);
    });
    await new Promise((resolve) => {
      frame.addEventListener('load', resolve);
      frame.contentWindow.history.go(1);
    });
    assert_equals(frame.contentDocument.body.textContent,
                  'method = GET, isHistoryNavigation = true');
  }, 'FetchEvent#request.isHistoryNavigation is true (with history.go(1))');

promise_test(async (t) => {
    const page_url = 'resources/simple.html?isHistoryNavigation';
    const anotherUrl = new Request('resources/simple.html?ignore').url;
    const frame =  await with_iframe(anotherUrl);
    t.add_cleanup(() => { frame.remove(); });
    assert_equals(frame.contentDocument.body.textContent, "Here's a simple html file.\n");
    // Use step_timeout(0) to ensure the history entry is created for Blink
    // and WebKit. See https://bugs.webkit.org/show_bug.cgi?id=42861.
    await wait(0);
    await new Promise((resolve) => {
      frame.addEventListener('load', resolve);
      frame.src = page_url;
    });
    assert_equals(frame.contentDocument.body.textContent,
                  'method = GET, isHistoryNavigation = false');
    await new Promise((resolve) => {
      frame.addEventListener('load', resolve);
      frame.contentWindow.history.go(-1);
    });
    await new Promise((resolve) => {
      frame.addEventListener('load', resolve);
      frame.contentWindow.history.go(1);
    });
    assert_equals(frame.contentDocument.body.textContent,
                  'method = GET, isHistoryNavigation = true');
    await new Promise((resolve) => {
      frame.addEventListener('load', resolve);
      frame.contentWindow.history.go(0);
    });
    assert_equals(frame.contentDocument.body.textContent,
                  'method = GET, isHistoryNavigation = false');
  }, 'FetchEvent#request.isHistoryNavigation is false (with history.go(0))');

promise_test(async (t) => {
    const page_url = 'resources/simple.html?isHistoryNavigation';
    const anotherUrl = new Request('resources/simple.html?ignore').url;
    const frame = await with_iframe(anotherUrl);
    t.add_cleanup(() => { frame.remove(); });
    assert_equals(frame.contentDocument.body.textContent, "Here's a simple html file.\n");
    // Use step_timeout(0) to ensure the history entry is created for Blink
    // and WebKit. See https://bugs.webkit.org/show_bug.cgi?id=42861.
    await wait(0);
    await new Promise((resolve) => {
      frame.addEventListener('load', resolve);
      frame.src = page_url;
    });
    assert_equals(frame.contentDocument.body.textContent,
                  'method = GET, isHistoryNavigation = false');
    await new Promise((resolve) => {
      frame.addEventListener('load', resolve);
      frame.contentWindow.history.go(-1);
    });
    await new Promise((resolve) => {
      frame.addEventListener('load', resolve);
      frame.contentWindow.history.go(1);
    });
    assert_equals(frame.contentDocument.body.textContent,
                  'method = GET, isHistoryNavigation = true');
    await new Promise((resolve) => {
      frame.addEventListener('load', resolve);
      frame.contentWindow.location.reload();
    });
    assert_equals(frame.contentDocument.body.textContent,
                  'method = GET, isHistoryNavigation = false');
  }, 'FetchEvent#request.isHistoryNavigation is false (with location.reload)');

promise_test(async (t) => {
    const page_url = 'resources/simple.html?isHistoryNavigation';
    const anotherUrl = new Request('resources/simple.html?ignore').url;
    const oneAnotherUrl = new Request('resources/simple.html?ignore2').url;

    const frame = await with_iframe(page_url);
    t.add_cleanup(() => { frame.remove(); });
    assert_equals(frame.contentDocument.body.textContent,
                  'method = GET, isHistoryNavigation = false');
    // Use step_timeout(0) to ensure the history entry is created for Blink
    // and WebKit. See https://bugs.webkit.org/show_bug.cgi?id=42861.
    await wait(0);
    await new Promise((resolve) => {
      frame.addEventListener('load', resolve);
      frame.src = anotherUrl;
    });
    assert_equals(frame.contentDocument.body.textContent, "Here's a simple html file.\n");
    await wait(0);
    await new Promise((resolve) => {
      frame.addEventListener('load', resolve);
      frame.src = oneAnotherUrl;
    });
    assert_equals(frame.contentDocument.body.textContent, "Here's a simple html file.\n");
    await new Promise((resolve) => {
      frame.addEventListener('load', resolve);
      frame.contentWindow.history.go(-2);
    });
    assert_equals(frame.contentDocument.body.textContent,
                  'method = GET, isHistoryNavigation = true');
  }, 'FetchEvent#request.isHistoryNavigation is true (with history.go(-2))');

promise_test(async (t) => {
    const page_url = 'resources/simple.html?isHistoryNavigation';
    const anotherUrl = new Request('resources/simple.html?ignore').url;
    const oneAnotherUrl = new Request('resources/simple.html?ignore2').url;
    const frame = await with_iframe(anotherUrl);
    t.add_cleanup(() => { frame.remove(); });
    assert_equals(frame.contentDocument.body.textContent, "Here's a simple html file.\n");
    // Use step_timeout(0) to ensure the history entry is created for Blink
    // and WebKit. See https://bugs.webkit.org/show_bug.cgi?id=42861.
    await wait(0);
    await new Promise((resolve) => {
      frame.addEventListener('load', resolve);
      frame.src = oneAnotherUrl;
    });
    assert_equals(frame.contentDocument.body.textContent, "Here's a simple html file.\n");
    await wait(0);
    await new Promise((resolve) => {
      frame.addEventListener('load', resolve);
      frame.src = page_url;
    });
    assert_equals(frame.contentDocument.body.textContent,
                  'method = GET, isHistoryNavigation = false');
    await new Promise((resolve) => {
      frame.addEventListener('load', resolve);
      frame.contentWindow.history.go(-2);
    });
    await new Promise((resolve) => {
      frame.addEventListener('load', resolve);
      frame.contentWindow.history.go(2);
    });
    assert_equals(frame.contentDocument.body.textContent,
                  'method = GET, isHistoryNavigation = true');
  }, 'FetchEvent#request.isHistoryNavigation is true (with history.go(2))');

promise_test(async (t) => {
    const page_url = 'resources/simple.html?isHistoryNavigation';
    const anotherUrl = new Request('resources/simple.html?ignore').url;
    const frame = await with_iframe(page_url);
    t.add_cleanup(() => { frame.remove(); });
    assert_equals(frame.contentDocument.body.textContent,
                  'method = GET, isHistoryNavigation = false');
    await new Promise((resolve) => {
      frame.addEventListener('load', resolve);
      const form = frame.contentDocument.createElement('form');
      form.method = 'POST';
      form.name = 'form';
      form.action = new Request(page_url).url;
      frame.contentDocument.body.appendChild(form);
      form.submit();
    });
    assert_equals(frame.contentDocument.body.textContent,
                  'method = POST, isHistoryNavigation = false');
    // Use step_timeout(0) to ensure the history entry is created for Blink
    // and WebKit. See https://bugs.webkit.org/show_bug.cgi?id=42861.
    await wait(0);
    await new Promise((resolve) => {
      frame.addEventListener('load', resolve);
      frame.src = anotherUrl;
    });
    assert_equals(frame.contentDocument.body.textContent, "Here's a simple html file.\n");
    await wait(0);
    await new Promise((resolve) => {
      frame.addEventListener('load', resolve);
      frame.contentWindow.history.go(-1);
    });
    assert_equals(frame.contentDocument.body.textContent,
                  'method = POST, isHistoryNavigation = true');
  }, 'FetchEvent#request.isHistoryNavigation is true (POST + history.go(-1))');

// When service worker responds with a Response, no XHR upload progress
// events are delivered.
promise_test(async t => {
    const page_url = 'resources/simple.html?ignore-for-request-body-string';
    const frame = await with_iframe(page_url);
    t.add_cleanup(() => { frame.remove(); });

    const xhr = new frame.contentWindow.XMLHttpRequest();
    xhr.open('POST', 'simple.html?request-body');
    xhr.upload.addEventListener('progress', t.unreached_func('progress'));
    xhr.upload.addEventListener('error', t.unreached_func('error'));
    xhr.upload.addEventListener('abort', t.unreached_func('abort'));
    xhr.upload.addEventListener('timeout', t.unreached_func('timeout'));
    xhr.upload.addEventListener('load', t.unreached_func('load'));
    xhr.upload.addEventListener('loadend', t.unreached_func('loadend'));
    xhr.send('i am the request body');

    await new Promise((resolve) => xhr.addEventListener('load', resolve));
  }, 'XHR upload progress events for response coming from SW');

// Upload progress events should be delivered for the network fallback case.
promise_test(async t => {
    const page_url = 'resources/simple.html?ignore-for-request-body-string';
    const frame = await with_iframe(page_url);
    t.add_cleanup(() => { frame.remove(); });

    let progress = false;
    let load = false;
    let loadend = false;

    const xhr = new frame.contentWindow.XMLHttpRequest();
    xhr.open('POST', '/fetch/api/resources/echo-content.py?ignore');
    xhr.upload.addEventListener('progress', () => progress = true);
    xhr.upload.addEventListener('error', t.unreached_func('error'));
    xhr.upload.addEventListener('abort', t.unreached_func('abort'));
    xhr.upload.addEventListener('timeout', t.unreached_func('timeout'));
    xhr.upload.addEventListener('load', () => load = true);
    xhr.upload.addEventListener('loadend', () => loadend = true);
    xhr.send('i am the request body');

    await new Promise((resolve) => xhr.addEventListener('load', resolve));
    assert_true(progress, 'progress');
    assert_true(load, 'load');
    assert_true(loadend, 'loadend');
  }, 'XHR upload progress events for network fallback');

promise_test(async t => {
    // Set page_url to "?ignore" so the service worker falls back to network
    // for the main resource request, and add a suffix to avoid colliding
    // with other tests.
    const page_url = 'resources/?ignore-for-request-body-fallback-string';

    const frame = await with_iframe(page_url);
    t.add_cleanup(() => { frame.remove(); });
    // Add "?clone-and-ignore" so the service worker falls back to
    // echo-content.py.
    const echo_url = '/fetch/api/resources/echo-content.py?status=421';
    const response = await frame.contentWindow.fetch(echo_url, {
        method: 'POST',
        body: 'text body'
    });
    assert_equals(response.status, 421);
    const text = await response.text();
    assert_equals(
        text,
        'text body. Request was sent 1 times.',
        'the network fallback request should include the request body');
  }, 'Fetch with POST with text on sw 421 response should not be retried.');
</script>
</body>