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

Side by Side Diff: third_party/WebKit/LayoutTests/http/tests/fetch/script-tests/stream-reader.js

Issue 1418813004: [Fetch API] Reflect spec changes of bodyUsed property (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 5 years, 1 month 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 unified diff | Download patch
OLDNEW
1 if (self.importScripts) { 1 if (self.importScripts) {
2 importScripts('/fetch/resources/fetch-test-helpers.js'); 2 importScripts('/fetch/resources/fetch-test-helpers.js');
3 } 3 }
4 4
5 function read_until_end(reader) { 5 function read_until_end(reader) {
6 var chunks = []; 6 var chunks = [];
7 function consume() { 7 function consume() {
8 return reader.read().then(function(r) { 8 return reader.read().then(function(r) {
9 if (r.done) { 9 if (r.done) {
10 return chunks; 10 return chunks;
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
55 assert_false(res.bodyUsed); 55 assert_false(res.bodyUsed);
56 var reader = res.body.getReader(); 56 var reader = res.body.getReader();
57 assert_true(res.bodyUsed); 57 assert_true(res.bodyUsed);
58 return res.text(); 58 return res.text();
59 }).then(unreached_rejection(t), function() { 59 }).then(unreached_rejection(t), function() {
60 // text() should fail because bodyUsed is set. 60 // text() should fail because bodyUsed is set.
61 }); 61 });
62 }, 'acquiring a reader should set bodyUsed.'); 62 }, 'acquiring a reader should set bodyUsed.');
63 63
64 promise_test(function(t) { 64 promise_test(function(t) {
65 var reader;
65 var response; 66 var response;
66 return fetch('/fetch/resources/progressive.php').then(function(res) { 67 return fetch('/fetch/resources/progressive.php').then(function(res) {
67 response = res; 68 response = res;
68 assert_false(res.bodyUsed);
69 var p = res.arrayBuffer();
70 assert_true(res.bodyUsed);
71 assert_throws({name: 'TypeError'}, function() { res.body.getReader() });
72 return p;
73 }).then(function(buffer) {
74 assert_equals(buffer.byteLength, 190);
75 // Now we can obtain a (closed) reader.
76 return response.body.getReader().closed;
77 });
78 }, 'Setting bodyUsed means the body is locked.');
79
80 promise_test(function(t) {
81 return fetch('/fetch/resources/slow-failure.cgi').then(function(res) {
82 return res.text().then(function() {
83 assert_unreached('text() should fail');
84 }, function(e) {
85 return res.body.getReader().closed.then(function() {
86 assert_unreached('res.body should be errored');
87 }, function(e) {});
88 });
89 });
90 }, 'Error in text() should be propagated to the body stream.');
91
92 promise_test(function(t) {
93 var reader;
94 var read = 0;
95 var original;
96 return fetch('/fetch/resources/progressive.php').then(function(res) {
97 original = res;
98 reader = res.body.getReader(); 69 reader = res.body.getReader();
99 return reader.read(); 70 return reader.read();
100 }).then(function(r) { 71 }).then(() => {
101 assert_false(r.done);
102 read += r.value.byteLength;
103 // Make sure that we received something but we didn't receive all.
104 assert_not_equals(read, 0);
105 assert_not_equals(read, 190);
106
107 reader.releaseLock(); 72 reader.releaseLock();
108 var original_body = original.body; 73 assert_true(response.bodyUsed);
109 var clone = original.clone(); 74 assert_throws(new TypeError, () => { response.clone(); });
110 assert_not_equals(original.body, clone.body);
111 assert_not_equals(original.body, original_body);
112 assert_not_equals(clone.body, original_body);
113 assert_throws({name: 'TypeError'}, function() {
114 original_body.getReader();
115 });
116 var reader1 = original.body.getReader();
117 var reader2 = clone.body.getReader();
118 return Promise.all([read_until_end(reader1), read_until_end(reader2)]);
119 }).then(function(r) {
120 var read1 = 0;
121 var read2 = 0;
122 for (var chunk of r[0]) {
123 read1 += chunk.byteLength;
124 }
125 for (var chunk of r[1]) {
126 read2 += chunk.byteLength;
127 }
128 // Make sure that we received all data in total.
129 assert_equals(read + read1, 190);
130 assert_equals(read + read2, 190);
131 }); 75 });
132 }, 'Clone after reading partially'); 76 }, 'Clone after reading');
133 77
134 promise_test(function(t) { 78 promise_test(function(t) {
135 return fetch('/fetch/resources/progressive.php').then(function(res) { 79 return fetch('/fetch/resources/progressive.php').then(function(res) {
136 res.body.cancel(); 80 res.body.cancel();
137 return res.text(); 81 assert_true(res.bodyUsed);
138 }).then(function(text) {
139 assert_equals(text, '');
140 }); 82 });
141 }, 'Cancelling stream stops downloading.'); 83 }, 'Cancelling stream stops downloading.');
142 84
143 promise_test(function(t) { 85 promise_test(function(t) {
86 var clone;
144 return fetch('/fetch/resources/progressive.php').then(function(res) { 87 return fetch('/fetch/resources/progressive.php').then(function(res) {
145 var clone = res.clone(); 88 clone = res.clone();
146 res.body.cancel(); 89 res.body.cancel();
147 return Promise.all([res.arrayBuffer(), clone.arrayBuffer()]); 90 assert_true(res.bodyUsed);
91 assert_false(clone.bodyUsed);
92 return clone.arrayBuffer();
148 }).then(function(r) { 93 }).then(function(r) {
149 assert_equals(r[0].byteLength, 0); 94 assert_equals(r.byteLength, 190);
150 assert_equals(r[1].byteLength, 190); 95 assert_true(clone.bodyUsed);
151 }); 96 });
152 }, 'Cancelling stream should not affect cloned one.'); 97 }, 'Cancelling stream should not affect cloned one.');
153 98
tyoshino (SeeGerritForStatus) 2015/11/18 09:55:36 how about moving these to response.js as it's not
yhirano 2015/11/19 08:41:29 request.js and response.js don't contain tests usi
154 promise_test(function(t) {
155 var stream;
156 return fetch('/fetch/resources/progressive.php').then(function(res) {
157 var p = res.text();
158 stream = res.body;
159 assert_throws({name: 'TypeError'}, function() { stream.getReader() });
160 return p;
161 }).then(function(text) {
162 assert_equals(text.length, 190);
163 return stream.getReader().read();
164 }).then(function(r) {
165 assert_true(r.done);
166 assert_equals(r.value, undefined);
167 });
168 }, 'Accessing body when processing text().');
169
170 done(); 99 done();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698