Skip to content

Commit

Permalink
Fetch: test identity encoding for range requests
Browse files Browse the repository at this point in the history
  • Loading branch information
jakearchibald authored and David Heiberg committed Jun 7, 2018
1 parent 32d5fe0 commit 36e2f96
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 33 deletions.
30 changes: 30 additions & 0 deletions fetch/range/general.any.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// META: script=/common/utils.js

// Helpers that return headers objects with a particular guard
function headersGuardNone(fill) {
if (fill) return new Headers(fill);
Expand Down Expand Up @@ -58,3 +60,31 @@ test(() => {
assert_false(headers.has('Range'));
}, `Privileged header not allowed for guard type: request-no-cors`);

promise_test(async () => {
const wavURL = new URL('resources/long-wav.py', location);
const stashTakeURL = new URL('resources/stash-take.py', location);

function changeToken() {
const stashToken = token();
wavURL.searchParams.set('accept-encoding-key', stashToken);
stashTakeURL.searchParams.set('key', stashToken);
}

const rangeHeaders = [
'bytes=0-10',
'foo=0-10',
'foo',
''
];

for (const rangeHeader of rangeHeaders) {
changeToken();

await fetch(wavURL, {
headers: { Range: rangeHeader }
});

const response = await fetch(stashTakeURL);
assert_equals(await response.json(), 'identity', `Expect identity accept-encoding if range header is ${JSON.stringify(rangeHeader)}`);
}
}, `Fetch with range header will be sent with Accept-Encoding: identity`);
29 changes: 29 additions & 0 deletions fetch/range/general.window.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// META: script=resources/utils.js
// META: script=/common/utils.js

const onload = new Promise(r => window.addEventListener('load', r));

// It's weird that browsers do this, but it should continue to work.
promise_test(async t => {
await loadScript('resources/partial-script.py?pretend-offset=90000');
assert_true(self.scriptExecuted);
}, `Script executed from partial response`);

promise_test(async () => {
const wavURL = new URL('resources/long-wav.py', location);
const stashTakeURL = new URL('resources/stash-take.py', location);
const stashToken = token();
wavURL.searchParams.set('accept-encoding-key', stashToken);
stashTakeURL.searchParams.set('key', stashToken);

// The testing framework waits for window onload. If the audio element
// is appended before onload, it extends it, and the test times out.
await onload;

const audio = appendAudio(document, wavURL);
await new Promise(r => audio.addEventListener('progress', r));
audio.remove();

const response = await fetch(stashTakeURL);
assert_equals(await response.json(), 'identity', `Expect identity accept-encoding on media request`);
}, `Fetch with range header will be sent with Accept-Encoding: identity`);
7 changes: 0 additions & 7 deletions fetch/range/partial-script.window.js

This file was deleted.

23 changes: 20 additions & 3 deletions fetch/range/resources/long-wav.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,28 @@ def main(request, response):
response.headers.set("Cache-Control", "no-cache")

range_header = request.headers.get('Range', '')
range_header_match = range_header and re.search(r'^bytes=(\d*)-(\d*)$', range_header)
range_received_key = request.GET.first('range-received-key', '')
accept_encoding_key = request.GET.first('accept-encoding-key', '')

if range_received_key and range_header:
# Remove any current value
request.server.stash.take(range_received_key, '/fetch/range/')
# This is later collected using stash-take.py
request.stash.put(range_received_key, 'range-header-received', '/fetch/range/')
request.server.stash.put(range_received_key, 'range-header-received', '/fetch/range/')

if accept_encoding_key:
# Remove any current value
request.server.stash.take(
accept_encoding_key,
'/fetch/range/'
)
# This is later collected using stash-take.py
request.server.stash.put(
accept_encoding_key,
request.headers.get('Accept-Encoding', ''),
'/fetch/range/'
)

# Audio details
sample_rate = 8000
Expand All @@ -66,9 +83,9 @@ def main(request, response):
bytes_remaining_to_send = total_length
initial_write = ''

if range_header:
if range_header_match:
response.status = 206
start, end = re.search(r'^bytes=(\d*)-(\d*)$', range_header).groups()
start, end = range_header_match.groups()

start = int(start)
end = int(end) if end else 0
Expand Down
19 changes: 18 additions & 1 deletion fetch/range/resources/range-sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ addEventListener('fetch', event => {
case 'use-stored-ranged-response':
useStoredRangeResponse(event);
return;
case 'broadcast-accept-encoding':
broadcastAcceptEncoding(event);
return;
}
});

Expand Down Expand Up @@ -108,7 +111,7 @@ function rangeHeaderPassthroughTest(event) {
promise_test(async () => {
await fetch(event.request);
const response = await fetch('stash-take.py?key=' + key);
assert_equals(await response.json(), '"range-header-received"');
assert_equals(await response.json(), 'range-header-received');
resolve();
}, `Include range header in network request`);

Expand Down Expand Up @@ -140,3 +143,17 @@ function useStoredRangeResponse(event) {
return response.clone();
}());
}

