OLD | NEW |
| (Empty) |
1 if (self.importScripts) { | |
2 importScripts('../resources/fetch-test-helpers.js'); | |
3 } | |
4 | |
5 function readStream(reader, values) { | |
6 reader.read().then(function(r) { | |
7 if (!r.done) { | |
8 values.push(r.value); | |
9 readStream(reader, values); | |
10 } | |
11 }); | |
12 return reader.closed; | |
13 } | |
14 | |
15 sequential_promise_test(function(test) { | |
16 return fetch('/fetch/resources/doctype.html') | |
17 .then(function(response) { | |
18 // Accessing the body property makes the stream start working. | |
19 var stream = response.body; | |
20 return response.text(); | |
21 }) | |
22 .then(function(text) { | |
23 assert_equals(text, '<!DOCTYPE html>\n'); | |
24 }) | |
25 }, 'FetchTextAfterAccessingStreamTest'); | |
26 | |
27 sequential_promise_test(function(test) { | |
28 var chunks = []; | |
29 var actual = ''; | |
30 return fetch('/fetch/resources/doctype.html') | |
31 .then(function(response) { | |
32 r = response; | |
33 return readStream(response.body.getReader(), chunks); | |
34 }) | |
35 .then(function() { | |
36 var decoder = new TextDecoder(); | |
37 for (var chunk of chunks) { | |
38 actual += decoder.decode(chunk, {stream: true}); | |
39 } | |
40 // Put an empty buffer without the stream option to end decoding. | |
41 actual += decoder.decode(new Uint8Array(0)); | |
42 assert_equals(actual, '<!DOCTYPE html>\n'); | |
43 }) | |
44 }, 'FetchStreamTest'); | |
45 | |
46 sequential_promise_test(function(test) { | |
47 return fetch('/fetch/resources/progressive.php') | |
48 .then(function(response) { | |
49 var p1 = response.text(); | |
50 // Because progressive.php takes some time to load, we expect | |
51 // response.text() is not yet completed here. | |
52 var p2 = response.text().then(function() { | |
53 return Promise.reject(new Error('resolved unexpectedly')); | |
54 }, function(e) { | |
55 return e; | |
56 }); | |
57 return Promise.all([p1, p2]); | |
58 }) | |
59 .then(function(results) { | |
60 assert_equals(results[0].length, 190); | |
61 assert_equals(results[1].name, 'TypeError'); | |
62 }) | |
63 }, 'FetchTwiceTest'); | |
64 | |
65 sequential_promise_test(function(test) { | |
66 return fetch('/fetch/resources/doctype.html') | |
67 .then(function(response) { | |
68 return response.arrayBuffer(); | |
69 }) | |
70 .then(function(b) { | |
71 assert_equals(b.byteLength, 16); | |
72 }) | |
73 }, 'ArrayBufferTest'); | |
74 | |
75 sequential_promise_test(function(test) { | |
76 return fetch('/fetch/resources/doctype.html') | |
77 .then(function(response) { | |
78 return response.blob(); | |
79 }) | |
80 .then(function(blob) { | |
81 assert_equals(blob.size, 16); | |
82 assert_equals(blob.type, 'text/html'); | |
83 }) | |
84 }, 'BlobTest'); | |
85 | |
86 sequential_promise_test(function(test) { | |
87 return fetch('/fetch/resources/doctype.html') | |
88 .then(function(response) { | |
89 return response.json(); | |
90 }) | |
91 .then( | |
92 test.unreached_func('json() must fail'), | |
93 function(e) { | |
94 assert_equals(e.name, 'SyntaxError', 'expected JSON error'); | |
95 }) | |
96 }, 'JSONFailedTest'); | |
97 | |
98 sequential_promise_test(function(test) { | |
99 return fetch('/fetch/resources/simple.json') | |
100 .then(function(response) { | |
101 return response.json(); | |
102 }) | |
103 .then(function(json) { | |
104 assert_equals(json['a'], 1); | |
105 assert_equals(json['b'], 2); | |
106 }) | |
107 }, 'JSONTest'); | |
108 | |
109 sequential_promise_test(function(test) { | |
110 return fetch('/fetch/resources/doctype.html') | |
111 .then(function(response) { | |
112 return response.text(); | |
113 }) | |
114 .then(function(text) { | |
115 assert_equals(text, '<!DOCTYPE html>\n'); | |
116 }) | |
117 }, 'TextTest'); | |
118 | |
119 sequential_promise_test(function(test) { | |
120 return fetch('/fetch/resources/non-ascii.txt') | |
121 .then(function(response) { | |
122 return response.text(); | |
123 }) | |
124 .then(function(text) { | |
125 assert_equals(text, '\u4e2d\u6587 Gem\u00fcse\n'); | |
126 }) | |
127 }, 'NonAsciiTextTest'); | |
128 | |
129 sequential_promise_test(function(test) { | |
130 var expected = ''; | |
131 for (var i = 0; i < 100; ++i) | |
132 expected += i; | |
133 | |
134 var decoder = new TextDecoder(); | |
135 var actual = ''; | |
136 var response; | |
137 var reader; | |
138 return fetch('/fetch/resources/progressive.php') | |
139 .then(function(res) { | |
140 response = res; | |
141 reader = response.body.getReader(); | |
142 return reader.read(); | |
143 }) | |
144 .then(function(r) { | |
145 assert_false(r.done); | |
146 actual += decoder.decode(r.value, {stream: true}); | |
147 }) | |
148 .then(function() { | |
149 return response.text().then(unreached_fulfillment(test), function() { | |
150 // response.text() should fail because we have a reader. | |
151 }); | |
152 }) | |
153 .then(function() { | |
154 reader.releaseLock(); | |
155 return response.arrayBuffer(); | |
156 }) | |
157 .then(function(buffer) { | |
158 actual += decoder.decode(buffer); | |
159 assert_equals(actual, expected); | |
160 }) | |
161 }, 'PartiallyReadFromStreamAndReadArrayBufferTest'); | |
162 | |
163 done(); | |
OLD | NEW |