feat: ignore set/remove header/status when header sent (#1145)

This commit is contained in:
Yiyu He 2018-02-12 16:43:13 +08:00 committed by fengmk2
parent 494a365427
commit f04714227b
5 changed files with 46 additions and 0 deletions

View file

@ -79,6 +79,8 @@ module.exports = {
*/ */
set status(code) { set status(code) {
if (this.headerSent) return;
assert('number' == typeof code, 'status code must be a number'); assert('number' == typeof code, 'status code must be a number');
assert(statuses[code], 'invalid status code: ' + code); assert(statuses[code], 'invalid status code: ' + code);
this._explicitStatus = true; this._explicitStatus = true;
@ -229,6 +231,8 @@ module.exports = {
*/ */
vary: function(field){ vary: function(field){
if (this.headerSent) return;
vary(this.res, field); vary(this.res, field);
}, },
@ -430,6 +434,8 @@ module.exports = {
*/ */
set: function(field, val){ set: function(field, val){
if (this.headerSent) return;
if (2 == arguments.length) { if (2 == arguments.length) {
if (Array.isArray(val)) val = val.map(String); if (Array.isArray(val)) val = val.map(String);
else val = String(val); else val = String(val);
@ -475,6 +481,8 @@ module.exports = {
*/ */
remove: function(field){ remove: function(field){
if (this.headerSent) return;
this.res.removeHeader(field); this.res.removeHeader(field);
}, },

View file

@ -2,6 +2,7 @@
'use strict'; 'use strict';
var context = require('../context'); var context = require('../context');
var should = require('should');
describe('ctx.remove(name)', function(){ describe('ctx.remove(name)', function(){
it('should remove a field', function(){ it('should remove a field', function(){
@ -10,4 +11,14 @@ describe('ctx.remove(name)', function(){
ctx.remove('x-foo'); ctx.remove('x-foo');
ctx.response.header.should.eql({}); ctx.response.header.should.eql({});
}) })
describe('after header sent', function(){
it('should ignore', function(){
var ctx = context();
ctx.set('foo', 'bar');
ctx.res.headersSent = true;
ctx.remove('foo', 'bar');
ctx.response.header.foo.should.equal('bar');
})
})
}) })

View file

@ -2,6 +2,7 @@
'use strict'; 'use strict';
var context = require('../context'); var context = require('../context');
var should = require('should');
describe('ctx.set(name, val)', function(){ describe('ctx.set(name, val)', function(){
it('should set a field value', function(){ it('should set a field value', function(){
@ -21,6 +22,14 @@ describe('ctx.set(name, val)', function(){
ctx.set('x-foo', ['foo', 'bar']); ctx.set('x-foo', ['foo', 'bar']);
ctx.response.header['x-foo'].should.eql([ 'foo', 'bar' ]); ctx.response.header['x-foo'].should.eql([ 'foo', 'bar' ]);
}) })
describe('after header sent', function(){
it('should ignore', function(){
var ctx = context(null, { headersSent: true });
ctx.set('foo', 'bar');
should.not.exist(ctx.response.header.foo);
})
})
}) })
describe('ctx.set(object)', function(){ describe('ctx.set(object)', function(){

View file

@ -5,6 +5,7 @@ var response = require('../context').response;
var request = require('supertest'); var request = require('supertest');
var statuses = require('statuses'); var statuses = require('statuses');
var assert = require('assert'); var assert = require('assert');
var should = require('should');
var koa = require('../..'); var koa = require('../..');
describe('res.status=', function(){ describe('res.status=', function(){
@ -50,6 +51,14 @@ describe('res.status=', function(){
}) })
}) })
describe('after header sent', function(){
it('should ignore', function(){
var res = response(null, { headersSent: true });
res.status = 200;
should.not.exist(res.status);
})
})
describe('when a status string', function(){ describe('when a status string', function(){
it('should throw', function(){ it('should throw', function(){
assert.throws(function() { assert.throws(function() {

View file

@ -2,6 +2,7 @@
'use strict'; 'use strict';
var context = require('../context'); var context = require('../context');
var should = require('should');
describe('ctx.vary(field)', function(){ describe('ctx.vary(field)', function(){
describe('when Vary is not set', function(){ describe('when Vary is not set', function(){
@ -31,4 +32,12 @@ describe('ctx.vary(field)', function(){
ctx.response.header.vary.should.equal('Accept, Accept-Encoding'); ctx.response.header.vary.should.equal('Accept, Accept-Encoding');
}) })
}) })
describe('after header sent', function(){
it('should ignore', function(){
var ctx = context(null, { headersSent: true });
ctx.vary('Accept');
should.not.exist(ctx.response.header.vary);
})
})
}) })