OLD | NEW |
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 size(headers) { | 5 function size(headers) { |
6 var count = 0; | 6 var count = 0; |
7 for (var header of headers) { | 7 for (var header of headers) { |
8 ++count; | 8 ++count; |
9 } | 9 } |
10 return count; | 10 return count; |
11 } | 11 } |
12 | 12 |
13 function consume(reader) { | 13 function consume(reader) { |
14 var chunks = []; | 14 var chunks = []; |
15 function rec(reader) { | 15 function rec(reader) { |
16 return reader.read().then(function(r) { | 16 return reader.read().then(function(r) { |
17 if (r.done) { | 17 if (r.done) { |
18 return chunks; | 18 return chunks; |
19 } | 19 } |
20 chunks.push(r.value); | 20 chunks.push(r.value); |
21 return rec(reader); | 21 return rec(reader); |
22 }); | 22 }); |
23 } | 23 } |
24 return rec(reader); | 24 return rec(reader); |
25 } | 25 } |
26 | 26 |
27 function flatten(chunks) { | 27 function decode(chunks) { |
28 var size = 0; | 28 var decoder = new TextDecoder(); |
| 29 var result = ''; |
29 for (var chunk of chunks) { | 30 for (var chunk of chunks) { |
30 size += chunk.byteLength; | 31 result += decoder.decode(chunk, {stream: true}); |
31 } | 32 } |
32 var buffer = new Uint8Array(size); | 33 result += decoder.decode(new Uint8Array(0)); |
33 var current = 0; | 34 return result; |
34 for (var chunk of chunks) { | |
35 buffer.set(chunk, 0); | |
36 } | |
37 return buffer; | |
38 } | 35 } |
39 | 36 |
40 test(function() { | 37 test(function() { |
41 var response = new Response(new Blob()); | 38 var response = new Response(new Blob()); |
42 assert_equals(response.type, 'default', | 39 assert_equals(response.type, 'default', |
43 'Default Response.type should be \'default\''); | 40 'Default Response.type should be \'default\''); |
44 assert_equals(response.url, '', 'Response.url should be the empty string'); | 41 assert_equals(response.url, '', 'Response.url should be the empty string'); |
45 assert_equals(response.status, 200, | 42 assert_equals(response.status, 200, |
46 'Default Response.status should be 200'); | 43 'Default Response.status should be 200'); |
47 assert_true(response.ok, 'Default Response.ok must be true'); | 44 assert_true(response.ok, 'Default Response.ok must be true'); |
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
289 new Response(new Blob(), {statusText: text}); | 286 new Response(new Blob(), {statusText: text}); |
290 }, | 287 }, |
291 'new Response with invalid statusText (' + text + | 288 'new Response with invalid statusText (' + text + |
292 ') must throw'); | 289 ') must throw'); |
293 }); | 290 }); |
294 }, 'Response throw error test'); | 291 }, 'Response throw error test'); |
295 | 292 |
296 promise_test(function(t) { | 293 promise_test(function(t) { |
297 var res = new Response('hello'); | 294 var res = new Response('hello'); |
298 return consume(res.body.getReader()).then(function(chunks) { | 295 return consume(res.body.getReader()).then(function(chunks) { |
299 return flatten(chunks); | 296 return decode(chunks); |
300 }).then(function(view) { | 297 }).then(function(text) { |
301 assert_equals(new TextDecoder().decode(view), 'hello'); | 298 assert_equals(text, 'hello'); |
302 return res.body.getReader().read(); | 299 return res.body.getReader().read(); |
303 }).then(function(r) { | 300 }).then(function(r) { |
304 assert_true(r.done); | 301 assert_true(r.done); |
305 return res.text(); | 302 return res.text(); |
306 }).then(function(r) { | 303 }).then(function(r) { |
307 assert_equals(r, ''); | 304 assert_equals(r, ''); |
308 }); | 305 }); |
309 }, 'Read Response body via stream'); | 306 }, 'Read Response body via stream'); |
310 | 307 |
311 promise_test(function(t) { | 308 promise_test(function(t) { |
(...skipping 30 matching lines...) Expand all Loading... |
342 }).then(function(buffer) { | 339 }).then(function(buffer) { |
343 assert_equals(buffer.byteLength + head.byteLength, size); | 340 assert_equals(buffer.byteLength + head.byteLength, size); |
344 return res.body.getReader().read(); | 341 return res.body.getReader().read(); |
345 }).then(function(r) { | 342 }).then(function(r) { |
346 assert_true(r.done); | 343 assert_true(r.done); |
347 }); | 344 }); |
348 }, 'Partial read on Response'); | 345 }, 'Partial read on Response'); |
349 | 346 |
350 promise_test(function(t) { | 347 promise_test(function(t) { |
351 var res = new Response('hello'); | 348 var res = new Response('hello'); |
| 349 var body = res.body; |
352 var clone = res.clone(); | 350 var clone = res.clone(); |
| 351 assert_not_equals(res.body, body); |
| 352 assert_not_equals(res.body, clone.body); |
| 353 assert_not_equals(body, clone.body); |
| 354 assert_throws({name: 'TypeError'}, function() { body.getReader(); }); |
353 return Promise.all([res.text(), clone.text()]).then(function(r) { | 355 return Promise.all([res.text(), clone.text()]).then(function(r) { |
354 assert_equals(r[0], 'hello'); | 356 assert_equals(r[0], 'hello'); |
355 assert_equals(r[1], 'hello'); | 357 assert_equals(r[1], 'hello'); |
356 }); | 358 }); |
357 }, 'Clone on Response'); | 359 }, 'Clone on Response (text)'); |
358 | 360 |
359 promise_test(function(t) { | 361 promise_test(function(t) { |
360 var res = new Response('hello'); | 362 var res = new Response('hello'); |
| 363 var body = res.body; |
| 364 var clone = res.clone(); |
| 365 assert_not_equals(res.body, body); |
| 366 assert_not_equals(res.body, clone.body); |
| 367 assert_not_equals(body, clone.body); |
| 368 assert_throws({name: 'TypeError'}, function() { body.getReader(); }); |
| 369 var reader1 = res.body.getReader(); |
| 370 var reader2 = clone.body.getReader(); |
| 371 return Promise.all([consume(reader1), consume(reader2)]).then(function(r) { |
| 372 assert_equals(decode(r[0]), 'hello'); |
| 373 assert_equals(decode(r[1]), 'hello'); |
| 374 }); |
| 375 }, 'Clone on Response (manual read)'); |
| 376 |
| 377 promise_test(function(t) { |
| 378 var res = new Response('hello'); |
361 var clone = res.clone(); | 379 var clone = res.clone(); |
362 res.body.cancel(); | 380 res.body.cancel(); |
363 return Promise.all([res.text(), clone.text()]).then(function(r) { | 381 return Promise.all([res.text(), clone.text()]).then(function(r) { |
364 assert_equals(r[0], ''); | 382 assert_equals(r[0], ''); |
365 assert_equals(r[1], 'hello'); | 383 assert_equals(r[1], 'hello'); |
366 }); | 384 }); |
367 }, 'Clone and Cancel on Response'); | 385 }, 'Clone and Cancel on Response'); |
368 | 386 |
369 promise_test(function(t) { | 387 promise_test(function(t) { |
370 var res = new Response('hello'); | 388 var res = new Response('hello'); |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
469 res.body.cancel(); | 487 res.body.cancel(); |
470 res = res.clone(); | 488 res = res.clone(); |
471 return res.blob() | 489 return res.blob() |
472 .then(function(blob) { | 490 .then(function(blob) { |
473 assert_equals(blob.type, 'text/html'); | 491 assert_equals(blob.type, 'text/html'); |
474 assert_equals(res.headers.get('Content-Type'), 'Text/Html'); | 492 assert_equals(res.headers.get('Content-Type'), 'Text/Html'); |
475 }); | 493 }); |
476 }, 'Extract a MIME type (3)'); | 494 }, 'Extract a MIME type (3)'); |
477 | 495 |
478 done(); | 496 done(); |
OLD | NEW |