refactor to use ES6 const

change var to const for static require()'d modules

make constant var references in app use const keyword

refactor context to use es6 constants

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

use const in response object for static refs

make context tests use es6 constants

experimental unit tests -> const

use const for static references in unit test over req

use const for static refs in res tests

update app tests to use const for static refs

make the context test use es6 constants for static refs

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

use const's for immutable refs in benchmarks

ensure all JS files have blank newline at top

add newline to bottom of file where missing

add a webchat freenode link to irc channel

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

app.silent option to turn off err logging

keep test env logging for backwards-compat
master
Tejas Manohar 2015-10-05 13:23:47 -05:00 committed by Jonathan Ong
parent 07619f65c3
commit 9f27c1c414
64 changed files with 555 additions and 555 deletions

View File

@ -47,15 +47,15 @@ $ npm install koa
## Example ## Example
```js ```js
var koa = require('koa'); const koa = require('koa');
var app = koa(); const app = koa();
// logger // logger
app.use(function *(next){ app.use(function *(next){
var start = new Date; const start = new Date;
yield next; yield next;
var ms = new Date - start; const ms = new Date - start;
console.log('%s %s - %s', this.method, this.url, ms); console.log('%s %s - %s', this.method, this.url, ms);
}); });

View File

@ -1,15 +1,15 @@
'use strict'; 'use strict';
var http = require('http'); const http = require('http');
var koa = require('../..'); const koa = require('../..');
var app = koa(); const app = koa();
app.experimental = true; app.experimental = true;
// number of middleware // number of middleware
var n = parseInt(process.env.MW || '1', 10); const n = parseInt(process.env.MW || '1', 10);
console.log(' %s async middleware', n); console.log(' %s async middleware', n);
while (n--) { while (n--) {
@ -18,7 +18,7 @@ while (n--) {
}); });
} }
var body = new Buffer('Hello World'); const body = new Buffer('Hello World');
app.use(async function (next){ app.use(async function (next){
await next; await next;

View File

@ -1,13 +1,13 @@
'use strict'; 'use strict';
var http = require('http'); const http = require('http');
var koa = require('..'); const koa = require('..');
var app = koa(); const app = koa();
// number of middleware // number of middleware
var n = parseInt(process.env.MW || '1', 10); const n = parseInt(process.env.MW || '1', 10);
console.log(' %s middleware', n); console.log(' %s middleware', n);
while (n--) { while (n--) {
@ -16,7 +16,7 @@ while (n--) {
}); });
} }
var body = new Buffer('Hello World'); const body = new Buffer('Hello World');
app.use(function *(next){ app.use(function *(next){
yield *next; yield *next;

View File

@ -5,29 +5,29 @@
* Module dependencies. * Module dependencies.
*/ */
var debug = require('debug')('koa:application'); const debug = require('debug')('koa:application');
var Emitter = require('events').EventEmitter; const Emitter = require('events').EventEmitter;
var compose_es7 = require('composition'); const compose_es7 = require('composition');
var onFinished = require('on-finished'); const onFinished = require('on-finished');
var response = require('./response'); const response = require('./response');
var compose = require('koa-compose'); const compose = require('koa-compose');
var isJSON = require('koa-is-json'); const isJSON = require('koa-is-json');
var context = require('./context'); const context = require('./context');
var request = require('./request'); const request = require('./request');
var statuses = require('statuses'); const statuses = require('statuses');
var Cookies = require('cookies'); const Cookies = require('cookies');
var accepts = require('accepts'); const accepts = require('accepts');
var assert = require('assert'); const assert = require('assert');
var Stream = require('stream'); const Stream = require('stream');
var http = require('http'); const http = require('http');
var only = require('only'); const only = require('only');
var co = require('co'); const co = require('co');
/** /**
* Application prototype. * Application prototype.
*/ */
var app = Application.prototype; const app = Application.prototype;
/** /**
* Expose `Application`. * Expose `Application`.
@ -69,7 +69,7 @@ Object.setPrototypeOf(Application.prototype, Emitter.prototype);
app.listen = function(){ app.listen = function(){
debug('listen'); debug('listen');
var server = http.createServer(this.callback()); const server = http.createServer(this.callback());
return server.listen.apply(server, arguments); return server.listen.apply(server, arguments);
}; };
@ -116,16 +116,16 @@ app.use = function(fn){
*/ */
app.callback = function(){ app.callback = function(){
var fn = this.experimental const fn = this.experimental
? compose_es7(this.middleware) ? compose_es7(this.middleware)
: co.wrap(compose(this.middleware)); : co.wrap(compose(this.middleware));
var self = this; const self = this;
if (!this.listeners('error').length) this.on('error', this.onerror); if (!this.listeners('error').length) this.on('error', this.onerror);
return function(req, res){ return function(req, res){
res.statusCode = 404; res.statusCode = 404;
var ctx = self.createContext(req, res); const ctx = self.createContext(req, res);
onFinished(res, ctx.onerror); onFinished(res, ctx.onerror);
fn.call(ctx).then(function () { fn.call(ctx).then(function () {
respond.call(ctx); respond.call(ctx);
@ -140,9 +140,9 @@ app.callback = function(){
*/ */
app.createContext = function(req, res){ app.createContext = function(req, res){
var context = Object.create(this.context); const context = Object.create(this.context);
var request = context.request = Object.create(this.request); const request = context.request = Object.create(this.request);
var response = context.response = Object.create(this.response); const response = context.response = Object.create(this.response);
context.app = request.app = response.app = this; context.app = request.app = response.app = this;
context.req = request.req = response.req = req; context.req = request.req = response.req = req;
context.res = request.res = response.res = res; context.res = request.res = response.res = res;
@ -172,7 +172,7 @@ app.onerror = function(err){
// DEPRECATE env-specific logging in v2 // DEPRECATE env-specific logging in v2
if ('test' == this.env) return; if ('test' == this.env) return;
var msg = err.stack || err.toString(); const msg = err.stack || err.toString();
console.error(); console.error();
console.error(msg.replace(/^/gm, ' ')); console.error(msg.replace(/^/gm, ' '));
console.error(); console.error();
@ -186,11 +186,11 @@ function respond() {
// allow bypassing koa // allow bypassing koa
if (false === this.respond) return; if (false === this.respond) return;
var res = this.res; const res = this.res;
if (res.headersSent || !this.writable) return; if (res.headersSent || !this.writable) return;
var body = this.body; var body = this.body;
var code = this.status; const code = this.status;
// ignore body // ignore body
if (statuses.empty[code]) { if (statuses.empty[code]) {

View File

@ -5,16 +5,16 @@
* Module dependencies. * Module dependencies.
*/ */
var createError = require('http-errors'); const createError = require('http-errors');
var httpAssert = require('http-assert'); const httpAssert = require('http-assert');
var delegate = require('delegates'); const delegate = require('delegates');
var statuses = require('statuses'); const statuses = require('statuses');
/** /**
* Context prototype. * Context prototype.
*/ */
var proto = module.exports = { const proto = module.exports = {
/** /**
* util.inspect() implementation, which * util.inspect() implementation, which
@ -130,8 +130,8 @@ var proto = module.exports = {
if ('number' != typeof err.status || !statuses[err.status]) err.status = 500; if ('number' != typeof err.status || !statuses[err.status]) err.status = 500;
// respond // respond
var code = statuses[err.status]; const code = statuses[err.status];
var msg = err.expose ? err.message : code; const msg = err.expose ? err.message : code;
this.status = err.status; this.status = err.status;
this.length = Buffer.byteLength(msg); this.length = Buffer.byteLength(msg);
this.res.end(msg); this.res.end(msg);

View File

@ -5,12 +5,12 @@
* Module dependencies. * Module dependencies.
*/ */
var contentType = require('content-type'); const contentType = require('content-type');
var stringify = require('url').format; const stringify = require('url').format;
var parse = require('parseurl'); const parse = require('parseurl');
var qs = require('querystring'); const qs = require('querystring');
var typeis = require('type-is'); const typeis = require('type-is');
var fresh = require('fresh'); const fresh = require('fresh');
/** /**
* Prototype. * Prototype.
@ -128,7 +128,7 @@ module.exports = {
*/ */
set path(path) { set path(path) {
var url = parse(this.req); const url = parse(this.req);
url.pathname = path; url.pathname = path;
url.path = null; url.path = null;
@ -143,8 +143,8 @@ module.exports = {
*/ */
get query() { get query() {
var str = this.querystring; const str = this.querystring;
var c = this._querycache = this._querycache || {}; const c = this._querycache = this._querycache || {};
return c[str] || (c[str] = qs.parse(str)); return c[str] || (c[str] = qs.parse(str));
}, },
@ -179,7 +179,7 @@ module.exports = {
*/ */
set querystring(str) { set querystring(str) {
var url = parse(this.req); const url = parse(this.req);
url.search = str; url.search = str;
url.path = null; url.path = null;
@ -221,7 +221,7 @@ module.exports = {
*/ */
get host() { get host() {
var proxy = this.app.proxy; const proxy = this.app.proxy;
var host = proxy && this.get('X-Forwarded-Host'); var host = proxy && this.get('X-Forwarded-Host');
host = host || this.get('Host'); host = host || this.get('Host');
if (!host) return ''; if (!host) return '';
@ -238,7 +238,7 @@ module.exports = {
*/ */
get hostname() { get hostname() {
var host = this.host; const host = this.host;
if (!host) return ''; if (!host) return '';
return host.split(':')[0]; return host.split(':')[0];
}, },
@ -253,8 +253,8 @@ module.exports = {
*/ */
get fresh() { get fresh() {
var method = this.method; const method = this.method;
var s = this.ctx.status; const s = this.ctx.status;
// GET or HEAD for weak freshness validation only // GET or HEAD for weak freshness validation only
if ('GET' != method && 'HEAD' != method) return false; if ('GET' != method && 'HEAD' != method) return false;
@ -288,7 +288,7 @@ module.exports = {
*/ */
get idempotent() { get idempotent() {
var methods = ['GET', 'HEAD', 'PUT', 'DELETE', 'OPTIONS', 'TRACE']; const methods = ['GET', 'HEAD', 'PUT', 'DELETE', 'OPTIONS', 'TRACE'];
return !!~methods.indexOf(this.method); return !!~methods.indexOf(this.method);
}, },
@ -312,7 +312,7 @@ module.exports = {
*/ */
get charset() { get charset() {
var type = this.get('Content-Type'); const type = this.get('Content-Type');
if (!type) return ''; if (!type) return '';
return contentType.parse(type).parameters.charset || ''; return contentType.parse(type).parameters.charset || '';
@ -326,7 +326,7 @@ module.exports = {
*/ */
get length() { get length() {
var len = this.get('Content-Length'); const len = this.get('Content-Length');
if (len == '') return; if (len == '') return;
return ~~len; return ~~len;
}, },
@ -344,10 +344,10 @@ module.exports = {
*/ */
get protocol() { get protocol() {
var proxy = this.app.proxy; const proxy = this.app.proxy;
if (this.socket.encrypted) return 'https'; if (this.socket.encrypted) return 'https';
if (!proxy) return 'http'; if (!proxy) return 'http';
var proto = this.get('X-Forwarded-Proto') || 'http'; const proto = this.get('X-Forwarded-Proto') || 'http';
return proto.split(/\s*,\s*/)[0]; return proto.split(/\s*,\s*/)[0];
}, },
@ -390,8 +390,8 @@ module.exports = {
*/ */
get ips() { get ips() {
var proxy = this.app.proxy; const proxy = this.app.proxy;
var val = this.get('X-Forwarded-For'); const val = this.get('X-Forwarded-For');
return proxy && val return proxy && val
? val.split(/\s*,\s*/) ? val.split(/\s*,\s*/)
: []; : [];
@ -413,7 +413,7 @@ module.exports = {
*/ */
get subdomains() { get subdomains() {
var offset = this.app.subdomainOffset; const offset = this.app.subdomainOffset;
return (this.host || '') return (this.host || '')
.split('.') .split('.')
.reverse() .reverse()
@ -557,7 +557,7 @@ module.exports = {
*/ */
get type() { get type() {
var type = this.get('Content-Type'); const type = this.get('Content-Type');
if (!type) return ''; if (!type) return '';
return type.split(';')[0]; return type.split(';')[0];
}, },
@ -585,7 +585,7 @@ module.exports = {
*/ */
get: function(field){ get: function(field){
var req = this.req; const req = this.req;
switch (field = field.toLowerCase()) { switch (field = field.toLowerCase()) {
case 'referer': case 'referer':
case 'referrer': case 'referrer':

View File

@ -5,19 +5,19 @@
* Module dependencies. * Module dependencies.
*/ */
var contentDisposition = require('content-disposition'); const contentDisposition = require('content-disposition');
var ensureErrorHandler = require('error-inject'); const ensureErrorHandler = require('error-inject');
var getType = require('mime-types').contentType; const getType = require('mime-types').contentType;
var onFinish = require('on-finished'); const onFinish = require('on-finished');
var isJSON = require('koa-is-json'); const isJSON = require('koa-is-json');
var escape = require('escape-html'); const escape = require('escape-html');
var typeis = require('type-is').is; const typeis = require('type-is').is;
var statuses = require('statuses'); const statuses = require('statuses');
var destroy = require('destroy'); const destroy = require('destroy');
var assert = require('assert'); const assert = require('assert');
var path = require('path'); const path = require('path');
var vary = require('vary'); const vary = require('vary');
var extname = path.extname; const extname = path.extname;
/** /**
* Prototype. * Prototype.
@ -128,7 +128,7 @@ module.exports = {
*/ */
set body(val) { set body(val) {
var original = this._body; const original = this._body;
this._body = val; this._body = val;
// no content // no content
@ -144,7 +144,7 @@ module.exports = {
if (!this._explicitStatus) this.status = 200; if (!this._explicitStatus) this.status = 200;
// set the content-type only if not yet set // set the content-type only if not yet set
var setType = !this.header['content-type']; const setType = !this.header['content-type'];
// string // string
if ('string' == typeof val) { if ('string' == typeof val) {
@ -196,8 +196,8 @@ module.exports = {
*/ */
get length() { get length() {
var len = this.header['content-length']; const len = this.header['content-length'];
var body = this.body; const body = this.body;
if (null == len) { if (null == len) {
if (!body) return; if (!body) return;
@ -327,7 +327,7 @@ module.exports = {
*/ */
get lastModified() { get lastModified() {
var date = this.get('last-modified'); const date = this.get('last-modified');
if (date) return new Date(date); if (date) return new Date(date);
}, },
@ -368,7 +368,7 @@ module.exports = {
*/ */
get type() { get type() {
var type = this.get('Content-Type'); const type = this.get('Content-Type');
if (!type) return ''; if (!type) return '';
return type.split(';')[0]; return type.split(';')[0];
}, },
@ -383,7 +383,7 @@ module.exports = {
*/ */
is: function(types){ is: function(types){
var type = this.type; const type = this.type;
if (!types) return type || false; if (!types) return type || false;
if (!Array.isArray(types)) types = [].slice.call(arguments); if (!Array.isArray(types)) types = [].slice.call(arguments);
return typeis(type, types); return typeis(type, types);
@ -451,7 +451,7 @@ module.exports = {
*/ */
append: function(field, val){ append: function(field, val){
var prev = this.get(field); const prev = this.get(field);
if (prev) { if (prev) {
val = Array.isArray(prev) val = Array.isArray(prev)
@ -483,7 +483,7 @@ module.exports = {
*/ */
get writable() { get writable() {
var socket = this.res.socket; const socket = this.res.socket;
if (!socket) return false; if (!socket) return false;
return socket.writable; return socket.writable;
}, },
@ -497,7 +497,7 @@ module.exports = {
inspect: function(){ inspect: function(){
if (!this.res) return; if (!this.res) return;
var o = this.toJSON(); const o = this.toJSON();
o.body = this.body; o.body = this.body;
return o; return o;
}, },

View File

@ -1,18 +1,18 @@
'use strict'; 'use strict';
var stderr = require('test-console').stderr; const stderr = require('test-console').stderr;
var request = require('supertest'); const request = require('supertest');
var statuses = require('statuses'); const statuses = require('statuses');
var assert = require('assert'); const assert = require('assert');
var http = require('http'); const http = require('http');
var koa = require('..'); const koa = require('..');
var fs = require('fs'); const fs = require('fs');
var AssertionError = assert.AssertionError; const AssertionError = assert.AssertionError;
describe('app', function(){ describe('app', function(){
it('should handle socket errors', function(done){ it('should handle socket errors', function(done){
var app = koa(); const app = koa();
app.use(function *(next){ app.use(function *(next){
// triggers this.socket.writable == false // triggers this.socket.writable == false
@ -30,7 +30,7 @@ describe('app', function(){
}) })
it('should not .writeHead when !socket.writable', function(done){ it('should not .writeHead when !socket.writable', function(done){
var app = koa(); const app = koa();
app.use(function *(next){ app.use(function *(next){
// set .writable to false // set .writable to false
@ -52,9 +52,9 @@ describe('app', function(){
}) })
it('should set development env when NODE_ENV missing', function(){ it('should set development env when NODE_ENV missing', function(){
var NODE_ENV = process.env.NODE_ENV; const NODE_ENV = process.env.NODE_ENV;
process.env.NODE_ENV = ''; process.env.NODE_ENV = '';
var app = koa(); const app = koa();
process.env.NODE_ENV = NODE_ENV; process.env.NODE_ENV = NODE_ENV;
assert.equal(app.env, 'development'); assert.equal(app.env, 'development');
}) })
@ -62,8 +62,8 @@ describe('app', function(){
describe('app.toJSON()', function(){ describe('app.toJSON()', function(){
it('should work', function(){ it('should work', function(){
var app = koa(); const app = koa();
var obj = app.toJSON(); const obj = app.toJSON();
obj.should.eql({ obj.should.eql({
subdomainOffset: 2, subdomainOffset: 2,
@ -74,16 +74,16 @@ describe('app.toJSON()', function(){
describe('app.inspect()', function(){ describe('app.inspect()', function(){
it('should work', function(){ it('should work', function(){
var app = koa(); const app = koa();
var util = require('util'); const util = require('util');
var str = util.inspect(app); const str = util.inspect(app);
}) })
}) })
describe('app.use(fn)', function(){ describe('app.use(fn)', function(){
it('should compose middleware', function(done){ it('should compose middleware', function(done){
var app = koa(); const app = koa();
var calls = []; const calls = [];
app.use(function *(next){ app.use(function *(next){
calls.push(1); calls.push(1);
@ -103,7 +103,7 @@ describe('app.use(fn)', function(){
calls.push(4); calls.push(4);
}); });
var server = app.listen(); const server = app.listen();
request(server) request(server)
.get('/') .get('/')
@ -116,7 +116,7 @@ describe('app.use(fn)', function(){
}) })
it('should error when a non-generator function is passed', function(){ it('should error when a non-generator function is passed', function(){
var app = koa(); const app = koa();
try { try {
app.use(function(){}); app.use(function(){});
@ -126,7 +126,7 @@ describe('app.use(fn)', function(){
}) })
it('should not error when a non-generator function is passed when .experimental=true', function(){ it('should not error when a non-generator function is passed when .experimental=true', function(){
var app = koa(); const app = koa();
app.experimental = true; app.experimental = true;
app.use(function(){}); app.use(function(){});
}) })
@ -134,7 +134,7 @@ describe('app.use(fn)', function(){
describe('app.onerror(err)', function(){ describe('app.onerror(err)', function(){
it('should throw an error if a non-error is given', function(done){ it('should throw an error if a non-error is given', function(done){
var app = koa(); const app = koa();
try { try {
app.onerror('foo'); app.onerror('foo');
@ -149,12 +149,12 @@ describe('app.onerror(err)', function(){
}) })
it('should do nothing if status is 404', function(done){ it('should do nothing if status is 404', function(done){
var app = koa(); const app = koa();
var err = new Error(); const err = new Error();
err.status = 404; err.status = 404;
var output = stderr.inspectSync(function() { const output = stderr.inspectSync(function() {
app.onerror(err); app.onerror(err);
}); });
@ -164,11 +164,11 @@ describe('app.onerror(err)', function(){
}) })
it('should do nothing if .silent', function(done){ it('should do nothing if .silent', function(done){
var app = koa(); const app = koa();
app.silent = true; app.silent = true;
var err = new Error(); const err = new Error();
var output = stderr.inspectSync(function() { const output = stderr.inspectSync(function() {
app.onerror(err); app.onerror(err);
}); });
@ -178,13 +178,13 @@ describe('app.onerror(err)', function(){
}) })
it('should log the error to stderr', function(done){ it('should log the error to stderr', function(done){
var app = koa(); const app = koa();
app.env = 'dev'; app.env = 'dev';
var err = new Error(); const err = new Error();
err.stack = 'Foo'; err.stack = 'Foo';
var output = stderr.inspectSync(function() { const output = stderr.inspectSync(function() {
app.onerror(err); app.onerror(err);
}); });
@ -194,13 +194,13 @@ describe('app.onerror(err)', function(){
}) })
it('should use err.toString() instad of err.stack', function(done){ it('should use err.toString() instad of err.stack', function(done){
var app = koa(); const app = koa();
app.env = 'dev'; app.env = 'dev';
var err = new Error('mock stack null'); const err = new Error('mock stack null');
err.stack = null; err.stack = null;
var output = stderr.inspectSync(function() { const output = stderr.inspectSync(function() {
app.onerror(err); app.onerror(err);
}); });
@ -213,13 +213,13 @@ describe('app.onerror(err)', function(){
describe('app.respond', function(){ describe('app.respond', function(){
describe('when this.respond === false', function(){ describe('when this.respond === false', function(){
it('should bypass app.respond', function(done){ it('should bypass app.respond', function(done){
var app = koa(); const app = koa();
app.use(function *(){ app.use(function *(){
this.body = 'Hello'; this.body = 'Hello';
this.respond = false; this.respond = false;
var res = this.res; const res = this.res;
res.statusCode = 200; res.statusCode = 200;
setImmediate(function(){ setImmediate(function(){
res.setHeader('Content-Type', 'text/plain'); res.setHeader('Content-Type', 'text/plain');
@ -227,7 +227,7 @@ describe('app.respond', function(){
}) })
}) })
var server = app.listen(); const server = app.listen();
request(server) request(server)
.get('/') .get('/')
@ -239,13 +239,13 @@ describe('app.respond', function(){
describe('when HEAD is used', function(){ describe('when HEAD is used', function(){
it('should not respond with the body', function(done){ it('should not respond with the body', function(done){
var app = koa(); const app = koa();
app.use(function *(){ app.use(function *(){
this.body = 'Hello'; this.body = 'Hello';
}); });
var server = app.listen(); const server = app.listen();
request(server) request(server)
.head('/') .head('/')
@ -260,13 +260,13 @@ describe('app.respond', function(){
}) })
it('should keep json headers', function(done){ it('should keep json headers', function(done){
var app = koa(); const app = koa();
app.use(function *(){ app.use(function *(){
this.body = { hello: 'world' }; this.body = { hello: 'world' };
}); });
var server = app.listen(); const server = app.listen();
request(server) request(server)
.head('/') .head('/')
@ -281,13 +281,13 @@ describe('app.respond', function(){
}) })
it('should keep string headers', function(done){ it('should keep string headers', function(done){
var app = koa(); const app = koa();
app.use(function *(){ app.use(function *(){
this.body = 'hello world'; this.body = 'hello world';
}); });
var server = app.listen(); const server = app.listen();
request(server) request(server)
.head('/') .head('/')
@ -302,13 +302,13 @@ describe('app.respond', function(){
}) })
it('should keep buffer headers', function(done){ it('should keep buffer headers', function(done){
var app = koa(); const app = koa();
app.use(function *(){ app.use(function *(){
this.body = new Buffer('hello world'); this.body = new Buffer('hello world');
}); });
var server = app.listen(); const server = app.listen();
request(server) request(server)
.head('/') .head('/')
@ -323,13 +323,13 @@ 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', function(done){
var app = koa(); const app = koa();
app.use(function *(){ app.use(function *(){
}) })
var server = app.listen(); const server = app.listen();
request(server) request(server)
.head('/') .head('/')
@ -337,13 +337,13 @@ describe('app.respond', function(){
}) })
it('should respond with a 200 if body = ""', function(done){ it('should respond with a 200 if body = ""', function(done){
var app = koa(); const app = koa();
app.use(function *(){ app.use(function *(){
this.body = ''; this.body = '';
}) })
var server = app.listen(); const server = app.listen();
request(server) request(server)
.head('/') .head('/')
@ -351,14 +351,14 @@ describe('app.respond', function(){
}) })
it('should not overwrite the content-type', function(done){ it('should not overwrite the content-type', function(done){
var app = koa(); const app = koa();
app.use(function *(){ app.use(function *(){
this.status = 200; this.status = 200;
this.type = 'application/javascript'; this.type = 'application/javascript';
}) })
var server = app.listen(); const server = app.listen();
request(server) request(server)
.head('/') .head('/')
@ -369,9 +369,9 @@ describe('app.respond', function(){
describe('when no middleware are present', function(){ describe('when no middleware are present', function(){
it('should 404', function(done){ it('should 404', function(done){
var app = koa(); const app = koa();
var server = app.listen(); const server = app.listen();
request(server) request(server)
.get('/') .get('/')
@ -381,10 +381,10 @@ describe('app.respond', function(){
describe('when res has already been written to', function(){ describe('when res has already been written to', function(){
it('should not cause an app error', function(done){ it('should not cause an app error', function(done){
var app = koa(); const app = koa();
app.use(function *(next){ app.use(function *(next){
var res = this.res; const res = this.res;
this.status = 200; this.status = 200;
res.setHeader("Content-Type", "text/html") res.setHeader("Content-Type", "text/html")
res.write('Hello'); res.write('Hello');
@ -393,13 +393,13 @@ describe('app.respond', function(){
}, 0); }, 0);
}); });
var errorCaught = false; const errorCaught = false;
app.on('error', function(err){ app.on('error', function(err){
errorCaught = err; errorCaught = err;
}); });
var server = app.listen(); const server = app.listen();
request(server) request(server)
.get('/') .get('/')
@ -412,10 +412,10 @@ describe('app.respond', function(){
}) })
it('should send the right body', function(done){ it('should send the right body', function(done){
var app = koa(); const app = koa();
app.use(function *(next){ app.use(function *(next){
var res = this.res; const res = this.res;
this.status = 200; this.status = 200;
res.setHeader("Content-Type", "text/html") res.setHeader("Content-Type", "text/html")
res.write('Hello'); res.write('Hello');
@ -424,7 +424,7 @@ describe('app.respond', function(){
}, 0); }, 0);
}); });
var server = app.listen(); const server = app.listen();
request(server) request(server)
.get('/') .get('/')
@ -436,13 +436,13 @@ describe('app.respond', function(){
describe('when .body is missing', function(){ describe('when .body is missing', function(){
describe('with status=400', function(){ describe('with status=400', function(){
it('should respond with the associated status message', function(done){ it('should respond with the associated status message', function(done){
var app = koa(); const app = koa();
app.use(function *(){ app.use(function *(){
this.status = 400; this.status = 400;
}); });
var server = app.listen(); const server = app.listen();
request(server) request(server)
.get('/') .get('/')
@ -454,13 +454,13 @@ describe('app.respond', function(){
describe('with status=204', function(){ describe('with status=204', function(){
it('should respond without a body', function(done){ it('should respond without a body', function(done){
var app = koa(); const app = koa();
app.use(function *(){ app.use(function *(){
this.status = 204; this.status = 204;
}) })
var server = app.listen(); const server = app.listen();
request(server) request(server)
.get('/') .get('/')
@ -477,13 +477,13 @@ describe('app.respond', function(){
describe('with status=205', function(){ describe('with status=205', function(){
it('should respond without a body', function(done){ it('should respond without a body', function(done){
var app = koa(); const app = koa();
app.use(function *(){ app.use(function *(){
this.status = 205; this.status = 205;
}) })
var server = app.listen(); const server = app.listen();
request(server) request(server)
.get('/') .get('/')
@ -500,13 +500,13 @@ describe('app.respond', function(){
describe('with status=304', function(){ describe('with status=304', function(){
it('should respond without a body', function(done){ it('should respond without a body', function(done){
var app = koa(); const app = koa();
app.use(function *(){ app.use(function *(){
this.status = 304; this.status = 304;
}) })
var server = app.listen(); const server = app.listen();
request(server) request(server)
.get('/') .get('/')
@ -523,14 +523,14 @@ describe('app.respond', function(){
describe('with custom status=700', function(){ describe('with custom status=700', function(){
it('should respond with the associated status message', function (done){ it('should respond with the associated status message', function (done){
var app = koa(); const app = koa();
statuses['700'] = 'custom status'; statuses['700'] = 'custom status';
app.use(function *(){ app.use(function *(){
this.status = 700; this.status = 700;
}) })
var server = app.listen(); const server = app.listen();
request(server) request(server)
.get('/') .get('/')
@ -546,14 +546,14 @@ describe('app.respond', function(){
describe('with custom statusMessage=ok', function(){ describe('with custom statusMessage=ok', function(){
it('should respond with the custom status message', function (done){ it('should respond with the custom status message', function (done){
var app = koa(); const app = koa();
app.use(function *(){ app.use(function *(){
this.status = 200; this.status = 200;
this.message = 'ok'; this.message = 'ok';
}) })
var server = app.listen(); const server = app.listen();
request(server) request(server)
.get('/') .get('/')
@ -569,13 +569,13 @@ describe('app.respond', function(){
describe('with custom status without message', function (){ describe('with custom status without message', function (){
it('should respond with the status code number', function (done){ it('should respond with the status code number', function (done){
var app = koa(); const app = koa();
app.use(function *(){ app.use(function *(){
this.res.statusCode = 701; this.res.statusCode = 701;
}) })
var server = app.listen(); const server = app.listen();
request(server) request(server)
.get('/') .get('/')
@ -587,13 +587,13 @@ describe('app.respond', function(){
describe('when .body is a null', function(){ describe('when .body is a null', function(){
it('should respond 204 by default', function(done){ it('should respond 204 by default', function(done){
var app = koa(); const app = koa();
app.use(function *(){ app.use(function *(){
this.body = null; this.body = null;
}) })
var server = app.listen(); const server = app.listen();
request(server) request(server)
.get('/') .get('/')
@ -608,14 +608,14 @@ describe('app.respond', function(){
}) })
it('should respond 204 with status=200', function(done){ it('should respond 204 with status=200', function(done){
var app = koa(); const app = koa();
app.use(function *(){ app.use(function *(){
this.status = 200; this.status = 200;
this.body = null; this.body = null;
}) })
var server = app.listen(); const server = app.listen();
request(server) request(server)
.get('/') .get('/')
@ -630,14 +630,14 @@ describe('app.respond', function(){
}) })
it('should respond 205 with status=205', function(done){ it('should respond 205 with status=205', function(done){
var app = koa(); const app = koa();
app.use(function *(){ app.use(function *(){
this.status = 205; this.status = 205;
this.body = null; this.body = null;
}) })
var server = app.listen(); const server = app.listen();
request(server) request(server)
.get('/') .get('/')
@ -652,14 +652,14 @@ describe('app.respond', function(){
}) })
it('should respond 304 with status=304', function(done){ it('should respond 304 with status=304', function(done){
var app = koa(); const app = koa();
app.use(function *(){ app.use(function *(){
this.status = 304; this.status = 304;
this.body = null; this.body = null;
}) })
var server = app.listen(); const server = app.listen();
request(server) request(server)
.get('/') .get('/')
@ -676,13 +676,13 @@ describe('app.respond', function(){
describe('when .body is a string', function(){ describe('when .body is a string', function(){
it('should respond', function(done){ it('should respond', function(done){
var app = koa(); const app = koa();
app.use(function *(){ app.use(function *(){
this.body = 'Hello'; this.body = 'Hello';
}); });
var server = app.listen(); const server = app.listen();
request(server) request(server)
.get('/') .get('/')
@ -692,13 +692,13 @@ describe('app.respond', function(){
describe('when .body is a Buffer', function(){ describe('when .body is a Buffer', function(){
it('should respond', function(done){ it('should respond', function(done){
var app = koa(); const app = koa();
app.use(function *(){ app.use(function *(){
this.body = new Buffer('Hello'); this.body = new Buffer('Hello');
}); });
var server = app.listen(); const server = app.listen();
request(server) request(server)
.get('/') .get('/')
@ -708,21 +708,21 @@ describe('app.respond', function(){
describe('when .body is a Stream', function(){ describe('when .body is a Stream', function(){
it('should respond', function(done){ it('should respond', function(done){
var app = koa(); const app = koa();
app.use(function *(){ app.use(function *(){
this.body = fs.createReadStream('package.json'); this.body = fs.createReadStream('package.json');
this.set('Content-Type', 'application/json; charset=utf-8'); this.set('Content-Type', 'application/json; charset=utf-8');
}); });
var server = app.listen(); const server = app.listen();
request(server) request(server)
.get('/') .get('/')
.expect('Content-Type', 'application/json; charset=utf-8') .expect('Content-Type', 'application/json; charset=utf-8')
.end(function(err, res){ .end(function(err, res){
if (err) return done(err); if (err) return done(err);
var pkg = require('../package'); const pkg = require('../package');
res.should.not.have.header('Content-Length'); res.should.not.have.header('Content-Length');
res.body.should.eql(pkg); res.body.should.eql(pkg);
done(); done();
@ -730,7 +730,7 @@ describe('app.respond', function(){
}) })
it('should strip content-length when overwriting', function(done){ it('should strip content-length when overwriting', function(done){
var app = koa(); const app = koa();
app.use(function *(){ app.use(function *(){
this.body = 'hello'; this.body = 'hello';
@ -738,14 +738,14 @@ describe('app.respond', function(){
this.set('Content-Type', 'application/json; charset=utf-8'); this.set('Content-Type', 'application/json; charset=utf-8');
}); });
var server = app.listen(); const server = app.listen();
request(server) request(server)
.get('/') .get('/')
.expect('Content-Type', 'application/json; charset=utf-8') .expect('Content-Type', 'application/json; charset=utf-8')
.end(function(err, res){ .end(function(err, res){
if (err) return done(err); if (err) return done(err);
var pkg = require('../package'); const pkg = require('../package');
res.should.not.have.header('Content-Length'); res.should.not.have.header('Content-Length');
res.body.should.eql(pkg); res.body.should.eql(pkg);
done(); done();
@ -753,7 +753,7 @@ describe('app.respond', function(){
}) })
it('should keep content-length if not overwritten', function(done){ it('should keep content-length if not overwritten', function(done){
var app = koa(); const app = koa();
app.use(function *(){ app.use(function *(){
this.length = fs.readFileSync('package.json').length; this.length = fs.readFileSync('package.json').length;
@ -761,14 +761,14 @@ describe('app.respond', function(){
this.set('Content-Type', 'application/json; charset=utf-8'); this.set('Content-Type', 'application/json; charset=utf-8');
}); });
var server = app.listen(); const server = app.listen();
request(server) request(server)
.get('/') .get('/')
.expect('Content-Type', 'application/json; charset=utf-8') .expect('Content-Type', 'application/json; charset=utf-8')
.end(function(err, res){ .end(function(err, res){
if (err) return done(err); if (err) return done(err);
var pkg = require('../package'); const pkg = require('../package');
res.should.have.header('Content-Length'); res.should.have.header('Content-Length');
res.body.should.eql(pkg); res.body.should.eql(pkg);
done(); done();
@ -776,24 +776,24 @@ describe('app.respond', function(){
}) })
it('should keep content-length if overwritten with the same stream', function(done){ it('should keep content-length if overwritten with the same stream', function(done){
var app = koa(); const app = koa();
app.use(function *(){ app.use(function *(){
this.length = fs.readFileSync('package.json').length; this.length = fs.readFileSync('package.json').length;
var stream = fs.createReadStream('package.json'); const stream = fs.createReadStream('package.json');
this.body = stream; this.body = stream;
this.body = stream; this.body = stream;
this.set('Content-Type', 'application/json; charset=utf-8'); this.set('Content-Type', 'application/json; charset=utf-8');
}); });
var server = app.listen(); const server = app.listen();
request(server) request(server)
.get('/') .get('/')
.expect('Content-Type', 'application/json; charset=utf-8') .expect('Content-Type', 'application/json; charset=utf-8')
.end(function(err, res){ .end(function(err, res){
if (err) return done(err); if (err) return done(err);
var pkg = require('../package'); const pkg = require('../package');
res.should.have.header('Content-Length'); res.should.have.header('Content-Length');
res.body.should.eql(pkg); res.body.should.eql(pkg);
done(); done();
@ -801,14 +801,14 @@ describe('app.respond', function(){
}) })
it('should handle errors', function(done){ it('should handle errors', function(done){
var app = koa(); const app = koa();
app.use(function *(){ app.use(function *(){
this.set('Content-Type', 'application/json; charset=utf-8'); this.set('Content-Type', 'application/json; charset=utf-8');
this.body = fs.createReadStream('does not exist'); this.body = fs.createReadStream('does not exist');
}); });
var server = app.listen(); const server = app.listen();
request(server) request(server)
.get('/') .get('/')
@ -818,14 +818,14 @@ describe('app.respond', function(){
}) })
it('should handle errors when no content status', function(done){ it('should handle errors when no content status', function(done){
var app = koa(); const app = koa();
app.use(function *(){ app.use(function *(){
this.status = 204; this.status = 204;
this.body = fs.createReadStream('does not exist'); this.body = fs.createReadStream('does not exist');
}); });
var server = app.listen(); const server = app.listen();
request(server) request(server)
.get('/') .get('/')
@ -834,7 +834,7 @@ describe('app.respond', function(){
it('should handle all intermediate stream body errors', function(done){ it('should handle all intermediate stream body errors', function(done){
var app = koa(); const app = koa();
app.use(function *(){ app.use(function *(){
this.body = fs.createReadStream('does not exist'); this.body = fs.createReadStream('does not exist');
@ -842,7 +842,7 @@ describe('app.respond', function(){
this.body = fs.createReadStream('does not exist'); this.body = fs.createReadStream('does not exist');
}); });
var server = app.listen(); const server = app.listen();
request(server) request(server)
.get('/') .get('/')
@ -852,13 +852,13 @@ describe('app.respond', function(){
describe('when .body is an Object', function(){ describe('when .body is an Object', function(){
it('should respond with json', function(done){ it('should respond with json', function(done){
var app = koa(); const app = koa();
app.use(function *(){ app.use(function *(){
this.body = { hello: 'world' }; this.body = { hello: 'world' };
}); });
var server = app.listen(); const server = app.listen();
request(server) request(server)
.get('/') .get('/')
@ -869,7 +869,7 @@ describe('app.respond', function(){
describe('when an error occurs', function(){ describe('when an error occurs', function(){
it('should emit "error" on the app', function(done){ it('should emit "error" on the app', function(done){
var app = koa(); const app = koa();
app.use(function *(){ app.use(function *(){
throw new Error('boom'); throw new Error('boom');
@ -887,10 +887,10 @@ describe('app.respond', function(){
describe('with an .expose property', function(){ describe('with an .expose property', function(){
it('should expose the message', function(done){ it('should expose the message', function(done){
var app = koa(); const app = koa();
app.use(function *(){ app.use(function *(){
var err = new Error('sorry!'); const err = new Error('sorry!');
err.status = 403; err.status = 403;
err.expose = true; err.expose = true;
throw err; throw err;
@ -905,10 +905,10 @@ describe('app.respond', function(){
describe('with a .status property', function(){ describe('with a .status property', function(){
it('should respond with .status', function(done){ it('should respond with .status', function(done){
var app = koa(); const app = koa();
app.use(function *(){ app.use(function *(){
var err = new Error('s3 explodes'); const err = new Error('s3 explodes');
err.status = 403; err.status = 403;
throw err; throw err;
}); });
@ -921,13 +921,13 @@ describe('app.respond', function(){
}) })
it('should respond with 500', function(done){ it('should respond with 500', function(done){
var app = koa(); const app = koa();
app.use(function *(){ app.use(function *(){
throw new Error('boom!'); throw new Error('boom!');
}); });
var server = app.listen(); const server = app.listen();
request(server) request(server)
.get('/') .get('/')
@ -936,7 +936,7 @@ describe('app.respond', function(){
}) })
it('should be catchable', function(done){ it('should be catchable', function(done){
var app = koa(); const app = koa();
app.use(function *(next){ app.use(function *(next){
try { try {
@ -952,7 +952,7 @@ describe('app.respond', function(){
this.body = 'Oh no'; this.body = 'Oh no';
}); });
var server = app.listen(); const server = app.listen();
request(server) request(server)
.get('/') .get('/')
@ -963,7 +963,7 @@ describe('app.respond', function(){
describe('when status and body property', function(){ describe('when status and body property', function(){
it('should 200', function(done){ it('should 200', function(done){
var app = koa(); const app = koa();
app.use(function *(){ app.use(function *(){
this.status = 304; this.status = 304;
@ -971,7 +971,7 @@ describe('app.respond', function(){
this.status = 200; this.status = 200;
}); });
var server = app.listen(); const server = app.listen();
request(server) request(server)
.get('/') .get('/')
@ -980,7 +980,7 @@ describe('app.respond', function(){
}) })
it('should 204', function(done) { it('should 204', function(done) {
var app = koa(); const app = koa();
app.use(function *(){ app.use(function *(){
this.status = 200; this.status = 200;
@ -989,7 +989,7 @@ describe('app.respond', function(){
this.status = 204; this.status = 204;
}); });
var server = app.listen(); const server = app.listen();
request(server) request(server)
.get('/') .get('/')
@ -1003,9 +1003,9 @@ describe('app.respond', function(){
}) })
describe('app.context', function(){ describe('app.context', function(){
var app1 = koa(); const app1 = koa();
app1.context.msg = 'hello'; app1.context.msg = 'hello';
var app2 = koa(); const app2 = koa();
it('should merge properties', function(done){ it('should merge properties', function(done){
app1.use(function *(next){ app1.use(function *(next){
@ -1031,9 +1031,9 @@ describe('app.context', function(){
}) })
describe('app.request', function(){ describe('app.request', function(){
var app1 = koa(); const app1 = koa();
app1.request.message = 'hello'; app1.request.message = 'hello';
var app2 = koa(); const app2 = koa();
it('should merge properties', function(done){ it('should merge properties', function(done){
app1.use(function *(next){ app1.use(function *(next){
@ -1059,9 +1059,9 @@ describe('app.request', function(){
}) })
describe('app.response', function(){ describe('app.response', function(){
var app1 = koa(); const app1 = koa();
app1.response.msg = 'hello'; app1.response.msg = 'hello';
var app2 = koa(); const app2 = koa();
it('should merge properties', function(done){ it('should merge properties', function(done){
app1.use(function *(next){ app1.use(function *(next){

View File

@ -1,11 +1,11 @@
'use strict'; 'use strict';
var Stream = require('stream'); const Stream = require('stream');
var koa = require('..'); const koa = require('..');
exports = module.exports = function(req, res){ exports = module.exports = function(req, res){
var socket = new Stream.Duplex(); const socket = new Stream.Duplex();
req = req || { headers: {}, socket: socket, __proto__: Stream.Readable.prototype }; req = req || { headers: {}, socket: socket, __proto__: Stream.Readable.prototype };
res = res || { _headers: {}, socket: socket, __proto__: Stream.Writable.prototype }; res = res || { _headers: {}, socket: socket, __proto__: Stream.Writable.prototype };
res.getHeader = function(k){ return res._headers[k.toLowerCase()] }; res.getHeader = function(k){ return res._headers[k.toLowerCase()] };

View File

@ -1,12 +1,12 @@
'use strict'; 'use strict';
var context = require('../context'); const context = require('../context');
var assert = require('assert'); const assert = require('assert');
describe('ctx.assert(value, status)', function(){ describe('ctx.assert(value, status)', function(){
it('should throw an error', function(){ it('should throw an error', function(){
var ctx = context(); const ctx = context();
try { try {
ctx.assert(false, 404); ctx.assert(false, 404);

View File

@ -1,19 +1,19 @@
'use strict'; 'use strict';
var request = require('supertest'); const request = require('supertest');
var koa = require('../..'); const koa = require('../..');
describe('ctx.cookies.set()', function(){ describe('ctx.cookies.set()', function(){
it('should set an unsigned cookie', function(done){ it('should set an unsigned cookie', function(done){
var app = koa(); const app = koa();
app.use(function *(next){ app.use(function *(next){
this.cookies.set('name', 'jon'); this.cookies.set('name', 'jon');
this.status = 204; this.status = 204;
}) })
var server = app.listen(); const server = app.listen();
request(server) request(server)
.get('/') .get('/')
@ -32,7 +32,7 @@ describe('ctx.cookies.set()', function(){
describe('with .signed', function(){ describe('with .signed', function(){
describe('when no .keys are set', function(){ describe('when no .keys are set', function(){
it('should error', function(done){ it('should error', function(done){
var app = koa(); const app = koa();
app.use(function *(next){ app.use(function *(next){
try { try {
@ -49,7 +49,7 @@ describe('ctx.cookies.set()', function(){
}) })
it('should send a signed cookie', function(done){ it('should send a signed cookie', function(done){
var app = koa(); const app = koa();
app.keys = ['a', 'b']; app.keys = ['a', 'b'];
@ -58,7 +58,7 @@ describe('ctx.cookies.set()', function(){
this.status = 204; this.status = 204;
}) })
var server = app.listen(); const server = app.listen();
request(server) request(server)
.get('/') .get('/')
@ -66,7 +66,7 @@ describe('ctx.cookies.set()', function(){
.end(function(err, res){ .end(function(err, res){
if (err) return done(err); if (err) return done(err);
var cookies = res.headers['set-cookie']; const cookies = res.headers['set-cookie'];
cookies.some(function(cookie){ cookies.some(function(cookie){
return /^name=/.test(cookie); return /^name=/.test(cookie);

View File

@ -1,12 +1,12 @@
'use strict'; 'use strict';
var context = require('../context'); const context = require('../context');
describe('ctx.inspect()', function(){ describe('ctx.inspect()', function(){
it('should return a json representation', function(){ it('should return a json representation', function(){
var ctx = context(); const ctx = context();
var toJSON = ctx.toJSON(ctx); const toJSON = ctx.toJSON(ctx);
toJSON.should.eql(ctx.inspect()); toJSON.should.eql(ctx.inspect());
}) })

View File

@ -1,12 +1,12 @@
'use strict'; 'use strict';
var request = require('supertest'); const request = require('supertest');
var koa = require('../..'); const koa = require('../..');
describe('ctx.onerror(err)', function(){ describe('ctx.onerror(err)', function(){
it('should respond', function(done){ it('should respond', function(done){
var app = koa(); const app = koa();
app.use(function *(next){ app.use(function *(next){
this.body = 'something else'; this.body = 'something else';
@ -14,7 +14,7 @@ describe('ctx.onerror(err)', function(){
this.throw(418, 'boom'); this.throw(418, 'boom');
}) })
var server = app.listen(); const server = app.listen();
request(server) request(server)
.get('/') .get('/')
@ -25,7 +25,7 @@ describe('ctx.onerror(err)', function(){
}) })
it('should unset all headers', function(done){ it('should unset all headers', function(done){
var app = koa(); const app = koa();
app.use(function *(next){ app.use(function *(next){
this.set('Vary', 'Accept-Encoding'); this.set('Vary', 'Accept-Encoding');
@ -35,7 +35,7 @@ describe('ctx.onerror(err)', function(){
this.throw(418, 'boom'); this.throw(418, 'boom');
}) })
var server = app.listen(); const server = app.listen();
request(server) request(server)
.get('/') .get('/')
@ -55,16 +55,16 @@ describe('ctx.onerror(err)', function(){
describe('when invalid err.status', function(){ describe('when invalid err.status', function(){
describe('not number', function(){ describe('not number', function(){
it('should respond 500', function(done){ it('should respond 500', function(done){
var app = koa(); const app = koa();
app.use(function *(next){ app.use(function *(next){
this.body = 'something else'; this.body = 'something else';
var err = new Error('some error'); const err = new Error('some error');
err.status = 'notnumber'; err.status = 'notnumber';
throw err; throw err;
}) })
var server = app.listen(); const server = app.listen();
request(server) request(server)
.get('/') .get('/')
@ -76,16 +76,16 @@ describe('ctx.onerror(err)', function(){
describe('not http status code', function(){ describe('not http status code', function(){
it('should respond 500', function(done){ it('should respond 500', function(done){
var app = koa(); const app = koa();
app.use(function *(next){ app.use(function *(next){
this.body = 'something else'; this.body = 'something else';
var err = new Error('some error'); const err = new Error('some error');
err.status = 9999; err.status = 9999;
throw err; throw err;
}) })
var server = app.listen(); const server = app.listen();
request(server) request(server)
.get('/') .get('/')
@ -98,13 +98,13 @@ describe('ctx.onerror(err)', function(){
describe('when non-error thrown', function(){ describe('when non-error thrown', function(){
it('should response non-error thrown message', function(done){ it('should response non-error thrown message', function(done){
var app = koa(); const app = koa();
app.use(function *(next){ app.use(function *(next){
throw 'string error'; throw 'string error';
}) })
var server = app.listen(); const server = app.listen();
request(server) request(server)
.get('/') .get('/')

View File

@ -1,19 +1,19 @@
'use strict'; 'use strict';
var request = require('supertest'); const request = require('supertest');
var assert = require('assert'); const assert = require('assert');
var koa = require('../..'); const koa = require('../..');
describe('ctx.state', function() { describe('ctx.state', function() {
it('should provide a ctx.state namespace', function(done) { it('should provide a ctx.state namespace', function(done) {
var app = koa(); const app = koa();
app.use(function *() { app.use(function *() {
assert.deepEqual(this.state, {}); assert.deepEqual(this.state, {});
}); });
var server = app.listen(); const server = app.listen();
request(server) request(server)
.get('/') .get('/')

View File

@ -1,12 +1,12 @@
'use strict'; 'use strict';
var context = require('../context'); const context = require('../context');
var assert = require('assert'); const assert = require('assert');
describe('ctx.throw(msg)', function(){ describe('ctx.throw(msg)', function(){
it('should set .status to 500', function(done){ it('should set .status to 500', function(done){
var ctx = context(); const ctx = context();
try { try {
ctx.throw('boom'); ctx.throw('boom');
@ -20,8 +20,8 @@ describe('ctx.throw(msg)', function(){
describe('ctx.throw(err)', function(){ describe('ctx.throw(err)', function(){
it('should set .status to 500', function(done){ it('should set .status to 500', function(done){
var ctx = context(); const ctx = context();
var err = new Error('test'); const err = new Error('test');
try { try {
ctx.throw(err); ctx.throw(err);
@ -36,8 +36,8 @@ describe('ctx.throw(err)', function(){
describe('ctx.throw(err, status)', function(){ describe('ctx.throw(err, status)', function(){
it('should throw the error and set .status', function(done){ it('should throw the error and set .status', function(done){
var ctx = context(); const ctx = context();
var error = new Error('test'); const error = new Error('test');
try { try {
ctx.throw(error, 422); ctx.throw(error, 422);
@ -52,8 +52,8 @@ describe('ctx.throw(err, status)', function(){
describe('ctx.throw(status, err)', function(){ describe('ctx.throw(status, err)', function(){
it('should throw the error and set .status', function(done){ it('should throw the error and set .status', function(done){
var ctx = context(); const ctx = context();
var error = new Error('test'); const error = new Error('test');
try { try {
ctx.throw(422, error); ctx.throw(422, error);
@ -68,7 +68,7 @@ describe('ctx.throw(status, err)', function(){
describe('ctx.throw(msg, status)', function(){ describe('ctx.throw(msg, status)', function(){
it('should throw an error', function(done){ it('should throw an error', function(done){
var ctx = context(); const ctx = context();
try { try {
ctx.throw('name required', 400); ctx.throw('name required', 400);
@ -83,7 +83,7 @@ describe('ctx.throw(msg, status)', function(){
describe('ctx.throw(status, msg)', function(){ describe('ctx.throw(status, msg)', function(){
it('should throw an error', function(done){ it('should throw an error', function(done){
var ctx = context(); const ctx = context();
try { try {
ctx.throw(400, 'name required'); ctx.throw(400, 'name required');
@ -98,7 +98,7 @@ describe('ctx.throw(status, msg)', function(){
describe('ctx.throw(status)', function(){ describe('ctx.throw(status)', function(){
it('should throw an error', function(done){ it('should throw an error', function(done){
var ctx = context(); const ctx = context();
try { try {
ctx.throw(400); ctx.throw(400);
@ -112,10 +112,10 @@ describe('ctx.throw(status)', function(){
describe('when not valid status', function(){ describe('when not valid status', function(){
it('should not expose', function(done){ it('should not expose', function(done){
var ctx = context(); const ctx = context();
try { try {
var err = new Error('some error'); const err = new Error('some error');
err.status = -1; err.status = -1;
ctx.throw(err); ctx.throw(err);
} catch(err) { } catch(err) {
@ -129,7 +129,7 @@ describe('ctx.throw(status)', function(){
describe('ctx.throw(status, msg, props)', function(){ describe('ctx.throw(status, msg, props)', function(){
it('should mixin props', function(done){ it('should mixin props', function(done){
var ctx = context(); const ctx = context();
try { try {
ctx.throw(400, 'msg', { prop: true }); ctx.throw(400, 'msg', { prop: true });
@ -144,7 +144,7 @@ describe('ctx.throw(status, msg, props)', function(){
describe('when props include status', function(){ describe('when props include status', function(){
it('should be ignored', function(done){ it('should be ignored', function(done){
var ctx = context(); const ctx = context();
try { try {
ctx.throw(400, 'msg', { ctx.throw(400, 'msg', {
@ -164,7 +164,7 @@ describe('ctx.throw(status, msg, props)', function(){
describe('ctx.throw(msg, props)', function(){ describe('ctx.throw(msg, props)', function(){
it('should mixin props', function(done){ it('should mixin props', function(done){
var ctx = context(); const ctx = context();
try { try {
ctx.throw('msg', { prop: true }); ctx.throw('msg', { prop: true });
@ -180,7 +180,7 @@ describe('ctx.throw(msg, props)', function(){
describe('ctx.throw(status, props)', function(){ describe('ctx.throw(status, props)', function(){
it('should mixin props', function(done){ it('should mixin props', function(done){
var ctx = context(); const ctx = context();
try { try {
ctx.throw(400, { prop: true }); ctx.throw(400, { prop: true });
@ -196,7 +196,7 @@ describe('ctx.throw(status, props)', function(){
describe('ctx.throw(err, props)', function(){ describe('ctx.throw(err, props)', function(){
it('should mixin props', function(done){ it('should mixin props', function(done){
var ctx = context(); const ctx = context();
try { try {
ctx.throw(new Error('test'), { prop: true }); ctx.throw(new Error('test'), { prop: true });

View File

@ -1,11 +1,11 @@
'use strict'; 'use strict';
var context = require('../context'); const context = require('../context');
describe('ctx.toJSON()', function(){ describe('ctx.toJSON()', function(){
it('should return a json representation', function(){ it('should return a json representation', function(){
var ctx = context(); const ctx = context();
ctx.req.method = 'POST'; ctx.req.method = 'POST';
ctx.req.url = '/items'; ctx.req.url = '/items';
@ -13,9 +13,9 @@ describe('ctx.toJSON()', function(){
ctx.status = 200; ctx.status = 200;
ctx.body = '<p>Hey</p>'; ctx.body = '<p>Hey</p>';
var obj = JSON.parse(JSON.stringify(ctx)); const obj = JSON.parse(JSON.stringify(ctx));
var req = obj.request; const req = obj.request;
var res = obj.response; const res = obj.response;
req.should.eql({ req.should.eql({
method: 'POST', method: 'POST',

View File

@ -5,15 +5,15 @@
* Separate file primarily because we use `require('babel/register')`. * Separate file primarily because we use `require('babel/register')`.
*/ */
var request = require('supertest'); const request = require('supertest');
var koa = require('../..'); const koa = require('../..');
describe('.experimental=true', function () { describe('.experimental=true', function () {
it('should support async functions', function (done) { it('should support async functions', function (done) {
var app = koa(); const app = koa();
app.experimental = true; app.experimental = true;
app.use(async function (next) { app.use(async function (next) {
var string = await Promise.resolve('asdf'); const string = await Promise.resolve('asdf');
this.body = string; this.body = string;
}); });

View File

@ -1,13 +1,13 @@
'use strict'; 'use strict';
var context = require('../context'); const context = require('../context');
describe('ctx.accepts(types)', function(){ describe('ctx.accepts(types)', function(){
describe('with no arguments', function(){ describe('with no arguments', function(){
describe('when Accept is populated', function(){ describe('when Accept is populated', function(){
it('should return all accepted types', function(){ it('should return all accepted types', function(){
var ctx = context(); const ctx = context();
ctx.req.headers.accept = 'application/*;q=0.2, image/jpeg;q=0.8, text/html, text/plain'; ctx.req.headers.accept = 'application/*;q=0.2, image/jpeg;q=0.8, text/html, text/plain';
ctx.accepts().should.eql(['text/html', 'text/plain', 'image/jpeg', 'application/*']); ctx.accepts().should.eql(['text/html', 'text/plain', 'image/jpeg', 'application/*']);
}) })
@ -17,7 +17,7 @@ describe('ctx.accepts(types)', function(){
describe('with no valid types', function(){ describe('with no valid types', function(){
describe('when Accept is populated', function(){ describe('when Accept is populated', function(){
it('should return false', function(){ it('should return false', function(){
var ctx = context(); const ctx = context();
ctx.req.headers.accept = 'application/*;q=0.2, image/jpeg;q=0.8, text/html, text/plain'; ctx.req.headers.accept = 'application/*;q=0.2, image/jpeg;q=0.8, text/html, text/plain';
ctx.accepts('image/png', 'image/tiff').should.be.false; ctx.accepts('image/png', 'image/tiff').should.be.false;
}) })
@ -25,7 +25,7 @@ describe('ctx.accepts(types)', function(){
describe('when Accept is not populated', function(){ describe('when Accept is not populated', function(){
it('should return the first type', function(){ it('should return the first type', function(){
var ctx = context(); const ctx = context();
ctx.accepts('text/html', 'text/plain', 'image/jpeg', 'application/*').should.equal('text/html'); ctx.accepts('text/html', 'text/plain', 'image/jpeg', 'application/*').should.equal('text/html');
}) })
}) })
@ -33,7 +33,7 @@ describe('ctx.accepts(types)', function(){
describe('when extensions are given', function(){ describe('when extensions are given', function(){
it('should convert to mime types', function(){ it('should convert to mime types', function(){
var ctx = context(); const ctx = context();
ctx.req.headers.accept = 'text/plain, text/html'; ctx.req.headers.accept = 'text/plain, text/html';
ctx.accepts('html').should.equal('html'); ctx.accepts('html').should.equal('html');
ctx.accepts('.html').should.equal('.html'); ctx.accepts('.html').should.equal('.html');
@ -45,7 +45,7 @@ describe('ctx.accepts(types)', function(){
describe('when an array is given', function(){ describe('when an array is given', function(){
it('should return the first match', function(){ it('should return the first match', function(){
var ctx = context(); const ctx = context();
ctx.req.headers.accept = 'text/plain, text/html'; ctx.req.headers.accept = 'text/plain, text/html';
ctx.accepts(['png', 'text', 'html']).should.equal('text'); ctx.accepts(['png', 'text', 'html']).should.equal('text');
ctx.accepts(['png', 'html']).should.equal('html'); ctx.accepts(['png', 'html']).should.equal('html');
@ -54,7 +54,7 @@ describe('ctx.accepts(types)', function(){
describe('when multiple arguments are given', function(){ describe('when multiple arguments are given', function(){
it('should return the first match', function(){ it('should return the first match', function(){
var ctx = context(); const ctx = context();
ctx.req.headers.accept = 'text/plain, text/html'; ctx.req.headers.accept = 'text/plain, text/html';
ctx.accepts('png', 'text', 'html').should.equal('text'); ctx.accepts('png', 'text', 'html').should.equal('text');
ctx.accepts('png', 'html').should.equal('html'); ctx.accepts('png', 'html').should.equal('html');
@ -63,7 +63,7 @@ describe('ctx.accepts(types)', function(){
describe('when present in Accept as an exact match', function(){ describe('when present in Accept as an exact match', function(){
it('should return the type', function(){ it('should return the type', function(){
var ctx = context(); const ctx = context();
ctx.req.headers.accept = 'text/plain, text/html'; ctx.req.headers.accept = 'text/plain, text/html';
ctx.accepts('text/html').should.equal('text/html'); ctx.accepts('text/html').should.equal('text/html');
ctx.accepts('text/plain').should.equal('text/plain'); ctx.accepts('text/plain').should.equal('text/plain');
@ -72,7 +72,7 @@ describe('ctx.accepts(types)', function(){
describe('when present in Accept as a type match', function(){ describe('when present in Accept as a type match', function(){
it('should return the type', function(){ it('should return the type', function(){
var ctx = context(); const ctx = context();
ctx.req.headers.accept = 'application/json, */*'; ctx.req.headers.accept = 'application/json, */*';
ctx.accepts('text/html').should.equal('text/html'); ctx.accepts('text/html').should.equal('text/html');
ctx.accepts('text/plain').should.equal('text/plain'); ctx.accepts('text/plain').should.equal('text/plain');
@ -82,7 +82,7 @@ describe('ctx.accepts(types)', function(){
describe('when present in Accept as a subtype match', function(){ describe('when present in Accept as a subtype match', function(){
it('should return the type', function(){ it('should return the type', function(){
var ctx = context(); const ctx = context();
ctx.req.headers.accept = 'application/json, text/*'; ctx.req.headers.accept = 'application/json, text/*';
ctx.accepts('text/html').should.equal('text/html'); ctx.accepts('text/html').should.equal('text/html');
ctx.accepts('text/plain').should.equal('text/plain'); ctx.accepts('text/plain').should.equal('text/plain');

View File

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

View File

@ -1,13 +1,13 @@
'use strict'; 'use strict';
var context = require('../context'); const context = require('../context');
describe('ctx.acceptsEncodings()', function(){ describe('ctx.acceptsEncodings()', function(){
describe('with no arguments', function(){ describe('with no arguments', function(){
describe('when Accept-Encoding is populated', function(){ describe('when Accept-Encoding is populated', function(){
it('should return accepted types', function(){ it('should return accepted types', function(){
var ctx = context(); const ctx = context();
ctx.req.headers['accept-encoding'] = 'gzip, compress;q=0.2'; ctx.req.headers['accept-encoding'] = 'gzip, compress;q=0.2';
ctx.acceptsEncodings().should.eql(['gzip', 'compress', 'identity']); ctx.acceptsEncodings().should.eql(['gzip', 'compress', 'identity']);
ctx.acceptsEncodings('gzip', 'compress').should.equal('gzip'); ctx.acceptsEncodings('gzip', 'compress').should.equal('gzip');
@ -16,7 +16,7 @@ describe('ctx.acceptsEncodings()', function(){
describe('when Accept-Encoding is not populated', function(){ describe('when Accept-Encoding is not populated', function(){
it('should return identity', function(){ it('should return identity', function(){
var ctx = context(); const ctx = context();
ctx.acceptsEncodings().should.eql(['identity']); ctx.acceptsEncodings().should.eql(['identity']);
ctx.acceptsEncodings('gzip', 'deflate', 'identity').should.equal('identity'); ctx.acceptsEncodings('gzip', 'deflate', 'identity').should.equal('identity');
}) })
@ -25,7 +25,7 @@ describe('ctx.acceptsEncodings()', function(){
describe('with multiple arguments', function(){ describe('with multiple arguments', function(){
it('should return the best fit', function(){ it('should return the best fit', function(){
var ctx = context(); const ctx = context();
ctx.req.headers['accept-encoding'] = 'gzip, compress;q=0.2'; ctx.req.headers['accept-encoding'] = 'gzip, compress;q=0.2';
ctx.acceptsEncodings('compress', 'gzip').should.eql('gzip'); ctx.acceptsEncodings('compress', 'gzip').should.eql('gzip');
ctx.acceptsEncodings('gzip', 'compress').should.eql('gzip'); ctx.acceptsEncodings('gzip', 'compress').should.eql('gzip');
@ -34,7 +34,7 @@ describe('ctx.acceptsEncodings()', function(){
describe('with an array', function(){ describe('with an array', function(){
it('should return the best fit', function(){ it('should return the best fit', function(){
var ctx = context(); const ctx = context();
ctx.req.headers['accept-encoding'] = 'gzip, compress;q=0.2'; ctx.req.headers['accept-encoding'] = 'gzip, compress;q=0.2';
ctx.acceptsEncodings(['compress', 'gzip']).should.eql('gzip'); ctx.acceptsEncodings(['compress', 'gzip']).should.eql('gzip');
}) })

View File

@ -1,13 +1,13 @@
'use strict'; 'use strict';
var context = require('../context'); const context = require('../context');
describe('ctx.acceptsLanguages(langs)', function(){ describe('ctx.acceptsLanguages(langs)', function(){
describe('with no arguments', function(){ describe('with no arguments', function(){
describe('when Accept-Language is populated', function(){ describe('when Accept-Language is populated', function(){
it('should return accepted types', function(){ it('should return accepted types', function(){
var ctx = context(); const ctx = context();
ctx.req.headers['accept-language'] = 'en;q=0.8, es, pt'; ctx.req.headers['accept-language'] = 'en;q=0.8, es, pt';
ctx.acceptsLanguages().should.eql(['es', 'pt', 'en']); ctx.acceptsLanguages().should.eql(['es', 'pt', 'en']);
}) })
@ -18,7 +18,7 @@ describe('ctx.acceptsLanguages(langs)', function(){
describe('when Accept-Language is populated', function(){ describe('when Accept-Language is populated', function(){
describe('if any types types match', function(){ describe('if any types types match', function(){
it('should return the best fit', function(){ it('should return the best fit', function(){
var ctx = context(); const ctx = context();
ctx.req.headers['accept-language'] = 'en;q=0.8, es, pt'; ctx.req.headers['accept-language'] = 'en;q=0.8, es, pt';
ctx.acceptsLanguages('es', 'en').should.equal('es'); ctx.acceptsLanguages('es', 'en').should.equal('es');
}) })
@ -26,7 +26,7 @@ describe('ctx.acceptsLanguages(langs)', function(){
describe('if no types match', function(){ describe('if no types match', function(){
it('should return false', function(){ it('should return false', function(){
var ctx = context(); const ctx = context();
ctx.req.headers['accept-language'] = 'en;q=0.8, es, pt'; ctx.req.headers['accept-language'] = 'en;q=0.8, es, pt';
ctx.acceptsLanguages('fr', 'au').should.be.false; ctx.acceptsLanguages('fr', 'au').should.be.false;
}) })
@ -35,7 +35,7 @@ describe('ctx.acceptsLanguages(langs)', function(){
describe('when Accept-Language is not populated', function(){ describe('when Accept-Language is not populated', function(){
it('should return the first type', function(){ it('should return the first type', function(){
var ctx = context(); const ctx = context();
ctx.acceptsLanguages('es', 'en').should.equal('es'); ctx.acceptsLanguages('es', 'en').should.equal('es');
}) })
}) })
@ -43,7 +43,7 @@ describe('ctx.acceptsLanguages(langs)', function(){
describe('with an array', function(){ describe('with an array', function(){
it('should return the best fit', function(){ it('should return the best fit', function(){
var ctx = context(); const ctx = context();
ctx.req.headers['accept-language'] = 'en;q=0.8, es, pt'; ctx.req.headers['accept-language'] = 'en;q=0.8, es, pt';
ctx.acceptsLanguages(['es', 'en']).should.equal('es'); ctx.acceptsLanguages(['es', 'en']).should.equal('es');
}) })

View File

@ -1,20 +1,20 @@
'use strict'; 'use strict';
var request = require('../context').request; const request = require('../context').request;
var assert = require('assert'); const assert = require('assert');
describe('req.charset', function(){ describe('req.charset', function(){
describe('with no content-type present', function(){ describe('with no content-type present', function(){
it('should return ""', function(){ it('should return ""', function(){
var req = request(); const req = request();
assert('' === req.charset); assert('' === req.charset);
}) })
}) })
describe('with charset present', function(){ describe('with charset present', function(){
it('should return ""', function(){ it('should return ""', function(){
var req = request(); const req = request();
req.header['content-type'] = 'text/plain'; req.header['content-type'] = 'text/plain';
assert('' === req.charset); assert('' === req.charset);
}) })
@ -22,7 +22,7 @@ describe('req.charset', function(){
describe('with a charset', function(){ describe('with a charset', function(){
it('should return the charset', function(){ it('should return the charset', function(){
var req = request(); const req = request();
req.header['content-type'] = 'text/plain; charset=utf-8'; req.header['content-type'] = 'text/plain; charset=utf-8';
req.charset.should.equal('utf-8'); req.charset.should.equal('utf-8');
}) })

View File

@ -1,12 +1,12 @@
'use strict'; 'use strict';
var context = require('../context'); const context = require('../context');
describe('ctx.fresh', function(){ describe('ctx.fresh', function(){
describe('the request method is not GET and HEAD', function (){ describe('the request method is not GET and HEAD', function (){
it('should return false', function (){ it('should return false', function (){
var ctx = context(); const ctx = context();
ctx.req.method = 'POST'; ctx.req.method = 'POST';
ctx.fresh.should.be.false; ctx.fresh.should.be.false;
}) })
@ -14,7 +14,7 @@ describe('ctx.fresh', function(){
describe('the response is non-2xx', function(){ describe('the response is non-2xx', function(){
it('should return false', function(){ it('should return false', function(){
var ctx = context(); const ctx = context();
ctx.status = 404; ctx.status = 404;
ctx.req.method = 'GET'; ctx.req.method = 'GET';
ctx.req.headers['if-none-match'] = '123'; ctx.req.headers['if-none-match'] = '123';
@ -26,7 +26,7 @@ describe('ctx.fresh', function(){
describe('the response is 2xx', function(){ describe('the response is 2xx', function(){
describe('and etag matches', function(){ describe('and etag matches', function(){
it('should return true', function(){ it('should return true', function(){
var ctx = context(); const ctx = context();
ctx.status = 200; ctx.status = 200;
ctx.req.method = 'GET'; ctx.req.method = 'GET';
ctx.req.headers['if-none-match'] = '123'; ctx.req.headers['if-none-match'] = '123';
@ -37,7 +37,7 @@ describe('ctx.fresh', function(){
describe('and etag do not match', function(){ describe('and etag do not match', function(){
it('should return false', function(){ it('should return false', function(){
var ctx = context(); const ctx = context();
ctx.status = 200; ctx.status = 200;
ctx.req.method = 'GET'; ctx.req.method = 'GET';
ctx.req.headers['if-none-match'] = '123'; ctx.req.headers['if-none-match'] = '123';

View File

@ -1,11 +1,11 @@
'use strict'; 'use strict';
var context = require('../context'); const context = require('../context');
describe('ctx.get(name)', function(){ describe('ctx.get(name)', function(){
it('should return the field value', function(){ it('should return the field value', function(){
var ctx = context(); const ctx = context();
ctx.req.headers.host = 'http://google.com'; ctx.req.headers.host = 'http://google.com';
ctx.req.headers.referer = 'http://google.com'; ctx.req.headers.referer = 'http://google.com';
ctx.get('HOST').should.equal('http://google.com'); ctx.get('HOST').should.equal('http://google.com');

View File

@ -1,11 +1,11 @@
'use strict'; 'use strict';
var request = require('../context').request; const request = require('../context').request;
describe('req.header', function(){ describe('req.header', function(){
it('should return the request header object', function(){ it('should return the request header object', function(){
var req = request(); const req = request();
req.header.should.equal(req.req.headers); req.header.should.equal(req.req.headers);
}) })
}) })

View File

@ -1,11 +1,11 @@
'use strict'; 'use strict';
var request = require('../context').request; const request = require('../context').request;
describe('req.headers', function(){ describe('req.headers', function(){
it('should return the request header object', function(){ it('should return the request header object', function(){
var req = request(); const req = request();
req.headers.should.equal(req.req.headers); req.headers.should.equal(req.req.headers);
}) })
}) })

View File

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

View File

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

View File

@ -1,15 +1,15 @@
'use strict'; 'use strict';
var Stream = require('stream'); const Stream = require('stream');
var http = require('http'); const http = require('http');
var koa = require('../../'); const koa = require('../../');
var context = require('../context'); const context = require('../context');
describe('ctx.href', function(){ describe('ctx.href', function(){
it('should return the full request url', function(){ it('should return the full request url', function(){
var socket = new Stream.Duplex(); const socket = new Stream.Duplex();
var req = { const req = {
url: '/users/1?next=/dashboard', url: '/users/1?next=/dashboard',
headers: { headers: {
host: 'localhost' host: 'localhost'
@ -17,7 +17,7 @@ describe('ctx.href', function(){
socket: socket, socket: socket,
__proto__: Stream.Readable.prototype __proto__: Stream.Readable.prototype
}; };
var ctx = context(req); const ctx = context(req);
ctx.href.should.equal('http://localhost/users/1?next=/dashboard'); ctx.href.should.equal('http://localhost/users/1?next=/dashboard');
// change it also work // change it also work
ctx.url = '/foo/users/1?next=/dashboard'; ctx.url = '/foo/users/1?next=/dashboard';
@ -25,12 +25,12 @@ describe('ctx.href', function(){
}) })
it('should work with `GET http://example.com/foo`', function(done){ it('should work with `GET http://example.com/foo`', function(done){
var app = koa() const app = koa()
app.use(function* (){ app.use(function* (){
this.body = this.href this.body = this.href
}) })
app.listen(function(){ app.listen(function(){
var address = this.address() const address = this.address()
http.get({ http.get({
host: 'localhost', host: 'localhost',
path: 'http://example.com/foo', path: 'http://example.com/foo',

View File

@ -1,14 +1,14 @@
'use strict'; 'use strict';
var request = require('../context').request; const request = require('../context').request;
describe('ctx.idempotent', function(){ describe('ctx.idempotent', function(){
describe('when the request method is idempotent', function (){ describe('when the request method is idempotent', function (){
it('should return true', function (){ it('should return true', function (){
['GET', 'HEAD', 'PUT', 'DELETE', 'OPTIONS', 'TRACE'].forEach(check); ['GET', 'HEAD', 'PUT', 'DELETE', 'OPTIONS', 'TRACE'].forEach(check);
function check(method) { function check(method) {
var req = request(); const req = request();
req.method = method; req.method = method;
req.idempotent.should.equal(true); req.idempotent.should.equal(true);
} }
@ -17,7 +17,7 @@ describe('ctx.idempotent', function(){
describe('when the request method is not idempotent', function(){ describe('when the request method is not idempotent', function(){
it('should return false', function (){ it('should return false', function (){
var req = request(); const req = request();
req.method = 'POST'; req.method = 'POST';
req.idempotent.should.equal(false); req.idempotent.should.equal(false);
}) })

View File

@ -1,13 +1,13 @@
'use strict'; 'use strict';
var request = require('../context').request; const request = require('../context').request;
var assert = require('assert'); const assert = require('assert');
describe('req.inspect()', function(){ describe('req.inspect()', function(){
describe('with no request.req present', function(){ describe('with no request.req present', function(){
it('should return null', function(){ it('should return null', function(){
var req = request(); const req = request();
req.method = 'GET'; req.method = 'GET';
delete req.req; delete req.req;
assert(null == req.inspect()); assert(null == req.inspect());
@ -15,7 +15,7 @@ describe('req.inspect()', function(){
}) })
it('should return a json representation', function(){ it('should return a json representation', function(){
var req = request(); const req = request();
req.method = 'GET'; req.method = 'GET';
req.url = 'example.com'; req.url = 'example.com';
req.header.host = 'example.com'; req.header.host = 'example.com';

View File

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

View File

@ -1,13 +1,13 @@
'use strict'; 'use strict';
var request = require('../context').request; const request = require('../context').request;
describe('req.ips', function(){ describe('req.ips', function(){
describe('when X-Forwarded-For is present', function(){ describe('when X-Forwarded-For is present', function(){
describe('and proxy is not trusted', function(){ describe('and proxy is not trusted', function(){
it('should be ignored', function(){ it('should be ignored', function(){
var req = request(); const req = request();
req.app.proxy = false; req.app.proxy = false;
req.header['x-forwarded-for'] = '127.0.0.1,127.0.0.2'; req.header['x-forwarded-for'] = '127.0.0.1,127.0.0.2';
req.ips.should.eql([]); req.ips.should.eql([]);
@ -16,7 +16,7 @@ describe('req.ips', function(){
describe('and proxy is trusted', function(){ describe('and proxy is trusted', function(){
it('should be used', function(){ it('should be used', function(){
var req = request(); const req = request();
req.app.proxy = true; req.app.proxy = true;
req.header['x-forwarded-for'] = '127.0.0.1,127.0.0.2'; req.header['x-forwarded-for'] = '127.0.0.1,127.0.0.2';
req.ips.should.eql(['127.0.0.1', '127.0.0.2']); req.ips.should.eql(['127.0.0.1', '127.0.0.2']);

View File

@ -1,13 +1,13 @@
'use strict'; 'use strict';
var context = require('../context'); const context = require('../context');
var should = require('should'); const should = require('should');
var assert = require('assert'); const assert = require('assert');
describe('ctx.is(type)', function(){ describe('ctx.is(type)', function(){
it('should ignore params', function(){ it('should ignore params', function(){
var ctx = context(); const ctx = context();
ctx.header['content-type'] = 'text/html; charset=utf-8'; ctx.header['content-type'] = 'text/html; charset=utf-8';
ctx.header['transfer-encoding'] = 'chunked'; ctx.header['transfer-encoding'] = 'chunked';
@ -16,7 +16,7 @@ describe('ctx.is(type)', function(){
describe('when no body is given', function(){ describe('when no body is given', function(){
it('should return null', function(){ it('should return null', function(){
var ctx = context(); const ctx = context();
assert(null == ctx.is()); assert(null == ctx.is());
assert(null == ctx.is('image/*')); assert(null == ctx.is('image/*'));
@ -26,7 +26,7 @@ describe('ctx.is(type)', function(){
describe('when no content type is given', function(){ describe('when no content type is given', function(){
it('should return false', function(){ it('should return false', function(){
var ctx = context(); const ctx = context();
ctx.header['transfer-encoding'] = 'chunked'; ctx.header['transfer-encoding'] = 'chunked';
ctx.is().should.be.false; ctx.is().should.be.false;
@ -37,7 +37,7 @@ describe('ctx.is(type)', function(){
describe('give no types', function(){ describe('give no types', function(){
it('should return the mime type', function(){ it('should return the mime type', function(){
var ctx = context(); const ctx = context();
ctx.header['content-type'] = 'image/png'; ctx.header['content-type'] = 'image/png';
ctx.header['transfer-encoding'] = 'chunked'; ctx.header['transfer-encoding'] = 'chunked';
@ -47,7 +47,7 @@ describe('ctx.is(type)', function(){
describe('given one type', function(){ describe('given one type', function(){
it('should return the type or false', function(){ it('should return the type or false', function(){
var ctx = context(); const ctx = context();
ctx.header['content-type'] = 'image/png'; ctx.header['content-type'] = 'image/png';
ctx.header['transfer-encoding'] = 'chunked'; ctx.header['transfer-encoding'] = 'chunked';
@ -67,7 +67,7 @@ describe('ctx.is(type)', function(){
describe('given multiple types', function(){ describe('given multiple types', function(){
it('should return the first match or false', function(){ it('should return the first match or false', function(){
var ctx = context(); const ctx = context();
ctx.header['content-type'] = 'image/png'; ctx.header['content-type'] = 'image/png';
ctx.header['transfer-encoding'] = 'chunked'; ctx.header['transfer-encoding'] = 'chunked';
@ -92,7 +92,7 @@ describe('ctx.is(type)', function(){
describe('when Content-Type: application/x-www-form-urlencoded', function(){ describe('when Content-Type: application/x-www-form-urlencoded', function(){
it('should match "urlencoded"', function(){ it('should match "urlencoded"', function(){
var ctx = context(); const ctx = context();
ctx.header['content-type'] = 'application/x-www-form-urlencoded'; ctx.header['content-type'] = 'application/x-www-form-urlencoded';
ctx.header['transfer-encoding'] = 'chunked'; ctx.header['transfer-encoding'] = 'chunked';

View File

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

View File

@ -1,15 +1,15 @@
'use strict'; 'use strict';
var Stream = require('stream'); const Stream = require('stream');
var http = require('http'); const http = require('http');
var koa = require('../../'); const koa = require('../../');
var context = require('../context'); const context = require('../context');
describe('ctx.origin', function(){ describe('ctx.origin', function(){
it('should return the origin of url', function(){ it('should return the origin of url', function(){
var socket = new Stream.Duplex(); const socket = new Stream.Duplex();
var req = { const req = {
url: '/users/1?next=/dashboard', url: '/users/1?next=/dashboard',
headers: { headers: {
host: 'localhost' host: 'localhost'
@ -17,7 +17,7 @@ describe('ctx.origin', function(){
socket: socket, socket: socket,
__proto__: Stream.Readable.prototype __proto__: Stream.Readable.prototype
}; };
var ctx = context(req); const ctx = context(req);
ctx.origin.should.equal('http://localhost'); ctx.origin.should.equal('http://localhost');
// change it also work // change it also work
ctx.url = '/foo/users/1?next=/dashboard'; ctx.url = '/foo/users/1?next=/dashboard';

View File

@ -1,11 +1,11 @@
'use strict'; 'use strict';
var context = require('../context'); const context = require('../context');
describe('ctx.path', function(){ describe('ctx.path', function(){
it('should return the pathname', function(){ it('should return the pathname', function(){
var ctx = context(); const ctx = context();
ctx.url = '/login?next=/dashboard'; ctx.url = '/login?next=/dashboard';
ctx.path.should.equal('/login'); ctx.path.should.equal('/login');
}) })
@ -13,7 +13,7 @@ describe('ctx.path', function(){
describe('ctx.path=', function(){ describe('ctx.path=', function(){
it('should set the pathname', function(){ it('should set the pathname', function(){
var ctx = context(); const ctx = context();
ctx.url = '/login?next=/dashboard'; ctx.url = '/login?next=/dashboard';
ctx.path = '/logout'; ctx.path = '/logout';
@ -22,7 +22,7 @@ describe('ctx.path=', function(){
}) })
it('should change .url but not .originalUrl', function(){ it('should change .url but not .originalUrl', function(){
var ctx = context({ url: '/login' }); const ctx = context({ url: '/login' });
ctx.path = '/logout'; ctx.path = '/logout';
ctx.url.should.equal('/logout'); ctx.url.should.equal('/logout');
ctx.originalUrl.should.equal('/login'); ctx.originalUrl.should.equal('/login');

View File

@ -1,12 +1,12 @@
'use strict'; 'use strict';
var request = require('../context').request; const request = require('../context').request;
describe('req.protocol', function(){ describe('req.protocol', function(){
describe('when encrypted', function(){ describe('when encrypted', function(){
it('should return "https"', function(){ it('should return "https"', function(){
var req = request(); const req = request();
req.req.socket = { encrypted: true }; req.req.socket = { encrypted: true };
req.protocol.should.equal('https'); req.protocol.should.equal('https');
}) })
@ -14,7 +14,7 @@ describe('req.protocol', function(){
describe('when unencrypted', function(){ describe('when unencrypted', function(){
it('should return "http"', function(){ it('should return "http"', function(){
var req = request(); const req = request();
req.req.socket = {}; req.req.socket = {};
req.protocol.should.equal('http'); req.protocol.should.equal('http');
}) })
@ -23,7 +23,7 @@ describe('req.protocol', function(){
describe('when X-Forwarded-Proto is set', function(){ describe('when X-Forwarded-Proto is set', function(){
describe('and proxy is trusted', function(){ describe('and proxy is trusted', function(){
it('should be used', function(){ it('should be used', function(){
var req = request(); const req = request();
req.app.proxy = true; req.app.proxy = true;
req.req.socket = {}; req.req.socket = {};
req.header['x-forwarded-proto'] = 'https, http'; req.header['x-forwarded-proto'] = 'https, http';
@ -32,7 +32,7 @@ describe('req.protocol', function(){
describe('and X-Forwarded-Proto is empty', function(){ describe('and X-Forwarded-Proto is empty', function(){
it('should return "http"', function(){ it('should return "http"', function(){
var req = request(); const req = request();
req.app.proxy = true; req.app.proxy = true;
req.req.socket = {}; req.req.socket = {};
req.header['x-forwarded-proto'] = ''; req.header['x-forwarded-proto'] = '';
@ -43,7 +43,7 @@ describe('req.protocol', function(){
describe('and proxy is not trusted', function(){ describe('and proxy is not trusted', function(){
it('should not be used', function(){ it('should not be used', function(){
var req = request(); const req = request();
req.req.socket = {}; req.req.socket = {};
req.header['x-forwarded-proto'] = 'https, http'; req.header['x-forwarded-proto'] = 'https, http';
req.protocol.should.equal('http'); req.protocol.should.equal('http');

View File

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

View File

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

View File

@ -1,18 +1,18 @@
'use strict'; 'use strict';
var context = require('../context'); const context = require('../context');
describe('ctx.search=', function(){ describe('ctx.search=', function(){
it('should replace the search', function(){ it('should replace the search', function(){
var ctx = context({ url: '/store/shoes' }); const ctx = context({ url: '/store/shoes' });
ctx.search = '?page=2&color=blue'; ctx.search = '?page=2&color=blue';
ctx.url.should.equal('/store/shoes?page=2&color=blue'); ctx.url.should.equal('/store/shoes?page=2&color=blue');
ctx.search.should.equal('?page=2&color=blue'); ctx.search.should.equal('?page=2&color=blue');
}) })
it('should update ctx.querystring and ctx.query', function(){ it('should update ctx.querystring and ctx.query', function(){
var ctx = context({ url: '/store/shoes' }); const ctx = context({ url: '/store/shoes' });
ctx.search = '?page=2&color=blue'; ctx.search = '?page=2&color=blue';
ctx.url.should.equal('/store/shoes?page=2&color=blue'); ctx.url.should.equal('/store/shoes?page=2&color=blue');
ctx.querystring.should.equal('page=2&color=blue'); ctx.querystring.should.equal('page=2&color=blue');
@ -23,7 +23,7 @@ describe('ctx.search=', function(){
}) })
it('should change .url but not .originalUrl', function(){ it('should change .url but not .originalUrl', function(){
var ctx = context({ url: '/store/shoes' }); const ctx = context({ url: '/store/shoes' });
ctx.search = '?page=2&color=blue'; ctx.search = '?page=2&color=blue';
ctx.url.should.equal('/store/shoes?page=2&color=blue'); ctx.url.should.equal('/store/shoes?page=2&color=blue');
ctx.originalUrl.should.equal('/store/shoes'); ctx.originalUrl.should.equal('/store/shoes');
@ -32,7 +32,7 @@ describe('ctx.search=', function(){
describe('when missing', function(){ describe('when missing', function(){
it('should return ""', function(){ it('should return ""', function(){
var ctx = context({ url: '/store/shoes' }); const ctx = context({ url: '/store/shoes' });
ctx.search.should.equal(''); ctx.search.should.equal('');
}) })
}) })

View File

@ -1,11 +1,11 @@
'use strict'; 'use strict';
var request = require('../context').request; const request = require('../context').request;
describe('req.secure', function(){ describe('req.secure', function(){
it('should return true when encrypted', function(){ it('should return true when encrypted', function(){
var req = request(); const req = request();
req.req.socket = { encrypted: true }; req.req.socket = { encrypted: true };
req.secure.should.be.true; req.secure.should.be.true;
}) })

View File

@ -1,11 +1,11 @@
'use strict'; 'use strict';
var context = require('../context'); const context = require('../context');
describe('req.stale', function(){ describe('req.stale', function(){
it('should be the inverse of req.fresh', function(){ it('should be the inverse of req.fresh', function(){
var ctx = context(); const ctx = context();
ctx.status = 200; ctx.status = 200;
ctx.method = 'GET'; ctx.method = 'GET';
ctx.req.headers['if-none-match'] = '"123"'; ctx.req.headers['if-none-match'] = '"123"';

View File

@ -1,11 +1,11 @@
'use strict'; 'use strict';
var request = require('../context').request; const request = require('../context').request;
describe('req.subdomains', function(){ describe('req.subdomains', function(){
it('should return subdomain array', function(){ it('should return subdomain array', function(){
var req = request(); const req = request();
req.header.host = 'tobi.ferrets.example.com'; req.header.host = 'tobi.ferrets.example.com';
req.app.subdomainOffset = 2; req.app.subdomainOffset = 2;
req.subdomains.should.eql(['ferrets', 'tobi']); req.subdomains.should.eql(['ferrets', 'tobi']);
@ -15,7 +15,7 @@ describe('req.subdomains', function(){
}) })
describe('with no host present', function(){ describe('with no host present', function(){
var req = request(); const req = request();
req.subdomains.should.eql([]); req.subdomains.should.eql([]);
}) })
}) })

View File

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

View File

@ -1,18 +1,18 @@
'use strict'; 'use strict';
var context = require('../context'); const context = require('../context');
describe('ctx.append(name, val)', function(){ describe('ctx.append(name, val)', function(){
it('should append multiple headers', function(){ it('should append multiple headers', function(){
var ctx = context(); const ctx = context();
ctx.append('x-foo', 'bar1'); ctx.append('x-foo', 'bar1');
ctx.append('x-foo', 'bar2'); ctx.append('x-foo', 'bar2');
ctx.response.header['x-foo'].should.eql(['bar1', 'bar2']); ctx.response.header['x-foo'].should.eql(['bar1', 'bar2']);
}) })
it('should accept array of values', function (){ it('should accept array of values', function (){
var ctx = context(); const ctx = context();
ctx.append('Set-Cookie', ['foo=bar', 'fizz=buzz']); ctx.append('Set-Cookie', ['foo=bar', 'fizz=buzz']);
ctx.append('Set-Cookie', 'hi=again'); ctx.append('Set-Cookie', 'hi=again');
@ -20,7 +20,7 @@ describe('ctx.append(name, val)', function(){
}) })
it('should get reset by res.set(field, val)', function (){ it('should get reset by res.set(field, val)', function (){
var ctx = context(); const ctx = context();
ctx.append('Link', '<http://localhost/>'); ctx.append('Link', '<http://localhost/>');
ctx.append('Link', '<http://localhost:80/>'); ctx.append('Link', '<http://localhost:80/>');
@ -31,7 +31,7 @@ describe('ctx.append(name, val)', function(){
}) })
it('should work with res.set(field, val) first', function (){ it('should work with res.set(field, val) first', function (){
var ctx = context(); const ctx = context();
ctx.set('Link', '<http://localhost/>'); ctx.set('Link', '<http://localhost/>');
ctx.append('Link', '<http://localhost:80/>'); ctx.append('Link', '<http://localhost:80/>');

View File

@ -1,23 +1,23 @@
'use strict'; 'use strict';
var context = require('../context'); const context = require('../context');
var request = require('supertest'); const request = require('supertest');
var koa = require('../..'); const koa = require('../..');
describe('ctx.attachment([filename])', function(){ describe('ctx.attachment([filename])', function(){
describe('when given a filename', function(){ describe('when given a filename', function(){
it('should set the filename param', function(){ it('should set the filename param', function(){
var ctx = context(); const ctx = context();
ctx.attachment('path/to/tobi.png'); ctx.attachment('path/to/tobi.png');
var str = 'attachment; filename="tobi.png"'; const str = 'attachment; filename="tobi.png"';
ctx.response.header['content-disposition'].should.equal(str); ctx.response.header['content-disposition'].should.equal(str);
}) })
}) })
describe('when omitting filename', function(){ describe('when omitting filename', function(){
it('should not set filename param', function(){ it('should not set filename param', function(){
var ctx = context(); const ctx = context();
ctx.attachment(); ctx.attachment();
ctx.response.header['content-disposition'].should.equal('attachment'); ctx.response.header['content-disposition'].should.equal('attachment');
}) })
@ -25,14 +25,14 @@ describe('ctx.attachment([filename])', function(){
describe('when given a no-ascii filename', function(){ describe('when given a no-ascii filename', function(){
it('should set the encodeURI filename param', function(){ it('should set the encodeURI filename param', function(){
var ctx = context(); const ctx = context();
ctx.attachment('path/to/include-no-ascii-char-中文名-ok.png'); ctx.attachment('path/to/include-no-ascii-char-中文名-ok.png');
var str = 'attachment; filename=\"include-no-ascii-char-???-ok.png\"; filename*=UTF-8\'\'include-no-ascii-char-%E4%B8%AD%E6%96%87%E5%90%8D-ok.png'; const str = 'attachment; filename=\"include-no-ascii-char-???-ok.png\"; filename*=UTF-8\'\'include-no-ascii-char-%E4%B8%AD%E6%96%87%E5%90%8D-ok.png';
ctx.response.header['content-disposition'].should.equal(str); ctx.response.header['content-disposition'].should.equal(str);
}) })
it('should work with http client', function(done){ it('should work with http client', function(done){
var app = koa(); const app = koa();
app.use(function* (next){ app.use(function* (next){
this.attachment('path/to/include-no-ascii-char-中文名-ok.json') this.attachment('path/to/include-no-ascii-char-中文名-ok.json')

View File

@ -1,14 +1,14 @@
'use strict'; 'use strict';
var response = require('../context').response; const response = require('../context').response;
var assert = require('assert'); const assert = require('assert');
var fs = require('fs'); const fs = require('fs');
describe('res.body=', function(){ describe('res.body=', function(){
describe('when Content-Type is set', function(){ describe('when Content-Type is set', function(){
it('should not override', function(){ it('should not override', function(){
var res = response(); const res = response();
res.type = 'png'; res.type = 'png';
res.body = new Buffer('something'); res.body = new Buffer('something');
assert('image/png' == res.header['content-type']); assert('image/png' == res.header['content-type']);
@ -16,7 +16,7 @@ describe('res.body=', function(){
describe('when body is an object', function(){ describe('when body is an object', function(){
it('should override as json', function(){ it('should override as json', function(){
var res = response(); const res = response();
res.body = '<em>hey</em>'; res.body = '<em>hey</em>';
assert('text/html; charset=utf-8' == res.header['content-type']); assert('text/html; charset=utf-8' == res.header['content-type']);
@ -27,7 +27,7 @@ describe('res.body=', function(){
}) })
it('should override length', function(){ it('should override length', function(){
var res = response(); const res = response();
res.type = 'html'; res.type = 'html';
res.body = 'something'; res.body = 'something';
res.length.should.equal(9); res.length.should.equal(9);
@ -36,20 +36,20 @@ describe('res.body=', function(){
describe('when a string is given', function(){ describe('when a string is given', function(){
it('should default to text', function(){ it('should default to text', function(){
var res = response(); const res = response();
res.body = 'Tobi'; res.body = 'Tobi';
assert('text/plain; charset=utf-8' == res.header['content-type']); assert('text/plain; charset=utf-8' == res.header['content-type']);
}) })
it('should set length', function(){ it('should set length', function(){
var res = response(); const res = response();
res.body = 'Tobi'; res.body = 'Tobi';
assert('4' == res.header['content-length']); assert('4' == res.header['content-length']);
}) })
describe('and contains a non-leading <', function(){ describe('and contains a non-leading <', function(){
it('should default to text', function(){ it('should default to text', function(){
var res = response(); const res = response();
res.body = 'aklsdjf < klajsdlfjasd'; res.body = 'aklsdjf < klajsdlfjasd';
assert('text/plain; charset=utf-8' == res.header['content-type']); assert('text/plain; charset=utf-8' == res.header['content-type']);
}) })
@ -58,21 +58,21 @@ describe('res.body=', function(){
describe('when an html string is given', function(){ describe('when an html string is given', function(){
it('should default to html', function(){ it('should default to html', function(){
var res = response(); const res = response();
res.body = '<h1>Tobi</h1>'; res.body = '<h1>Tobi</h1>';
assert('text/html; charset=utf-8' == res.header['content-type']); assert('text/html; charset=utf-8' == res.header['content-type']);
}) })
it('should set length', function(){ it('should set length', function(){
var string = '<h1>Tobi</h1>'; const string = '<h1>Tobi</h1>';
var res = response(); const res = response();
res.body = string; res.body = string;
assert.equal(res.length, Buffer.byteLength(string)); assert.equal(res.length, Buffer.byteLength(string));
}) })
it('should set length when body is overridden', function(){ it('should set length when body is overridden', function(){
var string = '<h1>Tobi</h1>'; const string = '<h1>Tobi</h1>';
var res = response(); const res = response();
res.body = string; res.body = string;
res.body = string + string; res.body = string + string;
assert.equal(res.length, 2 * Buffer.byteLength(string)); assert.equal(res.length, 2 * Buffer.byteLength(string));
@ -80,7 +80,7 @@ describe('res.body=', function(){
describe('when it contains leading whitespace', function(){ describe('when it contains leading whitespace', function(){
it('should default to html', function(){ it('should default to html', function(){
var res = response(); const res = response();
res.body = ' <h1>Tobi</h1>'; res.body = ' <h1>Tobi</h1>';
assert('text/html; charset=utf-8' == res.header['content-type']); assert('text/html; charset=utf-8' == res.header['content-type']);
}) })
@ -96,7 +96,7 @@ describe('res.body=', function(){
* You should `.type=` if this simple test fails. * You should `.type=` if this simple test fails.
*/ */
var res = response(); const res = response();
res.body = '<?xml version="1.0" encoding="UTF-8"?>\n<俄语>данные</俄语>'; res.body = '<?xml version="1.0" encoding="UTF-8"?>\n<俄语>данные</俄语>';
assert('text/html; charset=utf-8' == res.header['content-type']); assert('text/html; charset=utf-8' == res.header['content-type']);
}) })
@ -104,7 +104,7 @@ describe('res.body=', function(){
describe('when a stream is given', function(){ describe('when a stream is given', function(){
it('should default to an octet stream', function(){ it('should default to an octet stream', function(){
var res = response(); const res = response();
res.body = fs.createReadStream('LICENSE'); res.body = fs.createReadStream('LICENSE');
assert('application/octet-stream' == res.header['content-type']); assert('application/octet-stream' == res.header['content-type']);
}) })
@ -112,13 +112,13 @@ describe('res.body=', function(){
describe('when a buffer is given', function(){ describe('when a buffer is given', function(){
it('should default to an octet stream', function(){ it('should default to an octet stream', function(){
var res = response(); const res = response();
res.body = new Buffer('hey'); res.body = new Buffer('hey');
assert('application/octet-stream' == res.header['content-type']); assert('application/octet-stream' == res.header['content-type']);
}) })
it('should set length', function(){ it('should set length', function(){
var res = response(); const res = response();
res.body = new Buffer('Tobi'); res.body = new Buffer('Tobi');
assert('4' == res.header['content-length']); assert('4' == res.header['content-length']);
}) })
@ -126,7 +126,7 @@ describe('res.body=', function(){
describe('when an object is given', function(){ describe('when an object is given', function(){
it('should default to json', function(){ it('should default to json', function(){
var res = response(); const res = response();
res.body = { foo: 'bar' }; res.body = { foo: 'bar' };
assert('application/json; charset=utf-8' == res.header['content-type']); assert('application/json; charset=utf-8' == res.header['content-type']);
}) })

View File

@ -1,23 +1,23 @@
'use strict'; 'use strict';
var response = require('../context').response; const response = require('../context').response;
describe('res.etag=', function(){ describe('res.etag=', function(){
it('should not modify an etag with quotes', function(){ it('should not modify an etag with quotes', function(){
var res = response(); const res = response();
res.etag = '"asdf"'; res.etag = '"asdf"';
res.header.etag.should.equal('"asdf"'); res.header.etag.should.equal('"asdf"');
}) })
it('should not modify a weak etag', function(){ it('should not modify a weak etag', function(){
var res = response(); const res = response();
res.etag = 'W/"asdf"'; res.etag = 'W/"asdf"';
res.header.etag.should.equal('W/"asdf"'); res.header.etag.should.equal('W/"asdf"');
}) })
it('should add quotes around an etag if necessary', function(){ it('should add quotes around an etag if necessary', function(){
var res = response(); const res = response();
res.etag = 'asdf'; res.etag = 'asdf';
res.header.etag.should.equal('"asdf"'); res.header.etag.should.equal('"asdf"');
}) })
@ -25,7 +25,7 @@ describe('res.etag=', function(){
describe('res.etag', function(){ describe('res.etag', function(){
it('should return etag', function(){ it('should return etag', function(){
var res = response(); const res = response();
res.etag = '"asdf"'; res.etag = '"asdf"';
res.etag.should.equal('"asdf"'); res.etag.should.equal('"asdf"');
}) })

View File

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

View File

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

View File

@ -1,13 +1,13 @@
'use strict'; 'use strict';
var response = require('../context').response; const response = require('../context').response;
var assert = require('assert'); const assert = require('assert');
describe('res.inspect()', function(){ describe('res.inspect()', function(){
describe('with no response.res present', function(){ describe('with no response.res present', function(){
it('should return null', function(){ it('should return null', function(){
var res = response(); const res = response();
res.body = 'hello'; res.body = 'hello';
delete res.res; delete res.res;
assert(null == res.inspect()); assert(null == res.inspect());
@ -15,7 +15,7 @@ describe('res.inspect()', function(){
}) })
it('should return a json representation', function(){ it('should return a json representation', function(){
var res = response(); const res = response();
res.body = 'hello'; res.body = 'hello';
res.inspect().should.eql({ res.inspect().should.eql({

View File

@ -1,13 +1,13 @@
'use strict'; 'use strict';
var context = require('../context'); const context = require('../context');
var should = require('should'); const should = require('should');
var assert = require('assert'); const assert = require('assert');
describe('response.is(type)', function(){ describe('response.is(type)', function(){
it('should ignore params', function(){ it('should ignore params', function(){
var res = context().response; const res = context().response;
res.type = 'text/html; charset=utf-8'; res.type = 'text/html; charset=utf-8';
res.is('text/*').should.equal('text/html'); res.is('text/*').should.equal('text/html');
@ -15,7 +15,7 @@ describe('response.is(type)', function(){
describe('when no type is set', function(){ describe('when no type is set', function(){
it('should return false', function(){ it('should return false', function(){
var res = context().response; const res = context().response;
assert(false === res.is()); assert(false === res.is());
assert(false === res.is('html')); assert(false === res.is('html'));
@ -24,7 +24,7 @@ describe('response.is(type)', function(){
describe('when given no types', function(){ describe('when given no types', function(){
it('should return the type', function(){ it('should return the type', function(){
var res = context().response; const res = context().response;
res.type = 'text/html; charset=utf-8'; res.type = 'text/html; charset=utf-8';
res.is().should.equal('text/html'); res.is().should.equal('text/html');
@ -33,7 +33,7 @@ describe('response.is(type)', function(){
describe('given one type', function(){ describe('given one type', function(){
it('should return the type or false', function(){ it('should return the type or false', function(){
var res = context().response; const res = context().response;
res.type = 'image/png'; res.type = 'image/png';
res.is('png').should.equal('png'); res.is('png').should.equal('png');
@ -52,7 +52,7 @@ describe('response.is(type)', function(){
describe('given multiple types', function(){ describe('given multiple types', function(){
it('should return the first match or false', function(){ it('should return the first match or false', function(){
var res = context().response; const res = context().response;
res.type = 'image/png'; res.type = 'image/png';
res.is('png').should.equal('png'); res.is('png').should.equal('png');
@ -76,7 +76,7 @@ describe('response.is(type)', function(){
describe('when Content-Type: application/x-www-form-urlencoded', function(){ describe('when Content-Type: application/x-www-form-urlencoded', function(){
it('should match "urlencoded"', function(){ it('should match "urlencoded"', function(){
var res = context().response; const res = context().response;
res.type = 'application/x-www-form-urlencoded'; res.type = 'application/x-www-form-urlencoded';
res.is('urlencoded').should.equal('urlencoded'); res.is('urlencoded').should.equal('urlencoded');

View File

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

View File

@ -1,15 +1,15 @@
'use strict'; 'use strict';
var response = require('../context').response; const response = require('../context').response;
var should = require('should'); const should = require('should');
var assert = require('assert'); const assert = require('assert');
var fs = require('fs'); const fs = require('fs');
describe('res.length', function(){ describe('res.length', function(){
describe('when Content-Length is defined', function(){ describe('when Content-Length is defined', function(){
it('should return a number', function(){ it('should return a number', function(){
var res = response(); const res = response();
res.header['content-length'] = '120'; res.header['content-length'] = '120';
res.length.should.equal(120); res.length.should.equal(120);
}) })
@ -19,7 +19,7 @@ describe('res.length', function(){
describe('res.length', function(){ describe('res.length', function(){
describe('when Content-Length is defined', function(){ describe('when Content-Length is defined', function(){
it('should return a number', function(){ it('should return a number', function(){
var res = response(); const res = response();
res.set('Content-Length', '1024'); res.set('Content-Length', '1024');
res.length.should.equal(1024); res.length.should.equal(1024);
}) })
@ -28,7 +28,7 @@ describe('res.length', function(){
describe('when Content-Length is not defined', function(){ describe('when Content-Length is not defined', function(){
describe('and a .body is set', function(){ describe('and a .body is set', function(){
it('should return a number', function(){ it('should return a number', function(){
var res = response(); const res = response();
res.body = 'foo'; res.body = 'foo';
res.remove('Content-Length'); res.remove('Content-Length');
@ -61,7 +61,7 @@ describe('res.length', function(){
describe('and .body is not', function(){ describe('and .body is not', function(){
it('should return undefined', function(){ it('should return undefined', function(){
var res = response(); const res = response();
assert(null == res.length); assert(null == res.length);
}) })
}) })

View File

@ -1,19 +1,19 @@
'use strict'; 'use strict';
var response = require('../context').response; const response = require('../context').response;
var Stream = require('stream'); const Stream = require('stream');
describe('res.message', function(){ describe('res.message', function(){
it('should return the response status message', function(){ it('should return the response status message', function(){
var res = response(); const res = response();
res.status = 200; res.status = 200;
res.message.should.equal('OK'); res.message.should.equal('OK');
}) })
describe('when res.message not present', function(){ describe('when res.message not present', function(){
it('should look up in statuses', function(){ it('should look up in statuses', function(){
var res = response(); const res = response();
res.res.statusCode = 200; res.res.statusCode = 200;
res.message.should.equal('OK'); res.message.should.equal('OK');
}) })
@ -22,7 +22,7 @@ describe('res.message', function(){
describe('res.message=', function(){ describe('res.message=', function(){
it('should set response status message', function(){ it('should set response status message', function(){
var res = response(); const res = response();
res.status = 200; res.status = 200;
res.message = 'ok'; res.message = 'ok';
res.res.statusMessage.should.equal('ok'); res.res.statusMessage.should.equal('ok');

View File

@ -1,11 +1,11 @@
'use strict'; 'use strict';
var context = require('../context'); const context = require('../context');
describe('ctx.redirect(url)', function(){ describe('ctx.redirect(url)', function(){
it('should redirect to the given url', function(){ it('should redirect to the given url', function(){
var ctx = context(); const ctx = context();
ctx.redirect('http://google.com'); ctx.redirect('http://google.com');
ctx.response.header.location.should.equal('http://google.com'); ctx.response.header.location.should.equal('http://google.com');
ctx.status.should.equal(302); ctx.status.should.equal(302);
@ -13,27 +13,27 @@ describe('ctx.redirect(url)', function(){
describe('with "back"', function(){ describe('with "back"', function(){
it('should redirect to Referrer', function(){ it('should redirect to Referrer', function(){
var ctx = context(); const ctx = context();
ctx.req.headers.referrer = '/login'; ctx.req.headers.referrer = '/login';
ctx.redirect('back'); ctx.redirect('back');
ctx.response.header.location.should.equal('/login'); ctx.response.header.location.should.equal('/login');
}) })
it('should redirect to Referer', function(){ it('should redirect to Referer', function(){
var ctx = context(); const ctx = context();
ctx.req.headers.referer = '/login'; ctx.req.headers.referer = '/login';
ctx.redirect('back'); ctx.redirect('back');
ctx.response.header.location.should.equal('/login'); ctx.response.header.location.should.equal('/login');
}) })
it('should default to alt', function(){ it('should default to alt', function(){
var ctx = context(); const ctx = context();
ctx.redirect('back', '/index.html'); ctx.redirect('back', '/index.html');
ctx.response.header.location.should.equal('/index.html'); ctx.response.header.location.should.equal('/index.html');
}) })
it('should default redirect to /', function(){ it('should default redirect to /', function(){
var ctx = context(); const ctx = context();
ctx.redirect('back'); ctx.redirect('back');
ctx.response.header.location.should.equal('/'); ctx.response.header.location.should.equal('/');
}) })
@ -41,8 +41,8 @@ describe('ctx.redirect(url)', function(){
describe('when html is accepted', function(){ describe('when html is accepted', function(){
it('should respond with html', function(){ it('should respond with html', function(){
var ctx = context(); const ctx = context();
var url = 'http://google.com'; const url = 'http://google.com';
ctx.header.accept = 'text/html'; ctx.header.accept = 'text/html';
ctx.redirect(url); ctx.redirect(url);
ctx.response.header['content-type'].should.equal('text/html; charset=utf-8'); ctx.response.header['content-type'].should.equal('text/html; charset=utf-8');
@ -50,7 +50,7 @@ describe('ctx.redirect(url)', function(){
}) })
it('should escape the url', function(){ it('should escape the url', function(){
var ctx = context(); const ctx = context();
var url = '<script>'; var url = '<script>';
ctx.header.accept = 'text/html'; ctx.header.accept = 'text/html';
ctx.redirect(url); ctx.redirect(url);
@ -62,8 +62,8 @@ describe('ctx.redirect(url)', function(){
describe('when text is accepted', function(){ describe('when text is accepted', function(){
it('should respond with text', function(){ it('should respond with text', function(){
var ctx = context(); const ctx = context();
var url = 'http://google.com'; const url = 'http://google.com';
ctx.header.accept = 'text/plain'; ctx.header.accept = 'text/plain';
ctx.redirect(url); ctx.redirect(url);
ctx.body.should.equal('Redirecting to ' + url + '.'); ctx.body.should.equal('Redirecting to ' + url + '.');
@ -72,8 +72,8 @@ describe('ctx.redirect(url)', function(){
describe('when status is 301', function(){ describe('when status is 301', function(){
it('should not change the status code', function(){ it('should not change the status code', function(){
var ctx = context(); const ctx = context();
var url = 'http://google.com'; const url = 'http://google.com';
ctx.status = 301; ctx.status = 301;
ctx.header.accept = 'text/plain'; ctx.header.accept = 'text/plain';
ctx.redirect('http://google.com'); ctx.redirect('http://google.com');
@ -84,8 +84,8 @@ describe('ctx.redirect(url)', function(){
describe('when status is 304', function(){ describe('when status is 304', function(){
it('should change the status code', function(){ it('should change the status code', function(){
var ctx = context(); const ctx = context();
var url = 'http://google.com'; const url = 'http://google.com';
ctx.status = 304; ctx.status = 304;
ctx.header.accept = 'text/plain'; ctx.header.accept = 'text/plain';
ctx.redirect('http://google.com'); ctx.redirect('http://google.com');
@ -96,9 +96,9 @@ describe('ctx.redirect(url)', function(){
describe('when content-type was present', function(){ describe('when content-type was present', function(){
it('should overwrite content-type', function() { it('should overwrite content-type', function() {
var ctx = context(); const ctx = context();
ctx.body = {}; ctx.body = {};
var url = 'http://google.com'; const url = 'http://google.com';
ctx.header.accept = 'text/plain'; ctx.header.accept = 'text/plain';
ctx.redirect('http://google.com'); ctx.redirect('http://google.com');
ctx.status.should.equal(302); ctx.status.should.equal(302);

View File

@ -1,11 +1,11 @@
'use strict'; 'use strict';
var context = require('../context'); const context = require('../context');
describe('ctx.remove(name)', function(){ describe('ctx.remove(name)', function(){
it('should remove a field', function(){ it('should remove a field', function(){
var ctx = context(); const ctx = context();
ctx.set('x-foo', 'bar'); ctx.set('x-foo', 'bar');
ctx.remove('x-foo'); ctx.remove('x-foo');
ctx.response.header.should.eql({}); ctx.response.header.should.eql({});

View File

@ -1,23 +1,23 @@
'use strict'; 'use strict';
var context = require('../context'); const context = require('../context');
describe('ctx.set(name, val)', function(){ describe('ctx.set(name, val)', function(){
it('should set a field value', function(){ it('should set a field value', function(){
var ctx = context(); const ctx = context();
ctx.set('x-foo', 'bar'); ctx.set('x-foo', 'bar');
ctx.response.header['x-foo'].should.equal('bar'); ctx.response.header['x-foo'].should.equal('bar');
}) })
it('should coerce to a string', function(){ it('should coerce to a string', function(){
var ctx = context(); const ctx = context();
ctx.set('x-foo', 5); ctx.set('x-foo', 5);
ctx.response.header['x-foo'].should.equal('5'); ctx.response.header['x-foo'].should.equal('5');
}) })
it('should set a field value of array', function(){ it('should set a field value of array', function(){
var ctx = context(); const ctx = context();
ctx.set('x-foo', ['foo', 'bar']); ctx.set('x-foo', ['foo', 'bar']);
ctx.response.header['x-foo'].should.eql([ 'foo', 'bar' ]); ctx.response.header['x-foo'].should.eql([ 'foo', 'bar' ]);
}) })
@ -25,7 +25,7 @@ describe('ctx.set(name, val)', function(){
describe('ctx.set(object)', function(){ describe('ctx.set(object)', function(){
it('should set multiple fields', function(){ it('should set multiple fields', function(){
var ctx = context(); const ctx = context();
ctx.set({ ctx.set({
foo: '1', foo: '1',

View File

@ -1,12 +1,12 @@
'use strict'; 'use strict';
var response = require('../context').response; const response = require('../context').response;
var Stream = require('stream'); const Stream = require('stream');
describe('res.socket', function(){ describe('res.socket', function(){
it('should return the request socket object', function(){ it('should return the request socket object', function(){
var res = response(); const res = response();
res.socket.should.be.instanceOf(Stream); res.socket.should.be.instanceOf(Stream);
}) })
}) })

View File

@ -1,17 +1,17 @@
'use strict'; 'use strict';
var response = require('../context').response; const response = require('../context').response;
var request = require('supertest'); const request = require('supertest');
var statuses = require('statuses'); const statuses = require('statuses');
var assert = require('assert'); const assert = require('assert');
var koa = require('../..'); const koa = require('../..');
describe('res.status=', function(){ describe('res.status=', function(){
describe('when a status code', function(){ describe('when a status code', function(){
describe('and valid', function(){ describe('and valid', function(){
it('should set the status', function(){ it('should set the status', function(){
var res = response(); const res = response();
res.status = 403; res.status = 403;
res.status.should.equal(403); res.status.should.equal(403);
}) })
@ -37,7 +37,7 @@ describe('res.status=', function(){
}) })
it('should set the status', function (){ it('should set the status', function (){
var res = response(); const res = response();
res.status = 700; res.status = 700;
res.status.should.equal(700); res.status.should.equal(700);
}) })
@ -60,7 +60,7 @@ describe('res.status=', function(){
function strip(status) { function strip(status) {
it('should strip content related header fields', function(done){ it('should strip content related header fields', function(done){
var app = koa(); const app = koa();
app.use(function *(){ app.use(function *(){
this.body = { foo: 'bar' }; this.body = { foo: 'bar' };
@ -86,7 +86,7 @@ describe('res.status=', function(){
}) })
it('should strip content releated header fields after status set', function(done) { it('should strip content releated header fields after status set', function(done) {
var app = koa(); const app = koa();
app.use(function *(){ app.use(function *(){
this.status = status; this.status = status;

View File

@ -1,13 +1,13 @@
'use strict'; 'use strict';
var context = require('../context'); const context = require('../context');
var assert = require('assert'); const assert = require('assert');
describe('ctx.type=', function(){ describe('ctx.type=', function(){
describe('with a mime', function(){ describe('with a mime', function(){
it('should set the Content-Type', function(){ it('should set the Content-Type', function(){
var ctx = context(); const ctx = context();
ctx.type = 'text/plain'; ctx.type = 'text/plain';
ctx.type.should.equal('text/plain'); ctx.type.should.equal('text/plain');
ctx.response.header['content-type'].should.equal('text/plain; charset=utf-8'); ctx.response.header['content-type'].should.equal('text/plain; charset=utf-8');
@ -16,7 +16,7 @@ describe('ctx.type=', function(){
describe('with an extension', function(){ describe('with an extension', function(){
it('should lookup the mime', function(){ it('should lookup the mime', function(){
var ctx = context(); const ctx = context();
ctx.type = 'json'; ctx.type = 'json';
ctx.type.should.equal('application/json'); ctx.type.should.equal('application/json');
ctx.response.header['content-type'].should.equal('application/json; charset=utf-8'); ctx.response.header['content-type'].should.equal('application/json; charset=utf-8');
@ -25,7 +25,7 @@ describe('ctx.type=', function(){
describe('without a charset', function(){ describe('without a charset', function(){
it('should default the charset', function(){ it('should default the charset', function(){
var ctx = context(); const ctx = context();
ctx.type = 'text/html'; ctx.type = 'text/html';
ctx.type.should.equal('text/html'); ctx.type.should.equal('text/html');
ctx.response.header['content-type'].should.equal('text/html; charset=utf-8'); ctx.response.header['content-type'].should.equal('text/html; charset=utf-8');
@ -34,7 +34,7 @@ describe('ctx.type=', function(){
describe('with a charset', function(){ describe('with a charset', function(){
it('should not default the charset', function(){ it('should not default the charset', function(){
var ctx = context(); const ctx = context();
ctx.type = 'text/html; charset=foo'; ctx.type = 'text/html; charset=foo';
ctx.type.should.equal('text/html'); ctx.type.should.equal('text/html');
ctx.response.header['content-type'].should.equal('text/html; charset=foo'); ctx.response.header['content-type'].should.equal('text/html; charset=foo');
@ -43,7 +43,7 @@ describe('ctx.type=', function(){
describe('with an unknown extension', function(){ describe('with an unknown extension', function(){
it('should default to application/octet-stream',function(){ it('should default to application/octet-stream',function(){
var ctx = context(); const ctx = context();
ctx.type = 'asdf'; ctx.type = 'asdf';
ctx.type.should.equal('application/octet-stream'); ctx.type.should.equal('application/octet-stream');
ctx.response.header['content-type'].should.equal('application/octet-stream'); ctx.response.header['content-type'].should.equal('application/octet-stream');
@ -54,7 +54,7 @@ describe('ctx.type=', function(){
describe('ctx.type', function(){ describe('ctx.type', function(){
describe('with no Content-Type', function(){ describe('with no Content-Type', function(){
it('should return ""', function(){ it('should return ""', function(){
var ctx = context(); const ctx = context();
// TODO: this is lame // TODO: this is lame
assert('' === ctx.type); assert('' === ctx.type);
}) })
@ -62,7 +62,7 @@ describe('ctx.type', function(){
describe('with a Content-Type', function(){ describe('with a Content-Type', function(){
it('should return the mime', function(){ it('should return the mime', function(){
var ctx = context(); const ctx = context();
ctx.type = 'json'; ctx.type = 'json';
ctx.type.should.equal('application/json'); ctx.type.should.equal('application/json');
}) })

View File

@ -1,12 +1,12 @@
'use strict'; 'use strict';
var context = require('../context'); const context = require('../context');
describe('ctx.vary(field)', function(){ describe('ctx.vary(field)', function(){
describe('when Vary is not set', function(){ describe('when Vary is not set', function(){
it('should set it', function(){ it('should set it', function(){
var ctx = context(); const ctx = context();
ctx.vary('Accept'); ctx.vary('Accept');
ctx.response.header.vary.should.equal('Accept'); ctx.response.header.vary.should.equal('Accept');
}) })
@ -14,7 +14,7 @@ describe('ctx.vary(field)', function(){
describe('when Vary is set', function(){ describe('when Vary is set', function(){
it('should append', function(){ it('should append', function(){
var ctx = context(); const ctx = context();
ctx.vary('Accept'); ctx.vary('Accept');
ctx.vary('Accept-Encoding'); ctx.vary('Accept-Encoding');
ctx.response.header.vary.should.equal('Accept, Accept-Encoding'); ctx.response.header.vary.should.equal('Accept, Accept-Encoding');
@ -23,7 +23,7 @@ describe('ctx.vary(field)', function(){
describe('when Vary already contains the value', function(){ describe('when Vary already contains the value', function(){
it('should not append', function(){ it('should not append', function(){
var ctx = context(); const ctx = context();
ctx.vary('Accept'); ctx.vary('Accept');
ctx.vary('Accept-Encoding'); ctx.vary('Accept-Encoding');
ctx.vary('Accept'); ctx.vary('Accept');

View File

@ -1,17 +1,17 @@
'use strict'; 'use strict';
var response = require('../context').response; const response = require('../context').response;
describe('res.writable', function(){ describe('res.writable', function(){
it('should return the request is writable', function(){ it('should return the request is writable', function(){
var res = response(); const res = response();
res.writable.should.be.ok; res.writable.should.be.ok;
}) })
describe('when res.socket not present', function (){ describe('when res.socket not present', function (){
it('should return the request is not writable', function (){ it('should return the request is not writable', function (){
var res = response(); const res = response();
res.res.socket = null; res.res.socket = null;
res.writable.should.not.be.ok; res.writable.should.not.be.ok;
}) })