Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(639)

Unified Diff: third_party/WebKit/LayoutTests/http/tests/fetch/script-tests/response-stream-construction.js

Issue 1506023003: Response construction with a ReadableStream (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/LayoutTests/http/tests/fetch/script-tests/response-stream-construction.js
diff --git a/third_party/WebKit/LayoutTests/http/tests/fetch/script-tests/response-stream-construction.js b/third_party/WebKit/LayoutTests/http/tests/fetch/script-tests/response-stream-construction.js
new file mode 100644
index 0000000000000000000000000000000000000000..8f0b03baf413a11517ca42f0669d7900a5560d1f
--- /dev/null
+++ b/third_party/WebKit/LayoutTests/http/tests/fetch/script-tests/response-stream-construction.js
@@ -0,0 +1,118 @@
+// This file contains tests for Response construction with a readable stream.
+// Move these tests to response.js once the feature gets stable.
+
+if (self.importScripts) {
+ importScripts('../resources/fetch-test-helpers.js');
+}
+
+function read_until_end(reader) {
domenic 2015/12/09 13:03:18 You could probably reuse the helper from rs-utils.
yhirano 2015/12/10 10:51:05 Done.
+ var chunks = [];
+ function consume() {
+ return reader.read().then(function(r) {
+ if (r.done) {
+ return chunks;
+ } else {
+ chunks.push(r.value);
+ return consume();
+ }
+ });
+ }
+ return consume();
+}
+
+test(() => {
+ var controller;
+ var stream = new ReadableStream({start: c => controller = c});
+
+ var response = new Response(stream);
+ // TODO(yhirano): This should be assert_equals.
+ assert_not_equals(response.body, stream);
+ }, 'Response constructed with a stream');
+
+promise_test(() => {
+ var controller;
+ var stream = new ReadableStream({start: c => controller = c});
+ controller.enqueue(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x6f]));
+ controller.enqueue(new Uint8Array([0x77, 0x6f, 0x72, 0x6c, 0x64]));
+ controller.close();
+ assert_false(stream.locked);
+ var response = new Response(stream);
+ return response.text().then(t => {
+ assert_equals(t, 'helloworld');
+ });
+ assert_true(stream.locked);
domenic 2015/12/09 13:03:18 Unreachable code
yhirano 2015/12/10 10:51:05 Nice catch! Fixed.
+ }, 'Response constructed with a stream');
+
+promise_test(() => {
+ var controller;
+ var stream = new ReadableStream({start: c => controller = c});
+ controller.enqueue(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x6f]));
+ controller.enqueue(new Uint8Array([0x77, 0x6f, 0x72, 0x6c, 0x64]));
+ controller.close();
+
+ var response = new Response(stream);
+ return read_until_end(response.body.getReader()).then(chunks => {
+ var decoder = new TextDecoder('utf-8');
+ var r = '';
+ for (var chunk of chunks) {
+ r += decoder.decode(chunk, {stream: true});
+ }
+ r += decoder.decode();
+ assert_equals(r, 'helloworld');
+ });
+ }, 'Response constructed with a stream / Read from body stream');
+
+promise_test(t => {
+ var controller;
+ var stream = new ReadableStream({start: c => controller = c});
+ setTimeout(() => {
+ controller.enqueue(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x6f]));
+ controller.enqueue(new Uint8Array([0x77, 0x6f, 0x72, 0x6c, 0x64]));
+ controller.error();
+ }, 1);
+ var response = new Response(stream);
+ return response.text().then(unreached_fulfillment(t), e => {
+ assert_equals(e.name, 'TypeError');
+ });
+ }, 'Response constructed with an errored stream');
+
+promise_test(t => {
+ var controller;
+ var stream = new ReadableStream({start: c => controller = c});
+ stream.getReader();
+ var response = new Response(stream);
+ return response.text().then(unreached_fulfillment(t), e => {
domenic 2015/12/09 13:03:18 You can use promise_rejects for these types of tes
yhirano 2015/12/10 10:51:05 Done.
+ assert_equals(e.name, 'TypeError');
+ });
+ }, 'Response constructed with a locked stream');
+
+promise_test(t => {
+ var controller;
+ var stream = new ReadableStream({start: c => controller = c});
+ setTimeout(() => controller.enqueue(), 1);
+ var response = new Response(stream);
+ return response.text().then(unreached_fulfillment(t), e => {
+ assert_equals(e.name, 'TypeError');
+ });
+ }, 'Response constructed stream with an undefined chunk');
+
+promise_test(t => {
+ var controller;
+ var stream = new ReadableStream({start: c => controller = c});
+ setTimeout(() => controller.enqueue(null), 1);
+ var response = new Response(stream);
+ return response.text().then(unreached_fulfillment(t), e => {
+ assert_equals(e.name, 'TypeError');
+ });
+ }, 'Response constructed stream with a null chunk');
+
+promise_test(t => {
+ var controller;
+ var stream = new ReadableStream({start: c => controller = c});
+ setTimeout(() => controller.enqueue('hello'), 1);
+ var response = new Response(stream);
+ return response.text().then(unreached_fulfillment(t), e => {
+ assert_equals(e.name, 'TypeError');
+ });
+ }, 'Response constructed stream with a string chunk');
+

Powered by Google App Engine
This is Rietveld 408576698