Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(277)

Side by Side Diff: LayoutTests/http/tests/serviceworker/chromium/resources/service-worker-mixed-response-worker.js

Issue 1226473002: Add LayoutTests for mixed range response handling. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: third Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 importScripts('../../../resources/get-host-info.js');
2
3 var HOST_INFO = get_host_info();
4 var PARTIAL_RESOURCE_PATH =
5 '/serviceworker/chromium/resources/service-worker-mixed-response.php';
6
7 function get_query_params(url) {
8 var search = (new URL(url)).search;
9 if (!search) {
10 return {};
11 }
12 var ret = {};
13 var params = search.substring(1).split('&');
14 params.forEach(function(param) {
15 var element = param.split('=');
16 ret[decodeURIComponent(element[0])] = decodeURIComponent(element[1]);
17 });
18 return ret;
19 }
20
21 function generate_partial_byte_response(position) {
22 return new Response(
23 'Ogg'.substr(position, 1),
24 {
25 status: 206,
26 headers: {
27 'content-type': 'audio/ogg',
28 // 12983 is the file size of media/content/silence.oga.
29 'content-range': 'bytes ' + position + '-' + position + '/12983'
30 }
31 });
32 }
33
34 function fetch_same_origin_partial_resource(position) {
35 return fetch(HOST_INFO['HTTP_ORIGIN'] + PARTIAL_RESOURCE_PATH +
36 '?position=' + position)
37 }
38
39 function fetch_cross_origin_partial_resource(position) {
40 return fetch(HOST_INFO['HTTP_REMOTE_ORIGIN'] + PARTIAL_RESOURCE_PATH +
41 '?position=' + position)
42 }
43
44 self.addEventListener('fetch', function(event) {
45 if (event.request.url.indexOf('blank.html') != -1) {
46 // The request is for the page load.
47 return;
48 }
49 var params = get_query_params(event.request.url);
50 if (event.request.headers.get('range') == 'bytes=0-') {
51 if (params['SW_FIRST'] == 'gen') {
52 event.respondWith(generate_partial_byte_response(0));
53 } else if (params['SW_FIRST'] == 'same') {
54 event.respondWith(fetch_same_origin_partial_resource(0));
55 } else if (params['SW_FIRST'] == 'cross') {
56 event.respondWith(fetch_cross_origin_partial_resource(0));
57 }
58 } else if (event.request.headers.get('range') == 'bytes=1-') {
59 if (params['SW_SECOND'] == 'gen') {
60 event.respondWith(generate_partial_byte_response(1));
61 } else if (params['SW_SECOND'] == 'same') {
62 event.respondWith(fetch_same_origin_partial_resource(1));
63 } else if (params['SW_SECOND'] == 'cross') {
64 event.respondWith(fetch_cross_origin_partial_resource(1));
65 }
66 }
67 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698