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

Side by Side Diff: third_party/WebKit/LayoutTests/http/tests/fetch/script-tests/body-mixin.js

Issue 1418813004: [Fetch API] Reflect spec changes of bodyUsed property (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 unified diff | Download patch
OLDNEW
1 if (self.importScripts) { 1 if (self.importScripts) {
2 importScripts('../resources/fetch-test-helpers.js'); 2 importScripts('../resources/fetch-test-helpers.js');
3 } 3 }
4 4
5 function readStream(reader, values) { 5 function readStream(reader, values) {
6 reader.read().then(function(r) { 6 reader.read().then(function(r) {
7 if (!r.done) { 7 if (!r.done) {
8 values.push(r.value); 8 values.push(r.value);
9 readStream(reader, values); 9 readStream(reader, values);
10 } 10 }
11 }); 11 });
12 return reader.closed; 12 return reader.closed;
13 } 13 }
14 14
15 function isLocked(stream) {
16 try {
17 var reader = stream.getReader();
18 reader.releaseLock();
19 return false;
20 } catch(e) {
21 return true;
22 }
23 }
24
15 promise_test(function(test) { 25 promise_test(function(test) {
16 return fetch('/fetch/resources/doctype.html') 26 return fetch('/fetch/resources/doctype.html')
17 .then(function(response) { 27 .then(function(response) {
18 // Accessing the body property makes the stream start working. 28 // Accessing the body property makes the stream start working.
19 var stream = response.body; 29 var stream = response.body;
20 return response.text(); 30 return response.text();
21 }) 31 })
22 .then(function(text) { 32 .then(function(text) {
23 assert_equals(text, '<!DOCTYPE html>\n'); 33 assert_equals(text, '<!DOCTYPE html>\n');
24 }) 34 })
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
56 }); 66 });
57 return Promise.all([p1, p2]); 67 return Promise.all([p1, p2]);
58 }) 68 })
59 .then(function(results) { 69 .then(function(results) {
60 assert_equals(results[0].length, 190); 70 assert_equals(results[0].length, 190);
61 assert_equals(results[1].name, 'TypeError'); 71 assert_equals(results[1].name, 'TypeError');
62 }) 72 })
63 }, 'FetchTwiceTest'); 73 }, 'FetchTwiceTest');
64 74
65 promise_test(function(test) { 75 promise_test(function(test) {
76 var response;
66 return fetch('/fetch/resources/doctype.html') 77 return fetch('/fetch/resources/doctype.html')
67 .then(function(response) { 78 .then(function(res) {
68 return response.arrayBuffer(); 79 response = res;
80 assert_false(response.bodyUsed);
81 var p = response.arrayBuffer();
82 assert_true(response.bodyUsed);
83 assert_true(isLocked(response.body));
84 return p;
69 }) 85 })
70 .then(function(b) { 86 .then(function(b) {
87 assert_true(isLocked(response.body));
71 assert_equals(b.byteLength, 16); 88 assert_equals(b.byteLength, 16);
72 }) 89 })
73 }, 'ArrayBufferTest'); 90 }, 'ArrayBufferTest');
74 91
75 promise_test(function(test) { 92 promise_test(function(test) {
93 var response;
76 return fetch('/fetch/resources/doctype.html') 94 return fetch('/fetch/resources/doctype.html')
77 .then(function(response) { 95 .then(function(res) {
78 return response.blob(); 96 response = res;
97 assert_false(response.bodyUsed);
98 var p = response.blob();
99 assert_true(response.bodyUsed);
100 assert_true(isLocked(response.body));
101 return p;
79 }) 102 })
80 .then(function(blob) { 103 .then(function(blob) {
104 assert_true(isLocked(response.body));
81 assert_equals(blob.size, 16); 105 assert_equals(blob.size, 16);
82 assert_equals(blob.type, 'text/html'); 106 assert_equals(blob.type, 'text/html');
83 }) 107 })
84 }, 'BlobTest'); 108 }, 'BlobTest');
85 109
86 promise_test(function(test) { 110 promise_test(function(test) {
111 var response;
87 return fetch('/fetch/resources/doctype.html') 112 return fetch('/fetch/resources/doctype.html')
88 .then(function(response) { 113 .then(function(res) {
89 return response.json(); 114 response = res;
115 assert_false(response.bodyUsed);
116 var p = response.json();
117 assert_true(response.bodyUsed);
118 assert_true(isLocked(response.body));
119 return p;
90 }) 120 })
91 .then( 121 .then(
92 test.unreached_func('json() must fail'), 122 test.unreached_func('json() must fail'),
93 function(e) { 123 function(e) {
124 assert_true(isLocked(response.body));
94 assert_equals(e.name, 'SyntaxError', 'expected JSON error'); 125 assert_equals(e.name, 'SyntaxError', 'expected JSON error');
95 }) 126 })
96 }, 'JSONFailedTest'); 127 }, 'JSONFailedTest');
97 128
98 promise_test(function(test) { 129 promise_test(function(test) {
130 var response;
99 return fetch('/fetch/resources/simple.json') 131 return fetch('/fetch/resources/simple.json')
100 .then(function(response) { 132 .then(function(res) {
101 return response.json(); 133 response = res;
134 assert_false(response.bodyUsed);
135 var p = response.json();
136 assert_true(response.bodyUsed);
137 assert_true(isLocked(response.body));
138 return p;
102 }) 139 })
103 .then(function(json) { 140 .then(function(json) {
141 assert_true(isLocked(response.body));
104 assert_equals(json['a'], 1); 142 assert_equals(json['a'], 1);
105 assert_equals(json['b'], 2); 143 assert_equals(json['b'], 2);
106 }) 144 })
107 }, 'JSONTest'); 145 }, 'JSONTest');
108 146
109 promise_test(function(test) { 147 promise_test(function(test) {
148 var response;
110 return fetch('/fetch/resources/doctype.html') 149 return fetch('/fetch/resources/doctype.html')
111 .then(function(response) { 150 .then(function(res) {
112 return response.text(); 151 response = res;
152 assert_false(response.bodyUsed);
153 var p = response.text();
154 assert_true(response.bodyUsed);
155 assert_true(isLocked(response.body));
156 return p;
113 }) 157 })
114 .then(function(text) { 158 .then(function(text) {
159 assert_true(isLocked(response.body));
115 assert_equals(text, '<!DOCTYPE html>\n'); 160 assert_equals(text, '<!DOCTYPE html>\n');
116 }) 161 })
117 }, 'TextTest'); 162 }, 'TextTest');
118 163
119 promise_test(function(test) { 164 promise_test(function(test) {
120 return fetch('/fetch/resources/non-ascii.txt') 165 return fetch('/fetch/resources/non-ascii.txt')
121 .then(function(response) { 166 .then(function(response) {
122 return response.text(); 167 assert_false(response.bodyUsed);
168 var p = response.text();
169 assert_true(response.bodyUsed);
170 return p;
123 }) 171 })
124 .then(function(text) { 172 .then(function(text) {
125 assert_equals(text, '\u4e2d\u6587 Gem\u00fcse\n'); 173 assert_equals(text, '\u4e2d\u6587 Gem\u00fcse\n');
126 }) 174 })
127 }, 'NonAsciiTextTest'); 175 }, 'NonAsciiTextTest');
128 176
129 promise_test(function(test) { 177 test(t => {
130 var expected = ''; 178 var req = new Request('/');
131 for (var i = 0; i < 100; ++i) 179 assert_false(req.bodyUsed);
132 expected += i; 180 req.text();
181 assert_false(req.bodyUsed);
182 }, 'BodyUsedShouldNotBeSetForNullBody');
133 183
134 var decoder = new TextDecoder(); 184 test(t => {
135 var actual = ''; 185 var req = new Request('/', {method: 'POST', body: ''});
136 var response; 186 assert_false(req.bodyUsed);
137 var reader; 187 req.text();
138 return fetch('/fetch/resources/progressive.php') 188 assert_true(req.bodyUsed);
139 .then(function(res) { 189 }, 'BodyUsedShouldBeSetForEmptyBody');
140 response = res; 190
141 reader = response.body.getReader(); 191 test(t => {
142 return reader.read(); 192 var res = new Response('');
143 }) 193 assert_false(res.bodyUsed);
144 .then(function(r) { 194 var reader = res.body.getReader();
145 assert_false(r.done); 195 assert_false(res.bodyUsed);
146 actual += decoder.decode(r.value, {stream: true}); 196 reader.read();
147 }) 197 assert_true(res.bodyUsed);
148 .then(function() { 198 }, 'BodyUsedShouldBeSetWhenRead');
149 return response.text().then(unreached_fulfillment(test), function() { 199
150 // response.text() should fail because we have a reader. 200 test(t => {
151 }); 201 var res = new Response('');
152 }) 202 assert_false(res.bodyUsed);
153 .then(function() { 203 var reader = res.body.getReader();
154 reader.releaseLock(); 204 assert_false(res.bodyUsed);
155 return response.arrayBuffer(); 205 reader.cancel();
156 }) 206 assert_true(res.bodyUsed);
157 .then(function(buffer) { 207 }, 'BodyUsedShouldBeSetWhenCancelled');
158 actual += decoder.decode(buffer); 208
159 assert_equals(actual, expected); 209 promise_test(t => {
160 }) 210 var res = new Response('');
161 }, 'PartiallyReadFromStreamAndReadArrayBufferTest'); 211 res.body.cancel();
212 return res.arrayBuffer().then(unreached_fulfillment(t), e => {
213 assert_equals(e.name, 'TypeError');
214 });
215 }, 'Used => arrayBuffer');
216
217 promise_test(t => {
218 var res = new Response('');
219 res.body.cancel();
220 return res.blob().then(unreached_fulfillment(t), e => {
221 assert_equals(e.name, 'TypeError');
222 });
223 }, 'Used => blob');
224
225 promise_test(t => {
226 var res = new Response('');
227 res.body.cancel();
228 return res.json().then(unreached_fulfillment(t), e => {
229 assert_equals(e.name, 'TypeError');
230 });
231 }, 'Used => json');
232
233 promise_test(t => {
234 var res = new Response('');
235 res.body.cancel();
236 return res.text().then(unreached_fulfillment(t), e => {
237 assert_equals(e.name, 'TypeError');
238 });
239 }, 'Used => text');
240
241 promise_test(t => {
242 var res = new Response('');
243 res.body.getReader();
244 return res.arrayBuffer().then(unreached_fulfillment(t), e => {
245 assert_equals(e.name, 'TypeError');
246 });
247 }, 'Locked => arrayBuffer');
248
249 promise_test(t => {
250 var res = new Response('');
251 res.body.getReader();
252 return res.blob().then(unreached_fulfillment(t), e => {
253 assert_equals(e.name, 'TypeError');
254 });
255 }, 'Locked => blob');
256
257 promise_test(t => {
258 var res = new Response('');
259 res.body.getReader();
260 return res.json().then(unreached_fulfillment(t), e => {
261 assert_equals(e.name, 'TypeError');
262 });
263 }, 'Locked => json');
264
265 promise_test(t => {
266 var res = new Response('');
267 res.body.getReader();
268 return res.text().then(unreached_fulfillment(t), e => {
269 assert_equals(e.name, 'TypeError');
270 });
271 }, 'Locked => text');
162 272
163 done(); 273 done();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698