|
| 1 | +function readBlobAsText(blob) { |
| 2 | + return new Promise(function(resolve, reject) { |
| 3 | + var reader = new FileReader() |
| 4 | + reader.onload = function() { |
| 5 | + resolve(reader.result) |
| 6 | + } |
| 7 | + reader.onerror = function() { |
| 8 | + reject(reader.error) |
| 9 | + } |
| 10 | + reader.readAsText(blob) |
| 11 | + }) |
| 12 | +} |
| 13 | + |
| 14 | +function readBlobAsBytes(blob) { |
| 15 | + return new Promise(function(resolve, reject) { |
| 16 | + var reader = new FileReader() |
| 17 | + reader.onload = function() { |
| 18 | + var view = new Uint8Array(reader.result) |
| 19 | + resolve(Array.prototype.slice.call(view)) |
| 20 | + } |
| 21 | + reader.onerror = function() { |
| 22 | + reject(reader.error) |
| 23 | + } |
| 24 | + reader.readAsArrayBuffer(blob) |
| 25 | + }) |
| 26 | +} |
| 27 | + |
1 | 28 | test('resolves promise on 500 error', function() { |
2 | 29 | return fetch('/boom').then(function(response) { |
3 | 30 | assert.equal(response.status, 500) |
@@ -44,19 +71,6 @@ suite('Request', function() { |
44 | 71 |
|
45 | 72 | // https://fetch.spec.whatwg.org/#response-class |
46 | 73 | suite('Response', function() { |
47 | | - function readBlobAsText(blob) { |
48 | | - return new Promise(function(resolve, reject) { |
49 | | - var reader = new FileReader() |
50 | | - reader.onload = function() { |
51 | | - resolve(reader.result) |
52 | | - } |
53 | | - reader.onerror = function() { |
54 | | - reject(reader.error) |
55 | | - } |
56 | | - reader.readAsText(blob) |
57 | | - }) |
58 | | - } |
59 | | - |
60 | 74 | // https://fetch.spec.whatwg.org/#concept-bodyinit-extract |
61 | 75 | suite('BodyInit extract', function() { |
62 | 76 | ;(Response.prototype.blob ? suite : suite.skip)('type Blob', function() { |
@@ -134,6 +148,28 @@ suite('Body mixin', function() { |
134 | 148 | }) |
135 | 149 | }) |
136 | 150 |
|
| 151 | + test('arrayBuffer handles utf-8 data', function() { |
| 152 | + return fetch('/hello/utf8').then(function(response) { |
| 153 | + return response.arrayBuffer() |
| 154 | + }).then(function(buf) { |
| 155 | + assert(buf instanceof ArrayBuffer, 'buf is an ArrayBuffer instance') |
| 156 | + assert.equal(buf.byteLength, 5, 'buf.byteLength is correct') |
| 157 | + var octets = Array.prototype.slice.call(new Uint8Array(buf)) |
| 158 | + assert.deepEqual(octets, [104, 101, 108, 108, 111]) |
| 159 | + }) |
| 160 | + }) |
| 161 | + |
| 162 | + test('arrayBuffer handles utf-16le data', function() { |
| 163 | + return fetch('/hello/utf16le').then(function(response) { |
| 164 | + return response.arrayBuffer() |
| 165 | + }).then(function(buf) { |
| 166 | + assert(buf instanceof ArrayBuffer, 'buf is an ArrayBuffer instance') |
| 167 | + assert.equal(buf.byteLength, 10, 'buf.byteLength is correct') |
| 168 | + var octets = Array.prototype.slice.call(new Uint8Array(buf)) |
| 169 | + assert.deepEqual(octets, [104, 0, 101, 0, 108, 0, 108, 0, 111, 0]) |
| 170 | + }) |
| 171 | + }) |
| 172 | + |
137 | 173 | test('rejects arrayBuffer promise after body is consumed', function() { |
138 | 174 | return fetch('/hello').then(function(response) { |
139 | 175 | assert(response.arrayBuffer, 'Body does not implement arrayBuffer') |
@@ -164,6 +200,24 @@ suite('Body mixin', function() { |
164 | 200 | }) |
165 | 201 | }) |
166 | 202 |
|
| 203 | + test('blob handles utf-8 data', function() { |
| 204 | + return fetch('/hello/utf8').then(function(response) { |
| 205 | + return response.blob() |
| 206 | + }).then(readBlobAsBytes).then(function(octets) { |
| 207 | + assert.equal(octets.length, 5, 'blob.size is correct') |
| 208 | + assert.deepEqual(octets, [104, 101, 108, 108, 111]) |
| 209 | + }) |
| 210 | + }) |
| 211 | + |
| 212 | + test('blob handles utf-16le data', function() { |
| 213 | + return fetch('/hello/utf16le').then(function(response) { |
| 214 | + return response.blob() |
| 215 | + }).then(readBlobAsBytes).then(function(octets) { |
| 216 | + assert.equal(octets.length, 10, 'blob.size is correct') |
| 217 | + assert.deepEqual(octets, [104, 0, 101, 0, 108, 0, 108, 0, 111, 0]) |
| 218 | + }) |
| 219 | + }) |
| 220 | + |
167 | 221 | test('rejects blob promise after body is consumed', function() { |
168 | 222 | return fetch('/hello').then(function(response) { |
169 | 223 | assert(response.blob, 'Body does not implement blob') |
|
0 commit comments