summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/background-fetch/match.https.window.js
blob: a9be5e706162f8e09c0191a97c8ebd72dde4ab41 (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
// META: script=/common/get-host-info.sub.js
// META: script=/service-workers/service-worker/resources/test-helpers.sub.js
// META: script=resources/utils.js

'use strict';

// Covers basic functionality provided by BackgroundFetchRegistration.match(All)?.
// https://wicg.github.io/background-fetch/#dom-backgroundfetchregistration-match

backgroundFetchTest(async (test, backgroundFetch) => {
  const registrationId = 'matchexistingrequest';
  const registration =
      await backgroundFetch.fetch(registrationId, 'resources/feature-name.txt');

  assert_equals(registration.id, registrationId);

  const {type, eventRegistration, results} = await getMessageFromServiceWorker();
  assert_equals('backgroundfetchsuccess', type);
  assert_equals(results.length, 1);

  assert_equals(eventRegistration.id, registration.id);
  assert_equals(eventRegistration.result, 'success');
  assert_equals(eventRegistration.failureReason, '');

  assert_true(results[0].url.includes('resources/feature-name.txt'));
  assert_equals(results[0].status, 200);
  assert_equals(results[0].text, 'Background Fetch');

}, 'Matching to a single request should work');

backgroundFetchTest(async (test, backgroundFetch) => {
  const registrationId = 'matchmissingrequest';
  const registration =
      await backgroundFetch.fetch(registrationId, 'resources/feature-name.txt');

  assert_equals(registration.id, registrationId);

  const {type, eventRegistration, results} = await getMessageFromServiceWorker();
  assert_equals('backgroundfetchsuccess', type);
  assert_equals(results.length, 0);

  assert_equals(eventRegistration.id, registration.id);
  assert_equals(eventRegistration.result, 'success');
  assert_equals(eventRegistration.failureReason, '');

}, 'Matching to a non-existing request should work');

backgroundFetchTest(async (test, backgroundFetch) => {
  const registrationId = 'matchexistingrequesttwice';
  const registration =
      await backgroundFetch.fetch(registrationId, 'resources/feature-name.txt');

  assert_equals(registration.id, registrationId);

  const {type, eventRegistration, results} = await getMessageFromServiceWorker();
  assert_equals('backgroundfetchsuccess', type);
  assert_equals(results.length, 2);

  assert_equals(eventRegistration.id, registration.id);
  assert_equals(eventRegistration.result, 'success');
  assert_equals(eventRegistration.failureReason, '');

  assert_true(results[0].url.includes('resources/feature-name.txt'));
  assert_equals(results[0].status, 200);
  assert_equals(results[0].text, 'Background Fetch');

  assert_true(results[1].url.includes('resources/feature-name.txt'));
  assert_equals(results[1].status, 200);
  assert_equals(results[1].text, 'Background Fetch');

}, 'Matching multiple times on the same request works as expected.');

backgroundFetchTest(async (test, backgroundFetch) => {
  const registration = await backgroundFetch.fetch(
      uniqueId(), ['resources/feature-name.txt', '/common/slow.py']);

  const record = await registration.match('resources/feature-name.txt');
  const response = await record.responseReady;
  assert_true(response.url.includes('resources/feature-name.txt'));
  const completedResponseText = await response.text();
  assert_equals(completedResponseText, 'Background Fetch');

}, 'Access to active fetches is supported.');

backgroundFetchTest(async (test, backgroundFetch) => {
  const registration = await backgroundFetch.fetch(
      uniqueId(), [
          'resources/feature-name.txt',
          'resources/feature-name.txt',
          'resources/feature-name.txt?id=3',
          new Request('resources/feature-name.txt', {method: 'PUT'}),
          '/common/slow.py',
      ]);

  let matchedRecords = null;

  // We should match all the duplicates.
  matchedRecords = await registration.matchAll('resources/feature-name.txt');
  assert_equals(matchedRecords.length, 2);

  // We should match the request with the query param as well.
  matchedRecords = await registration.matchAll('resources/feature-name.txt', {ignoreSearch: true});
  assert_equals(matchedRecords.length, 3);

  // We should match the PUT request as well.
  matchedRecords = await registration.matchAll('resources/feature-name.txt', {ignoreMethod: true});
  assert_equals(matchedRecords.length, 3);

  // We should match all requests.
  matchedRecords = await registration.matchAll('resources/feature-name.txt', {ignoreSearch: true, ignoreMethod: true});
  assert_equals(matchedRecords.length, 4);

}, 'Match with query options.');