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
```js
var koa = require('koa');
var app = koa();
const koa = require('koa');
const app = koa();
// logger
app.use(function *(next){
var start = new Date;
const start = new Date;
yield next;
var ms = new Date - start;
const ms = new Date - start;
console.log('%s %s - %s', this.method, this.url, ms);
});

View File

@ -1,15 +1,15 @@
'use strict';
var http = require('http');
var koa = require('../..');
var app = koa();
const http = require('http');
const koa = require('../..');
const app = koa();
app.experimental = true;
// 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);
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){
await next;

View File

@ -1,13 +1,13 @@
'use strict';
var http = require('http');
var koa = require('..');
var app = koa();
const http = require('http');
const koa = require('..');
const app = koa();
// number of middleware
var n = parseInt(process.env.MW || '1', 10);
const n = parseInt(process.env.MW || '1', 10);
console.log(' %s middleware', 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){
yield *next;

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,11 +1,11 @@
'use strict';
var Stream = require('stream');
var koa = require('..');
const Stream = require('stream');
const koa = require('..');
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 };
res = res || { _headers: {}, socket: socket, __proto__: Stream.Writable.prototype };
res.getHeader = function(k){ return res._headers[k.toLowerCase()] };

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,13 +1,13 @@
'use strict';
var context = require('../context');
const context = require('../context');
describe('ctx.accepts(types)', function(){
describe('with no arguments', function(){
describe('when Accept is populated', 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.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('when Accept is populated', 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.accepts('image/png', 'image/tiff').should.be.false;
})
@ -25,7 +25,7 @@ describe('ctx.accepts(types)', function(){
describe('when Accept is not populated', 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');
})
})
@ -33,7 +33,7 @@ describe('ctx.accepts(types)', function(){
describe('when extensions are given', function(){
it('should convert to mime types', function(){
var ctx = context();
const ctx = context();
ctx.req.headers.accept = 'text/plain, text/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(){
it('should return the first match', function(){
var ctx = context();
const ctx = context();
ctx.req.headers.accept = 'text/plain, text/html';
ctx.accepts(['png', 'text', 'html']).should.equal('text');
ctx.accepts(['png', 'html']).should.equal('html');
@ -54,7 +54,7 @@ describe('ctx.accepts(types)', function(){
describe('when multiple arguments are given', function(){
it('should return the first match', function(){
var ctx = context();
const ctx = context();
ctx.req.headers.accept = 'text/plain, text/html';
ctx.accepts('png', 'text', 'html').should.equal('text');
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(){
it('should return the type', function(){
var ctx = context();
const ctx = context();
ctx.req.headers.accept = 'text/plain, text/html';
ctx.accepts('text/html').should.equal('text/html');
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(){
it('should return the type', function(){
var ctx = context();
const ctx = context();
ctx.req.headers.accept = 'application/json, */*';
ctx.accepts('text/html').should.equal('text/html');
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(){
it('should return the type', function(){
var ctx = context();
const ctx = context();
ctx.req.headers.accept = 'application/json, text/*';
ctx.accepts('text/html').should.equal('text/html');
ctx.accepts('text/plain').should.equal('text/plain');

View File

@ -1,13 +1,13 @@
'use strict';
var context = require('../context');
const context = require('../context');
describe('ctx.acceptsCharsets()', function(){
describe('with no arguments', function(){
describe('when Accept-Charset is populated', 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.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('if any types match', 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.acceptsCharsets('utf-7', 'utf-8').should.equal('utf-8');
})
@ -26,7 +26,7 @@ describe('ctx.acceptsCharsets()', function(){
describe('if no types match', 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.acceptsCharsets('utf-16').should.be.false;
})
@ -35,7 +35,7 @@ describe('ctx.acceptsCharsets()', function(){
describe('when Accept-Charset is not populated', function(){
it('should return the first type', function(){
var ctx = context();
const ctx = context();
ctx.acceptsCharsets('utf-7', 'utf-8').should.equal('utf-7');
})
})
@ -43,7 +43,7 @@ describe('ctx.acceptsCharsets()', function(){
describe('with an array', 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.acceptsCharsets(['utf-7', 'utf-8']).should.equal('utf-8');
})

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,17 +1,17 @@
'use strict';
var context = require('../context');
const context = require('../context');
describe('ctx.query', function(){
describe('when missing', function(){
it('should return an empty object', function(){
var ctx = context({ url: '/' });
const ctx = context({ url: '/' });
ctx.query.should.eql({});
})
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.should.equal('2');
done();
@ -19,14 +19,14 @@ describe('ctx.query', 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');
})
})
describe('ctx.query=', 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.url.should.equal('/store/shoes?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(){
var ctx = context({ url: '/store/shoes' });
const ctx = context({ url: '/store/shoes' });
ctx.query = { page: 2 };
ctx.url.should.equal('/store/shoes?page=2');
ctx.originalUrl.should.equal('/store/shoes');

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,18 +1,18 @@
'use strict';
var request = require('../context').request;
var assert = require('assert');
const request = require('../context').request;
const assert = require('assert');
describe('req.type', 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.type.should.equal('text/html');
})
describe('with no host present', function(){
var req = request();
const req = request();
assert('' === req.type);
})
})

View File

@ -1,18 +1,18 @@
'use strict';
var context = require('../context');
const context = require('../context');
describe('ctx.append(name, val)', function(){
it('should append multiple headers', function(){
var ctx = context();
const ctx = context();
ctx.append('x-foo', 'bar1');
ctx.append('x-foo', 'bar2');
ctx.response.header['x-foo'].should.eql(['bar1', 'bar2']);
})
it('should accept array of values', function (){
var ctx = context();
const ctx = context();
ctx.append('Set-Cookie', ['foo=bar', 'fizz=buzz']);
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 (){
var ctx = context();
const ctx = context();
ctx.append('Link', '<http://localhost/>');
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 (){
var ctx = context();
const ctx = context();
ctx.set('Link', '<http://localhost/>');
ctx.append('Link', '<http://localhost:80/>');

View File

@ -1,23 +1,23 @@
'use strict';
var context = require('../context');
var request = require('supertest');
var koa = require('../..');
const context = require('../context');
const request = require('supertest');
const koa = require('../..');
describe('ctx.attachment([filename])', function(){
describe('when given a filename', function(){
it('should set the filename param', function(){
var ctx = context();
const ctx = context();
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);
})
})
describe('when omitting filename', function(){
it('should not set filename param', function(){
var ctx = context();
const ctx = context();
ctx.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(){
it('should set the encodeURI filename param', function(){
var ctx = context();
const ctx = context();
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);
})
it('should work with http client', function(done){
var app = koa();
const app = koa();
app.use(function* (next){
this.attachment('path/to/include-no-ascii-char-中文名-ok.json')

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,13 +1,13 @@
'use strict';
var context = require('../context');
var assert = require('assert');
const context = require('../context');
const assert = require('assert');
describe('ctx.type=', function(){
describe('with a mime', function(){
it('should set the Content-Type', function(){
var ctx = context();
const ctx = context();
ctx.type = 'text/plain';
ctx.type.should.equal('text/plain');
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(){
it('should lookup the mime', function(){
var ctx = context();
const ctx = context();
ctx.type = 'json';
ctx.type.should.equal('application/json');
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(){
it('should default the charset', function(){
var ctx = context();
const ctx = context();
ctx.type = 'text/html';
ctx.type.should.equal('text/html');
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(){
it('should not default the charset', function(){
var ctx = context();
const ctx = context();
ctx.type = 'text/html; charset=foo';
ctx.type.should.equal('text/html');
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(){
it('should default to application/octet-stream',function(){
var ctx = context();
const ctx = context();
ctx.type = 'asdf';
ctx.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('with no Content-Type', function(){
it('should return ""', function(){
var ctx = context();
const ctx = context();
// TODO: this is lame
assert('' === ctx.type);
})
@ -62,7 +62,7 @@ describe('ctx.type', function(){
describe('with a Content-Type', function(){
it('should return the mime', function(){
var ctx = context();
const ctx = context();
ctx.type = 'json';
ctx.type.should.equal('application/json');
})

View File

@ -1,12 +1,12 @@
'use strict';
var context = require('../context');
const context = require('../context');
describe('ctx.vary(field)', function(){
describe('when Vary is not set', function(){
it('should set it', function(){
var ctx = context();
const ctx = context();
ctx.vary('Accept');
ctx.response.header.vary.should.equal('Accept');
})
@ -14,7 +14,7 @@ describe('ctx.vary(field)', function(){
describe('when Vary is set', function(){
it('should append', function(){
var ctx = context();
const ctx = context();
ctx.vary('Accept');
ctx.vary('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(){
it('should not append', function(){
var ctx = context();
const ctx = context();
ctx.vary('Accept');
ctx.vary('Accept-Encoding');
ctx.vary('Accept');

View File

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