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 } | |
7 | |
8 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.
| |
9 var chunks = []; | |
10 function consume() { | |
11 return reader.read().then(function(r) { | |
12 if (r.done) { | |
13 return chunks; | |
14 } else { | |
15 chunks.push(r.value); | |
16 return consume(); | |
17 } | |
18 }); | |
19 } | |
20 return consume(); | |
21 } | |
22 | |
23 test(() => { | |
24 var controller; | |
25 var stream = new ReadableStream({start: c => controller = c}); | |
26 | |
27 var response = new Response(stream); | |
28 // TODO(yhirano): This should be assert_equals. | |
29 assert_not_equals(response.body, stream); | |
30 }, 'Response constructed with a stream'); | |
31 | |
32 promise_test(() => { | |
33 var controller; | |
34 var stream = new ReadableStream({start: c => controller = c}); | |
35 controller.enqueue(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x6f])); | |
36 controller.enqueue(new Uint8Array([0x77, 0x6f, 0x72, 0x6c, 0x64])); | |
37 controller.close(); | |
38 assert_false(stream.locked); | |
39 var response = new Response(stream); | |
40 return response.text().then(t => { | |
41 assert_equals(t, 'helloworld'); | |
42 }); | |
43 assert_true(stream.locked); | |
domenic
2015/12/09 13:03:18
Unreachable code
yhirano
2015/12/10 10:51:05
Nice catch! Fixed.
| |
44 }, 'Response constructed with a stream'); | |
45 | |
46 promise_test(() => { | |
47 var controller; | |
48 var stream = new ReadableStream({start: c => controller = c}); | |
49 controller.enqueue(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x6f])); | |
50 controller.enqueue(new Uint8Array([0x77, 0x6f, 0x72, 0x6c, 0x64])); | |
51 controller.close(); | |
52 | |
53 var response = new Response(stream); | |
54 return read_until_end(response.body.getReader()).then(chunks => { | |
55 var decoder = new TextDecoder('utf-8'); | |
56 var r = ''; | |
57 for (var chunk of chunks) { | |
58 r += decoder.decode(chunk, {stream: true}); | |
59 } | |
60 r += decoder.decode(); | |
61 assert_equals(r, 'helloworld'); | |
62 }); | |
63 }, 'Response constructed with a stream / Read from body stream'); | |
64 | |
65 promise_test(t => { | |
66 var controller; | |
67 var stream = new ReadableStream({start: c => controller = c}); | |
68 setTimeout(() => { | |
69 controller.enqueue(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x6f])); | |
70 controller.enqueue(new Uint8Array([0x77, 0x6f, 0x72, 0x6c, 0x64])); | |
71 controller.error(); | |
72 }, 1); | |
73 var response = new Response(stream); | |
74 return response.text().then(unreached_fulfillment(t), e => { | |
75 assert_equals(e.name, 'TypeError'); | |
76 }); | |
77 }, 'Response constructed with an errored stream'); | |
78 | |
79 promise_test(t => { | |
80 var controller; | |
81 var stream = new ReadableStream({start: c => controller = c}); | |
82 stream.getReader(); | |
83 var response = new Response(stream); | |
84 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.
| |
85 assert_equals(e.name, 'TypeError'); | |
86 }); | |
87 }, 'Response constructed with a locked stream'); | |
88 | |
89 promise_test(t => { | |
90 var controller; | |
91 var stream = new ReadableStream({start: c => controller = c}); | |
92 setTimeout(() => controller.enqueue(), 1); | |
93 var response = new Response(stream); | |
94 return response.text().then(unreached_fulfillment(t), e => { | |
95 assert_equals(e.name, 'TypeError'); | |
96 }); | |
97 }, 'Response constructed stream with an undefined chunk'); | |
98 | |
99 promise_test(t => { | |
100 var controller; | |
101 var stream = new ReadableStream({start: c => controller = c}); | |
102 setTimeout(() => controller.enqueue(null), 1); | |
103 var response = new Response(stream); | |
104 return response.text().then(unreached_fulfillment(t), e => { | |
105 assert_equals(e.name, 'TypeError'); | |
106 }); | |
107 }, 'Response constructed stream with a null chunk'); | |
108 | |
109 promise_test(t => { | |
110 var controller; | |
111 var stream = new ReadableStream({start: c => controller = c}); | |
112 setTimeout(() => controller.enqueue('hello'), 1); | |
113 var response = new Response(stream); | |
114 return response.text().then(unreached_fulfillment(t), e => { | |
115 assert_equals(e.name, 'TypeError'); | |
116 }); | |
117 }, 'Response constructed stream with a string chunk'); | |
118 | |
OLD | NEW |