Index: LayoutTests/http/tests/xmlhttprequest/response-stream.html |
diff --git a/LayoutTests/http/tests/xmlhttprequest/response-stream.html b/LayoutTests/http/tests/xmlhttprequest/response-stream.html |
new file mode 100644 |
index 0000000000000000000000000000000000000000..ca24a9e881ccd7595d2d32cdea08e59c82b72814 |
--- /dev/null |
+++ b/LayoutTests/http/tests/xmlhttprequest/response-stream.html |
@@ -0,0 +1,94 @@ |
+<!DOCTYPE html> |
+<script src="../resources/testharness.js"></script> |
+<script src="../resources/testharnessreport.js"></script> |
+<script type="text/javascript"> |
+var test = async_test('Test response of XMLHttpRequest with responseType set to "stream" for various readyState.'); |
+ |
+test.step(function() |
+{ |
+ var xhr = new XMLHttpRequest; |
+ |
+ xhr.responseType = 'stream'; |
+ assert_equals(xhr.responseType, 'stream', 'xhr.responseType'); |
+ |
+ assert_equals(xhr.readyState, xhr.UNSENT, 'xhr.readyState'); |
+ assert_equals(xhr.response, null, 'xhr.response during UNSENT'); |
+ |
+ var seenStates = []; |
+ |
+ function readStream(stream) { |
+ var chunks = []; |
+ function rec(resolve, reject) { |
+ while (stream.state === 'readable') { |
+ chunks.push(stream.read()); |
+ } |
+ if (stream.state === 'closed') { |
+ resolve(chunks); |
+ return; |
+ } |
+ stream.wait().then(function() { |
+ rec(resolve, reject); |
+ }).catch(reject); |
+ } |
+ return new Promise(rec); |
+ } |
+ var streamPromise = undefined; |
+ |
+ xhr.onreadystatechange = test.step_func(function() { |
+ // onreadystatechange can be invoked multiple times in LOADING state. |
+ if (seenStates.length == 0 || xhr.readyState != seenStates[seenStates.length - 1]) |
+ seenStates.push(xhr.readyState); |
+ |
+ switch (xhr.readyState) { |
+ case xhr.UNSENT: |
+ assert_unreached('Unexpected readyState: UNSENT'); |
+ return; |
+ |
+ case xhr.OPENED: |
+ assert_equals(xhr.response, null, 'xhr.response during OPENED'); |
+ return; |
+ |
+ case xhr.HEADERS_RECEIVED: |
+ assert_equals(xhr.response, null, 'xhr.response during HEADERS_RECEIVED'); |
+ return; |
+ |
+ case xhr.LOADING: |
+ assert_not_equals(xhr.response, null, 'xhr.response during LOADING'); |
+ assert_true(xhr.response instanceof ReadableStream, |
+ 'xhr.response should be ReadableStream during LOADING'); |
+ if (streamPromise === undefined) { |
+ streamPromise = readStream(xhr.response); |
+ } |
+ return; |
+ |
+ case xhr.DONE: |
+ assert_equals(xhr.status, 200, 'xhr.status'); |
+ |
+ // Check that we saw all states. |
+ assert_array_equals(seenStates, |
+ [xhr.OPENED, xhr.HEADERS_RECEIVED, xhr.LOADING, xhr.DONE]); |
+ |
+ assert_not_equals(streamPromise, undefined, 'streamPromise'); |
+ streamPromise.then(test.step_func(function(chunks) { |
+ assert_equals(xhr.response.state, 'closed', 'stream status'); |
+ var size = 0; |
+ for (var i = 0; i < chunks.length; ++i) { |
+ size += chunks[i].byteLength; |
+ } |
+ assert_equals(size, 103746, 'response size'); |
+ test.done(); |
+ }), test.step_func(function(e) { |
+ assert_unreached('failed to read the response stream: ' + e); |
+ })); |
+ return; |
+ |
+ default: |
+ assert_unreached('Unexpected readyState: ' + xhr.readyState) |
+ return; |
+ } |
+ }); |
+ |
+ xhr.open('GET', '../resources/test.ogv', true); |
+ xhr.send(); |
+}); |
+</script> |