2013-11-13 17:01:15 +00:00
|
|
|
|
2015-10-11 22:59:51 +00:00
|
|
|
'use strict';
|
|
|
|
|
2015-10-12 04:59:30 +00:00
|
|
|
const response = require('../helpers/context').response;
|
2015-10-05 18:23:47 +00:00
|
|
|
const request = require('supertest');
|
|
|
|
const statuses = require('statuses');
|
|
|
|
const assert = require('assert');
|
2015-10-13 06:19:42 +00:00
|
|
|
const Koa = require('../..');
|
2013-11-13 17:01:15 +00:00
|
|
|
|
|
|
|
describe('res.status=', function(){
|
2014-06-07 23:26:19 +00:00
|
|
|
describe('when a status code', function(){
|
2013-11-13 17:01:15 +00:00
|
|
|
describe('and valid', function(){
|
|
|
|
it('should set the status', function(){
|
2015-10-05 18:23:47 +00:00
|
|
|
const res = response();
|
2014-06-07 23:26:19 +00:00
|
|
|
res.status = 403;
|
2013-11-13 17:01:15 +00:00
|
|
|
res.status.should.equal(403);
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2013-11-13 17:01:15 +00:00
|
|
|
|
2014-06-07 23:26:19 +00:00
|
|
|
it('should not throw', function(){
|
2015-10-12 20:36:41 +00:00
|
|
|
assert.doesNotThrow(function(){
|
2014-06-07 23:26:19 +00:00
|
|
|
response().status = 403;
|
|
|
|
});
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
2013-11-13 17:01:15 +00:00
|
|
|
|
|
|
|
describe('and invalid', function(){
|
|
|
|
it('should throw', function(){
|
2015-10-12 20:36:41 +00:00
|
|
|
assert.throws(function(){
|
2014-06-07 23:26:19 +00:00
|
|
|
response().status = 999;
|
|
|
|
}, 'invalid status code: 999');
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
2014-10-01 12:12:20 +00:00
|
|
|
|
2015-10-12 20:36:41 +00:00
|
|
|
describe('and custom status', function(){
|
|
|
|
before(function(){
|
2014-10-01 12:12:20 +00:00
|
|
|
statuses['700'] = 'custom status';
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2014-10-01 12:12:20 +00:00
|
|
|
|
2015-10-12 20:36:41 +00:00
|
|
|
it('should set the status', function(){
|
2015-10-05 18:23:47 +00:00
|
|
|
const res = response();
|
2014-10-01 12:12:20 +00:00
|
|
|
res.status = 700;
|
|
|
|
res.status.should.equal(700);
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2014-10-01 12:12:20 +00:00
|
|
|
|
|
|
|
it('should not throw', function(){
|
2015-10-12 20:36:41 +00:00
|
|
|
assert.doesNotThrow(function(){
|
2014-10-01 12:12:20 +00:00
|
|
|
response().status = 700;
|
|
|
|
});
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2013-11-13 17:01:15 +00:00
|
|
|
|
2014-06-07 23:26:19 +00:00
|
|
|
describe('when a status string', function(){
|
|
|
|
it('should throw', function(){
|
2015-10-12 20:36:41 +00:00
|
|
|
assert.throws(function(){
|
2014-06-07 23:26:19 +00:00
|
|
|
response().status = 'forbidden';
|
|
|
|
}, 'status code must be a number');
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
2014-06-07 23:26:19 +00:00
|
|
|
|
2015-10-12 20:36:41 +00:00
|
|
|
function strip(status){
|
2013-11-13 17:01:15 +00:00
|
|
|
it('should strip content related header fields', function(done){
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2013-11-13 17:01:15 +00:00
|
|
|
|
2013-12-19 18:03:08 +00:00
|
|
|
app.use(function *(){
|
|
|
|
this.body = { foo: 'bar' };
|
2014-06-04 04:44:25 +00:00
|
|
|
this.set('Content-Type', 'application/json; charset=utf-8');
|
2013-12-19 18:03:08 +00:00
|
|
|
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)
|
2015-10-12 20:36:41 +00:00
|
|
|
.end(function(err, res){
|
2014-04-09 16:21:15 +00:00
|
|
|
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);
|
|
|
|
});
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2014-04-09 16:21:15 +00:00
|
|
|
|
2015-10-12 20:36:41 +00:00
|
|
|
it('should strip content releated header fields after status set', function(done){
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2014-04-09 16:21:15 +00:00
|
|
|
|
|
|
|
app.use(function *(){
|
|
|
|
this.status = status;
|
|
|
|
this.body = { foo: 'bar' };
|
2014-06-04 04:44:25 +00:00
|
|
|
this.set('Content-Type', 'application/json; charset=utf-8');
|
2014-04-09 16:21:15 +00:00
|
|
|
this.set('Content-Length', '15');
|
|
|
|
this.set('Transfer-Encoding', 'chunked');
|
|
|
|
});
|
|
|
|
|
|
|
|
request(app.listen())
|
|
|
|
.get('/')
|
|
|
|
.expect(status)
|
2015-10-12 20:36:41 +00:00
|
|
|
.end(function(err, res){
|
2014-04-09 16:21:15 +00:00
|
|
|
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);
|
|
|
|
});
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2013-11-13 17:01:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
describe('when 204', function(){
|
|
|
|
strip(204);
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2013-11-13 17:01:15 +00:00
|
|
|
|
2014-04-09 16:21:15 +00:00
|
|
|
describe('when 205', function(){
|
|
|
|
strip(205);
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2014-04-09 16:21:15 +00:00
|
|
|
|
2013-11-13 17:01:15 +00:00
|
|
|
describe('when 304', function(){
|
|
|
|
strip(304);
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|