Skip to content

Commit

Permalink
Testing range header is actually sent to the server
Browse files Browse the repository at this point in the history
  • Loading branch information
jakearchibald committed Apr 6, 2018
1 parent 5310b7d commit 9e34bb1
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 22 deletions.
4 changes: 4 additions & 0 deletions fetch/privileged-headers/resources/long-wav.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ def main(request, response):
response.headers.set("Cache-Control", "no-cache")

range_header = request.headers.get('Range', '')
range_received_key = request.GET.first('range-received-key', '')

if range_received_key and range_header:
request.stash.put(range_received_key, 'range-header-received', '/fetch/privileged-headers/')

sample_rate = 8000
bit_depth = 8
Expand Down
75 changes: 59 additions & 16 deletions fetch/privileged-headers/resources/range-sw.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,35 @@
importScripts('/resources/testharness.js');

setup({
explicit_done: true
});
setup({ explicit_done: true });

function assert_range_request(request, expectedRangeHeader, name) {
assert_equals(request.headers.get('Range'), expectedRangeHeader, name);
}

let gotRangeResponse = false;

addEventListener('fetch', event => {
const { request } = event;
/** @type Request */
const request = event.request;
const url = new URL(request.url);
const action = url.searchParams.get('action');

switch (action) {
case 'range-header-filter-test':
rangeHeaderFilterTest(request);
return;
case 'range-header-passthrough-test':
rangeHeaderPassthroughTest(event);
return;
}
});

let gotRangeResponse = false;

/**
* @param {Request} request
*/
function rangeHeaderFilterTest(request) {
if (!request.headers.has('Range') || gotRangeResponse) return;
// Avoid running the test twice
gotRangeResponse = true;

const rangeValue = request.headers.get('Range');
Expand All @@ -38,40 +54,67 @@ addEventListener('fetch', event => {

headers = new Request(request).headers;
headers.delete('does-not-exist');
assert_equals(header.get('Range'), rangeValue, `Preserved if no header removed`);
assert_equals(headers.get('Range'), rangeValue, `Preserved if no header removed`);

headers = new Request(request).headers;
headers.append('foo', 'bar');
assert_equals(header.get('Range'), rangeValue, `Preserved if silent-failure on append (due to request-no-cors guard)`);
assert_equals(headers.get('Range'), rangeValue, `Preserved if silent-failure on append (due to request-no-cors guard)`);

headers = new Request(request).headers;
headers.set('foo', 'bar');
assert_equals(header.get('Range'), rangeValue, `Preserved if silent-failure on set (due to request-no-cors guard)`);
assert_equals(headers.get('Range'), rangeValue, `Preserved if silent-failure on set (due to request-no-cors guard)`);

headers = new Request(request).headers;
headers.append('Range', 'foo');
assert_equals(header.get('Range'), rangeValue, `Preserved if silent-failure on append (due to request-no-cors guard)`);
assert_equals(headers.get('Range'), rangeValue, `Preserved if silent-failure on append (due to request-no-cors guard)`);

headers = new Request(request).headers;
headers.set('Range', 'foo');
assert_equals(header.get('Range'), rangeValue, `Preserved if silent-failure on set (due to request-no-cors guard)`);
assert_equals(headers.get('Range'), rangeValue, `Preserved if silent-failure on set (due to request-no-cors guard)`);

headers = new Request(request).headers;
headers.append('Accept', 'whatever');
assert_equals(header.get('Range'), null, `Stripped if header successfully appended`);
assert_equals(headers.get('Range'), null, `Stripped if header successfully appended`);

headers = new Request(request).headers;
headers.set('Accept', 'whatever');
assert_equals(header.get('Range'), null, `Stripped if header successfully set`);
assert_equals(headers.get('Range'), null, `Stripped if header successfully set`);

headers = new Request(request).headers;
headers.delete('Accept');
assert_equals(header.get('Range'), null, `Stripped if header successfully deleted`);
assert_equals(headers.get('Range'), null, `Stripped if header successfully deleted`);

headers = new Request(request).headers;
headers.delete('Range');
assert_equals(header.get('Range'), null, `Stripped if range header successfully deleted`);
assert_equals(headers.get('Range'), null, `Stripped if range header successfully deleted`);
}, "Headers correctly filtered");

done();
});
}

