Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / http / tests / xmlhttprequest / range-test.html
blobd273dac68c7b6cae275e7a60ddf81f9e3516a7a0
1 <html>
2 <body>
3 <p>Test Range support in XMLHttpRequest</p>
4 <script>
5 if (window.testRunner) {
6 testRunner.dumpAsText();
7 testRunner.waitUntilDone();
10 var console_messages = document.createElement("ul");
11 document.body.appendChild(console_messages);
13 function log(message)
15 var item = document.createElement("li");
16 item.appendChild(document.createTextNode(message));
17 console_messages.appendChild(item);
20 function getRange(url, start, end, useUniqueUrls, fullResponse, done_callback) {
21 log("getRange(" + url + ", " + start + ", " + end + ", " + useUniqueUrls + ")");
22 var req = new XMLHttpRequest;
24 if (useUniqueUrls)
25 url = url + "?q=" + start + "-" + end;
27 req.open("GET", url, true);
28 req.setRequestHeader("Range", "bytes=" + start + "-" + (end - 1));
29 req.responseType = 'arraybuffer';
30 req.onreadystatechange = function() {
31 if (req.readyState == 4) {
32 if (req.status != 206)
33 log("Expected a 206 response, got " + req.status);
35 var expected = fullResponse.subarray(start, end);
36 var actual = new Uint8Array(req.response);
37 log("Length : expected " + expected.length + " got " + actual.length);
39 if (expected.length == actual.length) {
40 // Verify that all the data is what we expect.
41 for (var i = 0; i < expected.length; ++i) {
42 if (actual[i] != expected[i]) {
43 log("actual[" + i + "] != expected[" + i + "] (" + actual[i] + " != " + expected[i] + ")");
47 done_callback();
50 req.send(null);
53 function runRangeTest(useUniqueUrls, done_callback) {
54 // First fetch full resource.
55 var url = "resources/reply.xml";
56 var req = new XMLHttpRequest;
57 req.open("GET", url, true);
58 req.responseType = 'arraybuffer';
59 req.onreadystatechange = function() {
60 if (req.readyState == 4) {
61 var fullResponse = new Uint8Array(req.response);
62 var startOffset = fullResponse.length / 2;
63 var endOffset = startOffset + 2;
65 // Next fetch a subrange in the middle of the resource.
66 getRange(url, startOffset, endOffset, useUniqueUrls, fullResponse, function() {
68 // Fetch a different range near the middle of the resource.
69 getRange(url, startOffset + 1, endOffset + 2, useUniqueUrls, fullResponse, function() {
70 done_callback();
71 });
72 });
75 req.send(null);
78 // First test range requests using the exact same URL.
79 runRangeTest(false, function() {
80 // Next test w/ a query parameter appended so that each XHR
81 // has a unique URL.
82 runRangeTest(true, function() {
83 if (window.testRunner)
84 testRunner.notifyDone();
86 });
87 });
89 </script>
90 </body>
91 </html>