diff --git a/Readme.md b/Readme.md index 080ea19..f4e3653 100644 --- a/Readme.md +++ b/Readme.md @@ -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); }); diff --git a/benchmarks/experimental/async.js b/benchmarks/experimental/async.js index 53394ec..d7ea4a2 100644 --- a/benchmarks/experimental/async.js +++ b/benchmarks/experimental/async.js @@ -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; diff --git a/benchmarks/middleware.js b/benchmarks/middleware.js index 365b2d5..75d5f4a 100644 --- a/benchmarks/middleware.js +++ b/benchmarks/middleware.js @@ -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; diff --git a/lib/application.js b/lib/application.js index f1e1374..fc01a6c 100644 --- a/lib/application.js +++ b/lib/application.js @@ -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]) { diff --git a/lib/context.js b/lib/context.js index 88b0cf2..5f25161 100644 --- a/lib/context.js +++ b/lib/context.js @@ -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); diff --git a/lib/request.js b/lib/request.js index 93a090f..93715a4 100644 --- a/lib/request.js +++ b/lib/request.js @@ -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': diff --git a/lib/response.js b/lib/response.js index c2cd7c8..195aa41 100644 --- a/lib/response.js +++ b/lib/response.js @@ -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; }, diff --git a/test/application.js b/test/application.js index 2494e0e..5a455c2 100644 --- a/test/application.js +++ b/test/application.js @@ -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){ diff --git a/test/context.js b/test/context.js index 87b4bd8..2144ad8 100644 --- a/test/context.js +++ b/test/context.js @@ -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()] }; diff --git a/test/context/assert.js b/test/context/assert.js index e2e918d..eef693a 100644 --- a/test/context/assert.js +++ b/test/context/assert.js @@ -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); diff --git a/test/context/cookies.js b/test/context/cookies.js index 9097bf1..0527e8c 100644 --- a/test/context/cookies.js +++ b/test/context/cookies.js @@ -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); diff --git a/test/context/inspect.js b/test/context/inspect.js index ead9377..9257830 100644 --- a/test/context/inspect.js +++ b/test/context/inspect.js @@ -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()); }) diff --git a/test/context/onerror.js b/test/context/onerror.js index 076adb2..617fa48 100644 --- a/test/context/onerror.js +++ b/test/context/onerror.js @@ -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('/') diff --git a/test/context/state.js b/test/context/state.js index 9ac1651..eb9c1c2 100644 --- a/test/context/state.js +++ b/test/context/state.js @@ -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('/') diff --git a/test/context/throw.js b/test/context/throw.js index 5feb049..173789e 100644 --- a/test/context/throw.js +++ b/test/context/throw.js @@ -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 }); diff --git a/test/context/toJSON.js b/test/context/toJSON.js index 84218ef..7318423 100644 --- a/test/context/toJSON.js +++ b/test/context/toJSON.js @@ -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 = '

Hey

