OLD | NEW |
(Empty) | |
| 1 // This file contains tests for Response construction with a readable stream. |
| 2 // Move these tests to response.js once the feature gets stable. |
| 3 |
| 4 if (self.importScripts) { |
| 5 importScripts('../resources/fetch-test-helpers.js'); |
| 6 importScripts('/streams/resources/rs-utils.js'); |
| 7 } |
| 8 |
| 9 test(() => { |
| 10 var controller; |
| 11 var stream = new ReadableStream({start: c => controller = c}); |
| 12 |
| 13 var response = new Response(stream); |
| 14 // TODO(yhirano): This should be assert_equals. |
| 15 assert_not_equals(response.body, stream); |
| 16 }, 'Response constructed with a stream'); |
| 17 |
| 18 promise_test(() => { |
| 19 var controller; |
| 20 var stream = new ReadableStream({start: c => controller = c}); |
| 21 controller.enqueue(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x6f])); |
| 22 controller.enqueue(new Uint8Array([0x77, 0x6f, 0x72, 0x6c, 0x64])); |
| 23 controller.close(); |
| 24 assert_false(stream.locked); |
| 25 var response = new Response(stream); |
| 26 var p = response.text().then(t => { |
| 27 assert_equals(t, 'helloworld'); |
| 28 }); |
| 29 assert_true(stream.locked); |
| 30 return p; |
| 31 }, 'Response constructed with a stream'); |
| 32 |
| 33 promise_test(() => { |
| 34 var controller; |
| 35 var stream = new ReadableStream({start: c => controller = c}); |
| 36 controller.enqueue(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x6f])); |
| 37 controller.enqueue(new Uint8Array([0x77, 0x6f, 0x72, 0x6c, 0x64])); |
| 38 controller.close(); |
| 39 |
| 40 var response = new Response(stream); |
| 41 return readableStreamToArray(response.body).then(chunks => { |
| 42 var decoder = new TextDecoder('utf-8'); |
| 43 var r = ''; |
| 44 for (var chunk of chunks) { |
| 45 r += decoder.decode(chunk, {stream: true}); |
| 46 } |
| 47 r += decoder.decode(); |
| 48 assert_equals(r, 'helloworld'); |
| 49 }); |
| 50 }, 'Response constructed with a stream / Read from body stream'); |
| 51 |
| 52 promise_test(t => { |
| 53 var controller; |
| 54 var stream = new ReadableStream({start: c => controller = c}); |
| 55 setTimeout(() => { |
| 56 controller.enqueue(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x6f])); |
| 57 controller.enqueue(new Uint8Array([0x77, 0x6f, 0x72, 0x6c, 0x64])); |
| 58 controller.error(); |
| 59 }, 1); |
| 60 var response = new Response(stream); |
| 61 return promise_rejects(t, TypeError(), response.text()); |
| 62 }, 'Response constructed with an errored stream'); |
| 63 |
| 64 promise_test(t => { |
| 65 var controller; |
| 66 var stream = new ReadableStream({start: c => controller = c}); |
| 67 stream.getReader(); |
| 68 var response = new Response(stream); |
| 69 return promise_rejects(t, TypeError(), response.text()); |
| 70 }, 'Response constructed with a locked stream'); |
| 71 |
| 72 promise_test(t => { |
| 73 var controller; |
| 74 var stream = new ReadableStream({start: c => controller = c}); |
| 75 setTimeout(() => controller.enqueue(), 1); |
| 76 var response = new Response(stream); |
| 77 return promise_rejects(t, TypeError(), response.text()); |
| 78 }, 'Response constructed stream with an undefined chunk'); |
| 79 |
| 80 promise_test(t => { |
| 81 var controller; |
| 82 var stream = new ReadableStream({start: c => controller = c}); |
| 83 setTimeout(() => controller.enqueue(null), 1); |
| 84 var response = new Response(stream); |
| 85 return promise_rejects(t, TypeError(), response.text()); |
| 86 }, 'Response constructed stream with a null chunk'); |
| 87 |
| 88 promise_test(t => { |
| 89 var controller; |
| 90 var stream = new ReadableStream({start: c => controller = c}); |
| 91 setTimeout(() => controller.enqueue('hello'), 1); |
| 92 var response = new Response(stream); |
| 93 return promise_rejects(t, TypeError(), response.text()); |
| 94 }, 'Response constructed stream with a string chunk'); |
| 95 |
| 96 done(); |
OLD | NEW |