Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for invalid range responses #33990

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions fetch/range/invalid-content-range.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<!DOCTYPE html>
<meta name="timeout" content="long">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<body>
<script>
function range_rewrite_test(response, expect, label) {
promise_test(async t => {
const url = new URL('resources/video-with-invalid-range.py', location.href);
const params = new URLSearchParams();
if (response)
params.set('response', response);
url.search = params.toString();
const video = document.createElement('video');
video.autoplay = true;
video.muted = true;
video.src = url.toString();
const timeout = new Promise(resolve => t.step_timeout(() => resolve('timeout'), 1000));
const ok = new Promise(resolve => video.addEventListener('play', () => resolve('ok')));
t.add_cleanup(() => video.remove());
document.body.appendChild(video);
const result = await Promise.any([timeout, ok]);
assert_equals(result, expect ? 'ok' : 'timeout');
}, `A ${response} Content-Range response should be ${expect ? 'valid' : 'invalid'}`);
}

range_rewrite_test('', true);
range_rewrite_test('0-1/1000', false);
range_rewrite_test('bytes -10-100', false);
range_rewrite_test('7-4', false);
range_rewrite_test('bytes ABC-1', false);
range_rewrite_test('80', false);
range_rewrite_test('bytes something', false);
range_rewrite_test('-12', false);
range_rewrite_test('bytes 3 3', false);
</script>
</body>
24 changes: 24 additions & 0 deletions fetch/range/resources/video-with-invalid-range.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import re
import os
import json
from wptserve.utils import isomorphic_decode

def main(request, response):
path = os.path.join(request.doc_root, u"media", "sine440.mp3")
total_size = os.path.getsize(path)
range_header = request.headers.get(b'Range')
range_header_match = range_header and re.search(r'^bytes=(\d*)-(\d*)$', isomorphic_decode(range_header))
start, end = range_header_match.groups()
start = int(start or 0)
end = int(end or total_size)
content_range = request.GET.first(b'response', 'bytes %d-%d / %d' % (start, end - 1, total_size))
headers = []
headers.append((b"Content-Range", content_range))
headers.append((b"Accept-Ranges", b"bytes"))
headers.append((b"Content-Type", b"audio/mp3"))
headers.append((b"Content-Length", str(end - start)))
headers.append((b"Cache-Control", b"no-cache"))
video_file = open(path, "rb")
video_file.seek(start)
content = video_file.read(end)
return 206, headers, content