-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug 1452562 [wpt PR 10348] - Allow range headers to pass through a se…
…rvice worker, a=testonly Automatic update from web-platform-testsAllow range headers to pass through a service worker (#10348) Tests for whatwg/fetch#560 -- wpt-commits: fb6d16d92af29262b6137b79e61f0c4b136c6ac1 wpt-pr: 10348
- Loading branch information
1 parent
71986e7
commit dce6506
Showing
11 changed files
with
592 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Helpers that return headers objects with a particular guard | ||
function headersGuardNone(fill) { | ||
if (fill) return new Headers(fill); | ||
return new Headers(); | ||
} | ||
|
||
function headersGuardResponse(fill) { | ||
const opts = {}; | ||
if (fill) opts.headers = fill; | ||
return new Response('', opts).headers; | ||
} | ||
|
||
function headersGuardRequest(fill) { | ||
const opts = {}; | ||
if (fill) opts.headers = fill; | ||
return new Request('./', opts).headers; | ||
} | ||
|
||
function headersGuardRequestNoCors(fill) { | ||
const opts = { mode: 'no-cors' }; | ||
if (fill) opts.headers = fill; | ||
return new Request('./', opts).headers; | ||
} | ||
|
||
const headerGuardTypes = [ | ||
['none', headersGuardNone], | ||
['response', headersGuardResponse], | ||
['request', headersGuardRequest] | ||
]; | ||
|
||
for (const [guardType, createHeaders] of headerGuardTypes) { | ||
test(() => { | ||
// There are three ways to set headers. | ||
// Filling, appending, and setting. Test each: | ||
let headers = createHeaders({ Range: 'foo' }); | ||
assert_equals(headers.get('Range'), 'foo'); | ||
|
||
headers = createHeaders(); | ||
headers.append('Range', 'foo'); | ||
assert_equals(headers.get('Range'), 'foo'); | ||
|
||
headers = createHeaders(); | ||
headers.set('Range', 'foo'); | ||
assert_equals(headers.get('Range'), 'foo'); | ||
}, `Range header setting allowed for guard type: ${guardType}`); | ||
} | ||
|
||
test(() => { | ||
let headers = headersGuardRequestNoCors({ Range: 'foo' }); | ||
assert_false(headers.has('Range')); | ||
|
||
headers = headersGuardRequestNoCors(); | ||
headers.append('Range', 'foo'); | ||
assert_false(headers.has('Range')); | ||
|
||
headers = headersGuardRequestNoCors(); | ||
headers.set('Range', 'foo'); | ||
assert_false(headers.has('Range')); | ||
}, `Privileged header not allowed for guard type: request-no-cors`); | ||
|
7 changes: 7 additions & 0 deletions
7
testing/web-platform/tests/fetch/range/partial-script.window.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// META: script=resources/utils.js | ||
|
||
// 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`); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<!DOCTYPE html> |
111 changes: 111 additions & 0 deletions
111
testing/web-platform/tests/fetch/range/resources/long-wav.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
""" | ||
This generates a 30 minute silent wav, and is capable of | ||
responding to Range requests. | ||
""" | ||
import time | ||
import re | ||
import struct | ||
|
||
|
||
def create_wav_header(sample_rate, bit_depth, channels, duration): | ||
bytes_per_sample = bit_depth / 8 | ||
block_align = bytes_per_sample * channels | ||
byte_rate = sample_rate * block_align | ||
sub_chunk_2_size = duration * byte_rate | ||
|
||
data = b'' | ||
# ChunkID | ||
data += b'RIFF' | ||
# ChunkSize | ||
data += struct.pack('<L', 36 + sub_chunk_2_size) | ||
# Format | ||
data += b'WAVE' | ||
# Subchunk1ID | ||
data += b'fmt ' | ||
# Subchunk1Size | ||
data += struct.pack('<L', 16) | ||
# AudioFormat | ||
data += struct.pack('<H', 1) | ||
# NumChannels | ||
data += struct.pack('<H', channels) | ||
# SampleRate | ||
data += struct.pack('<L', sample_rate) | ||
# ByteRate | ||
data += struct.pack('<L', byte_rate) | ||
# BlockAlign | ||
data += struct.pack('<H', block_align) | ||
# BitsPerSample | ||
data += struct.pack('<H', bit_depth) | ||
# Subchunk2ID | ||
data += b'data' | ||
# Subchunk2Size | ||
data += struct.pack('<L', sub_chunk_2_size) | ||
|
||
return data | ||
|
||
|
||
def main(request, response): | ||
response.headers.set("Content-Type", "audio/wav") | ||
response.headers.set("Accept-Ranges", "bytes") | ||
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: | ||
# This is later collected using stash-take.py | ||
request.stash.put(range_received_key, 'range-header-received', '/fetch/range/') | ||
|
||
# Audio details | ||
sample_rate = 8000 | ||
bit_depth = 8 | ||
channels = 1 | ||
duration = 60 * 5 | ||
|
||
total_length = (sample_rate * bit_depth * channels * duration) / 8 | ||
bytes_remaining_to_send = total_length | ||
initial_write = '' | ||
|
||
if range_header: | ||
response.status = 206 | ||
start, end = re.search(r'^bytes=(\d*)-(\d*)$', range_header).groups() | ||
|
||
start = int(start) | ||
end = int(end) if end else 0 | ||
|
||
if end: | ||
bytes_remaining_to_send = (end + 1) - start | ||
else: | ||
bytes_remaining_to_send = total_length - start | ||
|
||
wav_header = create_wav_header(sample_rate, bit_depth, channels, duration) | ||
|
||
if start < len(wav_header): | ||
initial_write = wav_header[start:] | ||
|
||
if bytes_remaining_to_send < len(initial_write): | ||
initial_write = initial_write[0:bytes_remaining_to_send] | ||
|
||
content_range = "bytes {}-{}/{}".format(start, end or total_length - 1, total_length) | ||
|
||
response.headers.set("Content-Range", content_range) | ||
else: | ||
initial_write = create_wav_header(sample_rate, bit_depth, channels, duration) | ||
|
||
response.headers.set("Content-Length", bytes_remaining_to_send) | ||
|
||
response.write_status_headers() | ||
response.writer.write(initial_write) | ||
|
||
bytes_remaining_to_send -= len(initial_write) | ||
|
||
while bytes_remaining_to_send > 0: | ||
if not response.writer.flush(): | ||
break | ||
|
||
to_send = b'\x00' * min(bytes_remaining_to_send, sample_rate) | ||
bytes_remaining_to_send -= len(to_send) | ||
|
||
response.writer.write(to_send) | ||
# Throttle the stream | ||
time.sleep(0.5) |
30 changes: 30 additions & 0 deletions
30
testing/web-platform/tests/fetch/range/resources/partial-script.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
""" | ||
This generates a partial response containing valid JavaScript. | ||
""" | ||
|
||
|
||
def main(request, response): | ||
require_range = request.GET.first('require-range', '') | ||
pretend_offset = int(request.GET.first('pretend-offset', '0')) | ||
range_header = request.headers.get('Range', '') | ||
|
||
if require_range and not range_header: | ||
response.set_error(412, "Range header required") | ||
response.write() | ||
return | ||
|
||
response.headers.set("Content-Type", "text/plain") | ||
response.headers.set("Accept-Ranges", "bytes") | ||
response.headers.set("Cache-Control", "no-cache") | ||
response.status = 206 | ||
|
||
to_send = 'self.scriptExecuted = true;' | ||
length = len(to_send) | ||
|
||
content_range = "bytes {}-{}/{}".format( | ||
pretend_offset, pretend_offset + length - 1, pretend_offset + length) | ||
|
||
response.headers.set("Content-Range", content_range) | ||
response.headers.set("Content-Length", length) | ||
|
||
response.content = to_send |
Oops, something went wrong.