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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -4,28 +4,28 @@
const request = require('supertest');
const Koa = require('../..');
describe('app.use(fn)', function(){
it('should compose middleware', function(done){
describe('app.use(fn)', () => {
it('should compose middleware', done => {
const app = new Koa();
const calls = [];
app.use(function(ctx, next){
app.use((ctx, next) => {
calls.push(1);
return next().then(function(){
return next().then(() => {
calls.push(6);
});
});
app.use(function(ctx, next){
app.use((ctx, next) => {
calls.push(2);
return next().then(function(){
return next().then(() => {
calls.push(5);
});
});
app.use(function(ctx, next){
app.use((ctx, next) => {
calls.push(3);
return next().then(function(){
return next().then(() => {
calls.push(4);
});
});
@ -35,7 +35,7 @@ describe('app.use(fn)', function(){
request(server)
.get('/')
.expect(404)
.end(function(err){
.end(err => {
if (err) return done(err);
calls.should.eql([1, 2, 3, 4, 5, 6]);
done();
@ -43,12 +43,10 @@ describe('app.use(fn)', function(){
});
// 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();
app.use(ctx => {
ctx.throw('Not Found', 404);
});
app.use(ctx => ctx.throw('Not Found', 404));
request(app.listen())
.get('/')
@ -56,16 +54,23 @@ describe('app.use(fn)', function(){
.end(done);
});
it('should throw error for non function', function(done){
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();
});
it('should throw error for generator', function(){
it('should throw error for generator', () => {
const app = new Koa();
(() => 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 assert = require('assert');
describe('ctx.assert(value, status)', function(){
it('should throw an error', function(){
describe('ctx.assert(value, status)', () => {
it('should throw an error', () => {
const ctx = context();
try {

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -3,10 +3,10 @@
const context = require('../helpers/context');
describe('ctx.acceptsCharsets()', function(){
describe('with no arguments', function(){
describe('when Accept-Charset is populated', function(){
it('should return accepted types', function(){
describe('ctx.acceptsCharsets()', () => {
describe('with no arguments', () => {
describe('when Accept-Charset is populated', () => {
it('should return accepted types', () => {
const ctx = context();
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']);
@ -14,18 +14,18 @@ describe('ctx.acceptsCharsets()', function(){
});
});
describe('with multiple arguments', function(){
describe('when Accept-Charset is populated', function(){
describe('if any types match', function(){
it('should return the best fit', function(){
describe('with multiple arguments', () => {
describe('when Accept-Charset is populated', () => {
describe('if any types match', () => {
it('should return the best fit', () => {
const ctx = context();
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');
});
});
describe('if no types match', function(){
it('should return false', function(){
describe('if no types match', () => {
it('should return false', () => {
const ctx = context();
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;
@ -33,16 +33,16 @@ describe('ctx.acceptsCharsets()', function(){
});
});
describe('when Accept-Charset is not populated', function(){
it('should return the first type', function(){
describe('when Accept-Charset is not populated', () => {
it('should return the first type', () => {
const ctx = context();
ctx.acceptsCharsets('utf-7', 'utf-8').should.equal('utf-7');
});
});
});
describe('with an array', function(){
it('should return the best fit', function(){
describe('with an array', () => {
it('should return the best fit', () => {
const ctx = context();
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');

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -4,9 +4,9 @@
const request = require('../helpers/context').request;
const assert = require('assert');
describe('req.inspect()', function(){
describe('with no request.req present', function(){
it('should return null', function(){
describe('req.inspect()', () => {
describe('with no request.req present', () => {
it('should return null', () => {
const req = request();
req.method = 'GET';
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();
req.method = 'GET';
req.url = 'example.com';

View file

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

View file

@ -3,10 +3,10 @@
const request = require('../helpers/context').request;
describe('req.ips', function(){
describe('when X-Forwarded-For is present', function(){
describe('and proxy is not trusted', function(){
it('should be ignored', function(){
describe('req.ips', () => {
describe('when X-Forwarded-For is present', () => {
describe('and proxy is not trusted', () => {
it('should be ignored', () => {
const req = request();
req.app.proxy = false;
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(){
it('should be used', function(){
describe('and proxy is trusted', () => {
it('should be used', () => {
const req = request();
req.app.proxy = true;
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 assert = require('assert');
describe('ctx.is(type)', function(){
it('should ignore params', function(){
describe('ctx.is(type)', () => {
it('should ignore params', () => {
const ctx = context();
ctx.header['content-type'] = 'text/html; charset=utf-8';
ctx.header['transfer-encoding'] = 'chunked';
@ -13,8 +13,8 @@ describe('ctx.is(type)', function(){
ctx.is('text/*').should.equal('text/html');
});
describe('when no body is given', function(){
it('should return null', function(){
describe('when no body is given', () => {
it('should return null', () => {
const ctx = context();
assert(null == ctx.is());
@ -23,8 +23,8 @@ describe('ctx.is(type)', function(){
});
});
describe('when no content type is given', function(){
it('should return false', function(){
describe('when no content type is given', () => {
it('should return false', () => {
const ctx = context();
ctx.header['transfer-encoding'] = 'chunked';
@ -34,8 +34,8 @@ describe('ctx.is(type)', function(){
});
});
describe('give no types', function(){
it('should return the mime type', function(){
describe('give no types', () => {
it('should return the mime type', () => {
const ctx = context();
ctx.header['content-type'] = 'image/png';
ctx.header['transfer-encoding'] = 'chunked';
@ -44,8 +44,8 @@ describe('ctx.is(type)', function(){
});
});
describe('given one type', function(){
it('should return the type or false', function(){
describe('given one type', () => {
it('should return the type or false', () => {
const ctx = context();
ctx.header['content-type'] = 'image/png';
ctx.header['transfer-encoding'] = 'chunked';
@ -64,8 +64,8 @@ describe('ctx.is(type)', function(){
});
});
describe('given multiple types', function(){
it('should return the first match or false', function(){
describe('given multiple types', () => {
it('should return the first match or false', () => {
const ctx = context();
ctx.header['content-type'] = 'image/png';
ctx.header['transfer-encoding'] = 'chunked';
@ -89,8 +89,8 @@ describe('ctx.is(type)', function(){
});
});
describe('when Content-Type: application/x-www-form-urlencoded', function(){
it('should match "urlencoded"', function(){
describe('when Content-Type: application/x-www-form-urlencoded', () => {
it('should match "urlencoded"', () => {
const ctx = context();
ctx.header['content-type'] = 'application/x-www-form-urlencoded';
ctx.header['transfer-encoding'] = 'chunked';

View file

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

View file

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

View file

@ -3,16 +3,16 @@
const context = require('../helpers/context');
describe('ctx.path', function(){
it('should return the pathname', function(){
describe('ctx.path', () => {
it('should return the pathname', () => {
const ctx = context();
ctx.url = '/login?next=/dashboard';
ctx.path.should.equal('/login');
});
});
describe('ctx.path=', function(){
it('should set the pathname', function(){
describe('ctx.path=', () => {
it('should set the pathname', () => {
const ctx = context();
ctx.url = '/login?next=/dashboard';
@ -21,7 +21,7 @@ describe('ctx.path=', function(){
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' });
ctx.path = '/logout';
ctx.url.should.equal('/logout');

View file

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

View file

@ -3,14 +3,14 @@
const context = require('../helpers/context');
describe('ctx.query', function(){
describe('when missing', function(){
it('should return an empty object', function(){
describe('ctx.query', () => {
describe('when missing', () => {
it('should return an empty object', () => {
const ctx = context({ url: '/' });
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: '/' });
ctx.query.a = '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' });
ctx.query.page.should.equal('2');
});
});
describe('ctx.query=', function(){
it('should stringify and replace the querystring and search', function(){
describe('ctx.query=', () => {
it('should stringify and replace the querystring and search', () => {
const ctx = context({ url: '/store/shoes' });
ctx.query = { 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');
});
it('should change .url but not .originalUrl', function(){
it('should change .url but not .originalUrl', () => {
const ctx = context({ url: '/store/shoes' });
ctx.query = { page: 2 };
ctx.url.should.equal('/store/shoes?page=2');

View file

@ -3,14 +3,14 @@
const context = require('../helpers/context');
describe('ctx.querystring', function(){
it('should return the querystring', function(){
describe('ctx.querystring', () => {
it('should return the querystring', () => {
const ctx = context({ url: '/store/shoes?page=2&color=blue' });
ctx.querystring.should.equal('page=2&color=blue');
});
describe('when ctx.req not present', function(){
it('should return an empty string', function(){
describe('when ctx.req not present', () => {
it('should return an empty string', () => {
const ctx = context();
ctx.request.req = null;
ctx.querystring.should.equal('');
@ -18,15 +18,15 @@ describe('ctx.querystring', function(){
});
});
describe('ctx.querystring=', function(){
it('should replace the querystring', function(){
describe('ctx.querystring=', () => {
it('should replace the querystring', () => {
const ctx = context({ url: '/store/shoes' });
ctx.querystring = 'page=2&color=blue';
ctx.url.should.equal('/store/shoes?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' });
ctx.querystring = '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' });
ctx.querystring = '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');
describe('ctx.search=', function(){
it('should replace the search', function(){
describe('ctx.search=', () => {
it('should replace the search', () => {
const ctx = context({ url: '/store/shoes' });
ctx.search = '?page=2&color=blue';
ctx.url.should.equal('/store/shoes?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' });
ctx.search = '?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' });
ctx.search = '?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');
});
describe('when missing', function(){
it('should return ""', function(){
describe('when missing', () => {
it('should return ""', () => {
const ctx = context({ url: '/store/shoes' });
ctx.search.should.equal('');
});

View file

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

View file

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

View file

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

View file

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

View file

@ -3,15 +3,15 @@
const context = require('../helpers/context');
describe('ctx.append(name, val)', function(){
it('should append multiple headers', function(){
describe('ctx.append(name, val)', () => {
it('should append multiple headers', () => {
const ctx = context();
ctx.append('x-foo', 'bar1');
ctx.append('x-foo', '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();
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']);
});
it('should get reset by res.set(field, val)', function(){
it('should get reset by res.set(field, val)', () => {
const ctx = context();
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/>');
});
it('should work with res.set(field, val) first', function(){
it('should work with res.set(field, val) first', () => {
const ctx = context();
ctx.set('Link', '<http://localhost/>');

View file

@ -5,9 +5,9 @@ const context = require('../helpers/context');
const request = require('supertest');
const Koa = require('../..');
describe('ctx.attachment([filename])', function(){
describe('when given a filename', function(){
it('should set the filename param', function(){
describe('ctx.attachment([filename])', () => {
describe('when given a filename', () => {
it('should set the filename param', () => {
const ctx = context();
ctx.attachment('path/to/tobi.png');
const str = 'attachment; filename="tobi.png"';
@ -15,26 +15,26 @@ describe('ctx.attachment([filename])', function(){
});
});
describe('when omitting filename', function(){
it('should not set filename param', function(){
describe('when omitting filename', () => {
it('should not set filename param', () => {
const ctx = context();
ctx.attachment();
ctx.response.header['content-disposition'].should.equal('attachment');
});
});
describe('when given a no-ascii filename', function(){
it('should set the encodeURI filename param', function(){
describe('when given a no-ascii filename', () => {
it('should set the encodeURI filename param', () => {
const ctx = context();
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';
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();
app.use(function(ctx, next){
app.use((ctx, next) => {
ctx.attachment('path/to/include-no-ascii-char-中文名-ok.json');
ctx.body = {foo: 'bar'};
});

View file

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

View file

@ -3,28 +3,28 @@
const response = require('../helpers/context').response;
describe('res.etag=', function(){
it('should not modify an etag with quotes', function(){
describe('res.etag=', () => {
it('should not modify an etag with quotes', () => {
const res = response();
res.etag = '"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();
res.etag = '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();
res.etag = 'asdf';
res.header.etag.should.equal('"asdf"');
});
});
describe('res.etag', function(){
it('should return etag', function(){
describe('res.etag', () => {
it('should return etag', () => {
const res = response();
res.etag = '"asdf"';
res.etag.should.equal('"asdf"');

View file

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

View file

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

View file

@ -4,9 +4,9 @@
const response = require('../helpers/context').response;
const assert = require('assert');
describe('res.inspect()', function(){
describe('with no response.res present', function(){
it('should return null', function(){
describe('res.inspect()', () => {
describe('with no response.res present', () => {
it('should return null', () => {
const res = response();
res.body = 'hello';
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();
res.body = 'hello';

View file

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

View file

@ -3,22 +3,22 @@
const response = require('../helpers/context').response;
describe('res.lastModified', function(){
it('should set the header as a UTCString', function(){
describe('res.lastModified', () => {
it('should set the header as a UTCString', () => {
const res = response();
const date = new Date();
res.lastModified = date;
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 date = new Date();
res.lastModified = date.toString();
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.
const res = response();
const date = new Date();
@ -26,8 +26,8 @@ describe('res.lastModified', function(){
(res.lastModified.getTime() / 1000).should.equal(Math.floor(date.getTime() / 1000));
});
describe('when lastModified not set', function(){
it('should get undefined', function(){
describe('when lastModified not set', () => {
it('should get undefined', () => {
const res = response();
(res.lastModified === undefined).should.be.ok;
});

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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