3 <p>Test Range support in XMLHttpRequest
</p>
5 if (window
.testRunner
) {
6 testRunner
.dumpAsText();
7 testRunner
.waitUntilDone();
10 var console_messages
= document
.createElement("ul");
11 document
.body
.appendChild(console_messages
);
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
;
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
] + ")");
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() {
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
82 runRangeTest(true, function() {
83 if (window
.testRunner
)
84 testRunner
.notifyDone();