test: switch all functions to arrow functions

closes #553

Update test: application -> use() should throw if not a function

Fix lint

Use arrow function

Refactor test using arrow function

Remove non mandatory brackets

fix for merge

Fix: missing refactor after merge

Use arrow function for old generator
This commit is contained in:
broucz 2015-10-25 08:54:57 +01:00 committed by jongleberry
parent 5bfe0d4081
commit 4b1a1da652
64 changed files with 615 additions and 624 deletions

View file

@ -5,13 +5,13 @@ const request = require('supertest');
const assert = require('assert'); const assert = require('assert');
const Koa = require('../..'); const Koa = require('../..');
describe('app.context', function(){ describe('app.context', () => {
const app1 = new Koa(); const app1 = new Koa();
app1.context.msg = 'hello'; app1.context.msg = 'hello';
const app2 = new Koa(); const app2 = new Koa();
it('should merge properties', function(done){ it('should merge properties', done => {
app1.use(function(ctx, next){ app1.use((ctx, next) => {
assert.equal(ctx.msg, 'hello'); assert.equal(ctx.msg, 'hello');
ctx.status = 204; ctx.status = 204;
}); });
@ -21,8 +21,8 @@ describe('app.context', function(){
.expect(204, done); .expect(204, done);
}); });
it('should not affect the original prototype', function(done){ it('should not affect the original prototype', done => {
app2.use(function(ctx, next){ app2.use((ctx, next) => {
assert.equal(ctx.msg, undefined); assert.equal(ctx.msg, undefined);
ctx.status = 204; ctx.status = 204;
}); });

View file

@ -5,35 +5,35 @@ const request = require('supertest');
const assert = require('assert'); const assert = require('assert');
const Koa = require('../..'); const Koa = require('../..');
describe('app', function(){ describe('app', () => {
it('should handle socket errors', function(done){ it('should handle socket errors', done => {
const app = new Koa(); const app = new Koa();
app.use(function(ctx, next){ app.use((ctx, next) => {
// triggers ctx.socket.writable == false // triggers ctx.socket.writable == false
ctx.socket.emit('error', new Error('boom')); ctx.socket.emit('error', new Error('boom'));
}); });
app.on('error', function(err){ app.on('error', err => {
err.message.should.equal('boom'); err.message.should.equal('boom');
done(); done();
}); });
request(app.listen()) request(app.listen())
.get('/') .get('/')
.end(function(){}); .end(() => {});
}); });
it('should not .writeHead when !socket.writable', function(done){ it('should not .writeHead when !socket.writable', done => {
const app = new Koa(); const app = new Koa();
app.use(function(ctx, next){ app.use((ctx, next) => {
// set .writable to false // set .writable to false
ctx.socket.writable = false; ctx.socket.writable = false;
ctx.status = 204; ctx.status = 204;
// throw if .writeHead or .end is called // throw if .writeHead or .end is called
ctx.res.writeHead = ctx.res.writeHead =
ctx.res.end = function(){ ctx.res.end = () => {
throw new Error('response sent'); throw new Error('response sent');
}; };
}); });
@ -43,10 +43,10 @@ describe('app', function(){
request(app.listen()) request(app.listen())
.get('/') .get('/')
.end(function(){}); .end(() => {});
}); });
it('should set development env when NODE_ENV missing', function(){ it('should set development env when NODE_ENV missing', () => {
const NODE_ENV = process.env.NODE_ENV; const NODE_ENV = process.env.NODE_ENV;
process.env.NODE_ENV = ''; process.env.NODE_ENV = '';
const app = new Koa(); const app = new Koa();

View file

@ -4,8 +4,8 @@
const assert = require('assert'); const assert = require('assert');
const Koa = require('../..'); const Koa = require('../..');
describe('app.inspect()', function(){ describe('app.inspect()', () => {
it('should work', function(){ it('should work', () => {
const app = new Koa(); const app = new Koa();
const util = require('util'); const util = require('util');
const str = util.inspect(app); const str = util.inspect(app);

View file

@ -5,8 +5,8 @@ const stderr = require('test-console').stderr;
const Koa = require('../..'); const Koa = require('../..');
const AssertionError = require('assert').AssertionError; const AssertionError = require('assert').AssertionError;
describe('app.onerror(err)', function(){ describe('app.onerror(err)', () => {
it('should throw an error if a non-error is given', function(done){ it('should throw an error if a non-error is given', done => {
const app = new Koa(); const app = new Koa();
(() => app.onerror('foo')).should.throw(AssertionError, {message: 'non-error thrown: foo'}); (() => app.onerror('foo')).should.throw(AssertionError, {message: 'non-error thrown: foo'});
@ -14,7 +14,7 @@ describe('app.onerror(err)', function(){
done(); done();
}); });
it('should do nothing if status is 404', function(done){ it('should do nothing if status is 404', done => {
const app = new Koa(); const app = new Koa();
const err = new Error(); const err = new Error();
@ -27,7 +27,7 @@ describe('app.onerror(err)', function(){
done(); done();
}); });
it('should do nothing if .silent', function(done){ it('should do nothing if .silent', done => {
const app = new Koa(); const app = new Koa();
app.silent = true; app.silent = true;
const err = new Error(); const err = new Error();
@ -39,7 +39,7 @@ describe('app.onerror(err)', function(){
done(); done();
}); });
it('should log the error to stderr', function(done){ it('should log the error to stderr', done => {
const app = new Koa(); const app = new Koa();
app.env = 'dev'; app.env = 'dev';
@ -53,7 +53,7 @@ describe('app.onerror(err)', function(){
done(); done();
}); });
it('should use err.toString() instad of err.stack', function(done){ it('should use err.toString() instad of err.stack', done => {
const app = new Koa(); const app = new Koa();
app.env = 'dev'; app.env = 'dev';

View file

@ -5,13 +5,13 @@ const request = require('supertest');
const assert = require('assert'); const assert = require('assert');
const Koa = require('../..'); const Koa = require('../..');
describe('app.request', function(){ describe('app.request', () => {
const app1 = new Koa(); const app1 = new Koa();
app1.request.message = 'hello'; app1.request.message = 'hello';
const app2 = new Koa(); const app2 = new Koa();
it('should merge properties', function(done){ it('should merge properties', done => {
app1.use(function(ctx, next){ app1.use((ctx, next) => {
assert.equal(ctx.request.message, 'hello'); assert.equal(ctx.request.message, 'hello');
ctx.status = 204; ctx.status = 204;
}); });
@ -21,8 +21,8 @@ describe('app.request', function(){
.expect(204, done); .expect(204, done);
}); });
it('should not affect the original prototype', function(done){ it('should not affect the original prototype', done => {
app2.use(function(ctx, next){ app2.use((ctx, next) => {
assert.equal(ctx.request.message, undefined); assert.equal(ctx.request.message, undefined);
ctx.status = 204; ctx.status = 204;
}); });

View file

@ -7,18 +7,18 @@ const assert = require('assert');
const Koa = require('../..'); const Koa = require('../..');
const fs = require('fs'); const fs = require('fs');
describe('app.respond', function(){ describe('app.respond', () => {
describe('when ctx.respond === false', function(){ describe('when ctx.respond === false', () => {
it('should function (ctx)', function(done){ it('should function (ctx)', done => {
const app = new Koa(); const app = new Koa();
app.use(function(ctx){ app.use(ctx => {
ctx.body = 'Hello'; ctx.body = 'Hello';
ctx.respond = false; ctx.respond = false;
const res = ctx.res; const res = ctx.res;
res.statusCode = 200; res.statusCode = 200;
setImmediate(function(){ setImmediate(() => {
res.setHeader('Content-Type', 'text/plain'); res.setHeader('Content-Type', 'text/plain');
res.end('lol'); res.end('lol');
}); });
@ -34,11 +34,11 @@ describe('app.respond', function(){
}); });
}); });
describe('when this.type === null', function(){ describe('when this.type === null', () => {
it('should not send Content-Type header', function(done){ it('should not send Content-Type header', done => {
const app = new Koa(); const app = new Koa();
app.use(function(ctx){ app.use(ctx => {
ctx.body = ''; ctx.body = '';
ctx.type = null; ctx.type = null;
}); });
@ -48,7 +48,7 @@ describe('app.respond', function(){
request(server) request(server)
.get('/') .get('/')
.expect(200) .expect(200)
.end(function(err, res){ .end((err, res) => {
if (err) return done(err); if (err) return done(err);
res.should.not.have.header('content-type'); res.should.not.have.header('content-type');
done(); done();
@ -56,11 +56,11 @@ describe('app.respond', function(){
}); });
}); });
describe('when HEAD is used', function(){ describe('when HEAD is used', () => {
it('should not respond with the body', function(done){ it('should not respond with the body', done => {
const app = new Koa(); const app = new Koa();
app.use(function(ctx){ app.use(ctx => {
ctx.body = 'Hello'; ctx.body = 'Hello';
}); });
@ -69,7 +69,7 @@ describe('app.respond', function(){
request(server) request(server)
.head('/') .head('/')
.expect(200) .expect(200)
.end(function(err, res){ .end((err, res) => {
if (err) return done(err); if (err) return done(err);
res.should.have.header('Content-Type', 'text/plain; charset=utf-8'); res.should.have.header('Content-Type', 'text/plain; charset=utf-8');
res.should.have.header('Content-Length', '5'); res.should.have.header('Content-Length', '5');
@ -78,10 +78,10 @@ describe('app.respond', function(){
}); });
}); });
it('should keep json headers', function(done){ it('should keep json headers', done => {
const app = new Koa(); const app = new Koa();
app.use(function(ctx){ app.use(ctx => {
ctx.body = { hello: 'world' }; ctx.body = { hello: 'world' };
}); });
@ -90,7 +90,7 @@ describe('app.respond', function(){
request(server) request(server)
.head('/') .head('/')
.expect(200) .expect(200)
.end(function(err, res){ .end((err, res) => {
if (err) return done(err); if (err) return done(err);
res.should.have.header('Content-Type', 'application/json; charset=utf-8'); res.should.have.header('Content-Type', 'application/json; charset=utf-8');
res.should.have.header('Content-Length', '17'); res.should.have.header('Content-Length', '17');
@ -99,10 +99,10 @@ describe('app.respond', function(){
}); });
}); });
it('should keep string headers', function(done){ it('should keep string headers', done => {
const app = new Koa(); const app = new Koa();
app.use(function(ctx){ app.use(ctx => {
ctx.body = 'hello world'; ctx.body = 'hello world';
}); });
@ -111,7 +111,7 @@ describe('app.respond', function(){
request(server) request(server)
.head('/') .head('/')
.expect(200) .expect(200)
.end(function(err, res){ .end((err, res) => {
if (err) return done(err); if (err) return done(err);
res.should.have.header('Content-Type', 'text/plain; charset=utf-8'); res.should.have.header('Content-Type', 'text/plain; charset=utf-8');
res.should.have.header('Content-Length', '11'); res.should.have.header('Content-Length', '11');
@ -120,10 +120,10 @@ describe('app.respond', function(){
}); });
}); });
it('should keep buffer headers', function(done){ it('should keep buffer headers', done => {
const app = new Koa(); const app = new Koa();
app.use(function(ctx){ app.use(ctx => {
ctx.body = new Buffer('hello world'); ctx.body = new Buffer('hello world');
}); });
@ -132,7 +132,7 @@ describe('app.respond', function(){
request(server) request(server)
.head('/') .head('/')
.expect(200) .expect(200)
.end(function(err, res){ .end((err, res) => {
if (err) return done(err); if (err) return done(err);
res.should.have.header('Content-Type', 'application/octet-stream'); res.should.have.header('Content-Type', 'application/octet-stream');
res.should.have.header('Content-Length', '11'); res.should.have.header('Content-Length', '11');
@ -141,10 +141,10 @@ describe('app.respond', function(){
}); });
}); });
it('should respond with a 404 if no body was set', function(done){ it('should respond with a 404 if no body was set', done => {
const app = new Koa(); const app = new Koa();
app.use(function(ctx){ app.use(ctx => {
}); });
@ -155,10 +155,10 @@ describe('app.respond', function(){
.expect(404, done); .expect(404, done);
}); });
it('should respond with a 200 if body = ""', function(done){ it('should respond with a 200 if body = ""', done => {
const app = new Koa(); const app = new Koa();
app.use(function(ctx){ app.use(ctx => {
ctx.body = ''; ctx.body = '';
}); });
@ -169,10 +169,10 @@ describe('app.respond', function(){
.expect(200, done); .expect(200, done);
}); });
it('should not overwrite the content-type', function(done){ it('should not overwrite the content-type', done => {
const app = new Koa(); const app = new Koa();
app.use(function(ctx){ app.use(ctx => {
ctx.status = 200; ctx.status = 200;
ctx.type = 'application/javascript'; ctx.type = 'application/javascript';
}); });
@ -186,8 +186,8 @@ describe('app.respond', function(){
}); });
}); });
describe('when no middleware are present', function(){ describe('when no middleware are present', () => {
it('should 404', function(done){ it('should 404', done => {
const app = new Koa(); const app = new Koa();
const server = app.listen(); const server = app.listen();
@ -198,11 +198,11 @@ describe('app.respond', function(){
}); });
}); });
describe('when res has already been written to', function(){ describe('when res has already been written to', () => {
it('should not cause an app error', function(done){ it('should not cause an app error', done => {
const app = new Koa(); const app = new Koa();
app.use(function(ctx, next){ app.use((ctx, next) => {
const res = ctx.res; const res = ctx.res;
ctx.status = 200; ctx.status = 200;
res.setHeader('Content-Type', 'text/html'); res.setHeader('Content-Type', 'text/html');
@ -219,24 +219,22 @@ describe('app.respond', function(){
request(server) request(server)
.get('/') .get('/')
.expect(200) .expect(200)
.end(function(err, res){ .end((err, res) => {
if (err) return done(err); if (err) return done(err);
if (errorCaught) return done(errorCaught); if (errorCaught) return done(errorCaught);
done(); done();
}); });
}); });
it('should send the right body', function(done){ it('should send the right body', done => {
const app = new Koa(); const app = new Koa();
app.use(function(ctx, next){ app.use((ctx, next) => {
const res = ctx.res; const res = ctx.res;
ctx.status = 200; ctx.status = 200;
res.setHeader('Content-Type', 'text/html'); res.setHeader('Content-Type', 'text/html');
res.write('Hello'); res.write('Hello');
setTimeout(function(){ setTimeout(() => res.end('Goodbye'), 0);
res.end('Goodbye');
}, 0);
}); });
const server = app.listen(); const server = app.listen();
@ -248,12 +246,12 @@ describe('app.respond', function(){
}); });
}); });
describe('when .body is missing', function(){ describe('when .body is missing', () => {
describe('with status=400', function(){ describe('with status=400', () => {
it('should respond with the associated status message', function(done){ it('should respond with the associated status message', done => {
const app = new Koa(); const app = new Koa();
app.use(function(ctx){ app.use(ctx => {
ctx.status = 400; ctx.status = 400;
}); });
@ -267,11 +265,11 @@ describe('app.respond', function(){
}); });
}); });
describe('with status=204', function(){ describe('with status=204', () => {
it('should respond without a body', function(done){ it('should respond without a body', done => {
const app = new Koa(); const app = new Koa();
app.use(function(ctx){ app.use(ctx => {
ctx.status = 204; ctx.status = 204;
}); });
@ -281,7 +279,7 @@ describe('app.respond', function(){
.get('/') .get('/')
.expect(204) .expect(204)
.expect('') .expect('')
.end(function(err, res){ .end((err, res) => {
if (err) return done(err); if (err) return done(err);
res.header.should.not.have.property('content-type'); res.header.should.not.have.property('content-type');
@ -290,11 +288,11 @@ describe('app.respond', function(){
}); });
}); });
describe('with status=205', function(){ describe('with status=205', () => {
it('should respond without a body', function(done){ it('should respond without a body', done => {
const app = new Koa(); const app = new Koa();
app.use(function(ctx){ app.use(ctx => {
ctx.status = 205; ctx.status = 205;
}); });
@ -304,7 +302,7 @@ describe('app.respond', function(){
.get('/') .get('/')
.expect(205) .expect(205)
.expect('') .expect('')
.end(function(err, res){ .end((err, res) => {
if (err) return done(err); if (err) return done(err);
res.header.should.not.have.property('content-type'); res.header.should.not.have.property('content-type');
@ -313,11 +311,11 @@ describe('app.respond', function(){
}); });
}); });
describe('with status=304', function(){ describe('with status=304', () => {
it('should respond without a body', function(done){ it('should respond without a body', done => {
const app = new Koa(); const app = new Koa();
app.use(function(ctx){ app.use(ctx => {
ctx.status = 304; ctx.status = 304;
}); });
@ -327,7 +325,7 @@ describe('app.respond', function(){
.get('/') .get('/')
.expect(304) .expect(304)
.expect('') .expect('')
.end(function(err, res){ .end((err, res) => {
if (err) return done(err); if (err) return done(err);
res.header.should.not.have.property('content-type'); res.header.should.not.have.property('content-type');
@ -336,12 +334,12 @@ describe('app.respond', function(){
}); });
}); });
describe('with custom status=700', function(){ describe('with custom status=700', () => {
it('should respond with the associated status message', function(done){ it('should respond with the associated status message', done => {
const app = new Koa(); const app = new Koa();
statuses['700'] = 'custom status'; statuses['700'] = 'custom status';
app.use(function(ctx){ app.use(ctx => {
ctx.status = 700; ctx.status = 700;
}); });
@ -351,7 +349,7 @@ describe('app.respond', function(){
.get('/') .get('/')
.expect(700) .expect(700)
.expect('custom status') .expect('custom status')
.end(function(err, res){ .end((err, res) => {
if (err) return done(err); if (err) return done(err);
res.res.statusMessage.should.equal('custom status'); res.res.statusMessage.should.equal('custom status');
done(); done();
@ -359,11 +357,11 @@ describe('app.respond', function(){
}); });
}); });
describe('with custom statusMessage=ok', function(){ describe('with custom statusMessage=ok', () => {
it('should respond with the custom status message', function(done){ it('should respond with the custom status message', done => {
const app = new Koa(); const app = new Koa();
app.use(function(ctx){ app.use(ctx => {
ctx.status = 200; ctx.status = 200;
ctx.message = 'ok'; ctx.message = 'ok';
}); });
@ -374,7 +372,7 @@ describe('app.respond', function(){
.get('/') .get('/')
.expect(200) .expect(200)
.expect('ok') .expect('ok')
.end(function(err, res){ .end((err, res) => {
if (err) return done(err); if (err) return done(err);
res.res.statusMessage.should.equal('ok'); res.res.statusMessage.should.equal('ok');
done(); done();
@ -382,11 +380,11 @@ describe('app.respond', function(){
}); });
}); });
describe('with custom status without message', function(){ describe('with custom status without message', () => {
it('should respond with the status code number', function(done){ it('should respond with the status code number', done => {
const app = new Koa(); const app = new Koa();
app.use(function(ctx){ app.use(ctx => {
ctx.res.statusCode = 701; ctx.res.statusCode = 701;
}); });
@ -400,11 +398,11 @@ describe('app.respond', function(){
}); });
}); });
describe('when .body is a null', function(){ describe('when .body is a null', () => {
it('should respond 204 by default', function(done){ it('should respond 204 by default', done => {
const app = new Koa(); const app = new Koa();
app.use(function(ctx){ app.use(ctx => {
ctx.body = null; ctx.body = null;
}); });
@ -414,7 +412,7 @@ describe('app.respond', function(){
.get('/') .get('/')
.expect(204) .expect(204)
.expect('') .expect('')
.end(function(err, res){ .end((err, res) => {
if (err) return done(err); if (err) return done(err);
res.header.should.not.have.property('content-type'); res.header.should.not.have.property('content-type');
@ -422,10 +420,10 @@ describe('app.respond', function(){
}); });
}); });
it('should respond 204 with status=200', function(done){ it('should respond 204 with status=200', done => {
const app = new Koa(); const app = new Koa();
app.use(function(ctx){ app.use(ctx => {
ctx.status = 200; ctx.status = 200;
ctx.body = null; ctx.body = null;
}); });
@ -436,7 +434,7 @@ describe('app.respond', function(){
.get('/') .get('/')
.expect(204) .expect(204)
.expect('') .expect('')
.end(function(err, res){ .end((err, res) => {
if (err) return done(err); if (err) return done(err);
res.header.should.not.have.property('content-type'); res.header.should.not.have.property('content-type');
@ -444,10 +442,10 @@ describe('app.respond', function(){
}); });
}); });
it('should respond 205 with status=205', function(done){ it('should respond 205 with status=205', done => {
const app = new Koa(); const app = new Koa();
app.use(function(ctx){ app.use(ctx => {
ctx.status = 205; ctx.status = 205;
ctx.body = null; ctx.body = null;
}); });
@ -458,7 +456,7 @@ describe('app.respond', function(){
.get('/') .get('/')
.expect(205) .expect(205)
.expect('') .expect('')
.end(function(err, res){ .end((err, res) => {
if (err) return done(err); if (err) return done(err);
res.header.should.not.have.property('content-type'); res.header.should.not.have.property('content-type');
@ -466,10 +464,10 @@ describe('app.respond', function(){
}); });
}); });
it('should respond 304 with status=304', function(done){ it('should respond 304 with status=304', done => {
const app = new Koa(); const app = new Koa();
app.use(function(ctx){ app.use(ctx => {
ctx.status = 304; ctx.status = 304;
ctx.body = null; ctx.body = null;
}); });
@ -480,7 +478,7 @@ describe('app.respond', function(){
.get('/') .get('/')
.expect(304) .expect(304)
.expect('') .expect('')
.end(function(err, res){ .end((err, res) => {
if (err) return done(err); if (err) return done(err);
res.header.should.not.have.property('content-type'); res.header.should.not.have.property('content-type');
@ -489,11 +487,11 @@ describe('app.respond', function(){
}); });
}); });
describe('when .body is a string', function(){ describe('when .body is a string', () => {
it('should respond', function(done){ it('should respond', done => {
const app = new Koa(); const app = new Koa();
app.use(function(ctx){ app.use(ctx => {
ctx.body = 'Hello'; ctx.body = 'Hello';
}); });
@ -505,11 +503,11 @@ describe('app.respond', function(){
}); });
}); });
describe('when .body is a Buffer', function(){ describe('when .body is a Buffer', () => {
it('should respond', function(done){ it('should respond', done => {
const app = new Koa(); const app = new Koa();
app.use(function(ctx){ app.use(ctx => {
ctx.body = new Buffer('Hello'); ctx.body = new Buffer('Hello');
}); });
@ -521,11 +519,11 @@ describe('app.respond', function(){
}); });
}); });
describe('when .body is a Stream', function(){ describe('when .body is a Stream', () => {
it('should respond', function(done){ it('should respond', done => {
const app = new Koa(); const app = new Koa();
app.use(function(ctx){ app.use(ctx => {
ctx.body = fs.createReadStream('package.json'); ctx.body = fs.createReadStream('package.json');
ctx.set('Content-Type', 'application/json; charset=utf-8'); ctx.set('Content-Type', 'application/json; charset=utf-8');
}); });
@ -535,7 +533,7 @@ describe('app.respond', function(){
request(server) request(server)
.get('/') .get('/')
.expect('Content-Type', 'application/json; charset=utf-8') .expect('Content-Type', 'application/json; charset=utf-8')
.end(function(err, res){ .end((err, res) => {
if (err) return done(err); if (err) return done(err);
const pkg = require('../../package'); const pkg = require('../../package');
res.should.not.have.header('Content-Length'); res.should.not.have.header('Content-Length');
@ -544,10 +542,10 @@ describe('app.respond', function(){
}); });
}); });
it('should strip content-length when overwriting', function(done){ it('should strip content-length when overwriting', done => {
const app = new Koa(); const app = new Koa();
app.use(function(ctx){ app.use(ctx => {
ctx.body = 'hello'; ctx.body = 'hello';
ctx.body = fs.createReadStream('package.json'); ctx.body = fs.createReadStream('package.json');
ctx.set('Content-Type', 'application/json; charset=utf-8'); ctx.set('Content-Type', 'application/json; charset=utf-8');
@ -558,7 +556,7 @@ describe('app.respond', function(){
request(server) request(server)
.get('/') .get('/')
.expect('Content-Type', 'application/json; charset=utf-8') .expect('Content-Type', 'application/json; charset=utf-8')
.end(function(err, res){ .end((err, res) => {
if (err) return done(err); if (err) return done(err);
const pkg = require('../../package'); const pkg = require('../../package');
res.should.not.have.header('Content-Length'); res.should.not.have.header('Content-Length');
@ -567,10 +565,10 @@ describe('app.respond', function(){
}); });
}); });
it('should keep content-length if not overwritten', function(done){ it('should keep content-length if not overwritten', done => {
const app = new Koa(); const app = new Koa();
app.use(function(ctx){ app.use(ctx => {
ctx.length = fs.readFileSync('package.json').length; ctx.length = fs.readFileSync('package.json').length;
ctx.body = fs.createReadStream('package.json'); ctx.body = fs.createReadStream('package.json');
ctx.set('Content-Type', 'application/json; charset=utf-8'); ctx.set('Content-Type', 'application/json; charset=utf-8');
@ -581,7 +579,7 @@ describe('app.respond', function(){
request(server) request(server)
.get('/') .get('/')
.expect('Content-Type', 'application/json; charset=utf-8') .expect('Content-Type', 'application/json; charset=utf-8')
.end(function(err, res){ .end((err, res) => {
if (err) return done(err); if (err) return done(err);
const pkg = require('../../package'); const pkg = require('../../package');
res.should.have.header('Content-Length'); res.should.have.header('Content-Length');
@ -594,7 +592,7 @@ describe('app.respond', function(){
done => { done => {
const app = new Koa(); const app = new Koa();
app.use(function(ctx){ app.use(ctx => {
ctx.length = fs.readFileSync('package.json').length; ctx.length = fs.readFileSync('package.json').length;
const stream = fs.createReadStream('package.json'); const stream = fs.createReadStream('package.json');
ctx.body = stream; ctx.body = stream;
@ -607,7 +605,7 @@ describe('app.respond', function(){
request(server) request(server)
.get('/') .get('/')
.expect('Content-Type', 'application/json; charset=utf-8') .expect('Content-Type', 'application/json; charset=utf-8')
.end(function(err, res){ .end((err, res) => {
if (err) return done(err); if (err) return done(err);
const pkg = require('../../package'); const pkg = require('../../package');
res.should.have.header('Content-Length'); res.should.have.header('Content-Length');
@ -616,10 +614,10 @@ describe('app.respond', function(){
}); });
}); });
it('should handle errors', function(done){ it('should handle errors', done => {
const app = new Koa(); const app = new Koa();
app.use(function(ctx){ app.use(ctx => {
ctx.set('Content-Type', 'application/json; charset=utf-8'); ctx.set('Content-Type', 'application/json; charset=utf-8');
ctx.body = fs.createReadStream('does not exist'); ctx.body = fs.createReadStream('does not exist');
}); });
@ -633,10 +631,10 @@ describe('app.respond', function(){
.end(done); .end(done);
}); });
it('should handle errors when no content status', function(done){ it('should handle errors when no content status', done => {
const app = new Koa(); const app = new Koa();
app.use(function(ctx){ app.use(ctx => {
ctx.status = 204; ctx.status = 204;
ctx.body = fs.createReadStream('does not exist'); ctx.body = fs.createReadStream('does not exist');
}); });
@ -648,10 +646,10 @@ describe('app.respond', function(){
.expect(204, done); .expect(204, done);
}); });
it('should handle all intermediate stream body errors', function(done){ it('should handle all intermediate stream body errors', done => {
const app = new Koa(); const app = new Koa();
app.use(function(ctx){ app.use(ctx => {
ctx.body = fs.createReadStream('does not exist'); ctx.body = fs.createReadStream('does not exist');
ctx.body = fs.createReadStream('does not exist'); ctx.body = fs.createReadStream('does not exist');
ctx.body = fs.createReadStream('does not exist'); ctx.body = fs.createReadStream('does not exist');
@ -665,11 +663,11 @@ describe('app.respond', function(){
}); });
}); });
describe('when .body is an Object', function(){ describe('when .body is an Object', () => {
it('should respond with json', function(done){ it('should respond with json', done => {
const app = new Koa(); const app = new Koa();
app.use(function(ctx){ app.use(ctx => {
ctx.body = { hello: 'world' }; ctx.body = { hello: 'world' };
}); });
@ -682,29 +680,29 @@ describe('app.respond', function(){
}); });
}); });
describe('when an error occurs', function(){ describe('when an error occurs', () => {
it('should emit "error" on the app', function(done){ it('should emit "error" on the app', done => {
const app = new Koa(); const app = new Koa();
app.use(function(ctx){ app.use(ctx => {
throw new Error('boom'); throw new Error('boom');
}); });
app.on('error', function(err){ app.on('error', err => {
err.message.should.equal('boom'); err.message.should.equal('boom');
done(); done();
}); });
request(app.listen()) request(app.listen())
.get('/') .get('/')
.end(function(){}); .end(() => {});
}); });
describe('with an .expose property', function(){ describe('with an .expose property', () => {
it('should expose the message', function(done){ it('should expose the message', done => {
const app = new Koa(); const app = new Koa();
app.use(function(ctx){ app.use(ctx => {
const err = new Error('sorry!'); const err = new Error('sorry!');
err.status = 403; err.status = 403;
err.expose = true; err.expose = true;
@ -718,11 +716,11 @@ describe('app.respond', function(){
}); });
}); });
describe('with a .status property', function(){ describe('with a .status property', () => {
it('should respond with .status', function(done){ it('should respond with .status', done => {
const app = new Koa(); const app = new Koa();
app.use(function(ctx){ app.use(ctx => {
const err = new Error('s3 explodes'); const err = new Error('s3 explodes');
err.status = 403; err.status = 403;
throw err; throw err;
@ -735,10 +733,10 @@ describe('app.respond', function(){
}); });
}); });
it('should respond with 500', function(done){ it('should respond with 500', done => {
const app = new Koa(); const app = new Koa();
app.use(function(ctx){ app.use(ctx => {
throw new Error('boom!'); throw new Error('boom!');
}); });
@ -750,18 +748,18 @@ describe('app.respond', function(){
.end(done); .end(done);
}); });
it('should be catchable', function(done){ it('should be catchable', done => {
const app = new Koa(); const app = new Koa();
app.use(function(ctx, next){ app.use((ctx, next) => {
return next().then(function(){ return next().then(() => {
ctx.body = 'Hello'; ctx.body = 'Hello';
}).catch(function(){ }).catch(() => {
ctx.body = 'Got error'; ctx.body = 'Got error';
}); });
}); });
app.use(function(ctx, next){ app.use((ctx, next) => {
throw new Error('boom!'); throw new Error('boom!');
}); });
@ -774,11 +772,11 @@ describe('app.respond', function(){
}); });
}); });
describe('when status and body property', function(){ describe('when status and body property', () => {
it('should 200', function(done){ it('should 200', done => {
const app = new Koa(); const app = new Koa();
app.use(function(ctx){ app.use(ctx => {
ctx.status = 304; ctx.status = 304;
ctx.body = 'hello'; ctx.body = 'hello';
ctx.status = 200; ctx.status = 200;
@ -792,10 +790,10 @@ describe('app.respond', function(){
.expect('hello', done); .expect('hello', done);
}); });
it('should 204', function(done){ it('should 204', done => {
const app = new Koa(); const app = new Koa();
app.use(function(ctx){ app.use(ctx => {
ctx.status = 200; ctx.status = 200;
ctx.body = 'hello'; ctx.body = 'hello';
ctx.set('content-type', 'text/plain; charset=utf8'); ctx.set('content-type', 'text/plain; charset=utf8');
@ -807,7 +805,7 @@ describe('app.respond', function(){
request(server) request(server)
.get('/') .get('/')
.expect(204) .expect(204)
.end(function(err, res){ .end((err, res) => {
res.should.not.have.header('content-type'); res.should.not.have.header('content-type');
done(err); done(err);
}); });

View file

@ -5,13 +5,13 @@ const request = require('supertest');
const assert = require('assert'); const assert = require('assert');
const Koa = require('../..'); const Koa = require('../..');
describe('app.response', function(){ describe('app.response', () => {
const app1 = new Koa(); const app1 = new Koa();
app1.response.msg = 'hello'; app1.response.msg = 'hello';
const app2 = new Koa(); const app2 = new Koa();
it('should merge properties', function(done){ it('should merge properties', done => {
app1.use(function(ctx, next){ app1.use((ctx, next) => {
assert.equal(ctx.response.msg, 'hello'); assert.equal(ctx.response.msg, 'hello');
ctx.status = 204; ctx.status = 204;
}); });
@ -21,8 +21,8 @@ describe('app.response', function(){
.expect(204, done); .expect(204, done);
}); });
it('should not affect the original prototype', function(done){ it('should not affect the original prototype', done => {
app2.use(function(ctx, next){ app2.use((ctx, next) => {
assert.equal(ctx.response.msg, undefined); assert.equal(ctx.response.msg, undefined);
ctx.status = 204; ctx.status = 204;
}); });

View file

@ -3,8 +3,8 @@
const Koa = require('../..'); const Koa = require('../..');
describe('app.toJSON()', function(){ describe('app.toJSON()', () => {
it('should work', function(){ it('should work', () => {
const app = new Koa(); const app = new Koa();
const obj = app.toJSON(); const obj = app.toJSON();

View file

@ -4,28 +4,28 @@
const request = require('supertest'); const request = require('supertest');
const Koa = require('../..'); const Koa = require('../..');
describe('app.use(fn)', function(){ describe('app.use(fn)', () => {
it('should compose middleware', function(done){ it('should compose middleware', done => {
const app = new Koa(); const app = new Koa();
const calls = []; const calls = [];
app.use(function(ctx, next){ app.use((ctx, next) => {
calls.push(1); calls.push(1);
return next().then(function(){ return next().then(() => {
calls.push(6); calls.push(6);
}); });
}); });
app.use(function(ctx, next){ app.use((ctx, next) => {
calls.push(2); calls.push(2);
return next().then(function(){ return next().then(() => {
calls.push(5); calls.push(5);
}); });
}); });
app.use(function(ctx, next){ app.use((ctx, next) => {
calls.push(3); calls.push(3);
return next().then(function(){ return next().then(() => {
calls.push(4); calls.push(4);
}); });
}); });
@ -35,7 +35,7 @@ describe('app.use(fn)', function(){
request(server) request(server)
.get('/') .get('/')
.expect(404) .expect(404)
.end(function(err){ .end(err => {
if (err) return done(err); if (err) return done(err);
calls.should.eql([1, 2, 3, 4, 5, 6]); calls.should.eql([1, 2, 3, 4, 5, 6]);
done(); done();
@ -43,12 +43,10 @@ describe('app.use(fn)', function(){
}); });
// https://github.com/koajs/koa/pull/530#issuecomment-148138051 // https://github.com/koajs/koa/pull/530#issuecomment-148138051
it('should catch thrown errors in non-async functions', function(done){ it('should catch thrown errors in non-async functions', done => {
const app = new Koa(); const app = new Koa();
app.use(ctx => { app.use(ctx => ctx.throw('Not Found', 404));
ctx.throw('Not Found', 404);
});
request(app.listen()) request(app.listen())
.get('/') .get('/')
@ -56,16 +54,23 @@ describe('app.use(fn)', function(){
.end(done); .end(done);
}); });
it('should throw error for non function', function(done){ it('should throw error for non function', done => {
const app = new Koa(); const app = new Koa();
(() => app.use('not a function')).should.throw('middleware must be a function!'); (() => app.use('not a function')).should.throw('middleware must be a function!');
done(); done();
}); });
it('should throw error for generator', function(){ it('should throw error for generator', () => {
const app = new Koa(); const app = new Koa();
(() => app.use(function *(){})).should.throw(/.+/); (() => app.use(function *(){})).should.throw(/.+/);
}); });
it('should throw error for non function', done => {
const app = new Koa();
(() => app.use('not a function')).should.throw('middleware must be a function!');
done();
});
}); });

View file

@ -4,8 +4,8 @@
const context = require('../helpers/context'); const context = require('../helpers/context');
const assert = require('assert'); const assert = require('assert');
describe('ctx.assert(value, status)', function(){ describe('ctx.assert(value, status)', () => {
it('should throw an error', function(){ it('should throw an error', () => {
const ctx = context(); const ctx = context();
try { try {

View file

@ -4,11 +4,11 @@
const request = require('supertest'); const request = require('supertest');
const Koa = require('../..'); const Koa = require('../..');
describe('ctx.cookies.set()', function(){ describe('ctx.cookies.set()', () => {
it('should set an unsigned cookie', function(done){ it('should set an unsigned cookie', done => {
const app = new Koa(); const app = new Koa();
app.use(function(ctx, next){ app.use((ctx, next) => {
ctx.cookies.set('name', 'jon'); ctx.cookies.set('name', 'jon');
ctx.status = 204; ctx.status = 204;
}); });
@ -18,7 +18,7 @@ describe('ctx.cookies.set()', function(){
request(server) request(server)
.get('/') .get('/')
.expect(204) .expect(204)
.end(function(err, res){ .end((err, res) => {
if (err) return done(err); if (err) return done(err);
res.headers['set-cookie'].some(cookie => /^name=/.test(cookie)).should.be.ok; res.headers['set-cookie'].some(cookie => /^name=/.test(cookie)).should.be.ok;
@ -27,12 +27,12 @@ describe('ctx.cookies.set()', function(){
}); });
}); });
describe('with .signed', function(){ describe('with .signed', () => {
describe('when no .keys are set', function(){ describe('when no .keys are set', () => {
it('should error', function(done){ it('should error', done => {
const app = new Koa(); const app = new Koa();
app.use(function(ctx, next){ app.use((ctx, next) => {
try { try {
ctx.cookies.set('foo', 'bar', { signed: true }); ctx.cookies.set('foo', 'bar', { signed: true });
} catch (err) { } catch (err) {
@ -46,12 +46,12 @@ describe('ctx.cookies.set()', function(){
}); });
}); });
it('should send a signed cookie', function(done){ it('should send a signed cookie', done => {
const app = new Koa(); const app = new Koa();
app.keys = ['a', 'b']; app.keys = ['a', 'b'];
app.use(function(ctx, next){ app.use((ctx, next) => {
ctx.cookies.set('name', 'jon', { signed: true }); ctx.cookies.set('name', 'jon', { signed: true });
ctx.status = 204; ctx.status = 204;
}); });
@ -61,7 +61,7 @@ describe('ctx.cookies.set()', function(){
request(server) request(server)
.get('/') .get('/')
.expect(204) .expect(204)
.end(function(err, res){ .end((err, res) => {
if (err) return done(err); if (err) return done(err);
const cookies = res.headers['set-cookie']; const cookies = res.headers['set-cookie'];

View file

@ -3,8 +3,8 @@
const context = require('../helpers/context'); const context = require('../helpers/context');
describe('ctx.inspect()', function(){ describe('ctx.inspect()', () => {
it('should return a json representation', function(){ it('should return a json representation', () => {
const ctx = context(); const ctx = context();
const toJSON = ctx.toJSON(ctx); const toJSON = ctx.toJSON(ctx);

View file

@ -4,11 +4,11 @@
const request = require('supertest'); const request = require('supertest');
const Koa = require('../..'); const Koa = require('../..');
describe('ctx.onerror(err)', function(){ describe('ctx.onerror(err)', () => {
it('should respond', function(done){ it('should respond', done => {
const app = new Koa(); const app = new Koa();
app.use(function(ctx, next){ app.use((ctx, next) => {
ctx.body = 'something else'; ctx.body = 'something else';
ctx.throw(418, 'boom'); ctx.throw(418, 'boom');
@ -24,10 +24,10 @@ describe('ctx.onerror(err)', function(){
.end(done); .end(done);
}); });
it('should unset all headers', function(done){ it('should unset all headers', done => {
const app = new Koa(); const app = new Koa();
app.use(function(ctx, next){ app.use((ctx, next) => {
ctx.set('Vary', 'Accept-Encoding'); ctx.set('Vary', 'Accept-Encoding');
ctx.set('X-CSRF-Token', 'asdf'); ctx.set('X-CSRF-Token', 'asdf');
ctx.body = 'response'; ctx.body = 'response';
@ -42,7 +42,7 @@ describe('ctx.onerror(err)', function(){
.expect(418) .expect(418)
.expect('Content-Type', 'text/plain; charset=utf-8') .expect('Content-Type', 'text/plain; charset=utf-8')
.expect('Content-Length', '4') .expect('Content-Length', '4')
.end(function(err, res){ .end((err, res) => {
if (err) return done(err); if (err) return done(err);
res.headers.should.not.have.property('vary'); res.headers.should.not.have.property('vary');
@ -52,12 +52,12 @@ describe('ctx.onerror(err)', function(){
}); });
}); });
describe('when invalid err.status', function(){ describe('when invalid err.status', () => {
describe('not number', function(){ describe('not number', () => {
it('should respond 500', function(done){ it('should respond 500', done => {
const app = new Koa(); const app = new Koa();
app.use(function(ctx, next){ app.use((ctx, next) => {
ctx.body = 'something else'; ctx.body = 'something else';
const err = new Error('some error'); const err = new Error('some error');
err.status = 'notnumber'; err.status = 'notnumber';
@ -74,11 +74,11 @@ describe('ctx.onerror(err)', function(){
}); });
}); });
describe('not http status code', function(){ describe('not http status code', () => {
it('should respond 500', function(done){ it('should respond 500', done => {
const app = new Koa(); const app = new Koa();
app.use(function(ctx, next){ app.use((ctx, next) => {
ctx.body = 'something else'; ctx.body = 'something else';
const err = new Error('some error'); const err = new Error('some error');
err.status = 9999; err.status = 9999;
@ -96,11 +96,11 @@ describe('ctx.onerror(err)', function(){
}); });
}); });
describe('when non-error thrown', function(){ describe('when non-error thrown', () => {
it('should response non-error thrown message', function(done){ it('should response non-error thrown message', done => {
const app = new Koa(); const app = new Koa();
app.use(function(ctx, next){ app.use((ctx, next) => {
throw 'string error'; // eslint-disable-line no-throw-literal throw 'string error'; // eslint-disable-line no-throw-literal
}); });

View file

@ -5,11 +5,11 @@ const request = require('supertest');
const assert = require('assert'); const assert = require('assert');
const Koa = require('../..'); const Koa = require('../..');
describe('ctx.state', function(){ describe('ctx.state', () => {
it('should provide a ctx.state namespace', function(done){ it('should provide a ctx.state namespace', done => {
const app = new Koa(); const app = new Koa();
app.use(function(ctx){ app.use(ctx => {
assert.deepEqual(ctx.state, {}); assert.deepEqual(ctx.state, {});
}); });

View file

@ -4,8 +4,8 @@
const context = require('../helpers/context'); const context = require('../helpers/context');
const assert = require('assert'); const assert = require('assert');
describe('ctx.throw(msg)', function(){ describe('ctx.throw(msg)', () => {
it('should set .status to 500', function(done){ it('should set .status to 500', done => {
const ctx = context(); const ctx = context();
try { try {
@ -18,8 +18,8 @@ describe('ctx.throw(msg)', function(){
}); });
}); });
describe('ctx.throw(err)', function(){ describe('ctx.throw(err)', () => {
it('should set .status to 500', function(done){ it('should set .status to 500', done => {
const ctx = context(); const ctx = context();
const err = new Error('test'); const err = new Error('test');
@ -34,8 +34,8 @@ describe('ctx.throw(err)', function(){
}); });
}); });
describe('ctx.throw(err, status)', function(){ describe('ctx.throw(err, status)', () => {
it('should throw the error and set .status', function(done){ it('should throw the error and set .status', done => {
const ctx = context(); const ctx = context();
const error = new Error('test'); const error = new Error('test');
@ -50,8 +50,8 @@ describe('ctx.throw(err, status)', function(){
}); });
}); });
describe('ctx.throw(status, err)', function(){ describe('ctx.throw(status, err)', () => {
it('should throw the error and set .status', function(done){ it('should throw the error and set .status', done => {
const ctx = context(); const ctx = context();
const error = new Error('test'); const error = new Error('test');
@ -66,8 +66,8 @@ describe('ctx.throw(status, err)', function(){
}); });
}); });
describe('ctx.throw(msg, status)', function(){ describe('ctx.throw(msg, status)', () => {
it('should throw an error', function(done){ it('should throw an error', done => {
const ctx = context(); const ctx = context();
try { try {
@ -81,8 +81,8 @@ describe('ctx.throw(msg, status)', function(){
}); });
}); });
describe('ctx.throw(status, msg)', function(){ describe('ctx.throw(status, msg)', () => {
it('should throw an error', function(done){ it('should throw an error', done => {
const ctx = context(); const ctx = context();
try { try {
@ -96,8 +96,8 @@ describe('ctx.throw(status, msg)', function(){
}); });
}); });
describe('ctx.throw(status)', function(){ describe('ctx.throw(status)', () => {
it('should throw an error', function(done){ it('should throw an error', done => {
const ctx = context(); const ctx = context();
try { try {
@ -110,8 +110,8 @@ describe('ctx.throw(status)', function(){
} }
}); });
describe('when not valid status', function(){ describe('when not valid status', () => {
it('should not expose', function(done){ it('should not expose', done => {
const ctx = context(); const ctx = context();
try { try {
@ -127,8 +127,8 @@ describe('ctx.throw(status)', function(){
}); });
}); });
describe('ctx.throw(status, msg, props)', function(){ describe('ctx.throw(status, msg, props)', () => {
it('should mixin props', function(done){ it('should mixin props', done => {
const ctx = context(); const ctx = context();
try { try {
@ -142,8 +142,8 @@ describe('ctx.throw(status, msg, props)', function(){
} }
}); });
describe('when props include status', function(){ describe('when props include status', () => {
it('should be ignored', function(done){ it('should be ignored', done => {
const ctx = context(); const ctx = context();
try { try {
@ -162,8 +162,8 @@ describe('ctx.throw(status, msg, props)', function(){
}); });
}); });
describe('ctx.throw(msg, props)', function(){ describe('ctx.throw(msg, props)', () => {
it('should mixin props', function(done){ it('should mixin props', done => {
const ctx = context(); const ctx = context();
try { try {
@ -178,8 +178,8 @@ describe('ctx.throw(msg, props)', function(){
}); });
}); });
describe('ctx.throw(status, props)', function(){ describe('ctx.throw(status, props)', () => {
it('should mixin props', function(done){ it('should mixin props', done => {
const ctx = context(); const ctx = context();
try { try {
@ -194,8 +194,8 @@ describe('ctx.throw(status, props)', function(){
}); });
}); });
describe('ctx.throw(err, props)', function(){ describe('ctx.throw(err, props)', () => {
it('should mixin props', function(done){ it('should mixin props', done => {
const ctx = context(); const ctx = context();
try { try {

View file

@ -3,8 +3,8 @@
const context = require('../helpers/context'); const context = require('../helpers/context');
describe('ctx.toJSON()', function(){ describe('ctx.toJSON()', () => {
it('should return a json representation', function(){ it('should return a json representation', () => {
const ctx = context(); const ctx = context();
ctx.req.method = 'POST'; ctx.req.method = 'POST';

View file

@ -4,7 +4,7 @@
const Stream = require('stream'); const Stream = require('stream');
const Koa = require('../..'); const Koa = require('../..');
module.exports = function(req, res){ module.exports = (req, res) => {
const socket = new Stream.Duplex(); const socket = new Stream.Duplex();
req = req || { headers: {}, socket: socket, __proto__: Stream.Readable.prototype }; req = req || { headers: {}, socket: socket, __proto__: Stream.Readable.prototype };
res = res || { _headers: {}, socket: socket, __proto__: Stream.Writable.prototype }; res = res || { _headers: {}, socket: socket, __proto__: Stream.Writable.prototype };

View file

@ -3,10 +3,10 @@
const context = require('../helpers/context'); const context = require('../helpers/context');
describe('ctx.accepts(types)', function(){ describe('ctx.accepts(types)', () => {
describe('with no arguments', function(){ describe('with no arguments', () => {
describe('when Accept is populated', function(){ describe('when Accept is populated', () => {
it('should return all accepted types', function(){ it('should return all accepted types', () => {
const ctx = context(); const ctx = context();
ctx.req.headers.accept = 'application/*;q=0.2, image/jpeg;q=0.8, text/html, text/plain'; ctx.req.headers.accept = 'application/*;q=0.2, image/jpeg;q=0.8, text/html, text/plain';
ctx.accepts().should.eql(['text/html', 'text/plain', 'image/jpeg', 'application/*']); ctx.accepts().should.eql(['text/html', 'text/plain', 'image/jpeg', 'application/*']);
@ -14,25 +14,25 @@ describe('ctx.accepts(types)', function(){
}); });
}); });
describe('with no valid types', function(){ describe('with no valid types', () => {
describe('when Accept is populated', function(){ describe('when Accept is populated', () => {
it('should return false', function(){ it('should return false', () => {
const ctx = context(); const ctx = context();
ctx.req.headers.accept = 'application/*;q=0.2, image/jpeg;q=0.8, text/html, text/plain'; ctx.req.headers.accept = 'application/*;q=0.2, image/jpeg;q=0.8, text/html, text/plain';
ctx.accepts('image/png', 'image/tiff').should.be.false; ctx.accepts('image/png', 'image/tiff').should.be.false;
}); });
}); });
describe('when Accept is not populated', function(){ describe('when Accept is not populated', () => {
it('should return the first type', function(){ it('should return the first type', () => {
const ctx = context(); const ctx = context();
ctx.accepts('text/html', 'text/plain', 'image/jpeg', 'application/*').should.equal('text/html'); ctx.accepts('text/html', 'text/plain', 'image/jpeg', 'application/*').should.equal('text/html');
}); });
}); });
}); });
describe('when extensions are given', function(){ describe('when extensions are given', () => {
it('should convert to mime types', function(){ it('should convert to mime types', () => {
const ctx = context(); const ctx = context();
ctx.req.headers.accept = 'text/plain, text/html'; ctx.req.headers.accept = 'text/plain, text/html';
ctx.accepts('html').should.equal('html'); ctx.accepts('html').should.equal('html');
@ -43,8 +43,8 @@ describe('ctx.accepts(types)', function(){
}); });
}); });
describe('when an array is given', function(){ describe('when an array is given', () => {
it('should return the first match', function(){ it('should return the first match', () => {
const ctx = context(); const ctx = context();
ctx.req.headers.accept = 'text/plain, text/html'; ctx.req.headers.accept = 'text/plain, text/html';
ctx.accepts(['png', 'text', 'html']).should.equal('text'); ctx.accepts(['png', 'text', 'html']).should.equal('text');
@ -52,8 +52,8 @@ describe('ctx.accepts(types)', function(){
}); });
}); });
describe('when multiple arguments are given', function(){ describe('when multiple arguments are given', () => {
it('should return the first match', function(){ it('should return the first match', () => {
const ctx = context(); const ctx = context();
ctx.req.headers.accept = 'text/plain, text/html'; ctx.req.headers.accept = 'text/plain, text/html';
ctx.accepts('png', 'text', 'html').should.equal('text'); ctx.accepts('png', 'text', 'html').should.equal('text');
@ -61,8 +61,8 @@ describe('ctx.accepts(types)', function(){
}); });
}); });
describe('when present in Accept as an exact match', function(){ describe('when present in Accept as an exact match', () => {
it('should return the type', function(){ it('should return the type', () => {
const ctx = context(); const ctx = context();
ctx.req.headers.accept = 'text/plain, text/html'; ctx.req.headers.accept = 'text/plain, text/html';
ctx.accepts('text/html').should.equal('text/html'); ctx.accepts('text/html').should.equal('text/html');
@ -70,8 +70,8 @@ describe('ctx.accepts(types)', function(){
}); });
}); });
describe('when present in Accept as a type match', function(){ describe('when present in Accept as a type match', () => {
it('should return the type', function(){ it('should return the type', () => {
const ctx = context(); const ctx = context();
ctx.req.headers.accept = 'application/json, */*'; ctx.req.headers.accept = 'application/json, */*';
ctx.accepts('text/html').should.equal('text/html'); ctx.accepts('text/html').should.equal('text/html');
@ -80,8 +80,8 @@ describe('ctx.accepts(types)', function(){
}); });
}); });
describe('when present in Accept as a subtype match', function(){ describe('when present in Accept as a subtype match', () => {
it('should return the type', function(){ it('should return the type', () => {
const ctx = context(); const ctx = context();
ctx.req.headers.accept = 'application/json, text/*'; ctx.req.headers.accept = 'application/json, text/*';
ctx.accepts('text/html').should.equal('text/html'); ctx.accepts('text/html').should.equal('text/html');

View file

@ -3,10 +3,10 @@
const context = require('../helpers/context'); const context = require('../helpers/context');
describe('ctx.acceptsCharsets()', function(){ describe('ctx.acceptsCharsets()', () => {
describe('with no arguments', function(){ describe('with no arguments', () => {
describe('when Accept-Charset is populated', function(){ describe('when Accept-Charset is populated', () => {
it('should return accepted types', function(){ it('should return accepted types', () => {
const ctx = context(); const ctx = context();
ctx.req.headers['accept-charset'] = 'utf-8, iso-8859-1;q=0.2, utf-7;q=0.5'; ctx.req.headers['accept-charset'] = 'utf-8, iso-8859-1;q=0.2, utf-7;q=0.5';
ctx.acceptsCharsets().should.eql(['utf-8', 'utf-7', 'iso-8859-1']); ctx.acceptsCharsets().should.eql(['utf-8', 'utf-7', 'iso-8859-1']);
@ -14,18 +14,18 @@ describe('ctx.acceptsCharsets()', function(){
}); });
}); });
describe('with multiple arguments', function(){ describe('with multiple arguments', () => {
describe('when Accept-Charset is populated', function(){ describe('when Accept-Charset is populated', () => {
describe('if any types match', function(){ describe('if any types match', () => {
it('should return the best fit', function(){ it('should return the best fit', () => {
const ctx = context(); const ctx = context();
ctx.req.headers['accept-charset'] = 'utf-8, iso-8859-1;q=0.2, utf-7;q=0.5'; ctx.req.headers['accept-charset'] = 'utf-8, iso-8859-1;q=0.2, utf-7;q=0.5';
ctx.acceptsCharsets('utf-7', 'utf-8').should.equal('utf-8'); ctx.acceptsCharsets('utf-7', 'utf-8').should.equal('utf-8');
}); });
}); });
describe('if no types match', function(){ describe('if no types match', () => {
it('should return false', function(){ it('should return false', () => {
const ctx = context(); const ctx = context();
ctx.req.headers['accept-charset'] = 'utf-8, iso-8859-1;q=0.2, utf-7;q=0.5'; ctx.req.headers['accept-charset'] = 'utf-8, iso-8859-1;q=0.2, utf-7;q=0.5';
ctx.acceptsCharsets('utf-16').should.be.false; ctx.acceptsCharsets('utf-16').should.be.false;
@ -33,16 +33,16 @@ describe('ctx.acceptsCharsets()', function(){
}); });
}); });
describe('when Accept-Charset is not populated', function(){ describe('when Accept-Charset is not populated', () => {
it('should return the first type', function(){ it('should return the first type', () => {
const ctx = context(); const ctx = context();
ctx.acceptsCharsets('utf-7', 'utf-8').should.equal('utf-7'); ctx.acceptsCharsets('utf-7', 'utf-8').should.equal('utf-7');
}); });
}); });
}); });
describe('with an array', function(){ describe('with an array', () => {
it('should return the best fit', function(){ it('should return the best fit', () => {
const ctx = context(); const ctx = context();
ctx.req.headers['accept-charset'] = 'utf-8, iso-8859-1;q=0.2, utf-7;q=0.5'; ctx.req.headers['accept-charset'] = 'utf-8, iso-8859-1;q=0.2, utf-7;q=0.5';
ctx.acceptsCharsets(['utf-7', 'utf-8']).should.equal('utf-8'); ctx.acceptsCharsets(['utf-7', 'utf-8']).should.equal('utf-8');

View file

@ -3,10 +3,10 @@
const context = require('../helpers/context'); const context = require('../helpers/context');
describe('ctx.acceptsEncodings()', function(){ describe('ctx.acceptsEncodings()', () => {
describe('with no arguments', function(){ describe('with no arguments', () => {
describe('when Accept-Encoding is populated', function(){ describe('when Accept-Encoding is populated', () => {
it('should return accepted types', function(){ it('should return accepted types', () => {
const ctx = context(); const ctx = context();
ctx.req.headers['accept-encoding'] = 'gzip, compress;q=0.2'; ctx.req.headers['accept-encoding'] = 'gzip, compress;q=0.2';
ctx.acceptsEncodings().should.eql(['gzip', 'compress', 'identity']); ctx.acceptsEncodings().should.eql(['gzip', 'compress', 'identity']);
@ -14,8 +14,8 @@ describe('ctx.acceptsEncodings()', function(){
}); });
}); });
describe('when Accept-Encoding is not populated', function(){ describe('when Accept-Encoding is not populated', () => {
it('should return identity', function(){ it('should return identity', () => {
const ctx = context(); const ctx = context();
ctx.acceptsEncodings().should.eql(['identity']); ctx.acceptsEncodings().should.eql(['identity']);
ctx.acceptsEncodings('gzip', 'deflate', 'identity').should.equal('identity'); ctx.acceptsEncodings('gzip', 'deflate', 'identity').should.equal('identity');
@ -23,8 +23,8 @@ describe('ctx.acceptsEncodings()', function(){
}); });
}); });
describe('with multiple arguments', function(){ describe('with multiple arguments', () => {
it('should return the best fit', function(){ it('should return the best fit', () => {
const ctx = context(); const ctx = context();
ctx.req.headers['accept-encoding'] = 'gzip, compress;q=0.2'; ctx.req.headers['accept-encoding'] = 'gzip, compress;q=0.2';
ctx.acceptsEncodings('compress', 'gzip').should.eql('gzip'); ctx.acceptsEncodings('compress', 'gzip').should.eql('gzip');
@ -32,8 +32,8 @@ describe('ctx.acceptsEncodings()', function(){
}); });
}); });
describe('with an array', function(){ describe('with an array', () => {
it('should return the best fit', function(){ it('should return the best fit', () => {
const ctx = context(); const ctx = context();
ctx.req.headers['accept-encoding'] = 'gzip, compress;q=0.2'; ctx.req.headers['accept-encoding'] = 'gzip, compress;q=0.2';
ctx.acceptsEncodings(['compress', 'gzip']).should.eql('gzip'); ctx.acceptsEncodings(['compress', 'gzip']).should.eql('gzip');

View file

@ -3,10 +3,10 @@
const context = require('../helpers/context'); const context = require('../helpers/context');
describe('ctx.acceptsLanguages(langs)', function(){ describe('ctx.acceptsLanguages(langs)', () => {
describe('with no arguments', function(){ describe('with no arguments', () => {
describe('when Accept-Language is populated', function(){ describe('when Accept-Language is populated', () => {
it('should return accepted types', function(){ it('should return accepted types', () => {
const ctx = context(); const ctx = context();
ctx.req.headers['accept-language'] = 'en;q=0.8, es, pt'; ctx.req.headers['accept-language'] = 'en;q=0.8, es, pt';
ctx.acceptsLanguages().should.eql(['es', 'pt', 'en']); ctx.acceptsLanguages().should.eql(['es', 'pt', 'en']);
@ -14,18 +14,18 @@ describe('ctx.acceptsLanguages(langs)', function(){
}); });
}); });
describe('with multiple arguments', function(){ describe('with multiple arguments', () => {
describe('when Accept-Language is populated', function(){ describe('when Accept-Language is populated', () => {
describe('if any types types match', function(){ describe('if any types types match', () => {
it('should return the best fit', function(){ it('should return the best fit', () => {
const ctx = context(); const ctx = context();
ctx.req.headers['accept-language'] = 'en;q=0.8, es, pt'; ctx.req.headers['accept-language'] = 'en;q=0.8, es, pt';
ctx.acceptsLanguages('es', 'en').should.equal('es'); ctx.acceptsLanguages('es', 'en').should.equal('es');
}); });
}); });
describe('if no types match', function(){ describe('if no types match', () => {
it('should return false', function(){ it('should return false', () => {
const ctx = context(); const ctx = context();
ctx.req.headers['accept-language'] = 'en;q=0.8, es, pt'; ctx.req.headers['accept-language'] = 'en;q=0.8, es, pt';
ctx.acceptsLanguages('fr', 'au').should.be.false; ctx.acceptsLanguages('fr', 'au').should.be.false;
@ -33,16 +33,16 @@ describe('ctx.acceptsLanguages(langs)', function(){
}); });
}); });
describe('when Accept-Language is not populated', function(){ describe('when Accept-Language is not populated', () => {
it('should return the first type', function(){ it('should return the first type', () => {
const ctx = context(); const ctx = context();
ctx.acceptsLanguages('es', 'en').should.equal('es'); ctx.acceptsLanguages('es', 'en').should.equal('es');
}); });
}); });
}); });
describe('with an array', function(){ describe('with an array', () => {
it('should return the best fit', function(){ it('should return the best fit', () => {
const ctx = context(); const ctx = context();
ctx.req.headers['accept-language'] = 'en;q=0.8, es, pt'; ctx.req.headers['accept-language'] = 'en;q=0.8, es, pt';
ctx.acceptsLanguages(['es', 'en']).should.equal('es'); ctx.acceptsLanguages(['es', 'en']).should.equal('es');

View file

@ -4,24 +4,24 @@
const request = require('../helpers/context').request; const request = require('../helpers/context').request;
const assert = require('assert'); const assert = require('assert');
describe('req.charset', function(){ describe('req.charset', () => {
describe('with no content-type present', function(){ describe('with no content-type present', () => {
it('should return ""', function(){ it('should return ""', () => {
const req = request(); const req = request();
assert('' === req.charset); assert('' === req.charset);
}); });
}); });
describe('with charset present', function(){ describe('with charset present', () => {
it('should return ""', function(){ it('should return ""', () => {
const req = request(); const req = request();
req.header['content-type'] = 'text/plain'; req.header['content-type'] = 'text/plain';
assert('' === req.charset); assert('' === req.charset);
}); });
}); });
describe('with a charset', function(){ describe('with a charset', () => {
it('should return the charset', function(){ it('should return the charset', () => {
const req = request(); const req = request();
req.header['content-type'] = 'text/plain; charset=utf-8'; req.header['content-type'] = 'text/plain; charset=utf-8';
req.charset.should.equal('utf-8'); req.charset.should.equal('utf-8');

View file

@ -3,17 +3,17 @@
const context = require('../helpers/context'); const context = require('../helpers/context');
describe('ctx.fresh', function(){ describe('ctx.fresh', () => {
describe('the request method is not GET and HEAD', function(){ describe('the request method is not GET and HEAD', () => {
it('should return false', function(){ it('should return false', () => {
const ctx = context(); const ctx = context();
ctx.req.method = 'POST'; ctx.req.method = 'POST';
ctx.fresh.should.be.false; ctx.fresh.should.be.false;
}); });
}); });
describe('the response is non-2xx', function(){ describe('the response is non-2xx', () => {
it('should return false', function(){ it('should return false', () => {
const ctx = context(); const ctx = context();
ctx.status = 404; ctx.status = 404;
ctx.req.method = 'GET'; ctx.req.method = 'GET';
@ -23,9 +23,9 @@ describe('ctx.fresh', function(){
}); });
}); });
describe('the response is 2xx', function(){ describe('the response is 2xx', () => {
describe('and etag matches', function(){ describe('and etag matches', () => {
it('should return true', function(){ it('should return true', () => {
const ctx = context(); const ctx = context();
ctx.status = 200; ctx.status = 200;
ctx.req.method = 'GET'; ctx.req.method = 'GET';
@ -35,8 +35,8 @@ describe('ctx.fresh', function(){
}); });
}); });
describe('and etag do not match', function(){ describe('and etag do not match', () => {
it('should return false', function(){ it('should return false', () => {
const ctx = context(); const ctx = context();
ctx.status = 200; ctx.status = 200;
ctx.req.method = 'GET'; ctx.req.method = 'GET';

View file

@ -3,8 +3,8 @@
const context = require('../helpers/context'); const context = require('../helpers/context');
describe('ctx.get(name)', function(){ describe('ctx.get(name)', () => {
it('should return the field value', function(){ it('should return the field value', () => {
const ctx = context(); const ctx = context();
ctx.req.headers.host = 'http://google.com'; ctx.req.headers.host = 'http://google.com';
ctx.req.headers.referer = 'http://google.com'; ctx.req.headers.referer = 'http://google.com';

View file

@ -3,8 +3,8 @@
const request = require('../helpers/context').request; const request = require('../helpers/context').request;
describe('req.header', function(){ describe('req.header', () => {
it('should return the request header object', function(){ it('should return the request header object', () => {
const req = request(); const req = request();
req.header.should.equal(req.req.headers); req.header.should.equal(req.req.headers);
}); });

View file

@ -3,8 +3,8 @@
const request = require('../helpers/context').request; const request = require('../helpers/context').request;
describe('req.headers', function(){ describe('req.headers', () => {
it('should return the request header object', function(){ it('should return the request header object', () => {
const req = request(); const req = request();
req.headers.should.equal(req.req.headers); req.headers.should.equal(req.req.headers);
}); });

View file

@ -4,23 +4,23 @@
const request = require('../helpers/context').request; const request = require('../helpers/context').request;
const assert = require('assert'); const assert = require('assert');
describe('req.host', function(){ describe('req.host', () => {
it('should return host with port', function(){ it('should return host with port', () => {
const req = request(); const req = request();
req.header.host = 'foo.com:3000'; req.header.host = 'foo.com:3000';
req.host.should.equal('foo.com:3000'); req.host.should.equal('foo.com:3000');
}); });
describe('with no host present', function(){ describe('with no host present', () => {
it('should return ""', function(){ it('should return ""', () => {
const req = request(); const req = request();
assert.equal(req.host, ''); assert.equal(req.host, '');
}); });
}); });
describe('when X-Forwarded-Host is present', function(){ describe('when X-Forwarded-Host is present', () => {
describe('and proxy is not trusted', function(){ describe('and proxy is not trusted', () => {
it('should be ignored', function(){ it('should be ignored', () => {
const req = request(); const req = request();
req.header['x-forwarded-host'] = 'bar.com'; req.header['x-forwarded-host'] = 'bar.com';
req.header.host = 'foo.com'; req.header.host = 'foo.com';
@ -28,8 +28,8 @@ describe('req.host', function(){
}); });
}); });
describe('and proxy is trusted', function(){ describe('and proxy is trusted', () => {
it('should be used', function(){ it('should be used', () => {
const req = request(); const req = request();
req.app.proxy = true; req.app.proxy = true;
req.header['x-forwarded-host'] = 'bar.com, baz.com'; req.header['x-forwarded-host'] = 'bar.com, baz.com';

View file

@ -4,23 +4,23 @@
const request = require('../helpers/context').request; const request = require('../helpers/context').request;
const assert = require('assert'); const assert = require('assert');
describe('req.hostname', function(){ describe('req.hostname', () => {
it('should return hostname void of port', function(){ it('should return hostname void of port', () => {
const req = request(); const req = request();
req.header.host = 'foo.com:3000'; req.header.host = 'foo.com:3000';
req.hostname.should.equal('foo.com'); req.hostname.should.equal('foo.com');
}); });
describe('with no host present', function(){ describe('with no host present', () => {
it('should return ""', function(){ it('should return ""', () => {
const req = request(); const req = request();
assert.equal(req.hostname, ''); assert.equal(req.hostname, '');
}); });
}); });
describe('when X-Forwarded-Host is present', function(){ describe('when X-Forwarded-Host is present', () => {
describe('and proxy is not trusted', function(){ describe('and proxy is not trusted', () => {
it('should be ignored', function(){ it('should be ignored', () => {
const req = request(); const req = request();
req.header['x-forwarded-host'] = 'bar.com'; req.header['x-forwarded-host'] = 'bar.com';
req.header.host = 'foo.com'; req.header.host = 'foo.com';
@ -28,8 +28,8 @@ describe('req.hostname', function(){
}); });
}); });
describe('and proxy is trusted', function(){ describe('and proxy is trusted', () => {
it('should be used', function(){ it('should be used', () => {
const req = request(); const req = request();
req.app.proxy = true; req.app.proxy = true;
req.header['x-forwarded-host'] = 'bar.com, baz.com'; req.header['x-forwarded-host'] = 'bar.com, baz.com';

View file

@ -6,8 +6,8 @@ const http = require('http');
const Koa = require('../../'); const Koa = require('../../');
const context = require('../helpers/context'); const context = require('../helpers/context');
describe('ctx.href', function(){ describe('ctx.href', () => {
it('should return the full request url', function(){ it('should return the full request url', () => {
const socket = new Stream.Duplex(); const socket = new Stream.Duplex();
const req = { const req = {
url: '/users/1?next=/dashboard', url: '/users/1?next=/dashboard',
@ -24,9 +24,9 @@ describe('ctx.href', function(){
ctx.href.should.equal('http://localhost/users/1?next=/dashboard'); ctx.href.should.equal('http://localhost/users/1?next=/dashboard');
}); });
it('should work with `GET http://example.com/foo`', function(done){ it('should work with `GET http://example.com/foo`', done => {
const app = new Koa(); const app = new Koa();
app.use(function(ctx){ app.use(ctx => {
ctx.body = ctx.href; ctx.body = ctx.href;
}); });
app.listen(function(){ app.listen(function(){
@ -35,12 +35,12 @@ describe('ctx.href', function(){
host: 'localhost', host: 'localhost',
path: 'http://example.com/foo', path: 'http://example.com/foo',
port: address.port port: address.port
}, function(res){ }, res => {
res.statusCode.should.equal(200); res.statusCode.should.equal(200);
let buf = ''; let buf = '';
res.setEncoding('utf8'); res.setEncoding('utf8');
res.on('data', s => buf += s); res.on('data', s => buf += s);
res.on('end', function(){ res.on('end', () => {
buf.should.equal('http://example.com/foo'); buf.should.equal('http://example.com/foo');
done(); done();
}); });

View file

@ -3,9 +3,9 @@
const request = require('../helpers/context').request; const request = require('../helpers/context').request;
describe('ctx.idempotent', function(){ describe('ctx.idempotent', () => {
describe('when the request method is idempotent', function(){ describe('when the request method is idempotent', () => {
it('should return true', function(){ it('should return true', () => {
['GET', 'HEAD', 'PUT', 'DELETE', 'OPTIONS', 'TRACE'].forEach(check); ['GET', 'HEAD', 'PUT', 'DELETE', 'OPTIONS', 'TRACE'].forEach(check);
function check(method){ function check(method){
const req = request(); const req = request();
@ -15,8 +15,8 @@ describe('ctx.idempotent', function(){
}); });
}); });
describe('when the request method is not idempotent', function(){ describe('when the request method is not idempotent', () => {
it('should return false', function(){ it('should return false', () => {
const req = request(); const req = request();
req.method = 'POST'; req.method = 'POST';
req.idempotent.should.equal(false); req.idempotent.should.equal(false);

View file

@ -4,9 +4,9 @@
const request = require('../helpers/context').request; const request = require('../helpers/context').request;
const assert = require('assert'); const assert = require('assert');
describe('req.inspect()', function(){ describe('req.inspect()', () => {
describe('with no request.req present', function(){ describe('with no request.req present', () => {
it('should return null', function(){ it('should return null', () => {
const req = request(); const req = request();
req.method = 'GET'; req.method = 'GET';
delete req.req; delete req.req;
@ -14,7 +14,7 @@ describe('req.inspect()', function(){
}); });
}); });
it('should return a json representation', function(){ it('should return a json representation', () => {
const req = request(); const req = request();
req.method = 'GET'; req.method = 'GET';
req.url = 'example.com'; req.url = 'example.com';

View file

@ -3,9 +3,9 @@
const request = require('../helpers/context').request; const request = require('../helpers/context').request;
describe('req.ip', function(){ describe('req.ip', () => {
describe('with req.ips present', function(){ describe('with req.ips present', () => {
it('should return req.ips[0]', function(){ it('should return req.ips[0]', () => {
const req = request(); const req = request();
req.app.proxy = true; req.app.proxy = true;
req.header['x-forwarded-for'] = '127.0.0.1'; req.header['x-forwarded-for'] = '127.0.0.1';
@ -14,15 +14,15 @@ describe('req.ip', function(){
}); });
}); });
describe('with no req.ips present', function(){ describe('with no req.ips present', () => {
it('should return req.socket.remoteAddress', function(){ it('should return req.socket.remoteAddress', () => {
const req = request(); const req = request();
req.socket.remoteAddress = '127.0.0.2'; req.socket.remoteAddress = '127.0.0.2';
req.ip.should.equal('127.0.0.2'); req.ip.should.equal('127.0.0.2');
}); });
describe('with req.socket.remoteAddress not present', function(){ describe('with req.socket.remoteAddress not present', () => {
it('should return an empty string', function(){ it('should return an empty string', () => {
const req = request(); const req = request();
req.socket.remoteAddress = null; req.socket.remoteAddress = null;
req.ip.should.equal(''); req.ip.should.equal('');

View file

@ -3,10 +3,10 @@
const request = require('../helpers/context').request; const request = require('../helpers/context').request;
describe('req.ips', function(){ describe('req.ips', () => {
describe('when X-Forwarded-For is present', function(){ describe('when X-Forwarded-For is present', () => {
describe('and proxy is not trusted', function(){ describe('and proxy is not trusted', () => {
it('should be ignored', function(){ it('should be ignored', () => {
const req = request(); const req = request();
req.app.proxy = false; req.app.proxy = false;
req.header['x-forwarded-for'] = '127.0.0.1,127.0.0.2'; req.header['x-forwarded-for'] = '127.0.0.1,127.0.0.2';
@ -14,8 +14,8 @@ describe('req.ips', function(){
}); });
}); });
describe('and proxy is trusted', function(){ describe('and proxy is trusted', () => {
it('should be used', function(){ it('should be used', () => {
const req = request(); const req = request();
req.app.proxy = true; req.app.proxy = true;
req.header['x-forwarded-for'] = '127.0.0.1,127.0.0.2'; req.header['x-forwarded-for'] = '127.0.0.1,127.0.0.2';

View file

@ -4,8 +4,8 @@
const context = require('../helpers/context'); const context = require('../helpers/context');
const assert = require('assert'); const assert = require('assert');
describe('ctx.is(type)', function(){ describe('ctx.is(type)', () => {
it('should ignore params', function(){ it('should ignore params', () => {
const ctx = context(); const ctx = context();
ctx.header['content-type'] = 'text/html; charset=utf-8'; ctx.header['content-type'] = 'text/html; charset=utf-8';
ctx.header['transfer-encoding'] = 'chunked'; ctx.header['transfer-encoding'] = 'chunked';
@ -13,8 +13,8 @@ describe('ctx.is(type)', function(){
ctx.is('text/*').should.equal('text/html'); ctx.is('text/*').should.equal('text/html');
}); });
describe('when no body is given', function(){ describe('when no body is given', () => {
it('should return null', function(){ it('should return null', () => {
const ctx = context(); const ctx = context();
assert(null == ctx.is()); assert(null == ctx.is());
@ -23,8 +23,8 @@ describe('ctx.is(type)', function(){
}); });
}); });
describe('when no content type is given', function(){ describe('when no content type is given', () => {
it('should return false', function(){ it('should return false', () => {
const ctx = context(); const ctx = context();
ctx.header['transfer-encoding'] = 'chunked'; ctx.header['transfer-encoding'] = 'chunked';
@ -34,8 +34,8 @@ describe('ctx.is(type)', function(){
}); });
}); });
describe('give no types', function(){ describe('give no types', () => {
it('should return the mime type', function(){ it('should return the mime type', () => {
const ctx = context(); const ctx = context();
ctx.header['content-type'] = 'image/png'; ctx.header['content-type'] = 'image/png';
ctx.header['transfer-encoding'] = 'chunked'; ctx.header['transfer-encoding'] = 'chunked';
@ -44,8 +44,8 @@ describe('ctx.is(type)', function(){
}); });
}); });
describe('given one type', function(){ describe('given one type', () => {
it('should return the type or false', function(){ it('should return the type or false', () => {
const ctx = context(); const ctx = context();
ctx.header['content-type'] = 'image/png'; ctx.header['content-type'] = 'image/png';
ctx.header['transfer-encoding'] = 'chunked'; ctx.header['transfer-encoding'] = 'chunked';
@ -64,8 +64,8 @@ describe('ctx.is(type)', function(){
}); });
}); });
describe('given multiple types', function(){ describe('given multiple types', () => {
it('should return the first match or false', function(){ it('should return the first match or false', () => {
const ctx = context(); const ctx = context();
ctx.header['content-type'] = 'image/png'; ctx.header['content-type'] = 'image/png';
ctx.header['transfer-encoding'] = 'chunked'; ctx.header['transfer-encoding'] = 'chunked';
@ -89,8 +89,8 @@ describe('ctx.is(type)', function(){
}); });
}); });
describe('when Content-Type: application/x-www-form-urlencoded', function(){ describe('when Content-Type: application/x-www-form-urlencoded', () => {
it('should match "urlencoded"', function(){ it('should match "urlencoded"', () => {
const ctx = context(); const ctx = context();
ctx.header['content-type'] = 'application/x-www-form-urlencoded'; ctx.header['content-type'] = 'application/x-www-form-urlencoded';
ctx.header['transfer-encoding'] = 'chunked'; ctx.header['transfer-encoding'] = 'chunked';

View file

@ -4,14 +4,14 @@
const request = require('../helpers/context').request; const request = require('../helpers/context').request;
const assert = require('assert'); const assert = require('assert');
describe('ctx.length', function(){ describe('ctx.length', () => {
it('should return length in content-length', function(){ it('should return length in content-length', () => {
const req = request(); const req = request();
req.header['content-length'] = '10'; req.header['content-length'] = '10';
req.length.should.equal(10); req.length.should.equal(10);
}); });
describe('with no content-length present', function(){ describe('with no content-length present', () => {
const req = request(); const req = request();
assert(null == req.length); assert(null == req.length);
}); });

View file

@ -4,8 +4,8 @@
const Stream = require('stream'); const Stream = require('stream');
const context = require('../helpers/context'); const context = require('../helpers/context');
describe('ctx.origin', function(){ describe('ctx.origin', () => {
it('should return the origin of url', function(){ it('should return the origin of url', () => {
const socket = new Stream.Duplex(); const socket = new Stream.Duplex();
const req = { const req = {
url: '/users/1?next=/dashboard', url: '/users/1?next=/dashboard',

View file

@ -3,16 +3,16 @@
const context = require('../helpers/context'); const context = require('../helpers/context');
describe('ctx.path', function(){ describe('ctx.path', () => {
it('should return the pathname', function(){ it('should return the pathname', () => {
const ctx = context(); const ctx = context();
ctx.url = '/login?next=/dashboard'; ctx.url = '/login?next=/dashboard';
ctx.path.should.equal('/login'); ctx.path.should.equal('/login');
}); });
}); });
describe('ctx.path=', function(){ describe('ctx.path=', () => {
it('should set the pathname', function(){ it('should set the pathname', () => {
const ctx = context(); const ctx = context();
ctx.url = '/login?next=/dashboard'; ctx.url = '/login?next=/dashboard';
@ -21,7 +21,7 @@ describe('ctx.path=', function(){
ctx.url.should.equal('/logout?next=/dashboard'); ctx.url.should.equal('/logout?next=/dashboard');
}); });
it('should change .url but not .originalUrl', function(){ it('should change .url but not .originalUrl', () => {
const ctx = context({ url: '/login' }); const ctx = context({ url: '/login' });
ctx.path = '/logout'; ctx.path = '/logout';
ctx.url.should.equal('/logout'); ctx.url.should.equal('/logout');

View file

@ -3,26 +3,26 @@
const request = require('../helpers/context').request; const request = require('../helpers/context').request;
describe('req.protocol', function(){ describe('req.protocol', () => {
describe('when encrypted', function(){ describe('when encrypted', () => {
it('should return "https"', function(){ it('should return "https"', () => {
const req = request(); const req = request();
req.req.socket = { encrypted: true }; req.req.socket = { encrypted: true };
req.protocol.should.equal('https'); req.protocol.should.equal('https');
}); });
}); });
describe('when unencrypted', function(){ describe('when unencrypted', () => {
it('should return "http"', function(){ it('should return "http"', () => {
const req = request(); const req = request();
req.req.socket = {}; req.req.socket = {};
req.protocol.should.equal('http'); req.protocol.should.equal('http');
}); });
}); });
describe('when X-Forwarded-Proto is set', function(){ describe('when X-Forwarded-Proto is set', () => {
describe('and proxy is trusted', function(){ describe('and proxy is trusted', () => {
it('should be used', function(){ it('should be used', () => {
const req = request(); const req = request();
req.app.proxy = true; req.app.proxy = true;
req.req.socket = {}; req.req.socket = {};
@ -30,8 +30,8 @@ describe('req.protocol', function(){
req.protocol.should.equal('https'); req.protocol.should.equal('https');
}); });
describe('and X-Forwarded-Proto is empty', function(){ describe('and X-Forwarded-Proto is empty', () => {
it('should return "http"', function(){ it('should return "http"', () => {
const req = request(); const req = request();
req.app.proxy = true; req.app.proxy = true;
req.req.socket = {}; req.req.socket = {};
@ -41,8 +41,8 @@ describe('req.protocol', function(){
}); });
}); });
describe('and proxy is not trusted', function(){ describe('and proxy is not trusted', () => {
it('should not be used', function(){ it('should not be used', () => {
const req = request(); const req = request();
req.req.socket = {}; req.req.socket = {};
req.header['x-forwarded-proto'] = 'https, http'; req.header['x-forwarded-proto'] = 'https, http';

View file

@ -3,14 +3,14 @@
const context = require('../helpers/context'); const context = require('../helpers/context');
describe('ctx.query', function(){ describe('ctx.query', () => {
describe('when missing', function(){ describe('when missing', () => {
it('should return an empty object', function(){ it('should return an empty object', () => {
const ctx = context({ url: '/' }); const ctx = context({ url: '/' });
ctx.query.should.eql({}); ctx.query.should.eql({});
}); });
it('should return the same object each time it\'s accessed', function(done){ it('should return the same object each time it\'s accessed', done => {
const ctx = context({ url: '/' }); const ctx = context({ url: '/' });
ctx.query.a = '2'; ctx.query.a = '2';
ctx.query.a.should.equal('2'); ctx.query.a.should.equal('2');
@ -18,14 +18,14 @@ describe('ctx.query', function(){
}); });
}); });
it('should return a parsed query-string', function(){ it('should return a parsed query-string', () => {
const ctx = context({ url: '/?page=2' }); const ctx = context({ url: '/?page=2' });
ctx.query.page.should.equal('2'); ctx.query.page.should.equal('2');
}); });
}); });
describe('ctx.query=', function(){ describe('ctx.query=', () => {
it('should stringify and replace the querystring and search', function(){ it('should stringify and replace the querystring and search', () => {
const ctx = context({ url: '/store/shoes' }); const ctx = context({ url: '/store/shoes' });
ctx.query = { page: 2, color: 'blue' }; ctx.query = { page: 2, color: 'blue' };
ctx.url.should.equal('/store/shoes?page=2&color=blue'); ctx.url.should.equal('/store/shoes?page=2&color=blue');
@ -33,7 +33,7 @@ describe('ctx.query=', function(){
ctx.search.should.equal('?page=2&color=blue'); ctx.search.should.equal('?page=2&color=blue');
}); });
it('should change .url but not .originalUrl', function(){ it('should change .url but not .originalUrl', () => {
const ctx = context({ url: '/store/shoes' }); const ctx = context({ url: '/store/shoes' });
ctx.query = { page: 2 }; ctx.query = { page: 2 };
ctx.url.should.equal('/store/shoes?page=2'); ctx.url.should.equal('/store/shoes?page=2');

View file

@ -3,14 +3,14 @@
const context = require('../helpers/context'); const context = require('../helpers/context');
describe('ctx.querystring', function(){ describe('ctx.querystring', () => {
it('should return the querystring', function(){ it('should return the querystring', () => {
const ctx = context({ url: '/store/shoes?page=2&color=blue' }); const ctx = context({ url: '/store/shoes?page=2&color=blue' });
ctx.querystring.should.equal('page=2&color=blue'); ctx.querystring.should.equal('page=2&color=blue');
}); });
describe('when ctx.req not present', function(){ describe('when ctx.req not present', () => {
it('should return an empty string', function(){ it('should return an empty string', () => {
const ctx = context(); const ctx = context();
ctx.request.req = null; ctx.request.req = null;
ctx.querystring.should.equal(''); ctx.querystring.should.equal('');
@ -18,15 +18,15 @@ describe('ctx.querystring', function(){
}); });
}); });
describe('ctx.querystring=', function(){ describe('ctx.querystring=', () => {
it('should replace the querystring', function(){ it('should replace the querystring', () => {
const ctx = context({ url: '/store/shoes' }); const ctx = context({ url: '/store/shoes' });
ctx.querystring = 'page=2&color=blue'; ctx.querystring = 'page=2&color=blue';
ctx.url.should.equal('/store/shoes?page=2&color=blue'); ctx.url.should.equal('/store/shoes?page=2&color=blue');
ctx.querystring.should.equal('page=2&color=blue'); ctx.querystring.should.equal('page=2&color=blue');
}); });
it('should update ctx.search and ctx.query', function(){ it('should update ctx.search and ctx.query', () => {
const ctx = context({ url: '/store/shoes' }); const ctx = context({ url: '/store/shoes' });
ctx.querystring = 'page=2&color=blue'; ctx.querystring = 'page=2&color=blue';
ctx.url.should.equal('/store/shoes?page=2&color=blue'); ctx.url.should.equal('/store/shoes?page=2&color=blue');
@ -37,7 +37,7 @@ describe('ctx.querystring=', function(){
}); });
}); });
it('should change .url but not .originalUrl', function(){ it('should change .url but not .originalUrl', () => {
const ctx = context({ url: '/store/shoes' }); const ctx = context({ url: '/store/shoes' });
ctx.querystring = 'page=2&color=blue'; ctx.querystring = 'page=2&color=blue';
ctx.url.should.equal('/store/shoes?page=2&color=blue'); ctx.url.should.equal('/store/shoes?page=2&color=blue');

View file

@ -3,15 +3,15 @@
const context = require('../helpers/context'); const context = require('../helpers/context');
describe('ctx.search=', function(){ describe('ctx.search=', () => {
it('should replace the search', function(){ it('should replace the search', () => {
const ctx = context({ url: '/store/shoes' }); const ctx = context({ url: '/store/shoes' });
ctx.search = '?page=2&color=blue'; ctx.search = '?page=2&color=blue';
ctx.url.should.equal('/store/shoes?page=2&color=blue'); ctx.url.should.equal('/store/shoes?page=2&color=blue');
ctx.search.should.equal('?page=2&color=blue'); ctx.search.should.equal('?page=2&color=blue');
}); });
it('should update ctx.querystring and ctx.query', function(){ it('should update ctx.querystring and ctx.query', () => {
const ctx = context({ url: '/store/shoes' }); const ctx = context({ url: '/store/shoes' });
ctx.search = '?page=2&color=blue'; ctx.search = '?page=2&color=blue';
ctx.url.should.equal('/store/shoes?page=2&color=blue'); ctx.url.should.equal('/store/shoes?page=2&color=blue');
@ -22,7 +22,7 @@ describe('ctx.search=', function(){
}); });
}); });
it('should change .url but not .originalUrl', function(){ it('should change .url but not .originalUrl', () => {
const ctx = context({ url: '/store/shoes' }); const ctx = context({ url: '/store/shoes' });
ctx.search = '?page=2&color=blue'; ctx.search = '?page=2&color=blue';
ctx.url.should.equal('/store/shoes?page=2&color=blue'); ctx.url.should.equal('/store/shoes?page=2&color=blue');
@ -30,8 +30,8 @@ describe('ctx.search=', function(){
ctx.request.originalUrl.should.equal('/store/shoes'); ctx.request.originalUrl.should.equal('/store/shoes');
}); });
describe('when missing', function(){ describe('when missing', () => {
it('should return ""', function(){ it('should return ""', () => {
const ctx = context({ url: '/store/shoes' }); const ctx = context({ url: '/store/shoes' });
ctx.search.should.equal(''); ctx.search.should.equal('');
}); });

View file

@ -3,8 +3,8 @@
const request = require('../helpers/context').request; const request = require('../helpers/context').request;
describe('req.secure', function(){ describe('req.secure', () => {
it('should return true when encrypted', function(){ it('should return true when encrypted', () => {
const req = request(); const req = request();
req.req.socket = { encrypted: true }; req.req.socket = { encrypted: true };
req.secure.should.be.true; req.secure.should.be.true;

View file

@ -3,8 +3,8 @@
const context = require('../helpers/context'); const context = require('../helpers/context');
describe('req.stale', function(){ describe('req.stale', () => {
it('should be the inverse of req.fresh', function(){ it('should be the inverse of req.fresh', () => {
const ctx = context(); const ctx = context();
ctx.status = 200; ctx.status = 200;
ctx.method = 'GET'; ctx.method = 'GET';

View file

@ -3,8 +3,8 @@
const request = require('../helpers/context').request; const request = require('../helpers/context').request;
describe('req.subdomains', function(){ describe('req.subdomains', () => {
it('should return subdomain array', function(){ it('should return subdomain array', () => {
const req = request(); const req = request();
req.header.host = 'tobi.ferrets.example.com'; req.header.host = 'tobi.ferrets.example.com';
req.app.subdomainOffset = 2; req.app.subdomainOffset = 2;
@ -14,7 +14,7 @@ describe('req.subdomains', function(){
req.subdomains.should.eql(['tobi']); req.subdomains.should.eql(['tobi']);
}); });
describe('with no host present', function(){ describe('with no host present', () => {
const req = request(); const req = request();
req.subdomains.should.eql([]); req.subdomains.should.eql([]);
}); });

View file

@ -4,14 +4,14 @@
const request = require('../helpers/context').request; const request = require('../helpers/context').request;
const assert = require('assert'); const assert = require('assert');
describe('req.type', function(){ describe('req.type', () => {
it('should return type void of parameters', function(){ it('should return type void of parameters', () => {
const req = request(); const req = request();
req.header['content-type'] = 'text/html; charset=utf-8'; req.header['content-type'] = 'text/html; charset=utf-8';
req.type.should.equal('text/html'); req.type.should.equal('text/html');
}); });
describe('with no host present', function(){ describe('with no host present', () => {
const req = request(); const req = request();
assert('' === req.type); assert('' === req.type);
}); });

View file

@ -3,15 +3,15 @@
const context = require('../helpers/context'); const context = require('../helpers/context');
describe('ctx.append(name, val)', function(){ describe('ctx.append(name, val)', () => {
it('should append multiple headers', function(){ it('should append multiple headers', () => {
const ctx = context(); const ctx = context();
ctx.append('x-foo', 'bar1'); ctx.append('x-foo', 'bar1');
ctx.append('x-foo', 'bar2'); ctx.append('x-foo', 'bar2');
ctx.response.header['x-foo'].should.eql(['bar1', 'bar2']); ctx.response.header['x-foo'].should.eql(['bar1', 'bar2']);
}); });
it('should accept array of values', function(){ it('should accept array of values', () => {
const ctx = context(); const ctx = context();
ctx.append('Set-Cookie', ['foo=bar', 'fizz=buzz']); ctx.append('Set-Cookie', ['foo=bar', 'fizz=buzz']);
@ -19,7 +19,7 @@ describe('ctx.append(name, val)', function(){
ctx.response.header['set-cookie'].should.eql(['foo=bar', 'fizz=buzz', 'hi=again']); ctx.response.header['set-cookie'].should.eql(['foo=bar', 'fizz=buzz', 'hi=again']);
}); });
it('should get reset by res.set(field, val)', function(){ it('should get reset by res.set(field, val)', () => {
const ctx = context(); const ctx = context();
ctx.append('Link', '<http://localhost/>'); ctx.append('Link', '<http://localhost/>');
@ -30,7 +30,7 @@ describe('ctx.append(name, val)', function(){
ctx.response.header.link.should.equal('<http://127.0.0.1/>'); ctx.response.header.link.should.equal('<http://127.0.0.1/>');
}); });
it('should work with res.set(field, val) first', function(){ it('should work with res.set(field, val) first', () => {
const ctx = context(); const ctx = context();
ctx.set('Link', '<http://localhost/>'); ctx.set('Link', '<http://localhost/>');

View file

@ -5,9 +5,9 @@ const context = require('../helpers/context');
const request = require('supertest'); const request = require('supertest');
const Koa = require('../..'); const Koa = require('../..');
describe('ctx.attachment([filename])', function(){ describe('ctx.attachment([filename])', () => {
describe('when given a filename', function(){ describe('when given a filename', () => {
it('should set the filename param', function(){ it('should set the filename param', () => {
const ctx = context(); const ctx = context();
ctx.attachment('path/to/tobi.png'); ctx.attachment('path/to/tobi.png');
const str = 'attachment; filename="tobi.png"'; const str = 'attachment; filename="tobi.png"';
@ -15,26 +15,26 @@ describe('ctx.attachment([filename])', function(){
}); });
}); });
describe('when omitting filename', function(){ describe('when omitting filename', () => {
it('should not set filename param', function(){ it('should not set filename param', () => {
const ctx = context(); const ctx = context();
ctx.attachment(); ctx.attachment();
ctx.response.header['content-disposition'].should.equal('attachment'); ctx.response.header['content-disposition'].should.equal('attachment');
}); });
}); });
describe('when given a no-ascii filename', function(){ describe('when given a no-ascii filename', () => {
it('should set the encodeURI filename param', function(){ it('should set the encodeURI filename param', () => {
const ctx = context(); const ctx = context();
ctx.attachment('path/to/include-no-ascii-char-中文名-ok.png'); ctx.attachment('path/to/include-no-ascii-char-中文名-ok.png');
const str = 'attachment; filename=\"include-no-ascii-char-???-ok.png\"; filename*=UTF-8\'\'include-no-ascii-char-%E4%B8%AD%E6%96%87%E5%90%8D-ok.png'; const str = 'attachment; filename=\"include-no-ascii-char-???-ok.png\"; filename*=UTF-8\'\'include-no-ascii-char-%E4%B8%AD%E6%96%87%E5%90%8D-ok.png';
ctx.response.header['content-disposition'].should.equal(str); ctx.response.header['content-disposition'].should.equal(str);
}); });
it('should work with http client', function(done){ it('should work with http client', done => {
const app = new Koa(); const app = new Koa();
app.use(function(ctx, next){ app.use((ctx, next) => {
ctx.attachment('path/to/include-no-ascii-char-中文名-ok.json'); ctx.attachment('path/to/include-no-ascii-char-中文名-ok.json');
ctx.body = {foo: 'bar'}; ctx.body = {foo: 'bar'};
}); });

View file

@ -5,17 +5,17 @@ const response = require('../helpers/context').response;
const assert = require('assert'); const assert = require('assert');
const fs = require('fs'); const fs = require('fs');
describe('res.body=', function(){ describe('res.body=', () => {
describe('when Content-Type is set', function(){ describe('when Content-Type is set', () => {
it('should not override', function(){ it('should not override', () => {
const res = response(); const res = response();
res.type = 'png'; res.type = 'png';
res.body = new Buffer('something'); res.body = new Buffer('something');
assert('image/png' == res.header['content-type']); assert('image/png' == res.header['content-type']);
}); });
describe('when body is an object', function(){ describe('when body is an object', () => {
it('should override as json', function(){ it('should override as json', () => {
const res = response(); const res = response();
res.body = '<em>hey</em>'; res.body = '<em>hey</em>';
@ -26,7 +26,7 @@ describe('res.body=', function(){
}); });
}); });
it('should override length', function(){ it('should override length', () => {
const res = response(); const res = response();
res.type = 'html'; res.type = 'html';
res.body = 'something'; res.body = 'something';
@ -34,21 +34,21 @@ describe('res.body=', function(){
}); });
}); });
describe('when a string is given', function(){ describe('when a string is given', () => {
it('should default to text', function(){ it('should default to text', () => {
const res = response(); const res = response();
res.body = 'Tobi'; res.body = 'Tobi';
assert('text/plain; charset=utf-8' == res.header['content-type']); assert('text/plain; charset=utf-8' == res.header['content-type']);
}); });
it('should set length', function(){ it('should set length', () => {
const res = response(); const res = response();
res.body = 'Tobi'; res.body = 'Tobi';
assert('4' == res.header['content-length']); assert('4' == res.header['content-length']);
}); });
describe('and contains a non-leading <', function(){ describe('and contains a non-leading <', () => {
it('should default to text', function(){ it('should default to text', () => {
const res = response(); const res = response();
res.body = 'aklsdjf < klajsdlfjasd'; res.body = 'aklsdjf < klajsdlfjasd';
assert('text/plain; charset=utf-8' == res.header['content-type']); assert('text/plain; charset=utf-8' == res.header['content-type']);
@ -56,21 +56,21 @@ describe('res.body=', function(){
}); });
}); });
describe('when an html string is given', function(){ describe('when an html string is given', () => {
it('should default to html', function(){ it('should default to html', () => {
const res = response(); const res = response();
res.body = '<h1>Tobi</h1>'; res.body = '<h1>Tobi</h1>';
assert('text/html; charset=utf-8' == res.header['content-type']); assert('text/html; charset=utf-8' == res.header['content-type']);
}); });
it('should set length', function(){ it('should set length', () => {
const string = '<h1>Tobi</h1>'; const string = '<h1>Tobi</h1>';
const res = response(); const res = response();
res.body = string; res.body = string;
assert.equal(res.length, Buffer.byteLength(string)); assert.equal(res.length, Buffer.byteLength(string));
}); });
it('should set length when body is overridden', function(){ it('should set length when body is overridden', () => {
const string = '<h1>Tobi</h1>'; const string = '<h1>Tobi</h1>';
const res = response(); const res = response();
res.body = string; res.body = string;
@ -78,8 +78,8 @@ describe('res.body=', function(){
assert.equal(res.length, 2 * Buffer.byteLength(string)); assert.equal(res.length, 2 * Buffer.byteLength(string));
}); });
describe('when it contains leading whitespace', function(){ describe('when it contains leading whitespace', () => {
it('should default to html', function(){ it('should default to html', () => {
const res = response(); const res = response();
res.body = ' <h1>Tobi</h1>'; res.body = ' <h1>Tobi</h1>';
assert('text/html; charset=utf-8' == res.header['content-type']); assert('text/html; charset=utf-8' == res.header['content-type']);
@ -87,8 +87,8 @@ describe('res.body=', function(){
}); });
}); });
describe('when an xml string is given', function(){ describe('when an xml string is given', () => {
it('should default to html', function(){ it('should default to html', () => {
/** /**
* ctx test is to show that we're not going * ctx test is to show that we're not going
* to be stricter with the html sniff * to be stricter with the html sniff
@ -102,30 +102,30 @@ describe('res.body=', function(){
}); });
}); });
describe('when a stream is given', function(){ describe('when a stream is given', () => {
it('should default to an octet stream', function(){ it('should default to an octet stream', () => {
const res = response(); const res = response();
res.body = fs.createReadStream('LICENSE'); res.body = fs.createReadStream('LICENSE');
assert('application/octet-stream' == res.header['content-type']); assert('application/octet-stream' == res.header['content-type']);
}); });
}); });
describe('when a buffer is given', function(){ describe('when a buffer is given', () => {
it('should default to an octet stream', function(){ it('should default to an octet stream', () => {
const res = response(); const res = response();
res.body = new Buffer('hey'); res.body = new Buffer('hey');
assert('application/octet-stream' == res.header['content-type']); assert('application/octet-stream' == res.header['content-type']);
}); });
it('should set length', function(){ it('should set length', () => {
const res = response(); const res = response();
res.body = new Buffer('Tobi'); res.body = new Buffer('Tobi');
assert('4' == res.header['content-length']); assert('4' == res.header['content-length']);
}); });
}); });
describe('when an object is given', function(){ describe('when an object is given', () => {
it('should default to json', function(){ it('should default to json', () => {
const res = response(); const res = response();
res.body = { foo: 'bar' }; res.body = { foo: 'bar' };
assert('application/json; charset=utf-8' == res.header['content-type']); assert('application/json; charset=utf-8' == res.header['content-type']);

View file

@ -3,28 +3,28 @@
const response = require('../helpers/context').response; const response = require('../helpers/context').response;
describe('res.etag=', function(){ describe('res.etag=', () => {
it('should not modify an etag with quotes', function(){ it('should not modify an etag with quotes', () => {
const res = response(); const res = response();
res.etag = '"asdf"'; res.etag = '"asdf"';
res.header.etag.should.equal('"asdf"'); res.header.etag.should.equal('"asdf"');
}); });
it('should not modify a weak etag', function(){ it('should not modify a weak etag', () => {
const res = response(); const res = response();
res.etag = 'W/"asdf"'; res.etag = 'W/"asdf"';
res.header.etag.should.equal('W/"asdf"'); res.header.etag.should.equal('W/"asdf"');
}); });
it('should add quotes around an etag if necessary', function(){ it('should add quotes around an etag if necessary', () => {
const res = response(); const res = response();
res.etag = 'asdf'; res.etag = 'asdf';
res.header.etag.should.equal('"asdf"'); res.header.etag.should.equal('"asdf"');
}); });
}); });
describe('res.etag', function(){ describe('res.etag', () => {
it('should return etag', function(){ it('should return etag', () => {
const res = response(); const res = response();
res.etag = '"asdf"'; res.etag = '"asdf"';
res.etag.should.equal('"asdf"'); res.etag.should.equal('"asdf"');

View file

@ -3,15 +3,15 @@
const response = require('../helpers/context').response; const response = require('../helpers/context').response;
describe('res.header', function(){ describe('res.header', () => {
it('should return the response header object', function(){ it('should return the response header object', () => {
const res = response(); const res = response();
res.set('X-Foo', 'bar'); res.set('X-Foo', 'bar');
res.header.should.eql({ 'x-foo': 'bar' }); res.header.should.eql({ 'x-foo': 'bar' });
}); });
describe('when res._headers not present', function(){ describe('when res._headers not present', () => {
it('should return empty object', function(){ it('should return empty object', () => {
const res = response(); const res = response();
res.res._headers = null; res.res._headers = null;
res.header.should.eql({}); res.header.should.eql({});

View file

@ -3,15 +3,15 @@
const response = require('../helpers/context').response; const response = require('../helpers/context').response;
describe('res.header', function(){ describe('res.header', () => {
it('should return the response header object', function(){ it('should return the response header object', () => {
const res = response(); const res = response();
res.set('X-Foo', 'bar'); res.set('X-Foo', 'bar');
res.headers.should.eql({ 'x-foo': 'bar' }); res.headers.should.eql({ 'x-foo': 'bar' });
}); });
describe('when res._headers not present', function(){ describe('when res._headers not present', () => {
it('should return empty object', function(){ it('should return empty object', () => {
const res = response(); const res = response();
res.res._headers = null; res.res._headers = null;
res.headers.should.eql({}); res.headers.should.eql({});

View file

@ -4,9 +4,9 @@
const response = require('../helpers/context').response; const response = require('../helpers/context').response;
const assert = require('assert'); const assert = require('assert');
describe('res.inspect()', function(){ describe('res.inspect()', () => {
describe('with no response.res present', function(){ describe('with no response.res present', () => {
it('should return null', function(){ it('should return null', () => {
const res = response(); const res = response();
res.body = 'hello'; res.body = 'hello';
delete res.res; delete res.res;
@ -14,7 +14,7 @@ describe('res.inspect()', function(){
}); });
}); });
it('should return a json representation', function(){ it('should return a json representation', () => {
const res = response(); const res = response();
res.body = 'hello'; res.body = 'hello';

View file

@ -4,16 +4,16 @@
const context = require('../helpers/context'); const context = require('../helpers/context');
const assert = require('assert'); const assert = require('assert');
describe('response.is(type)', function(){ describe('response.is(type)', () => {
it('should ignore params', function(){ it('should ignore params', () => {
const res = context().response; const res = context().response;
res.type = 'text/html; charset=utf-8'; res.type = 'text/html; charset=utf-8';
res.is('text/*').should.equal('text/html'); res.is('text/*').should.equal('text/html');
}); });
describe('when no type is set', function(){ describe('when no type is set', () => {
it('should return false', function(){ it('should return false', () => {
const res = context().response; const res = context().response;
assert(false === res.is()); assert(false === res.is());
@ -21,8 +21,8 @@ describe('response.is(type)', function(){
}); });
}); });
describe('when given no types', function(){ describe('when given no types', () => {
it('should return the type', function(){ it('should return the type', () => {
const res = context().response; const res = context().response;
res.type = 'text/html; charset=utf-8'; res.type = 'text/html; charset=utf-8';
@ -30,8 +30,8 @@ describe('response.is(type)', function(){
}); });
}); });
describe('given one type', function(){ describe('given one type', () => {
it('should return the type or false', function(){ it('should return the type or false', () => {
const res = context().response; const res = context().response;
res.type = 'image/png'; res.type = 'image/png';
@ -49,8 +49,8 @@ describe('response.is(type)', function(){
}); });
}); });
describe('given multiple types', function(){ describe('given multiple types', () => {
it('should return the first match or false', function(){ it('should return the first match or false', () => {
const res = context().response; const res = context().response;
res.type = 'image/png'; res.type = 'image/png';
@ -73,8 +73,8 @@ describe('response.is(type)', function(){
}); });
}); });
describe('when Content-Type: application/x-www-form-urlencoded', function(){ describe('when Content-Type: application/x-www-form-urlencoded', () => {
it('should match "urlencoded"', function(){ it('should match "urlencoded"', () => {
const res = context().response; const res = context().response;
res.type = 'application/x-www-form-urlencoded'; res.type = 'application/x-www-form-urlencoded';

View file

@ -3,22 +3,22 @@
const response = require('../helpers/context').response; const response = require('../helpers/context').response;
describe('res.lastModified', function(){ describe('res.lastModified', () => {
it('should set the header as a UTCString', function(){ it('should set the header as a UTCString', () => {
const res = response(); const res = response();
const date = new Date(); const date = new Date();
res.lastModified = date; res.lastModified = date;
res.header['last-modified'].should.equal(date.toUTCString()); res.header['last-modified'].should.equal(date.toUTCString());
}); });
it('should work with date strings', function(){ it('should work with date strings', () => {
const res = response(); const res = response();
const date = new Date(); const date = new Date();
res.lastModified = date.toString(); res.lastModified = date.toString();
res.header['last-modified'].should.equal(date.toUTCString()); res.header['last-modified'].should.equal(date.toUTCString());
}); });
it('should get the header as a Date', function(){ it('should get the header as a Date', () => {
// Note: Date() removes milliseconds, but it's practically important. // Note: Date() removes milliseconds, but it's practically important.
const res = response(); const res = response();
const date = new Date(); const date = new Date();
@ -26,8 +26,8 @@ describe('res.lastModified', function(){
(res.lastModified.getTime() / 1000).should.equal(Math.floor(date.getTime() / 1000)); (res.lastModified.getTime() / 1000).should.equal(Math.floor(date.getTime() / 1000));
}); });
describe('when lastModified not set', function(){ describe('when lastModified not set', () => {
it('should get undefined', function(){ it('should get undefined', () => {
const res = response(); const res = response();
(res.lastModified === undefined).should.be.ok; (res.lastModified === undefined).should.be.ok;
}); });

View file

@ -6,9 +6,9 @@ const should = require('should');
const assert = require('assert'); const assert = require('assert');
const fs = require('fs'); const fs = require('fs');
describe('res.length', function(){ describe('res.length', () => {
describe('when Content-Length is defined', function(){ describe('when Content-Length is defined', () => {
it('should return a number', function(){ it('should return a number', () => {
const res = response(); const res = response();
res.header['content-length'] = '120'; res.header['content-length'] = '120';
res.length.should.equal(120); res.length.should.equal(120);
@ -16,18 +16,18 @@ describe('res.length', function(){
}); });
}); });
describe('res.length', function(){ describe('res.length', () => {
describe('when Content-Length is defined', function(){ describe('when Content-Length is defined', () => {
it('should return a number', function(){ it('should return a number', () => {
const res = response(); const res = response();
res.set('Content-Length', '1024'); res.set('Content-Length', '1024');
res.length.should.equal(1024); res.length.should.equal(1024);
}); });
}); });
describe('when Content-Length is not defined', function(){ describe('when Content-Length is not defined', () => {
describe('and a .body is set', function(){ describe('and a .body is set', () => {
it('should return a number', function(){ it('should return a number', () => {
const res = response(); const res = response();
res.body = 'foo'; res.body = 'foo';
@ -59,8 +59,8 @@ describe('res.length', function(){
}); });
}); });
describe('and .body is not', function(){ describe('and .body is not', () => {
it('should return undefined', function(){ it('should return undefined', () => {
const res = response(); const res = response();
assert(null == res.length); assert(null == res.length);
}); });

View file

@ -3,15 +3,15 @@
const response = require('../helpers/context').response; const response = require('../helpers/context').response;
describe('res.message', function(){ describe('res.message', () => {
it('should return the response status message', function(){ it('should return the response status message', () => {
const res = response(); const res = response();
res.status = 200; res.status = 200;
res.message.should.equal('OK'); res.message.should.equal('OK');
}); });
describe('when res.message not present', function(){ describe('when res.message not present', () => {
it('should look up in statuses', function(){ it('should look up in statuses', () => {
const res = response(); const res = response();
res.res.statusCode = 200; res.res.statusCode = 200;
res.message.should.equal('OK'); res.message.should.equal('OK');
@ -19,8 +19,8 @@ describe('res.message', function(){
}); });
}); });
describe('res.message=', function(){ describe('res.message=', () => {
it('should set response status message', function(){ it('should set response status message', () => {
const res = response(); const res = response();
res.status = 200; res.status = 200;
res.message = 'ok'; res.message = 'ok';

View file

@ -3,44 +3,44 @@
const context = require('../helpers/context'); const context = require('../helpers/context');
describe('ctx.redirect(url)', function(){ describe('ctx.redirect(url)', () => {
it('should redirect to the given url', function(){ it('should redirect to the given url', () => {
const ctx = context(); const ctx = context();
ctx.redirect('http://google.com'); ctx.redirect('http://google.com');
ctx.response.header.location.should.equal('http://google.com'); ctx.response.header.location.should.equal('http://google.com');
ctx.status.should.equal(302); ctx.status.should.equal(302);
}); });
describe('with "back"', function(){ describe('with "back"', () => {
it('should redirect to Referrer', function(){ it('should redirect to Referrer', () => {
const ctx = context(); const ctx = context();
ctx.req.headers.referrer = '/login'; ctx.req.headers.referrer = '/login';
ctx.redirect('back'); ctx.redirect('back');
ctx.response.header.location.should.equal('/login'); ctx.response.header.location.should.equal('/login');
}); });
it('should redirect to Referer', function(){ it('should redirect to Referer', () => {
const ctx = context(); const ctx = context();
ctx.req.headers.referer = '/login'; ctx.req.headers.referer = '/login';
ctx.redirect('back'); ctx.redirect('back');
ctx.response.header.location.should.equal('/login'); ctx.response.header.location.should.equal('/login');
}); });
it('should default to alt', function(){ it('should default to alt', () => {
const ctx = context(); const ctx = context();
ctx.redirect('back', '/index.html'); ctx.redirect('back', '/index.html');
ctx.response.header.location.should.equal('/index.html'); ctx.response.header.location.should.equal('/index.html');
}); });
it('should default redirect to /', function(){ it('should default redirect to /', () => {
const ctx = context(); const ctx = context();
ctx.redirect('back'); ctx.redirect('back');
ctx.response.header.location.should.equal('/'); ctx.response.header.location.should.equal('/');
}); });
}); });
describe('when html is accepted', function(){ describe('when html is accepted', () => {
it('should respond with html', function(){ it('should respond with html', () => {
const ctx = context(); const ctx = context();
const url = 'http://google.com'; const url = 'http://google.com';
ctx.header.accept = 'text/html'; ctx.header.accept = 'text/html';
@ -49,7 +49,7 @@ describe('ctx.redirect(url)', function(){
ctx.body.should.equal(`Redirecting to <a href="${url}">${url}</a>.`); ctx.body.should.equal(`Redirecting to <a href="${url}">${url}</a>.`);
}); });
it('should escape the url', function(){ it('should escape the url', () => {
const ctx = context(); const ctx = context();
let url = '<script>'; let url = '<script>';
ctx.header.accept = 'text/html'; ctx.header.accept = 'text/html';
@ -60,8 +60,8 @@ describe('ctx.redirect(url)', function(){
}); });
}); });
describe('when text is accepted', function(){ describe('when text is accepted', () => {
it('should respond with text', function(){ it('should respond with text', () => {
const ctx = context(); const ctx = context();
const url = 'http://google.com'; const url = 'http://google.com';
ctx.header.accept = 'text/plain'; ctx.header.accept = 'text/plain';
@ -70,8 +70,8 @@ describe('ctx.redirect(url)', function(){
}); });
}); });
describe('when status is 301', function(){ describe('when status is 301', () => {
it('should not change the status code', function(){ it('should not change the status code', () => {
const ctx = context(); const ctx = context();
const url = 'http://google.com'; const url = 'http://google.com';
ctx.status = 301; ctx.status = 301;
@ -82,8 +82,8 @@ describe('ctx.redirect(url)', function(){
}); });
}); });
describe('when status is 304', function(){ describe('when status is 304', () => {
it('should change the status code', function(){ it('should change the status code', () => {
const ctx = context(); const ctx = context();
const url = 'http://google.com'; const url = 'http://google.com';
ctx.status = 304; ctx.status = 304;
@ -94,8 +94,8 @@ describe('ctx.redirect(url)', function(){
}); });
}); });
describe('when content-type was present', function(){ describe('when content-type was present', () => {
it('should overwrite content-type', function(){ it('should overwrite content-type', () => {
const ctx = context(); const ctx = context();
ctx.body = {}; ctx.body = {};
const url = 'http://google.com'; const url = 'http://google.com';

View file

@ -3,8 +3,8 @@
const context = require('../helpers/context'); const context = require('../helpers/context');
describe('ctx.remove(name)', function(){ describe('ctx.remove(name)', () => {
it('should remove a field', function(){ it('should remove a field', () => {
const ctx = context(); const ctx = context();
ctx.set('x-foo', 'bar'); ctx.set('x-foo', 'bar');
ctx.remove('x-foo'); ctx.remove('x-foo');

View file

@ -3,28 +3,28 @@
const context = require('../helpers/context'); const context = require('../helpers/context');
describe('ctx.set(name, val)', function(){ describe('ctx.set(name, val)', () => {
it('should set a field value', function(){ it('should set a field value', () => {
const ctx = context(); const ctx = context();
ctx.set('x-foo', 'bar'); ctx.set('x-foo', 'bar');
ctx.response.header['x-foo'].should.equal('bar'); ctx.response.header['x-foo'].should.equal('bar');
}); });
it('should coerce to a string', function(){ it('should coerce to a string', () => {
const ctx = context(); const ctx = context();
ctx.set('x-foo', 5); ctx.set('x-foo', 5);
ctx.response.header['x-foo'].should.equal('5'); ctx.response.header['x-foo'].should.equal('5');
}); });
it('should set a field value of array', function(){ it('should set a field value of array', () => {
const ctx = context(); const ctx = context();
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('ctx.set(object)', function(){ describe('ctx.set(object)', () => {
it('should set multiple fields', function(){ it('should set multiple fields', () => {
const ctx = context(); const ctx = context();
ctx.set({ ctx.set({

View file

@ -4,8 +4,8 @@
const response = require('../helpers/context').response; const response = require('../helpers/context').response;
const Stream = require('stream'); const Stream = require('stream');
describe('res.socket', function(){ describe('res.socket', () => {
it('should return the request socket object', function(){ it('should return the request socket object', () => {
const res = response(); const res = response();
res.socket.should.be.instanceOf(Stream); res.socket.should.be.instanceOf(Stream);
}); });

View file

@ -7,62 +7,56 @@ const statuses = require('statuses');
const assert = require('assert'); const assert = require('assert');
const Koa = require('../..'); const Koa = require('../..');
describe('res.status=', function(){ describe('res.status=', () => {
describe('when a status code', function(){ describe('when a status code', () => {
describe('and valid', function(){ describe('and valid', () => {
it('should set the status', function(){ it('should set the status', () => {
const res = response(); const res = response();
res.status = 403; res.status = 403;
res.status.should.equal(403); res.status.should.equal(403);
}); });
it('should not throw', function(){ it('should not throw', () => {
assert.doesNotThrow(function(){ assert.doesNotThrow(() => {
response().status = 403; response().status = 403;
}); });
}); });
}); });
describe('and invalid', function(){ describe('and invalid', () => {
it('should throw', function(){ it('should throw', () => {
assert.throws(function(){ assert.throws(() => {
response().status = 999; response().status = 999;
}, 'invalid status code: 999'); }, 'invalid status code: 999');
}); });
}); });
describe('and custom status', function(){ describe('and custom status', () => {
before(function(){ before(() => statuses['700'] = 'custom status');
statuses['700'] = 'custom status';
});
it('should set the status', function(){ it('should set the status', () => {
const res = response(); const res = response();
res.status = 700; res.status = 700;
res.status.should.equal(700); res.status.should.equal(700);
}); });
it('should not throw', function(){ it('should not throw', () => {
assert.doesNotThrow(function(){ assert.doesNotThrow(() => response().status = 700);
response().status = 700;
});
}); });
}); });
}); });
describe('when a status string', function(){ describe('when a status string', () => {
it('should throw', function(){ it('should throw', () => {
assert.throws(function(){ assert.throws(() => response().status = 'forbidden', 'status code must be a number');
response().status = 'forbidden';
}, 'status code must be a number');
}); });
}); });
function strip(status){ function strip(status){
it('should strip content related header fields', function(done){ it('should strip content related header fields', done => {
const app = new Koa(); const app = new Koa();
app.use(function(ctx){ app.use(ctx => {
ctx.body = { foo: 'bar' }; ctx.body = { foo: 'bar' };
ctx.set('Content-Type', 'application/json; charset=utf-8'); ctx.set('Content-Type', 'application/json; charset=utf-8');
ctx.set('Content-Length', '15'); ctx.set('Content-Length', '15');
@ -76,7 +70,7 @@ describe('res.status=', function(){
request(app.listen()) request(app.listen())
.get('/') .get('/')
.expect(status) .expect(status)
.end(function(err, res){ .end((err, res) => {
res.should.not.have.header('content-type'); res.should.not.have.header('content-type');
res.should.not.have.header('content-length'); res.should.not.have.header('content-length');
res.should.not.have.header('content-encoding'); res.should.not.have.header('content-encoding');
@ -88,7 +82,7 @@ describe('res.status=', function(){
it('should strip content releated header fields after status set', done => { it('should strip content releated header fields after status set', done => {
const app = new Koa(); const app = new Koa();
app.use(function(ctx){ app.use(ctx => {
ctx.status = status; ctx.status = status;
ctx.body = { foo: 'bar' }; ctx.body = { foo: 'bar' };
ctx.set('Content-Type', 'application/json; charset=utf-8'); ctx.set('Content-Type', 'application/json; charset=utf-8');
@ -99,7 +93,7 @@ describe('res.status=', function(){
request(app.listen()) request(app.listen())
.get('/') .get('/')
.expect(status) .expect(status)
.end(function(err, res){ .end((err, res) => {
res.should.not.have.header('content-type'); res.should.not.have.header('content-type');
res.should.not.have.header('content-length'); res.should.not.have.header('content-length');
res.should.not.have.header('content-encoding'); res.should.not.have.header('content-encoding');
@ -109,15 +103,9 @@ describe('res.status=', function(){
}); });
} }
describe('when 204', function(){ describe('when 204', () => strip(204));
strip(204);
});
describe('when 205', function(){ describe('when 205', () => strip(205));
strip(205);
});
describe('when 304', function(){ describe('when 304', () => strip(304));
strip(304);
});
}); });

View file

@ -4,9 +4,9 @@
const context = require('../helpers/context'); const context = require('../helpers/context');
const assert = require('assert'); const assert = require('assert');
describe('ctx.type=', function(){ describe('ctx.type=', () => {
describe('with a mime', function(){ describe('with a mime', () => {
it('should set the Content-Type', function(){ it('should set the Content-Type', () => {
const ctx = context(); const ctx = context();
ctx.type = 'text/plain'; ctx.type = 'text/plain';
ctx.type.should.equal('text/plain'); ctx.type.should.equal('text/plain');
@ -14,8 +14,8 @@ describe('ctx.type=', function(){
}); });
}); });
describe('with an extension', function(){ describe('with an extension', () => {
it('should lookup the mime', function(){ it('should lookup the mime', () => {
const ctx = context(); const ctx = context();
ctx.type = 'json'; ctx.type = 'json';
ctx.type.should.equal('application/json'); ctx.type.should.equal('application/json');
@ -23,8 +23,8 @@ describe('ctx.type=', function(){
}); });
}); });
describe('without a charset', function(){ describe('without a charset', () => {
it('should default the charset', function(){ it('should default the charset', () => {
const ctx = context(); const ctx = context();
ctx.type = 'text/html'; ctx.type = 'text/html';
ctx.type.should.equal('text/html'); ctx.type.should.equal('text/html');
@ -32,8 +32,8 @@ describe('ctx.type=', function(){
}); });
}); });
describe('with a charset', function(){ describe('with a charset', () => {
it('should not default the charset', function(){ it('should not default the charset', () => {
const ctx = context(); const ctx = context();
ctx.type = 'text/html; charset=foo'; ctx.type = 'text/html; charset=foo';
ctx.type.should.equal('text/html'); ctx.type.should.equal('text/html');
@ -41,8 +41,8 @@ describe('ctx.type=', function(){
}); });
}); });
describe('with an unknown extension', function(){ describe('with an unknown extension', () => {
it('should not set a content-type', function(){ it('should not set a content-type', () => {
const ctx = context(); const ctx = context();
ctx.type = 'asdf'; ctx.type = 'asdf';
assert(!ctx.type); assert(!ctx.type);
@ -51,16 +51,16 @@ describe('ctx.type=', function(){
}); });
}); });
describe('ctx.type', function(){ describe('ctx.type', () => {
describe('with no Content-Type', function(){ describe('with no Content-Type', () => {
it('should return ""', function(){ it('should return ""', () => {
const ctx = context(); const ctx = context();
assert(!ctx.type); assert(!ctx.type);
}); });
}); });
describe('with a Content-Type', function(){ describe('with a Content-Type', () => {
it('should return the mime', function(){ it('should return the mime', () => {
const ctx = context(); const ctx = context();
ctx.type = 'json'; ctx.type = 'json';
ctx.type.should.equal('application/json'); ctx.type.should.equal('application/json');

View file

@ -3,17 +3,17 @@
const context = require('../helpers/context'); const context = require('../helpers/context');
describe('ctx.vary(field)', function(){ describe('ctx.vary(field)', () => {
describe('when Vary is not set', function(){ describe('when Vary is not set', () => {
it('should set it', function(){ it('should set it', () => {
const ctx = context(); const ctx = context();
ctx.vary('Accept'); ctx.vary('Accept');
ctx.response.header.vary.should.equal('Accept'); ctx.response.header.vary.should.equal('Accept');
}); });
}); });
describe('when Vary is set', function(){ describe('when Vary is set', () => {
it('should append', function(){ it('should append', () => {
const ctx = context(); const ctx = context();
ctx.vary('Accept'); ctx.vary('Accept');
ctx.vary('Accept-Encoding'); ctx.vary('Accept-Encoding');
@ -21,8 +21,8 @@ describe('ctx.vary(field)', function(){
}); });
}); });
describe('when Vary already contains the value', function(){ describe('when Vary already contains the value', () => {
it('should not append', function(){ it('should not append', () => {
const ctx = context(); const ctx = context();
ctx.vary('Accept'); ctx.vary('Accept');
ctx.vary('Accept-Encoding'); ctx.vary('Accept-Encoding');

View file

@ -3,14 +3,14 @@
const response = require('../helpers/context').response; const response = require('../helpers/context').response;
describe('res.writable', function(){ describe('res.writable', () => {
it('should return the request is writable', function(){ it('should return the request is writable', () => {
const res = response(); const res = response();
res.writable.should.be.ok; res.writable.should.be.ok;
}); });
describe('when res.socket not present', function(){ describe('when res.socket not present', () => {
it('should return the request is not writable', function(){ it('should return the request is not writable', () => {
const res = response(); const res = response();
res.res.socket = null; res.res.socket = null;
res.writable.should.not.be.ok; res.writable.should.not.be.ok;