Skip to content

Commit

Permalink
Add charset content-type tests
Browse files Browse the repository at this point in the history
  • Loading branch information
josh committed Jan 15, 2015
1 parent c4b7334 commit 7474e42
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 13 deletions.
16 changes: 16 additions & 0 deletions test/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@ var routes = {
});
res.end('hi');
},
'/hello/utf8': function(res) {
res.writeHead(200, {
'Content-Type': 'text/plain; charset=utf-8'
});
// "hello"
var buf = new Buffer([104, 101, 108, 108, 111]);
res.end(buf);
},
'/hello/utf16le': function(res) {
res.writeHead(200, {
'Content-Type': 'text/plain; charset=utf-16le'
});
// "hello"
var buf = new Buffer([104, 0, 101, 0, 108, 0, 108, 0, 111, 0]);
res.end(buf);
},
'/binary': function(res) {
res.writeHead(200, {'Content-Type': 'application/octet-stream'});
var buf = new Buffer(256);
Expand Down
80 changes: 67 additions & 13 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@
function readBlobAsText(blob) {
return new Promise(function(resolve, reject) {
var reader = new FileReader()
reader.onload = function() {
resolve(reader.result)
}
reader.onerror = function() {
reject(reader.error)
}
reader.readAsText(blob)
})
}

function readBlobAsBytes(blob) {
return new Promise(function(resolve, reject) {
var reader = new FileReader()
reader.onload = function() {
var view = new Uint8Array(reader.result)
resolve(Array.prototype.slice.call(view))
}
reader.onerror = function() {
reject(reader.error)
}
reader.readAsArrayBuffer(blob)
})
}

test('resolves promise on 500 error', function() {
return fetch('/boom').then(function(response) {
assert.equal(response.status, 500)
Expand Down Expand Up @@ -44,19 +71,6 @@ suite('Request', function() {

// https://fetch.spec.whatwg.org/#response-class
suite('Response', function() {
function readBlobAsText(blob) {
return new Promise(function(resolve, reject) {
var reader = new FileReader()
reader.onload = function() {
resolve(reader.result)
}
reader.onerror = function() {
reject(reader.error)
}
reader.readAsText(blob)
})
}

// https://fetch.spec.whatwg.org/#concept-bodyinit-extract
suite('BodyInit extract', function() {
;(Response.prototype.blob ? suite : suite.skip)('type Blob', function() {
Expand Down Expand Up @@ -134,6 +148,28 @@ suite('Body mixin', function() {
})
})

test('arrayBuffer handles utf-8 data', function() {
return fetch('/hello/utf8').then(function(response) {
return response.arrayBuffer()
}).then(function(buf) {
assert(buf instanceof ArrayBuffer, 'buf is an ArrayBuffer instance')
assert.equal(buf.byteLength, 5, 'buf.byteLength is correct')
var octets = Array.prototype.slice.call(new Uint8Array(buf))
assert.deepEqual(octets, [104, 101, 108, 108, 111])
})
})

test('arrayBuffer handles utf-16le data', function() {
return fetch('/hello/utf16le').then(function(response) {
return response.arrayBuffer()
}).then(function(buf) {
assert(buf instanceof ArrayBuffer, 'buf is an ArrayBuffer instance')
assert.equal(buf.byteLength, 10, 'buf.byteLength is correct')
var octets = Array.prototype.slice.call(new Uint8Array(buf))
assert.deepEqual(octets, [104, 0, 101, 0, 108, 0, 108, 0, 111, 0])
})
})

test('rejects arrayBuffer promise after body is consumed', function() {
return fetch('/hello').then(function(response) {
assert(response.arrayBuffer, 'Body does not implement arrayBuffer')
Expand Down Expand Up @@ -164,6 +200,24 @@ suite('Body mixin', function() {
})
})

test('blob handles utf-8 data', function() {
return fetch('/hello/utf8').then(function(response) {
return response.blob()
}).then(readBlobAsBytes).then(function(octets) {
assert.equal(octets.length, 5, 'blob.size is correct')
assert.deepEqual(octets, [104, 101, 108, 108, 111])
})
})

test('blob handles utf-16le data', function() {
return fetch('/hello/utf16le').then(function(response) {
return response.blob()
}).then(readBlobAsBytes).then(function(octets) {
assert.equal(octets.length, 10, 'blob.size is correct')
assert.deepEqual(octets, [104, 0, 101, 0, 108, 0, 108, 0, 111, 0])
})
})

test('rejects blob promise after body is consumed', function() {
return fetch('/hello').then(function(response) {
assert(response.blob, 'Body does not implement blob')
Expand Down

0 comments on commit 7474e42

Please sign in to comment.