'; - 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', diff --git a/test/experimental/async.js b/test/experimental/async.js index a50305f..2937946 100644 --- a/test/experimental/async.js +++ b/test/experimental/async.js @@ -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; }); diff --git a/test/request/accepts.js b/test/request/accepts.js index 317d264..6094ac5 100644 --- a/test/request/accepts.js +++ b/test/request/accepts.js @@ -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'); diff --git a/test/request/acceptsCharsets.js b/test/request/acceptsCharsets.js index 35b5198..ba67517 100644 --- a/test/request/acceptsCharsets.js +++ b/test/request/acceptsCharsets.js @@ -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'); }) diff --git a/test/request/acceptsEncodings.js b/test/request/acceptsEncodings.js index 5f17287..b24011d 100644 --- a/test/request/acceptsEncodings.js +++ b/test/request/acceptsEncodings.js @@ -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'); }) diff --git a/test/request/acceptsLanguages.js b/test/request/acceptsLanguages.js index 5dbf6d0..8e03668 100644 --- a/test/request/acceptsLanguages.js +++ b/test/request/acceptsLanguages.js @@ -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'); }) diff --git a/test/request/charset.js b/test/request/charset.js index 12eb09b..400f465 100644 --- a/test/request/charset.js +++ b/test/request/charset.js @@ -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'); }) diff --git a/test/request/fresh.js b/test/request/fresh.js index 8d2a89b..3f574ee 100644 --- a/test/request/fresh.js +++ b/test/request/fresh.js @@ -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'; diff --git a/test/request/get.js b/test/request/get.js index fd3d8ae..05ea090 100644 --- a/test/request/get.js +++ b/test/request/get.js @@ -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'); diff --git a/test/request/header.js b/test/request/header.js index 7eb23b2..7c706a8 100644 --- a/test/request/header.js +++ b/test/request/header.js @@ -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); }) }) diff --git a/test/request/headers.js b/test/request/headers.js index d68c141..983e038 100644 --- a/test/request/headers.js +++ b/test/request/headers.js @@ -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); }) }) diff --git a/test/request/host.js b/test/request/host.js index 96c4c1b..943caee 100644 --- a/test/request/host.js +++ b/test/request/host.js @@ -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'; diff --git a/test/request/hostname.js b/test/request/hostname.js index fb55f71..fb026b2 100644 --- a/test/request/hostname.js +++ b/test/request/hostname.js @@ -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'; diff --git a/test/request/href.js b/test/request/href.js index b927cc7..d934e03 100644 --- a/test/request/href.js +++ b/test/request/href.js @@ -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', diff --git a/test/request/idempotent.js b/test/request/idempotent.js index 264b7c8..86db95b 100644 --- a/test/request/idempotent.js +++ b/test/request/idempotent.js @@ -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); }) diff --git a/test/request/inspect.js b/test/request/inspect.js index dca0df8..841010f 100644 --- a/test/request/inspect.js +++ b/test/request/inspect.js @@ -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'; diff --git a/test/request/ip.js b/test/request/ip.js index af169cc..1fb1531 100644 --- a/test/request/ip.js +++ b/test/request/ip.js @@ -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(''); }) diff --git a/test/request/ips.js b/test/request/ips.js index 078d8c9..5f11e27 100644 --- a/test/request/ips.js +++ b/test/request/ips.js @@ -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']); diff --git a/test/request/is.js b/test/request/is.js index 30a79eb..03ebc59 100644 --- a/test/request/is.js +++ b/test/request/is.js @@ -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'; diff --git a/test/request/length.js b/test/request/length.js index 53928bb..abf5c40 100644 --- a/test/request/length.js +++ b/test/request/length.js @@ -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); }) }) diff --git a/test/request/origin.js b/test/request/origin.js index e5a9c52..e7829d9 100644 --- a/test/request/origin.js +++ b/test/request/origin.js @@ -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'; diff --git a/test/request/path.js b/test/request/path.js index f42dca0..0a49d7e 100644 --- a/test/request/path.js +++ b/test/request/path.js @@ -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'); diff --git a/test/request/protocol.js b/test/request/protocol.js index eec4f1c..7ba1da9 100644 --- a/test/request/protocol.js +++ b/test/request/protocol.js @@ -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'); diff --git a/test/request/query.js b/test/request/query.js index 20f4410..06ed1a6 100644 --- a/test/request/query.js +++ b/test/request/query.js @@ -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'); diff --git a/test/request/querystring.js b/test/request/querystring.js index e689d84..c78f44b 100644 --- a/test/request/querystring.js +++ b/test/request/querystring.js @@ -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'); diff --git a/test/request/search.js b/test/request/search.js index 99a1323..9a428c6 100644 --- a/test/request/search.js +++ b/test/request/search.js @@ -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(''); }) }) diff --git a/test/request/secure.js b/test/request/secure.js index 07f7013..dc8e337 100644 --- a/test/request/secure.js +++ b/test/request/secure.js @@ -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; }) diff --git a/test/request/stale.js b/test/request/stale.js index fb4e617..6bbd0f3 100644 --- a/test/request/stale.js +++ b/test/request/stale.js @@ -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"'; diff --git a/test/request/subdomains.js b/test/request/subdomains.js index 8df5be5..be9659e 100644 --- a/test/request/subdomains.js +++ b/test/request/subdomains.js @@ -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([]); }) }) diff --git a/test/request/type.js b/test/request/type.js index 6de60e3..7b3b222 100644 --- a/test/request/type.js +++ b/test/request/type.js @@ -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); }) }) diff --git a/test/response/append.js b/test/response/append.js index 91ad44d..f1188c1 100644 --- a/test/response/append.js +++ b/test/response/append.js @@ -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', ''); ctx.append('Link', ''); @@ -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', ''); ctx.append('Link', ''); diff --git a/test/response/attachment.js b/test/response/attachment.js index e4db826..37a7152 100644 --- a/test/response/attachment.js +++ b/test/response/attachment.js @@ -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') diff --git a/test/response/body.js b/test/response/body.js index b28252c..a1d6171 100644 --- a/test/response/body.js +++ b/test/response/body.js @@ -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 = 'hey'; 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 = '

Tobi

'; assert('text/html; charset=utf-8' == res.header['content-type']); }) it('should set length', function(){ - var string = '

Tobi

'; - var res = response(); + const string = '

Tobi

'; + const res = response(); res.body = string; assert.equal(res.length, Buffer.byteLength(string)); }) it('should set length when body is overridden', function(){ - var string = '

Tobi

'; - var res = response(); + const string = '

Tobi

'; + 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 = '

Tobi

'; 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 = '\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']); }) diff --git a/test/response/etag.js b/test/response/etag.js index 0a86af1..51a91fd 100644 --- a/test/response/etag.js +++ b/test/response/etag.js @@ -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"'); }) diff --git a/test/response/header.js b/test/response/header.js index 02820dd..04afe3b 100644 --- a/test/response/header.js +++ b/test/response/header.js @@ -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({}); }) diff --git a/test/response/headers.js b/test/response/headers.js index d60686e..409655a 100644 --- a/test/response/headers.js +++ b/test/response/headers.js @@ -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({}); }) diff --git a/test/response/inspect.js b/test/response/inspect.js index ee12ee2..088c3b5 100644 --- a/test/response/inspect.js +++ b/test/response/inspect.js @@ -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({ diff --git a/test/response/is.js b/test/response/is.js index c59a06a..52de6b1 100644 --- a/test/response/is.js +++ b/test/response/is.js @@ -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'); diff --git a/test/response/last-modified.js b/test/response/last-modified.js index 69f86e1..f2143cf 100644 --- a/test/response/last-modified.js +++ b/test/response/last-modified.js @@ -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; }) }) diff --git a/test/response/length.js b/test/response/length.js index e9234e1..c936aff 100644 --- a/test/response/length.js +++ b/test/response/length.js @@ -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); }) }) diff --git a/test/response/message.js b/test/response/message.js index 4b9f9a3..fdbea1d 100644 --- a/test/response/message.js +++ b/test/response/message.js @@ -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'); diff --git a/test/response/redirect.js b/test/response/redirect.js index 41bc3e2..8eba957 100644 --- a/test/response/redirect.js +++ b/test/response/redirect.js @@ -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 = '