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