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
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
describe('res.status=', () => {
|
|
|
|
describe('when a status code', () => {
|
|
|
|
describe('and valid', () => {
|
|
|
|
it('should set the status', () => {
|
2015-10-05 18:23:47 +00:00
|
|
|
const res = response();
|
2014-06-07 23:26:19 +00:00
|
|
|
res.status = 403;
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal(res.status, 403);
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2013-11-13 17:01:15 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
it('should not throw', () => {
|
|
|
|
assert.doesNotThrow(() => {
|
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
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
describe('and invalid', () => {
|
|
|
|
it('should throw', () => {
|
|
|
|
assert.throws(() => {
|
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-25 07:54:57 +00:00
|
|
|
describe('and custom status', () => {
|
2017-05-11 03:30:32 +00:00
|
|
|
beforeEach(() => statuses['700'] = 'custom status');
|
2014-10-01 12:12:20 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
it('should set the status', () => {
|
2015-10-05 18:23:47 +00:00
|
|
|
const res = response();
|
2014-10-01 12:12:20 +00:00
|
|
|
res.status = 700;
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal(res.status, 700);
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2014-10-01 12:12:20 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
it('should not throw', () => {
|
|
|
|
assert.doesNotThrow(() => response().status = 700);
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
2017-11-06 12:17:43 +00:00
|
|
|
|
|
|
|
describe('and HTTP/2', () => {
|
|
|
|
it('should not set the status message', () => {
|
|
|
|
const res = response({
|
|
|
|
'httpVersionMajor': 2,
|
|
|
|
'httpVersion': '2.0'
|
|
|
|
});
|
|
|
|
res.status = 200;
|
|
|
|
assert(!res.res.statusMessage);
|
|
|
|
});
|
|
|
|
});
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2013-11-13 17:01:15 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
describe('when a status string', () => {
|
|
|
|
it('should throw', () => {
|
|
|
|
assert.throws(() => 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
|
|
|
|
2017-06-17 22:48:55 +00:00
|
|
|
function strip(status){
|
2017-05-11 03:30:32 +00:00
|
|
|
it('should strip content related header fields', async () => {
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2013-11-13 17:01:15 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
app.use(ctx => {
|
2015-10-14 00:45:18 +00:00
|
|
|
ctx.body = { foo: 'bar' };
|
|
|
|
ctx.set('Content-Type', 'application/json; charset=utf-8');
|
|
|
|
ctx.set('Content-Length', '15');
|
|
|
|
ctx.set('Transfer-Encoding', 'chunked');
|
|
|
|
ctx.status = status;
|
|
|
|
assert(null == ctx.response.header['content-type']);
|
|
|
|
assert(null == ctx.response.header['content-length']);
|
|
|
|
assert(null == ctx.response.header['transfer-encoding']);
|
2013-11-13 17:01:15 +00:00
|
|
|
});
|
|
|
|
|
2017-09-26 04:07:57 +00:00
|
|
|
const res = await request(app.callback())
|
2013-11-13 17:01:15 +00:00
|
|
|
.get('/')
|
2017-05-11 03:30:32 +00:00
|
|
|
.expect(status);
|
|
|
|
|
|
|
|
assert.equal(res.headers.hasOwnProperty('content-type'), false);
|
|
|
|
assert.equal(res.headers.hasOwnProperty('content-length'), false);
|
|
|
|
assert.equal(res.headers.hasOwnProperty('content-encoding'), false);
|
|
|
|
assert.equal(res.text.length, 0);
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2014-04-09 16:21:15 +00:00
|
|
|
|
2017-05-11 03:30:32 +00:00
|
|
|
it('should strip content releated header fields after status set', async () => {
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2014-04-09 16:21:15 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
app.use(ctx => {
|
2015-10-14 00:45:18 +00:00
|
|
|
ctx.status = status;
|
|
|
|
ctx.body = { foo: 'bar' };
|
|
|
|
ctx.set('Content-Type', 'application/json; charset=utf-8');
|
|
|
|
ctx.set('Content-Length', '15');
|
|
|
|
ctx.set('Transfer-Encoding', 'chunked');
|
2014-04-09 16:21:15 +00:00
|
|
|
});
|
|
|
|
|
2017-09-26 04:07:57 +00:00
|
|
|
const res = await request(app.callback())
|
2014-04-09 16:21:15 +00:00
|
|
|
.get('/')
|
2017-05-11 03:30:32 +00:00
|
|
|
.expect(status);
|
|
|
|
|
|
|
|
assert.equal(res.headers.hasOwnProperty('content-type'), false);
|
|
|
|
assert.equal(res.headers.hasOwnProperty('content-length'), false);
|
|
|
|
assert.equal(res.headers.hasOwnProperty('content-encoding'), false);
|
|
|
|
assert.equal(res.text.length, 0);
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2013-11-13 17:01:15 +00:00
|
|
|
}
|
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
describe('when 204', () => strip(204));
|
2013-11-13 17:01:15 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
describe('when 205', () => strip(205));
|
2014-04-09 16:21:15 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
describe('when 304', () => strip(304));
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|