koa-lite/test/response/status.js

103 lines
2.8 KiB
JavaScript
Raw Normal View History

var response = require('../context').response;
var request = require('supertest');
var assert = require('assert');
var koa = require('../..');
describe('res.status=', function(){
2014-06-07 23:26:19 +00:00
describe('when a status code', function(){
describe('and valid', function(){
it('should set the status', function(){
var res = response();
2014-06-07 23:26:19 +00:00
res.status = 403;
res.status.should.equal(403);
})
2014-06-07 23:26:19 +00:00
it('should not throw', function(){
assert.doesNotThrow(function() {
response().status = 403;
});
})
})
describe('and invalid', function(){
it('should throw', function(){
2014-06-07 23:26:19 +00:00
assert.throws(function() {
response().status = 999;
}, 'invalid status code: 999');
})
})
})
2014-06-07 23:26:19 +00:00
describe('when a status string', function(){
it('should throw', function(){
assert.throws(function() {
response().status = 'forbidden';
}, 'status code must be a number');
})
})
function strip(status) {
it('should strip content related header fields', function(done){
var app = koa();
app.use(function *(){
this.body = { foo: 'bar' };
this.set('Content-Type', 'application/json; charset=utf-8');
this.set('Content-Length', '15');
this.set('Transfer-Encoding', 'chunked');
this.status = status;
assert(null == this.response.header['content-type']);
assert(null == this.response.header['content-length']);
assert(null == this.response.header['transfer-encoding']);
});
request(app.listen())
.get('/')
.expect(status)
.end(function(err, res) {
res.should.not.have.header('content-type');
res.should.not.have.header('content-length');
res.should.not.have.header('content-encoding');
res.text.should.have.length(0);
done(err);
});
})
it('should strip content releated header fields after status set', function(done) {
var app = koa();
app.use(function *(){
this.status = status;
this.body = { foo: 'bar' };
this.set('Content-Type', 'application/json; charset=utf-8');
this.set('Content-Length', '15');
this.set('Transfer-Encoding', 'chunked');
});
request(app.listen())
.get('/')
.expect(status)
.end(function(err, res) {
res.should.not.have.header('content-type');
res.should.not.have.header('content-length');
res.should.not.have.header('content-encoding');
res.text.should.have.length(0);
done(err);
});
})
}
describe('when 204', function(){
strip(204);
})
describe('when 205', function(){
strip(205);
})
describe('when 304', function(){
strip(304);
})
2014-03-24 18:21:15 +00:00
})