match.https.window.js (4509B)
1 // META: script=/common/get-host-info.sub.js 2 // META: script=/service-workers/service-worker/resources/test-helpers.sub.js 3 // META: script=resources/utils.js 4 5 'use strict'; 6 7 // Covers basic functionality provided by BackgroundFetchRegistration.match(All)?. 8 // https://wicg.github.io/background-fetch/#dom-backgroundfetchregistration-match 9 10 backgroundFetchTest(async (test, backgroundFetch) => { 11 const registrationId = 'matchexistingrequest'; 12 const registration = 13 await backgroundFetch.fetch(registrationId, 'resources/feature-name.txt'); 14 15 assert_equals(registration.id, registrationId); 16 17 const {type, eventRegistration, results} = await getMessageFromServiceWorker(); 18 assert_equals('backgroundfetchsuccess', type); 19 assert_equals(results.length, 1); 20 21 assert_equals(eventRegistration.id, registration.id); 22 assert_equals(eventRegistration.result, 'success'); 23 assert_equals(eventRegistration.failureReason, ''); 24 25 assert_true(results[0].url.includes('resources/feature-name.txt')); 26 assert_equals(results[0].status, 200); 27 assert_equals(results[0].text, 'Background Fetch'); 28 29 }, 'Matching to a single request should work'); 30 31 backgroundFetchTest(async (test, backgroundFetch) => { 32 const registrationId = 'matchmissingrequest'; 33 const registration = 34 await backgroundFetch.fetch(registrationId, 'resources/feature-name.txt'); 35 36 assert_equals(registration.id, registrationId); 37 38 const {type, eventRegistration, results} = await getMessageFromServiceWorker(); 39 assert_equals('backgroundfetchsuccess', type); 40 assert_equals(results.length, 0); 41 42 assert_equals(eventRegistration.id, registration.id); 43 assert_equals(eventRegistration.result, 'success'); 44 assert_equals(eventRegistration.failureReason, ''); 45 46 }, 'Matching to a non-existing request should work'); 47 48 backgroundFetchTest(async (test, backgroundFetch) => { 49 const registrationId = 'matchexistingrequesttwice'; 50 const registration = 51 await backgroundFetch.fetch(registrationId, 'resources/feature-name.txt'); 52 53 assert_equals(registration.id, registrationId); 54 55 const {type, eventRegistration, results} = await getMessageFromServiceWorker(); 56 assert_equals('backgroundfetchsuccess', type); 57 assert_equals(results.length, 2); 58 59 assert_equals(eventRegistration.id, registration.id); 60 assert_equals(eventRegistration.result, 'success'); 61 assert_equals(eventRegistration.failureReason, ''); 62 63 assert_true(results[0].url.includes('resources/feature-name.txt')); 64 assert_equals(results[0].status, 200); 65 assert_equals(results[0].text, 'Background Fetch'); 66 67 assert_true(results[1].url.includes('resources/feature-name.txt')); 68 assert_equals(results[1].status, 200); 69 assert_equals(results[1].text, 'Background Fetch'); 70 71 }, 'Matching multiple times on the same request works as expected.'); 72 73 backgroundFetchTest(async (test, backgroundFetch) => { 74 const registration = await backgroundFetch.fetch( 75 uniqueId(), ['resources/feature-name.txt', '/common/slow.py']); 76 77 const record = await registration.match('resources/feature-name.txt'); 78 const response = await record.responseReady; 79 assert_true(response.url.includes('resources/feature-name.txt')); 80 const completedResponseText = await response.text(); 81 assert_equals(completedResponseText, 'Background Fetch'); 82 83 }, 'Access to active fetches is supported.'); 84 85 backgroundFetchTest(async (test, backgroundFetch) => { 86 const registration = await backgroundFetch.fetch( 87 uniqueId(), [ 88 'resources/feature-name.txt', 89 'resources/feature-name.txt', 90 'resources/feature-name.txt?id=3', 91 new Request('resources/feature-name.txt', {method: 'PUT'}), 92 '/common/slow.py', 93 ]); 94 95 let matchedRecords = null; 96 97 // We should match all the duplicates. 98 matchedRecords = await registration.matchAll('resources/feature-name.txt'); 99 assert_equals(matchedRecords.length, 2); 100 101 // We should match the request with the query param as well. 102 matchedRecords = await registration.matchAll('resources/feature-name.txt', {ignoreSearch: true}); 103 assert_equals(matchedRecords.length, 3); 104 105 // We should match the PUT request as well. 106 matchedRecords = await registration.matchAll('resources/feature-name.txt', {ignoreMethod: true}); 107 assert_equals(matchedRecords.length, 3); 108 109 // We should match all requests. 110 matchedRecords = await registration.matchAll('resources/feature-name.txt', {ignoreSearch: true, ignoreMethod: true}); 111 assert_equals(matchedRecords.length, 4); 112 113 }, 'Match with query options.');