better test coverage on edge case

This commit is contained in:
David Frank 2015-01-28 13:01:10 +08:00
parent be778b9d8c
commit 60d232d66a
2 changed files with 99 additions and 0 deletions

View File

@ -83,6 +83,13 @@ TestServer.prototype.router = function(req, res) {
});
}
if (p === '/sdch') {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.setHeader('Content-Encoding', 'sdch');
res.end('fake sdch string');
}
if (p === '/timeout') {
setTimeout(function() {
res.statusCode = 200;
@ -115,6 +122,11 @@ TestServer.prototype.router = function(req, res) {
res.end(convert('<div>日本語</div>', 'Shift_JIS'));
}
if (p === '/encoding/utf8') {
res.statusCode = 200;
res.end('中文');
}
if (p === '/redirect/301') {
res.statusCode = 301;
res.setHeader('Location', '/inspect');
@ -173,6 +185,17 @@ TestServer.prototype.router = function(req, res) {
res.destroy();
}
if (p === '/error/json') {
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.end('invalid json');
}
if (p === '/empty') {
res.statusCode = 204;
res.end();
}
if (p === '/inspect') {
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');

View File

@ -246,6 +246,27 @@ describe('node-fetch', function() {
return expect(fetch(url)).to.eventually.be.rejectedWith(Error);
});
it('should reject invalid json response', function() {
url = base + '/error/json';
return fetch(url).then(function(res) {
expect(res.headers.get('content-type')).to.equal('application/json');
return expect(res.json()).to.eventually.be.rejectedWith(Error);
});
});
it('should handle empty response', function() {
url = base + '/empty';
return fetch(url).then(function(res) {
expect(res.status).to.equal(204);
expect(res.statusText).to.equal('No Content');
expect(res.ok).to.be.true;
return res.text().then(function(result) {
expect(result).to.be.a('string');
expect(result).to.be.empty;
});
});
});
it('should decompress gzip response', function() {
url = base + '/gzip';
return fetch(url).then(function(res) {
@ -268,6 +289,17 @@ describe('node-fetch', function() {
});
});
it('should skip decompression if unsupported', function() {
url = base + '/sdch';
return fetch(url).then(function(res) {
expect(res.headers.get('content-type')).to.equal('text/plain');
return res.text().then(function(result) {
expect(result).to.be.a('string');
expect(result).to.equal('fake sdch string');
});
});
});
it('should allow disabling auto decompression', function() {
url = base + '/gzip';
opts = {
@ -356,6 +388,20 @@ describe('node-fetch', function() {
});
});
it('should allow PATCH request', function() {
url = base + '/inspect';
opts = {
method: 'PATCH'
, body: 'a=1'
};
return fetch(url, opts).then(function(res) {
return res.json();
}).then(function(res) {
expect(res.method).to.equal('PATCH');
expect(res.body).to.equal('a=1');
});
});
it('should allow HEAD request', function() {
url = base + '/hello';
opts = {
@ -423,10 +469,40 @@ describe('node-fetch', function() {
});
});
it('should default to utf8 encoding', function() {
url = base + '/encoding/utf8';
return fetch(url).then(function(res) {
expect(res.status).to.equal(200);
expect(res.headers.get('content-type')).to.be.null;
return res.text().then(function(result) {
expect(result).to.equal('中文');
});
});
});
it('should allow piping response body as stream', function(done) {
url = base + '/hello';
fetch(url).then(function(res) {
expect(res.body).to.be.an.instanceof(stream.Transform);
res.body.on('data', function(chunk) {
if (chunk === null) {
return;
}
expect(chunk.toString()).to.equal('world');
});
res.body.on('end', function() {
done();
});
});
});
it('should allow get all responses of a header', function() {
url = base + '/cookie';
return fetch(url).then(function(res) {
expect(res.headers.get('set-cookie')).to.equal('a=1');
expect(res.headers.get('Set-Cookie')).to.equal('a=1');
expect(res.headers.getAll('set-cookie')).to.deep.equal(['a=1', 'b=1']);
expect(res.headers.getAll('Set-Cookie')).to.deep.equal(['a=1', 'b=1']);
});
});