2013-11-13 17:01:15 +00:00
|
|
|
|
2013-11-14 02:34:15 +00:00
|
|
|
var response = require('../context').response;
|
2013-11-13 17:01:15 +00:00
|
|
|
var request = require('supertest');
|
|
|
|
var assert = require('assert');
|
|
|
|
var koa = require('../..');
|
|
|
|
|
|
|
|
describe('res.status=', function(){
|
|
|
|
describe('when a status string', function(){
|
|
|
|
describe('and valid', function(){
|
|
|
|
it('should set the status', function(){
|
|
|
|
var res = response();
|
|
|
|
res.status = 'forbidden';
|
|
|
|
res.status.should.equal(403);
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should be case-insensitive', function(){
|
|
|
|
var res = response();
|
|
|
|
res.status = 'ForBidden';
|
|
|
|
res.status.should.equal(403);
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('and invalid', function(){
|
|
|
|
it('should throw', function(){
|
|
|
|
var res = response();
|
|
|
|
var err;
|
|
|
|
|
|
|
|
try {
|
|
|
|
res.status = 'maru';
|
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(err);
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
function strip(status) {
|
|
|
|
it('should strip content related header fields', function(done){
|
|
|
|
var app = koa();
|
|
|
|
|
2013-12-19 18:03:08 +00:00
|
|
|
app.use(function *(){
|
|
|
|
this.body = { foo: 'bar' };
|
|
|
|
this.set('Content-Type', 'application/json');
|
|
|
|
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']);
|
2013-11-13 17:01:15 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
request(app.listen())
|
|
|
|
.get('/')
|
|
|
|
.expect(status)
|
2014-04-09 16:21:15 +00:00
|
|
|
.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');
|
|
|
|
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);
|
|
|
|
});
|
2013-11-13 17:01:15 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('when 204', function(){
|
|
|
|
strip(204);
|
|
|
|
})
|
|
|
|
|
2014-04-09 16:21:15 +00:00
|
|
|
describe('when 205', function(){
|
|
|
|
strip(205);
|
|
|
|
})
|
|
|
|
|
2013-11-13 17:01:15 +00:00
|
|
|
describe('when 304', function(){
|
|
|
|
strip(304);
|
|
|
|
})
|
2014-03-24 18:21:15 +00:00
|
|
|
})
|