function broadcastAcceptEncoding(event) {
/** @type Request */
const request = event.request;
const id = new URL(request.url).searchParams.get('id');

broadcast({
id,
acceptEncoding: request.headers.get('Accept-Encoding')
});

// Just send back any response, it isn't important for the test.
event.respondWith(new Response(''));
}
15 changes: 15 additions & 0 deletions fetch/range/resources/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,18 @@ function loadScript(url, { doc = document }={}) {
doc.body.appendChild(script);
})
}

/**
*
* @param {Document} document
* @param {string|URL} url
* @returns {HTMLAudioElement}
*/
function appendAudio(document, url) {
const audio = document.createElement('audio');
audio.muted = true;
audio.src = url;
audio.preload = true;
document.body.appendChild(audio);
return audio;
}
67 changes: 45 additions & 22 deletions fetch/range/sw.https.window.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,21 @@
// META: script=resources/utils.js

const { REMOTE_HOST } = get_host_info();
const SCOPE = 'resources/basic.html' + Math.random();

function appendAudio(document, url) {
const audio = document.createElement('audio');
audio.muted = true;
audio.src = url;
audio.preload = true;
document.body.appendChild(audio);
}
const BASE_SCOPE = 'resources/basic.html?';

async function cleanup() {
for (const iframe of document.querySelectorAll('.test-iframe')) {
iframe.parentNode.removeChild(iframe);
}

const reg = await navigator.serviceWorker.getRegistration(SCOPE);
if (reg) await reg.unregister();
for (const reg of await navigator.serviceWorker.getRegistrations()) {
await reg.unregister();
}
}

async function setupRegistration(t) {
async function setupRegistration(t, scope) {
await cleanup();
const reg = await navigator.serviceWorker.register('resources/range-sw.js', { scope: SCOPE });
const reg = await navigator.serviceWorker.register('resources/range-sw.js', { scope });
await wait_for_state(t, reg.installing, 'activated');
return reg;
}
Expand All @@ -35,14 +28,15 @@ function awaitMessage(obj, id) {
obj.addEventListener('message', function listener(event) {
if (event.data.id !== id) return;
obj.removeEventListener('message', listener);
resolve();
resolve(event.data);
});
});
}

promise_test(async t => {
const reg = await setupRegistration(t);
const iframe = await with_iframe(SCOPE);
const scope = BASE_SCOPE + Math.random();
const reg = await setupRegistration(t, scope);
const iframe = await with_iframe(scope);
const w = iframe.contentWindow;

// Trigger a cross-origin range request using media
Expand All @@ -55,8 +49,9 @@ promise_test(async t => {
}, `Defer range header filter tests to service worker`);

promise_test(async t => {
const reg = await setupRegistration(t);
const iframe = await with_iframe(SCOPE);
const scope = BASE_SCOPE + Math.random();
const reg = await setupRegistration(t, scope);
const iframe = await with_iframe(scope);
const w = iframe.contentWindow;

// Trigger a cross-origin range request using media
Expand All @@ -71,8 +66,9 @@ promise_test(async t => {
}, `Defer range header passthrough tests to service worker`);

promise_test(async t => {
await setupRegistration(t);
const iframe = await with_iframe(SCOPE);
const scope = BASE_SCOPE + Math.random();
await setupRegistration(t, scope);
const iframe = await with_iframe(scope);
const w = iframe.contentWindow;
const id = Math.random() + '';
const storedRangeResponse = awaitMessage(w.navigator.serviceWorker, id);
Expand Down Expand Up @@ -102,8 +98,9 @@ promise_test(async t => {
}, `Ranged response not allowed following no-cors ranged request`);

promise_test(async t => {
await setupRegistration(t);
const iframe = await with_iframe(SCOPE);
const scope = BASE_SCOPE + Math.random();
await setupRegistration(t, scope);
const iframe = await with_iframe(scope);
const w = iframe.contentWindow;
const id = Math.random() + '';
const storedRangeResponse = awaitMessage(w.navigator.serviceWorker, id);
Expand All @@ -126,3 +123,29 @@ promise_test(async t => {

assert_true(w.scriptExecuted, `Partial response should be executed`);
}, `Non-opaque ranged response executed`);

promise_test(async t => {
const scope = BASE_SCOPE + Math.random();
await setupRegistration(t, scope);
const iframe = await with_iframe(scope);
const w = iframe.contentWindow;
const fetchId = Math.random() + '';
const fetchBroadcast = awaitMessage(w.navigator.serviceWorker, fetchId);
const audioId = Math.random() + '';
const audioBroadcast = awaitMessage(w.navigator.serviceWorker, audioId);

const url = new URL('long-wav.py', w.location);
url.searchParams.set('action', 'broadcast-accept-encoding');
url.searchParams.set('id', fetchId);

await w.fetch(url, {
headers: { Range: 'bytes=0-10' }
});

assert_equals((await fetchBroadcast).acceptEncoding, null, "Accept-Encoding should not be set for fetch");

url.searchParams.set('id', audioId);
appendAudio(w.document, url);

assert_equals((await audioBroadcast).acceptEncoding, null, "Accept-Encoding should not be set for media");
}, `Accept-Encoding should not appear in a service worker`);

0 comments on commit 36e2f96

Please sign in to comment.