function rangeHeaderPassthroughTest(event) {
/** @type Request */
const request = event.request;
const url = new URL(request.url);
const key = url.searchParams.get('range-received-key');

if (!request.headers.has('Range') || gotRangeResponse) return;
// Avoid running the test twice
gotRangeResponse = true;

let waitUntilResolve;
const waitUntilPromise = new Promise(r => waitUntilResolve = r);
event.waitUntil(waitUntilPromise);

// Just send back any response, it isn't important for the test.
event.respondWith(new Response(''));

promise_test(async () => {
await fetch(event.request);
const response = await fetch('stash-take.py?key=' + key);
assert_equals(await response.json(), 'range-header-received');
waitUntilResolve();
}, `Include range header in network request`);

done();
}
7 changes: 7 additions & 0 deletions fetch/privileged-headers/resources/stash-take.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from wptserve.handlers import json_handler


@json_handler
def main(request, response):
key = request.GET.first("key")
return request.server.stash.take(key, '/fetch/privileged-headers/')
28 changes: 24 additions & 4 deletions fetch/privileged-headers/sw.https.window.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// META: script=../../../service-workers/service-worker/resources/test-helpers.sub.js
// META: script=/common/utils.js

const SCOPE = 'resources/basic.html';

Expand All @@ -23,12 +24,31 @@ promise_test(async t => {
const iframe = await with_iframe(SCOPE);
const w = iframe.contentWindow;

// Trigger a range request
// Trigger a range request using media
const url = new URL('long-wav.py?action=range-header-filter-test', w.location);
url.hostname = 'www.web-platform.test';
const audio = w.document.createElement('audio');
audio.muted = true;
audio.src = 'long-wav.py';
audio.src = url;
audio.preload = true;
w.document.body.appendChild(audio);

fetch_tests_from_worker(reg.active);
}, "Defer tests to service worker");
await fetch_tests_from_worker(reg.active);
}, `Defer range header filter tests to service worker`);

promise_test(async t => {
const reg = await setupRegistration(t);
const iframe = await with_iframe(SCOPE);
const w = iframe.contentWindow;

// Trigger a range request using media
const url = new URL('long-wav.py?action=range-header-passthrough-test&range-received-key=' + token(), w.location);
url.hostname = 'www.web-platform.test';
const audio = w.document.createElement('audio');
audio.muted = true;
audio.src = url;
audio.preload = true;
w.document.body.appendChild(audio);

await fetch_tests_from_worker(reg.active);
}, `Defer range header passthrough tests to service worker`);
14 changes: 12 additions & 2 deletions resources/testharness.js
Original file line number Diff line number Diff line change
Expand Up @@ -1741,6 +1741,12 @@ policies and contribution forms [3].
}
};

if (self.Promise) {
this.done = new Promise(function(resolve) {
this_obj.doneResolve = resolve;
});
}

this.message_target.addEventListener("message", this.message_handler);
}

Expand Down Expand Up @@ -1790,6 +1796,8 @@ policies and contribution forms [3].
this.running = false;
this.remote = null;
this.message_target = null;
if (this.doneResolve) this.doneResolve();

if (tests.all_done()) {
tests.complete();
}
Expand Down Expand Up @@ -2146,11 +2154,13 @@ policies and contribution forms [3].
return;
}

this.pending_remotes.push(this.create_remote_worker(worker));
var remoteContext = this.create_remote_worker(worker);
this.pending_remotes.push(remoteContext);
return remoteContext.done;
};

function fetch_tests_from_worker(port) {
tests.fetch_tests_from_worker(port);
return tests.fetch_tests_from_worker(port);
}
expose(fetch_tests_from_worker, 'fetch_tests_from_worker');

Expand Down

0 comments on commit 9e34bb1

Please sign in to comment.