Chromium Code Reviews| Index: LayoutTests/http/tests/fetch/script-tests/stream-reader.js |
| diff --git a/LayoutTests/http/tests/fetch/script-tests/stream-reader.js b/LayoutTests/http/tests/fetch/script-tests/stream-reader.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..951640fec9e78a032af141cb62e02fd031f622b6 |
| --- /dev/null |
| +++ b/LayoutTests/http/tests/fetch/script-tests/stream-reader.js |
| @@ -0,0 +1,81 @@ |
| +if (self.importScripts) { |
| + importScripts('/fetch/resources/fetch-test-helpers.js'); |
| +} |
| + |
| +function read_until_end(reader) { |
| + var chunks = []; |
| + function rec(resolve, reject) { |
| + while (reader.state === 'readable') { |
| + chunks.push(reader.read()); |
| + } |
| + if (reader.state === 'closed') { |
| + resolve(chunks); |
| + return; |
| + } |
| + if (reader.state === 'errored') { |
| + resolve(reader.closed); |
| + } |
| + reader.ready.then(function() { |
| + rec(resolve, reject); |
| + }).catch(reject); |
| + } |
| + return new Promise(rec); |
| +} |
| + |
| +promise_test(function(t) { |
| + return fetch('/fetch/resources/doctype.html').then(function(res) { |
| + var stream = res.body; |
| + var reader = stream.getReader(); |
| + assert_true(reader.isActive); |
| + assert_throws({name: 'TypeError'}, function() { stream.getReader() }); |
| + reader.releaseLock(); |
| + stream.getReader(); |
|
tyoshino (SeeGerritForStatus)
2015/02/02 08:38:24
what is this getReader() call for?
yhirano
2015/02/02 10:21:20
For checking that an exception is not thrown.
I ad
|
| + }); |
| + }, 'ExclusiveStreamReader acquisition / releasing'); |
| + |
| +promise_test(function(t) { |
| + function wait_until_readable(reader) { |
| + return reader.ready.then(function() { |
| + if (reader.state === 'waiting') { |
| + return wait_until_readable(reader); |
| + } |
| + if (reader.state === 'readable') { |
| + return undefined; |
| + } |
| + return Promise.reject(new Error('state = ' + reader.state)); |
| + }); |
| + } |
| + var stream; |
| + var reader; |
| + return fetch('/fetch/resources/doctype.html').then(function(res) { |
| + stream = res.body; |
| + reader = stream.getReader(); |
| + return wait_until_readable(reader); |
| + }).then(function() { |
| + assert_equals(reader.state, 'readable'); |
| + assert_equals(stream.state, 'waiting'); |
| + reader.releaseLock(); |
| + assert_equals(reader.state, 'closed'); |
| + assert_equals(stream.state, 'readable'); |
| + var another = stream.getReader(); |
| + assert_equals(reader.state, 'closed'); |
| + assert_equals(stream.state, 'waiting'); |
| + assert_equals(another.state, 'readable'); |
| + }); |
| + }, 'ExclusiveStreamReader state masking'); |
| + |
| +promise_test(function(t) { |
| + return fetch('/fetch/resources/doctype.html').then(function(res) { |
| + var reader = res.body.getReader(); |
| + return read_until_end(reader); |
| + }).then(function(chunks) { |
| + return Promise.all(chunks.map(function(chunk) { |
|
tyoshino (SeeGerritForStatus)
2015/02/02 08:38:24
this works since only ASCII characters are used?
yhirano
2015/02/02 10:21:20
You're right, but I don't want to rely on it. Fixe
|
| + return new TextDecoder().decode(chunk); |
| + })); |
| + }).then(function(strings) { |
| + var string = String.prototype.concat.apply('', strings); |
| + assert_equals(string, '<!DOCTYPE html>\n'); |
| + }); |
| + }, 'read contents with ExclusiveStreamReader'); |
| + |
| +done(); |