fetch-event-test-worker.js (6625B)
1 function handleHeaders(event) { 2 const headers = Array.from(event.request.headers); 3 event.respondWith(new Response(JSON.stringify(headers))); 4 } 5 6 function handleString(event) { 7 event.respondWith(new Response('Test string')); 8 } 9 10 function handleBlob(event) { 11 event.respondWith(new Response(new Blob(['Test blob']))); 12 } 13 14 function handleReferrer(event) { 15 event.respondWith(new Response(new Blob( 16 ['Referrer: ' + event.request.referrer]))); 17 } 18 19 function handleReferrerPolicy(event) { 20 event.respondWith(new Response(new Blob( 21 ['ReferrerPolicy: ' + event.request.referrerPolicy]))); 22 } 23 24 function handleReferrerFull(event) { 25 event.respondWith(new Response(new Blob( 26 ['Referrer: ' + event.request.referrer + '\n' + 27 'ReferrerPolicy: ' + event.request.referrerPolicy]))); 28 } 29 30 function handleClientId(event) { 31 var body; 32 if (event.clientId !== "") { 33 body = 'Client ID Found: ' + event.clientId; 34 } else { 35 body = 'Client ID Not Found'; 36 } 37 event.respondWith(new Response(body)); 38 } 39 40 function handleResultingClientId(event) { 41 var body; 42 if (event.resultingClientId !== "") { 43 body = 'Resulting Client ID Found: ' + event.resultingClientId; 44 } else { 45 body = 'Resulting Client ID Not Found'; 46 } 47 event.respondWith(new Response(body)); 48 } 49 50 function handleNullBody(event) { 51 event.respondWith(new Response()); 52 } 53 54 function handleFetch(event) { 55 event.respondWith(fetch('other.html')); 56 } 57 58 function handleFormPost(event) { 59 event.respondWith(new Promise(function(resolve) { 60 event.request.text() 61 .then(function(result) { 62 resolve(new Response(event.request.method + ':' + 63 event.request.headers.get('Content-Type') + ':' + 64 result)); 65 }); 66 })); 67 } 68 69 function handleMultipleRespondWith(event) { 70 var logForMultipleRespondWith = ''; 71 for (var i = 0; i < 3; ++i) { 72 logForMultipleRespondWith += '(' + i + ')'; 73 try { 74 event.respondWith(new Promise(function(resolve) { 75 setTimeout(function() { 76 resolve(new Response(logForMultipleRespondWith)); 77 }, 0); 78 })); 79 } catch (e) { 80 logForMultipleRespondWith += '[' + e.name + ']'; 81 } 82 } 83 } 84 85 var lastResponseForUsedCheck = undefined; 86 87 function handleUsedCheck(event) { 88 if (!lastResponseForUsedCheck) { 89 event.respondWith(fetch('other.html').then(function(response) { 90 lastResponseForUsedCheck = response; 91 return response; 92 })); 93 } else { 94 event.respondWith(new Response( 95 'bodyUsed: ' + lastResponseForUsedCheck.bodyUsed)); 96 } 97 } 98 function handleFragmentCheck(event) { 99 var body; 100 if (event.request.url.indexOf('#') === -1) { 101 body = 'Fragment Not Found'; 102 } else { 103 body = 'Fragment Found :' + 104 event.request.url.substring(event.request.url.indexOf('#')); 105 } 106 event.respondWith(new Response(body)); 107 } 108 function handleCache(event) { 109 event.respondWith(new Response(event.request.cache)); 110 } 111 function handleEventSource(event) { 112 if (event.request.mode === 'navigate') { 113 return; 114 } 115 var data = { 116 mode: event.request.mode, 117 cache: event.request.cache, 118 credentials: event.request.credentials 119 }; 120 var body = 'data:' + JSON.stringify(data) + '\n\n'; 121 event.respondWith(new Response(body, { 122 headers: { 'Content-Type': 'text/event-stream' } 123 } 124 )); 125 } 126 127 function handleIntegrity(event) { 128 event.respondWith(new Response(event.request.integrity)); 129 } 130 131 function handleRequestBody(event) { 132 event.respondWith(event.request.text().then(text => { 133 return new Response(text); 134 })); 135 } 136 137 function handleKeepalive(event) { 138 event.respondWith(new Response(event.request.keepalive)); 139 } 140 141 function handleIsReloadNavigation(event) { 142 const request = event.request; 143 const body = 144 `method = ${request.method}, ` + 145 `isReloadNavigation = ${request.isReloadNavigation}`; 146 event.respondWith(new Response(body)); 147 } 148 149 function handleIsHistoryNavigation(event) { 150 const request = event.request; 151 const body = 152 `method = ${request.method}, ` + 153 `isHistoryNavigation = ${request.isHistoryNavigation}`; 154 event.respondWith(new Response(body)); 155 } 156 157 function handleUseAndIgnore(event) { 158 const request = event.request; 159 request.text(); 160 return; 161 } 162 163 function handleCloneAndIgnore(event) { 164 const request = event.request; 165 request.clone().text(); 166 return; 167 } 168 169 var handle_status_count = 0; 170 function handleStatus(event) { 171 handle_status_count++; 172 event.respondWith(async function() { 173 const res = await fetch(event.request); 174 const text = await res.text(); 175 return new Response(`${text}. Request was sent ${handle_status_count} times.`, 176 {"status": new URL(event.request.url).searchParams.get("status")}); 177 }()); 178 } 179 180 self.addEventListener('fetch', function(event) { 181 var url = event.request.url; 182 var handlers = [ 183 { pattern: '?headers', fn: handleHeaders }, 184 { pattern: '?string', fn: handleString }, 185 { pattern: '?blob', fn: handleBlob }, 186 { pattern: '?referrerFull', fn: handleReferrerFull }, 187 { pattern: '?referrerPolicy', fn: handleReferrerPolicy }, 188 { pattern: '?referrer', fn: handleReferrer }, 189 { pattern: '?clientId', fn: handleClientId }, 190 { pattern: '?resultingClientId', fn: handleResultingClientId }, 191 { pattern: '?ignore', fn: function() {} }, 192 { pattern: '?null', fn: handleNullBody }, 193 { pattern: '?fetch', fn: handleFetch }, 194 { pattern: '?form-post', fn: handleFormPost }, 195 { pattern: '?multiple-respond-with', fn: handleMultipleRespondWith }, 196 { pattern: '?used-check', fn: handleUsedCheck }, 197 { pattern: '?fragment-check', fn: handleFragmentCheck }, 198 { pattern: '?cache', fn: handleCache }, 199 { pattern: '?eventsource', fn: handleEventSource }, 200 { pattern: '?integrity', fn: handleIntegrity }, 201 { pattern: '?request-body', fn: handleRequestBody }, 202 { pattern: '?keepalive', fn: handleKeepalive }, 203 { pattern: '?isReloadNavigation', fn: handleIsReloadNavigation }, 204 { pattern: '?isHistoryNavigation', fn: handleIsHistoryNavigation }, 205 { pattern: '?use-and-ignore', fn: handleUseAndIgnore }, 206 { pattern: '?clone-and-ignore', fn: handleCloneAndIgnore }, 207 { pattern: '?status', fn: handleStatus }, 208 ]; 209 210 var handler = null; 211 for (var i = 0; i < handlers.length; ++i) { 212 if (url.indexOf(handlers[i].pattern) != -1) { 213 handler = handlers[i]; 214 break; 215 } 216 } 217 218 if (handler) { 219 handler.fn(event); 220 } else { 221 event.respondWith(new Response(new Blob( 222 ['Service Worker got an unexpected request: ' + url]))); 223 } 224 });