koa-lite/test/response/status.js
Tejas Manohar 9f27c1c414 refactor to use ES6 const
change var to const for static require()'d modules

make constant var references in app use const keyword

refactor context to use es6 constants

refactor request to use es6 constants, let block-scope coming next

use const in response object for static refs

make context tests use es6 constants

experimental unit tests -> const

use const for static references in unit test over req

use const for static refs in res tests

update app tests to use const for static refs

make the context test use es6 constants for static refs

use constants in the README
es6 constants seem to work in --harmony on 0.12 too

use const's for immutable refs in benchmarks

ensure all JS files have blank newline at top

add newline to bottom of file where missing

add a webchat freenode link to irc channel

no need to assign error in catch{}-able test

app.silent option to turn off err logging

keep test env logging for backwards-compat
2015-10-11 21:22:33 -07:00

123 lines
3.2 KiB
JavaScript

'use strict';
const response = require('../context').response;
const request = require('supertest');
const statuses = require('statuses');
const assert = require('assert');
const koa = require('../..');
describe('res.status=', function(){
describe('when a status code', function(){
describe('and valid', function(){
it('should set the status', function(){
const res = response();
res.status = 403;
res.status.should.equal(403);
})
it('should not throw', function(){
assert.doesNotThrow(function() {
response().status = 403;
});
})
})
describe('and invalid', function(){
it('should throw', function(){
assert.throws(function() {
response().status = 999;
}, 'invalid status code: 999');
})
})
describe('and custom status', function (){
before(function () {
statuses['700'] = 'custom status';
})
it('should set the status', function (){
const res = response();
res.status = 700;
res.status.should.equal(700);
})
it('should not throw', function(){
assert.doesNotThrow(function() {
response().status = 700;
});
})
})
})
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){
const 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) {
const 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);
})
})