diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000000..f0b6d03916 --- /dev/null +++ b/.eslintrc @@ -0,0 +1,9 @@ +parser: babel-eslint + +extends: standard + +rules: + eqeqeq: 0 + semi: [2, always] + space-before-function-paren: [2, never] + yoda: 0 diff --git a/.travis.yml b/.travis.yml index 504a82a9f8..12fdd4f32b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,10 +1,6 @@ language: node_js node_js: - - "0.12" - - "1" - - "2" - - "3" - "4" sudo: false diff --git a/Makefile b/Makefile index ab3a13a30d..5c9dcccc95 100644 --- a/Makefile +++ b/Makefile @@ -1,26 +1,26 @@ SRC = lib/*.js -include node_modules/make-lint/index.mk - -BIN = node - REQUIRED = --require should --require should-http TESTS = test/application/* \ test/context/* \ test/request/* \ test/response/* \ - test/middlewares/* + test/middlewares/* \ + test/experimental/index.js + +lint: + @./node_modules/.bin/eslint lib test test: - @NODE_ENV=test $(BIN) \ + @NODE_ENV=test node \ ./node_modules/.bin/_mocha \ $(REQUIRED) \ $(TESTS) \ --bail -test-travis: - @NODE_ENV=test $(BIN) \ +test-travis: lint + @NODE_ENV=test node \ ./node_modules/.bin/istanbul cover \ ./node_modules/.bin/_mocha \ --report lcovonly \ @@ -32,4 +32,4 @@ test-travis: bench: @$(MAKE) -C benchmarks -.PHONY: test bench +.PHONY: lint test bench diff --git a/lib/configuration/hooks/router/index.js b/lib/configuration/hooks/router/index.js index eff22dd705..294276c530 100644 --- a/lib/configuration/hooks/router/index.js +++ b/lib/configuration/hooks/router/index.js @@ -88,8 +88,7 @@ module.exports = function (strapi) { } }); - // Little hack to serve the admin panel on `./admin` waiting for - // this PR to be merged: https://github.com/koajs/static/pull/51. + // Little hack to serve the admin panel on `./admin`. strapi.router.get('/admin', function * () { this.status = 301; yield strapi.middlewares.send(this, path.resolve(strapi.config.appPath, strapi.config.paths.static, 'admin', 'index.html')); diff --git a/package.json b/package.json index 0a1bcd36cf..cd9a833d16 100644 --- a/package.json +++ b/package.json @@ -35,12 +35,12 @@ "bin": "./bin/_spawn.js", "dependencies": { "async": "^1.4.2", - "commander": "^2.8.1", + "commander": "^2.9.0", "consolidate": "^0.13.1", "fs-extra": "^0.24.0", "include-all": "^0.1.6", "json-stringify-safe": "^5.0.1", - "koa": "^1.0.0", + "koa": "^1.1.0", "koa-bodyparser": "^2.0.1", "koa-compose": "^2.3.0", "koa-cors": "^0.0.16", @@ -53,19 +53,19 @@ "koa-lusca": "^2.1.0", "koa-mount": "^1.3.0", "koa-passport": "^1.2.0", - "koa-proxy": "^0.4.0", + "koa-proxy": "^0.4.1", "koa-response-time": "^1.0.2", "koa-router": "^5.2.3", - "koa-send": "^1.3.1", + "koa-send": "^2.0.1", "koa-session": "^3.3.1", "koa-ssl": "^2.0.0", - "koa-static": "^1.4.9", + "koa-static": "^1.5.1", "koa-views": "^3.1.0", "lodash": "^3.10.1", - "node-rsa": "^0.2.25", + "node-rsa": "^0.2.26", "node-schedule": "^0.2.9", "prompt": "^0.2.14", - "request": "^2.64.0", + "request": "^2.65.0", "sails-disk": "^0.10.8", "socket.io": "^1.3.7", "socket.io-client": "^1.3.7", @@ -76,9 +76,14 @@ "strapi-generate-users": "^1.1.0", "unzip2": "^0.2.5", "waterline": "^0.10.26", - "winston": "^1.0.2" + "winston": "^1.1.0" }, "devDependencies": { + "babel": "^5.8.23", + "babel-eslint": "^4.1.3", + "eslint": "^1.6.0", + "eslint-config-standard": "^4.4.0", + "eslint-plugin-standard": "^1.3.1", "expect.js": "^0.3.1", "istanbul": "^0.3.22", "jade": "^1.11.0", diff --git a/test/application/context.js b/test/application/context.js new file mode 100755 index 0000000000..54b9118a5f --- /dev/null +++ b/test/application/context.js @@ -0,0 +1,35 @@ +'use strict'; + +const assert = require('assert'); +const request = require('supertest'); + +const Koa = require('../..').server; + +describe('app.context', function () { + const app1 = new Koa(); + app1.context.msg = 'hello'; + + const app2 = new Koa(); + + it('should merge properties', function (done) { + app1.use(function * () { + assert.equal(this.msg, 'hello'); + this.status = 204; + }); + + request(app1.listen()) + .get('/') + .expect(204, done); + }); + + it('should not affect the original prototype', function (done) { + app2.use(function * () { + assert.equal(this.msg, undefined); + this.status = 204; + }); + + request(app2.listen()) + .get('/') + .expect(204, done); + }); +}); diff --git a/test/application/index.js b/test/application/index.js new file mode 100755 index 0000000000..7fb5c5a415 --- /dev/null +++ b/test/application/index.js @@ -0,0 +1,54 @@ +'use strict'; + +const assert = require('assert'); +const request = require('supertest'); + +const Koa = require('../..').server; + +describe('app', function () { + it('should handle socket errors', function (done) { + const app = new Koa(); + + app.use(function * () { + this.socket.emit('error', new Error('boom')); + }); + + app.on('error', function (err) { + err.message.should.equal('boom'); + done(); + }); + + request(app.listen()) + .get('/') + .end(function () {}); + }); + + it('should not .writeHead when !socket.writable', function (done) { + const app = new Koa(); + + app.use(function * () { + this.socket.writable = false; + this.status = 204; + this.res.writeHead = + this.res.end = function () { + throw new Error('response sent'); + }; + }); + + setImmediate(done); + + request(app.listen()) + .get('/') + .end(function () {}); + }); + + it('should set development env when NODE_ENV missing', function () { + const NODE_ENV = process.env.NODE_ENV; + process.env.NODE_ENV = ''; + + const app = new Koa(); + process.env.NODE_ENV = NODE_ENV; + + assert.equal(app.env, 'development'); + }); +}); diff --git a/test/application/inspect.js b/test/application/inspect.js new file mode 100755 index 0000000000..69fab44d58 --- /dev/null +++ b/test/application/inspect.js @@ -0,0 +1,11 @@ +'use strict'; + +const Koa = require('../..').server; + +describe('app.inspect()', function () { + it('should work', function () { + const app = new Koa(); + const util = require('util'); + util.inspect(app); + }); +}); diff --git a/test/application/onerror.js b/test/application/onerror.js new file mode 100755 index 0000000000..838dfac42b --- /dev/null +++ b/test/application/onerror.js @@ -0,0 +1,82 @@ +'use strict'; + +const AssertionError = require('assert').AssertionError; +const stderr = require('test-console').stderr; + +const Koa = require('../..').server; + +describe('app.onerror(err)', function () { + it('should throw an error if a non-error is given', function (done) { + const app = new Koa(); + + (function () { + app.onerror('foo'); + }).should.throw(AssertionError, { + message: 'non-error thrown: foo' + }); + + done(); + }); + + it('should do nothing if status is 404', function (done) { + const app = new Koa(); + const err = new Error(); + + err.status = 404; + + const output = stderr.inspectSync(function () { + app.onerror(err); + }); + + output.should.eql([]); + + done(); + }); + + it('should do nothing if .silent', function (done) { + const app = new Koa(); + app.silent = true; + + const err = new Error(); + + const output = stderr.inspectSync(function () { + app.onerror(err); + }); + + output.should.eql([]); + + done(); + }); + + it('should log the error to stderr', function (done) { + const app = new Koa(); + app.env = 'dev'; + + const err = new Error(); + err.stack = 'Foo'; + + const output = stderr.inspectSync(function () { + app.onerror(err); + }); + + output.should.eql(['\n', ' Foo\n', '\n']); + + done(); + }); + + it('should use err.toString() instad of err.stack', function (done) { + const app = new Koa(); + app.env = 'dev'; + + const err = new Error('mock stack null'); + err.stack = null; + + const output = stderr.inspectSync(function () { + app.onerror(err); + }); + + output.should.eql(['\n', ' Error: mock stack null\n', '\n']); + + done(); + }); +}); diff --git a/test/application/request.js b/test/application/request.js new file mode 100755 index 0000000000..e5b373c7a0 --- /dev/null +++ b/test/application/request.js @@ -0,0 +1,35 @@ +'use strict'; + +const assert = require('assert'); +const request = require('supertest'); + +const Koa = require('../..').server; + +describe('app.request', function () { + const app1 = new Koa(); + app1.request.message = 'hello'; + + const app2 = new Koa(); + + it('should merge properties', function (done) { + app1.use(function * () { + assert.equal(this.request.message, 'hello'); + this.status = 204; + }); + + request(app1.listen()) + .get('/') + .expect(204, done); + }); + + it('should not affect the original prototype', function (done) { + app2.use(function * () { + assert.equal(this.request.message, undefined); + this.status = 204; + }); + + request(app2.listen()) + .get('/') + .expect(204, done); + }); +}); diff --git a/test/application/server.js b/test/application/respond.js similarity index 61% rename from test/application/server.js rename to test/application/respond.js index db8c2b9d41..01213ab9cc 100755 --- a/test/application/server.js +++ b/test/application/respond.js @@ -1,211 +1,17 @@ 'use strict'; const fs = require('fs'); -const stderr = require('test-console').stderr; -const request = require('supertest'); + const assert = require('assert'); +const request = require('supertest'); +const statuses = require('statuses'); -const strapi = require('../..'); - -const AssertionError = assert.AssertionError; - -describe('app', function () { - it('should handle socket errors', function (done) { - const app = strapi.server(); - - app.use(function * () { - this.socket.emit('error', new Error('boom')); - }); - - app.on('error', function (err) { - err.message.should.equal('boom'); - done(); - }); - - request(app.listen()) - .get('/') - .end(function () {}); - }); - - it('should not .writeHead when !socket.writable', function (done) { - const app = strapi.server(); - - app.use(function * () { - this.socket.writable = false; - this.status = 204; - this.res.writeHead = this.res.end = function () { - throw new Error('response sent'); - }; - }); - - setImmediate(done); - - request(app.listen()) - .get('/') - .end(function () {}); - }); - - it('should set development env when NODE_ENV missing', function () { - const NODE_ENV = process.env.NODE_ENV; - process.env.NODE_ENV = ''; - const app = strapi.server(); - process.env.NODE_ENV = NODE_ENV; - assert.equal(app.env, 'development'); - }); -}); - -describe('app.toJSON()', function () { - it('should work', function () { - const app = strapi.server(); - const obj = app.toJSON(); - - obj.should.eql({ - subdomainOffset: 2, - env: 'test' - }); - }); -}); - -describe('app.inspect()', function () { - it('should work', function () { - const util = require('util'); - const str = util.inspect(strapi); - }); -}); - -describe('app.use(fn)', function () { - it('should compose middleware', function (done) { - const app = strapi.server(); - const calls = []; - - app.use(function * (next) { - calls.push(1); - yield next; - calls.push(6); - }); - - app.use(function * (next) { - calls.push(2); - yield next; - calls.push(5); - }); - - app.use(function * (next) { - calls.push(3); - yield next; - calls.push(4); - }); - - request(app.listen()) - .get('/') - .expect(404) - .end(function (err) { - if (err) { - return done(err); - } - calls.should.eql([1, 2, 3, 4, 5, 6]); - done(); - }); - }); - - it('should error when a non-generator function is passed', function () { - const app = strapi.server(); - - try { - app.use(function () {}); - } catch (err) { - err.message.should.equal('app.use() requires a generator function'); - } - }); - - it('should not error when a non-generator function is passed when .experimental=true', function () { - const app = strapi.server(); - app.experimental = true; - app.use(function () {}); - }); -}); - -describe('app.onerror(err)', function () { - it('should throw an error if a non-error is given', function (done) { - const app = strapi.server(); - - try { - app.onerror('foo'); - - should.fail(); - } catch (err) { - err.should.be.instanceOf(AssertionError); - err.message.should.equal('non-error thrown: foo'); - } - - done(); - }); - - it('should do nothing if status is 404', function (done) { - const app = strapi.server(); - const err = new Error(); - - err.status = 404; - - const output = stderr.inspectSync(function () { - app.onerror(err); - }); - - output.should.eql([]); - - done(); - }); - - it('should do nothing if env is test', function (done) { - const app = strapi.server(); - const err = new Error(); - - const output = stderr.inspectSync(function () { - app.onerror(err); - }); - - output.should.eql([]); - - done(); - }); - - it('should log the error to stderr', function (done) { - const app = strapi.server(); - app.env = 'dev'; - - const err = new Error(); - err.stack = 'Foo'; - - const output = stderr.inspectSync(function () { - app.onerror(err); - }); - - output.should.eql(['\n', ' Foo\n', '\n']); - - done(); - }); - - it('should use err.toString() instad of err.stack', function (done) { - const app = strapi.server(); - app.env = 'dev'; - - const err = new Error('mock stack null'); - err.stack = null; - - const output = stderr.inspectSync(function () { - app.onerror(err); - }); - - output.should.eql(['\n', ' Error: mock stack null\n', '\n']); - - done(); - }); -}); +const Koa = require('../..').server; describe('app.respond', function () { describe('when this.respond === false', function () { - it('should bypass app.respond', function (done) { - const app = strapi.server(); + it('should bypass strapi.app.respond', function (done) { + const app = new Koa(); app.use(function * () { this.body = 'Hello'; @@ -219,7 +25,9 @@ describe('app.respond', function () { }); }); - request(app.listen()) + const server = app.listen(); + + request(server) .get('/') .expect(200) .expect('lol') @@ -229,13 +37,15 @@ describe('app.respond', function () { describe('when HEAD is used', function () { it('should not respond with the body', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(function * () { this.body = 'Hello'; }); - request(app.listen()) + const server = app.listen(); + + request(server) .head('/') .expect(200) .end(function (err, res) { @@ -244,13 +54,13 @@ describe('app.respond', function () { } res.should.have.header('Content-Type', 'text/plain; charset=utf-8'); res.should.have.header('Content-Length', '5'); - assert(res.text.length == 0); + assert(res.text.length === 0); done(); }); }); it('should keep json headers', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(function * () { this.body = { @@ -258,7 +68,9 @@ describe('app.respond', function () { }; }); - request(app.listen()) + const server = app.listen(); + + request(server) .head('/') .expect(200) .end(function (err, res) { @@ -267,19 +79,21 @@ describe('app.respond', function () { } res.should.have.header('Content-Type', 'application/json; charset=utf-8'); res.should.have.header('Content-Length', '17'); - assert(res.text.length == 0); + assert(res.text.length === 0); done(); }); }); it('should keep string headers', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(function * () { this.body = 'hello world'; }); - request(app.listen()) + const server = app.listen(); + + request(server) .head('/') .expect(200) .end(function (err, res) { @@ -288,19 +102,21 @@ describe('app.respond', function () { } res.should.have.header('Content-Type', 'text/plain; charset=utf-8'); res.should.have.header('Content-Length', '11'); - assert(res.text.length == 0); + assert(res.text.length === 0); done(); }); }); it('should keep buffer headers', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(function * () { this.body = new Buffer('hello world'); }); - request(app.listen()) + const server = app.listen(); + + request(server) .head('/') .expect(200) .end(function (err, res) { @@ -309,42 +125,48 @@ describe('app.respond', function () { } res.should.have.header('Content-Type', 'application/octet-stream'); res.should.have.header('Content-Length', '11'); - assert(res.text.length == 0); + assert(res.text.length === 0); done(); }); }); it('should respond with a 404 if no body was set', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(function * () {}); - request(app.listen()) + const server = app.listen(); + + request(server) .head('/') .expect(404, done); }); it('should respond with a 200 if body = ""', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(function * () { this.body = ''; }); - request(app.listen()) + const server = app.listen(); + + request(server) .head('/') .expect(200, done); }); it('should not overwrite the content-type', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(function * () { this.status = 200; this.type = 'application/javascript'; }); - request(app.listen()) + const server = app.listen(); + + request(server) .head('/') .expect('content-type', /application\/javascript/) .expect(200, done); @@ -353,9 +175,11 @@ describe('app.respond', function () { describe('when no middleware are present', function () { it('should 404', function (done) { - const app = strapi.server(); + const app = new Koa(); - request(app.listen()) + const server = app.listen(); + + request(server) .get('/') .expect(404, done); }); @@ -363,7 +187,7 @@ describe('app.respond', function () { describe('when res has already been written to', function () { it('should not cause an app error', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(function * () { const res = this.res; @@ -381,7 +205,9 @@ describe('app.respond', function () { errorCaught = err; }); - request(app.listen()) + const server = app.listen(); + + request(server) .get('/') .expect(200) .end(function (err) { @@ -396,7 +222,7 @@ describe('app.respond', function () { }); it('should send the right body', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(function * () { const res = this.res; @@ -408,7 +234,9 @@ describe('app.respond', function () { }, 0); }); - request(app.listen()) + const server = app.listen(); + + request(server) .get('/') .expect(200) .expect('HelloGoodbye', done); @@ -418,13 +246,15 @@ 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) { - const app = strapi.server(); + const app = new Koa(); app.use(function * () { this.status = 400; }); - request(app.listen()) + const server = app.listen(); + + request(server) .get('/') .expect(400) .expect('Content-Length', 11) @@ -434,13 +264,15 @@ describe('app.respond', function () { describe('with status=204', function () { it('should respond without a body', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(function * () { this.status = 204; }); - request(app.listen()) + const server = app.listen(); + + request(server) .get('/') .expect(204) .expect('') @@ -457,13 +289,15 @@ describe('app.respond', function () { describe('with status=205', function () { it('should respond without a body', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(function * () { this.status = 205; }); - request(app.listen()) + const server = app.listen(); + + request(server) .get('/') .expect(205) .expect('') @@ -480,13 +314,15 @@ describe('app.respond', function () { describe('with status=304', function () { it('should respond without a body', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(function * () { this.status = 304; }); - request(app.listen()) + const server = app.listen(); + + request(server) .get('/') .expect(304) .expect('') @@ -501,39 +337,43 @@ describe('app.respond', function () { }); }); - // describe('with custom status=700', function(){ - // it('should respond with the associated status message', function (done){ - // const app = strapi.server(); - // statuses['700'] = 'custom status'; - // - // app.use(function *(){ - // this.status = 700; - // }); - // - // request(app.listen()) - // .get('/') - // .expect(700) - // .expect('custom status') - // .end(function(err, res){ - // if (err) { - // return done(err); - // } - // res.res.statusMessage.should.equal('custom status'); - // done(); - // }); - // }); - // }); + describe('with custom status=700', function () { + it('should respond with the associated status message', function (done) { + const app = new Koa(); + statuses['700'] = 'custom status'; + + app.use(function * () { + this.status = 700; + }); + + const server = app.listen(); + + request(server) + .get('/') + .expect(700) + .expect('custom status') + .end(function (err, res) { + if (err) { + return done(err); + } + res.res.statusMessage.should.equal('custom status'); + done(); + }); + }); + }); describe('with custom statusMessage=ok', function () { it('should respond with the custom status message', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(function * () { this.status = 200; this.message = 'ok'; }); - request(app.listen()) + const server = app.listen(); + + request(server) .get('/') .expect(200) .expect('ok') @@ -549,13 +389,15 @@ describe('app.respond', function () { describe('with custom status without message', function () { it('should respond with the status code number', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(function * () { this.res.statusCode = 701; }); - request(app.listen()) + const server = app.listen(); + + request(server) .get('/') .expect(701) .expect('701', done); @@ -565,13 +407,15 @@ describe('app.respond', function () { describe('when .body is a null', function () { it('should respond 204 by default', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(function * () { this.body = null; }); - request(app.listen()) + const server = app.listen(); + + request(server) .get('/') .expect(204) .expect('') @@ -586,14 +430,16 @@ describe('app.respond', function () { }); it('should respond 204 with status=200', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(function * () { this.status = 200; this.body = null; }); - request(app.listen()) + const server = app.listen(); + + request(server) .get('/') .expect(204) .expect('') @@ -608,14 +454,16 @@ describe('app.respond', function () { }); it('should respond 205 with status=205', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(function * () { this.status = 205; this.body = null; }); - request(app.listen()) + const server = app.listen(); + + request(server) .get('/') .expect(205) .expect('') @@ -630,14 +478,16 @@ describe('app.respond', function () { }); it('should respond 304 with status=304', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(function * () { this.status = 304; this.body = null; }); - request(app.listen()) + const server = app.listen(); + + request(server) .get('/') .expect(304) .expect('') @@ -654,13 +504,15 @@ describe('app.respond', function () { describe('when .body is a string', function () { it('should respond', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(function * () { this.body = 'Hello'; }); - request(app.listen()) + const server = app.listen(); + + request(server) .get('/') .expect('Hello', done); }); @@ -668,13 +520,15 @@ describe('app.respond', function () { describe('when .body is a Buffer', function () { it('should respond', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(function * () { this.body = new Buffer('Hello'); }); - request(app.listen()) + const server = app.listen(); + + request(server) .get('/') .expect('Hello', done); }); @@ -682,14 +536,16 @@ describe('app.respond', function () { describe('when .body is a Stream', function () { it('should respond', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(function * () { this.body = fs.createReadStream('package.json'); this.set('Content-Type', 'application/json; charset=utf-8'); }); - request(app.listen()) + const server = app.listen(); + + request(server) .get('/') .expect('Content-Type', 'application/json; charset=utf-8') .end(function (err, res) { @@ -704,7 +560,7 @@ describe('app.respond', function () { }); it('should strip content-length when overwriting', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(function * () { this.body = 'hello'; @@ -712,7 +568,9 @@ describe('app.respond', function () { this.set('Content-Type', 'application/json; charset=utf-8'); }); - request(app.listen()) + const server = app.listen(); + + request(server) .get('/') .expect('Content-Type', 'application/json; charset=utf-8') .end(function (err, res) { @@ -727,7 +585,7 @@ describe('app.respond', function () { }); it('should keep content-length if not overwritten', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(function * () { this.length = fs.readFileSync('package.json').length; @@ -735,7 +593,9 @@ describe('app.respond', function () { this.set('Content-Type', 'application/json; charset=utf-8'); }); - request(app.listen()) + const server = app.listen(); + + request(server) .get('/') .expect('Content-Type', 'application/json; charset=utf-8') .end(function (err, res) { @@ -750,7 +610,7 @@ describe('app.respond', function () { }); it('should keep content-length if overwritten with the same stream', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(function * () { this.length = fs.readFileSync('package.json').length; @@ -760,7 +620,9 @@ describe('app.respond', function () { this.set('Content-Type', 'application/json; charset=utf-8'); }); - request(app.listen()) + const server = app.listen(); + + request(server) .get('/') .expect('Content-Type', 'application/json; charset=utf-8') .end(function (err, res) { @@ -775,14 +637,16 @@ describe('app.respond', function () { }); it('should handle errors', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(function * () { this.set('Content-Type', 'application/json; charset=utf-8'); this.body = fs.createReadStream('does not exist'); }); - request(app.listen()) + const server = app.listen(); + + request(server) .get('/') .expect('Content-Type', 'text/plain; charset=utf-8') .expect(404) @@ -790,20 +654,22 @@ describe('app.respond', function () { }); it('should handle errors when no content status', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(function * () { this.status = 204; this.body = fs.createReadStream('does not exist'); }); - request(app.listen()) + const server = app.listen(); + + request(server) .get('/') .expect(204, done); }); it('should handle all intermediate stream body errors', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(function * () { this.body = fs.createReadStream('does not exist'); @@ -811,7 +677,9 @@ describe('app.respond', function () { this.body = fs.createReadStream('does not exist'); }); - request(app.listen()) + const server = app.listen(); + + request(server) .get('/') .expect(404, done); }); @@ -819,7 +687,7 @@ describe('app.respond', function () { describe('when .body is an Object', function () { it('should respond with json', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(function * () { this.body = { @@ -827,7 +695,9 @@ describe('app.respond', function () { }; }); - request(app.listen()) + const server = app.listen(); + + request(server) .get('/') .expect('Content-Type', 'application/json; charset=utf-8') .expect('{"hello":"world"}', done); @@ -836,7 +706,7 @@ describe('app.respond', function () { describe('when an error occurs', function () { it('should emit "error" on the app', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(function * () { throw new Error('boom'); @@ -854,7 +724,7 @@ describe('app.respond', function () { describe('with an .expose property', function () { it('should expose the message', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(function * () { const err = new Error('sorry!'); @@ -872,7 +742,7 @@ describe('app.respond', function () { describe('with a .status property', function () { it('should respond with .status', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(function * () { const err = new Error('s3 explodes'); @@ -888,46 +758,48 @@ describe('app.respond', function () { }); it('should respond with 500', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(function * () { throw new Error('boom!'); }); - request(app.listen()) + const server = app.listen(); + + request(server) .get('/') .expect(500, 'Internal Server Error') .end(done); }); - // it('should be catchable', function (done) { - // const app = strapi.server(); - // - // app.use(function * (next) { - // try { - // yield next; - // this.body = 'Hello'; - // } catch (err) { - // error = err; - // this.body = 'Got error'; - // } - // }); - // - // app.use(function * () { - // throw new Error('boom!'); - // this.body = 'Oh no'; - // }); - // - // request(app.listen()) - // .get('/') - // .expect(200, 'Got error') - // .end(done); - // }); + it('should be catchable', function (done) { + const app = new Koa(); + + app.use(function * (next) { + try { + yield next; + this.body = 'Hello'; + } catch (err) { + this.body = 'Got error'; + } + }); + + app.use(function * () { + throw new Error('boom!'); + }); + + const server = app.listen(); + + request(server) + .get('/') + .expect(200, 'Got error') + .end(done); + }); }); describe('when status and body property', function () { it('should 200', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(function * () { this.status = 304; @@ -935,14 +807,16 @@ describe('app.respond', function () { this.status = 200; }); - request(app.listen()) + const server = app.listen(); + + request(server) .get('/') .expect(200) .expect('hello', done); }); it('should 204', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(function * () { this.status = 200; @@ -951,7 +825,9 @@ describe('app.respond', function () { this.status = 204; }); - request(app.listen()) + const server = app.listen(); + + request(server) .get('/') .expect(204) .end(function (err, res) { @@ -961,87 +837,3 @@ describe('app.respond', function () { }); }); }); - -describe('app.context', function () { - const app1 = strapi.server(); - app1.context.msg = 'hello'; - const app2 = strapi.server(); - - it('should merge properties', function (done) { - app1.use(function * () { - assert.equal(this.msg, 'hello'); - this.status = 204; - }); - - request(app1.listen()) - .get('/') - .expect(204, done); - }); - - it('should not affect the original prototype', function (done) { - app2.use(function * () { - assert.equal(this.msg, undefined); - this.status = 204; - }); - - request(app2.listen()) - .get('/') - .expect(204, done); - }); -}); - -describe('app.request', function () { - const app1 = strapi.server(); - app1.request.message = 'hello'; - const app2 = strapi.server(); - - it('should merge properties', function (done) { - app1.use(function * () { - assert.equal(this.request.message, 'hello'); - this.status = 204; - }); - - request(app1.listen()) - .get('/') - .expect(204, done); - }); - - it('should not affect the original prototype', function (done) { - app2.use(function * () { - assert.equal(this.request.message, undefined); - this.status = 204; - }); - - request(app2.listen()) - .get('/') - .expect(204, done); - }); -}); - -describe('app.response', function () { - const app1 = strapi.server(); - app1.response.msg = 'hello'; - const app2 = strapi.server(); - - it('should merge properties', function (done) { - app1.use(function * () { - assert.equal(this.response.msg, 'hello'); - this.status = 204; - }); - - request(app1.listen()) - .get('/') - .expect(204, done); - }); - - it('should not affect the original prototype', function (done) { - app2.use(function * () { - assert.equal(this.response.msg, undefined); - this.status = 204; - }); - - request(app2.listen()) - .get('/') - .expect(204, done); - }); -}); diff --git a/test/application/response.js b/test/application/response.js new file mode 100755 index 0000000000..4cc734d0d9 --- /dev/null +++ b/test/application/response.js @@ -0,0 +1,35 @@ +'use strict'; + +const assert = require('assert'); +const request = require('supertest'); + +const Koa = require('../..').server; + +describe('app.response', function () { + const app1 = new Koa(); + app1.response.msg = 'hello'; + + const app2 = new Koa(); + + it('should merge properties', function (done) { + app1.use(function * () { + assert.equal(this.response.msg, 'hello'); + this.status = 204; + }); + + request(app1.listen()) + .get('/') + .expect(204, done); + }); + + it('should not affect the original prototype', function (done) { + app2.use(function * () { + assert.equal(this.response.msg, undefined); + this.status = 204; + }); + + request(app2.listen()) + .get('/') + .expect(204, done); + }); +}); diff --git a/test/application/toJSON.js b/test/application/toJSON.js new file mode 100755 index 0000000000..a9e7511d72 --- /dev/null +++ b/test/application/toJSON.js @@ -0,0 +1,15 @@ +'use strict'; + +const Koa = require('../..').server; + +describe('app.toJSON()', function () { + it('should work', function () { + const app = new Koa(); + const obj = app.toJSON(); + + obj.should.eql({ + subdomainOffset: 2, + env: 'test' + }); + }); +}); diff --git a/test/application/use.js b/test/application/use.js new file mode 100755 index 0000000000..00a9b4008f --- /dev/null +++ b/test/application/use.js @@ -0,0 +1,59 @@ +'use strict'; + +const request = require('supertest'); + +const Koa = require('../..').server; + +describe('app.use(fn)', function () { + it('should compose middleware', function (done) { + const app = new Koa(); + const calls = []; + + app.use(function * (next) { + calls.push(1); + yield next; + calls.push(6); + }); + + app.use(function * (next) { + calls.push(2); + yield next; + calls.push(5); + }); + + app.use(function * (next) { + calls.push(3); + yield next; + calls.push(4); + }); + + const server = app.listen(); + + request(server) + .get('/') + .expect(404) + .end(function (err) { + if (err) { + return done(err); + } + calls.should.eql([1, 2, 3, 4, 5, 6]); + done(); + }); + }); + + it('should error when a non-generator function is passed', function () { + const app = new Koa(); + + try { + app.use(function () {}); + } catch (err) { + err.message.should.equal('app.use() requires a generator function'); + } + }); + + it('should not error when a non-generator function is passed when .experimental=true', function () { + const app = new Koa(); + app.experimental = true; + app.use(function () {}); + }); +}); diff --git a/test/context/cookies.js b/test/context/cookies.js index 1119bd62cb..3930c5ef96 100755 --- a/test/context/cookies.js +++ b/test/context/cookies.js @@ -2,18 +2,20 @@ const request = require('supertest'); -const strapi = require('../..'); +const Koa = require('../..').server; describe('ctx.cookies.set()', function () { it('should set an unsigned cookie', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(function * () { this.cookies.set('name', 'jon'); this.status = 204; }); - request(app.listen()) + const server = app.listen(); + + request(server) .get('/') .expect(204) .end(function (err, res) { @@ -32,7 +34,7 @@ describe('ctx.cookies.set()', function () { describe('with .signed', function () { describe('when no .keys are set', function () { it('should error', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(function * () { try { @@ -51,7 +53,7 @@ describe('ctx.cookies.set()', function () { }); it('should send a signed cookie', function (done) { - const app = strapi.server(); + const app = new Koa(); app.keys = ['a', 'b']; @@ -62,7 +64,9 @@ describe('ctx.cookies.set()', function () { this.status = 204; }); - request(app.listen()) + const server = app.listen(); + + request(server) .get('/') .expect(204) .end(function (err, res) { diff --git a/test/context/onerror.js b/test/context/onerror.js index a8b0a4da80..121ffe9589 100755 --- a/test/context/onerror.js +++ b/test/context/onerror.js @@ -2,28 +2,29 @@ const request = require('supertest'); -const strapi = require('../..'); +const Koa = require('../..').server; describe('ctx.onerror(err)', function () { it('should respond', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(function * () { this.body = 'something else'; - this.throw(418, 'boom'); }); - request(app.listen()) - .get('/') - .expect(418) - .expect('Content-Type', 'text/plain; charset=utf-8') - .expect('Content-Length', '4') - .end(done); + const server = app.listen(); + + request(server) + .get('/') + .expect(418) + .expect('Content-Type', 'text/plain; charset=utf-8') + .expect('Content-Length', '4') + .end(done); }); it('should unset all headers', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(function * () { this.set('Vary', 'Accept-Encoding'); @@ -33,7 +34,9 @@ describe('ctx.onerror(err)', function () { this.throw(418, 'boom'); }); - request(app.listen()) + const server = app.listen(); + + request(server) .get('/') .expect(418) .expect('Content-Type', 'text/plain; charset=utf-8') @@ -53,7 +56,7 @@ describe('ctx.onerror(err)', function () { describe('when invalid err.status', function () { describe('not number', function () { it('should respond 500', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(function * () { this.body = 'something else'; @@ -62,7 +65,9 @@ describe('ctx.onerror(err)', function () { throw err; }); - request(app.listen()) + const server = app.listen(); + + request(server) .get('/') .expect(500) .expect('Content-Type', 'text/plain; charset=utf-8') @@ -72,7 +77,7 @@ describe('ctx.onerror(err)', function () { describe('not http status code', function () { it('should respond 500', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(function * () { this.body = 'something else'; @@ -81,7 +86,9 @@ describe('ctx.onerror(err)', function () { throw err; }); - request(app.listen()) + const server = app.listen(); + + request(server) .get('/') .expect(500) .expect('Content-Type', 'text/plain; charset=utf-8') @@ -92,13 +99,15 @@ describe('ctx.onerror(err)', function () { describe('when non-error thrown', function () { it('should response non-error thrown message', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(function * () { throw 'string error'; }); - request(app.listen()) + const server = app.listen(); + + request(server) .get('/') .expect(500) .expect('Content-Type', 'text/plain; charset=utf-8') diff --git a/test/context/state.js b/test/context/state.js index 5b3d4c6c3a..9274d41db7 100755 --- a/test/context/state.js +++ b/test/context/state.js @@ -3,17 +3,19 @@ const assert = require('assert'); const request = require('supertest'); -const strapi = require('../..'); +const Koa = require('../..').server; describe('ctx.state', function () { it('should provide a ctx.state namespace', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(function * () { assert.deepEqual(this.state, {}); }); - request(app.listen()) + const server = app.listen(); + + request(server) .get('/') .expect(404) .end(done); diff --git a/test/context/throw.js b/test/context/throw.js index 49212ebbd4..8c67e13324 100755 --- a/test/context/throw.js +++ b/test/context/throw.js @@ -13,7 +13,7 @@ describe('ctx.throw(msg)', function () { } catch (err) { assert(err.status === 500); assert(!err.expose); - return done(); + done(); } }); }); @@ -29,7 +29,7 @@ describe('ctx.throw(err)', function () { assert(err.status === 500); assert(err.message === 'test'); assert(!err.expose); - return done(); + done(); } }); }); @@ -45,7 +45,7 @@ describe('ctx.throw(err, status)', function () { assert(err.status === 422); assert(err.message === 'test'); assert(err.expose === true); - return done(); + done(); } }); }); @@ -61,7 +61,7 @@ describe('ctx.throw(status, err)', function () { assert(err.status === 422); assert(err.message === 'test'); assert(err.expose === true); - return done(); + done(); } }); }); @@ -76,7 +76,7 @@ describe('ctx.throw(msg, status)', function () { assert(err.message === 'name required'); assert(err.status === 400); assert(err.expose === true); - return done(); + done(); } }); }); @@ -91,7 +91,7 @@ describe('ctx.throw(status, msg)', function () { assert(err.message === 'name required'); assert(err.status === 400); assert(err.expose === true); - return done(); + done(); } }); }); @@ -106,7 +106,7 @@ describe('ctx.throw(status)', function () { assert(err.message === 'Bad Request'); assert(err.status === 400); assert(err.expose === true); - return done(); + done(); } }); @@ -121,7 +121,7 @@ describe('ctx.throw(status)', function () { } catch (err) { assert(err.message === 'some error'); assert(!err.expose); - return done(); + done(); } }); }); @@ -140,7 +140,7 @@ describe('ctx.throw(status, msg, props)', function () { assert(err.status === 400); assert(err.expose === true); assert(err.prop === true); - return done(); + done(); } }); @@ -158,7 +158,7 @@ describe('ctx.throw(status, msg, props)', function () { assert(err.status === 400); assert(err.expose === true); assert(err.prop === true); - return done(); + done(); } }); }); @@ -177,7 +177,7 @@ describe('ctx.throw(msg, props)', function () { assert(err.status === 500); assert(err.expose === false); assert(err.prop === true); - return done(); + done(); } }); }); @@ -195,7 +195,7 @@ describe('ctx.throw(status, props)', function () { assert(err.status === 400); assert(err.expose === true); assert(err.prop === true); - return done(); + done(); } }); }); @@ -213,7 +213,7 @@ describe('ctx.throw(err, props)', function () { assert(err.status === 500); assert(err.expose === false); assert(err.prop === true); - return done(); + done(); } }); }); diff --git a/test/experimental/async.js b/test/experimental/async.js new file mode 100755 index 0000000000..14c958409e --- /dev/null +++ b/test/experimental/async.js @@ -0,0 +1,21 @@ +'use strict'; + +const request = require('supertest'); + +const Koa = require('../..').server; + +describe('.experimental=true', function () { + it('should support async functions', function (done) { + const app = new Koa(); + app.experimental = true; + app.use(async function () { + const string = await Promise.resolve('asdf'); + this.body = string; + }); + + request(app.callback()) + .get('/') + .expect('asdf') + .expect(200, done); + }); +}); diff --git a/test/experimental/index.js b/test/experimental/index.js new file mode 100755 index 0000000000..9be7fadc08 --- /dev/null +++ b/test/experimental/index.js @@ -0,0 +1,6 @@ +'use strict'; + +require('babel/register')({ + optional: ['asyncToGenerator'] +}); +require('./async'); diff --git a/test/helpers/context.js b/test/helpers/context.js index 7612e8629b..6cbd229664 100755 --- a/test/helpers/context.js +++ b/test/helpers/context.js @@ -2,9 +2,9 @@ const Stream = require('stream'); -const strapi = require('../..'); +const Koa = require('../..').server; -exports = module.exports = function (req, res) { +module.exports = function (req, res) { const socket = new Stream.Duplex(); req = req || { @@ -31,13 +31,13 @@ exports = module.exports = function (req, res) { delete res._headers[k.toLowerCase()]; }; - return strapi.server().createContext(req, res); + return (new Koa()).createContext(req, res); }; -exports.request = function (req, res) { - return exports(req, res).request; +module.exports.request = function (req, res) { + return module.exports(req, res).request; }; -exports.response = function (req, res) { - return exports(req, res).response; +module.exports.response = function (req, res) { + return module.exports(req, res).response; }; diff --git a/test/middlewares/bodyParser/index.js b/test/middlewares/bodyParser/index.js index ebce4b867b..6696246a54 100644 --- a/test/middlewares/bodyParser/index.js +++ b/test/middlewares/bodyParser/index.js @@ -4,12 +4,13 @@ const path = require('path'); const request = require('supertest'); const strapi = require('../../..'); +const Koa = strapi.server; const fixtures = path.join(__dirname, 'fixtures'); describe('bodyparser', function () { describe('json body', function () { - const app = strapi.server(); + const app = new Koa(); app.keys = ['a', 'b']; app.use(strapi.middlewares.bodyparser()); @@ -54,7 +55,7 @@ describe('bodyparser', function () { }); it('should parse json patch', function (done) { - const app = strapi.server(); + const app = new Koa(); app.keys = ['a', 'b']; app.use(strapi.middlewares.bodyparser()); @@ -80,7 +81,7 @@ describe('bodyparser', function () { }); it('should json body reach the limit size', function (done) { - const app = strapi.server(); + const app = new Koa(); app.keys = ['a', 'b']; @@ -99,7 +100,7 @@ describe('bodyparser', function () { }); it('should json body error with string in strict mode', function (done) { - const app = strapi.server(); + const app = new Koa(); app.keys = ['a', 'b']; @@ -119,7 +120,7 @@ describe('bodyparser', function () { }); it('should json body ok with string not in strict mode', function (done) { - const app = strapi.server(); + const app = new Koa(); app.keys = ['a', 'b']; @@ -142,7 +143,7 @@ describe('bodyparser', function () { describe('opts.detectJSON', function () { it('should parse json body on /foo.json request', function (done) { - const app = strapi.server(); + const app = new Koa(); app.keys = ['a', 'b']; @@ -170,7 +171,7 @@ describe('bodyparser', function () { }); it('should not parse json body on /foo request', function (done) { - const app = strapi.server(); + const app = new Koa(); app.keys = ['a', 'b']; @@ -197,7 +198,7 @@ describe('bodyparser', function () { }); describe('form body', function () { - const app = strapi.server(); + const app = new Koa(); app.keys = ['a', 'b']; app.use(strapi.middlewares.bodyparser()); @@ -228,7 +229,7 @@ describe('bodyparser', function () { }); it('should parse form body reach the limit size', function (done) { - const app = strapi.server(); + const app = new Koa(); app.keys = ['a', 'b']; @@ -250,7 +251,7 @@ describe('bodyparser', function () { describe('extent type', function () { it('should extent json ok', function (done) { - const app = strapi.server(); + const app = new Koa(); app.keys = ['a', 'b']; @@ -276,7 +277,7 @@ describe('bodyparser', function () { }); it('should extent json with array ok', function (done) { - const app = strapi.server(); + const app = new Koa(); app.keys = ['a', 'b']; @@ -303,7 +304,7 @@ describe('bodyparser', function () { }); describe('other type', function () { - const app = strapi.server(); + const app = new Koa(); app.keys = ['a', 'b']; app.use(strapi.middlewares.bodyparser()); @@ -321,7 +322,7 @@ describe('bodyparser', function () { }); describe('onerror', function () { - const app = strapi.server(); + const app = new Koa(); app.keys = ['a', 'b']; diff --git a/test/middlewares/favicon/index.js b/test/middlewares/favicon/index.js index d1eb3fd450..ec070b8e4e 100644 --- a/test/middlewares/favicon/index.js +++ b/test/middlewares/favicon/index.js @@ -6,12 +6,13 @@ const join = require('path').join; const request = require('supertest'); const strapi = require('../../..'); +const Koa = strapi.server; describe('favicon', function () { const path = join(__dirname, 'fixtures', 'favicon.ico'); - it('should only respond on `/favicon.ico`', function (done) { - const app = strapi.server(); + it('should only respond on /favicon.ico', function (done) { + const app = new Koa(); app.use(strapi.middlewares.favicon(path)); @@ -26,8 +27,8 @@ describe('favicon', function () { .expect('hello', done); }); - it('favicon should 404 if `path` is missing', function (done) { - const app = strapi.server(); + it('favicon should 404 if path is missing', function (done) { + const app = new Koa(); app.use(strapi.middlewares.favicon()); request(app.listen()) @@ -36,7 +37,7 @@ describe('favicon', function () { }); it('should not accept POST requests', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(strapi.middlewares.favicon(path)); request(app.listen()) @@ -48,7 +49,7 @@ describe('favicon', function () { it('should send the favicon', function (done) { const body = fs.readFileSync(path); - const app = strapi.server(); + const app = new Koa(); app.use(strapi.middlewares.favicon(path)); request(app.listen()) @@ -59,7 +60,7 @@ describe('favicon', function () { }); it('should set cache-control headers', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(strapi.middlewares.favicon(path)); request(app.listen()) @@ -70,7 +71,7 @@ describe('favicon', function () { describe('options.maxAge', function () { it('should set max-age', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(strapi.middlewares.favicon(path, { maxAge: 5000 })); @@ -82,7 +83,7 @@ describe('favicon', function () { }); it('should accept 0', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(strapi.middlewares.favicon(path, { maxAge: 0 })); @@ -94,7 +95,7 @@ describe('favicon', function () { }); it('should be valid delta-seconds', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(strapi.middlewares.favicon(path, { maxAge: 1234 })); @@ -106,7 +107,7 @@ describe('favicon', function () { }); it('should floor at 0', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(strapi.middlewares.favicon(path, { maxAge: -4000 })); @@ -118,7 +119,7 @@ describe('favicon', function () { }); it('should ceil at 31556926', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(strapi.middlewares.favicon(path, { maxAge: 900000000000 })); @@ -130,7 +131,7 @@ describe('favicon', function () { }); it('should accept Infinity', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(strapi.middlewares.favicon(path, { maxAge: Infinity })); diff --git a/test/middlewares/gzip/index.js b/test/middlewares/gzip/index.js index 4acc3f0e36..9ec573cb8e 100644 --- a/test/middlewares/gzip/index.js +++ b/test/middlewares/gzip/index.js @@ -6,6 +6,7 @@ const should = require('should'); const request = require('supertest'); const strapi = require('../../..'); +const Koa = strapi.server; describe('gzip', function () { const options = {}; diff --git a/test/middlewares/i18n/index.js b/test/middlewares/i18n/index.js index a3213227e9..f9f0b4bc91 100644 --- a/test/middlewares/i18n/index.js +++ b/test/middlewares/i18n/index.js @@ -5,11 +5,12 @@ const path = require('path'); const request = require('supertest'); const strapi = require('../../..'); +const Koa = strapi.server; describe('i18n', function () { describe('detect the query string', function () { it('should be "en" locale', function (done) { - const app = strapi.server(); + const app = new Koa(); strapi.middlewares.locale(app); @@ -31,7 +32,7 @@ describe('i18n', function () { }); describe('detect the subdomain', function () { - const app = strapi.server(); + const app = new Koa(); strapi.middlewares.locale(app); @@ -97,7 +98,7 @@ describe('i18n', function () { describe('detect the header', function () { it('should be "zh-tw" locale', function (done) { - const app = strapi.server(); + const app = new Koa(); strapi.middlewares.locale(app); @@ -121,7 +122,7 @@ describe('i18n', function () { describe('detect the cookie', function () { it('should be "zh-cn" locale', function (done) { - const app = strapi.server(); + const app = new Koa(); strapi.middlewares.locale(app); diff --git a/test/middlewares/lusca/csrf.js b/test/middlewares/lusca/csrf.js index ca1c7768c0..52e055eba5 100755 --- a/test/middlewares/lusca/csrf.js +++ b/test/middlewares/lusca/csrf.js @@ -13,7 +13,6 @@ describe('csrf', function () { }); it('expects a thrown error if no session object', function (done) { - const router = strapi.middlewares.router(); const app = mock({ csrf: true }, true); @@ -75,7 +74,7 @@ describe('csrf', function () { // assert(!err); // request(app.listen()) // .post('/csrf') - // .set('Cookie', res.headers['set-cookie'].join(';')) + // .set('cookie', res.headers['set-cookie'].join(';')) // .send({ // _csrf: res.body.token // }) @@ -84,7 +83,6 @@ describe('csrf', function () { // }); it('POST (403 Forbidden on no token)', function (done) { - const router = strapi.middlewares.router(); const app = mock({ csrf: true }); @@ -94,133 +92,133 @@ describe('csrf', function () { .expect(403, done); }); - // it('should allow custom keys (session type: {value})', function (done) { - // const router = strapi.middlewares.router(); - // const app = mock({ - // csrf: { - // key: 'foobar' - // } - // }); - // - // router.all('/csrf', function * () { - // this.body = { - // token: this.state.foobar - // }; - // }); - // - // app.use(router.routes()); - // app.use(router.allowedMethods()); - // - // request(app.listen()) - // .get('/csrf') - // .expect(200, function (err, res) { - // assert(!err); - // request(app.listen()) - // .post('/csrf') - // .set('cookie', res.headers['set-cookie'].join(';')) - // .send({ - // foobar: res.body.token - // }) - // .expect(200, done); - // }); - // }); - - // it('token can be sent through header instead of post body (session type: {value})', function (done) { - // const router = strapi.middlewares.router(); - // const app = mock({ - // csrf: true - // }); - // - // router.all('/csrf', function * () { - // this.body = { - // token: this.state._csrf - // }; - // }); - // - // app.use(router.routes()); - // app.use(router.allowedMethods()); - // - // request(app.listen()) - // .get('/csrf') - // .expect(200, function (err, res) { - // assert(!err); - // request(app.listen()) - // .post('/csrf') - // .set('cookie', res.headers['set-cookie'].join(';')) - // .set('x-csrf-token', res.body.token) - // .send({ - // name: 'Test' - // }) - // .expect(200, done); - // }); - // }); - - // it('should allow custom headers (session type: {value})', function (done) { - // const router = strapi.middlewares.router(); - // const app = mock({ - // csrf: { - // header: 'x-xsrf-token', - // secret: 'csrfSecret' - // } - // }); - // - // app.use(router.routes()); - // app.use(router.allowedMethods()); - // - // router.all('/csrf', function * () { - // this.body = { - // token: this.state._csrf - // }; - // }); - // - // request(app.listen()) - // .get('/csrf') - // .expect(200, function (err, res) { - // assert(!err); - // request(app.listen()) - // .post('/csrf') - // .set('cookie', res.headers['set-cookie'].join(';')) - // .set('x-xsrf-token', res.body.token) - // .send({ - // name: 'Test' - // }) - // .expect(200, done); - // }); - // }); - - // it('should allow custom functions (session type: {value})', function (done) { - // const myToken = require('./mocks/token'); - // - // const mockConfig = { - // csrf: { - // impl: myToken - // } - // }; - // - // const router = strapi.middlewares.router(); - // const app = mock(mockConfig); - // - // app.use(router.routes()); - // app.use(router.allowedMethods()); - // - // router.all('/csrf', function * () { - // this.body = { - // token: this.state._csrf - // }; - // }); - // - // request(app.listen()) - // .get('/csrf') - // .expect(200, function (err, res) { - // assert(!err); - // assert(myToken.value === res.body.token); - // request(app.listen()) - // .post('/csrf') - // .set('cookie', res.headers['set-cookie'].join(';')) - // .send({ - // _csrf: res.body.token - // }) - // .expect(200, done); - // }); - // }); +// it('should allow custom keys (session type: {value})', function (done) { +// const router = strapi.middlewares.router(); +// const app = mock({ +// csrf: { +// key: 'foobar' +// } +// }); +// +// router.all('/csrf', function * () { +// this.body = { +// token: this.state.foobar +// }; +// }); +// +// app.use(router.routes()); +// app.use(router.allowedMethods()); +// +// request(app.listen()) +// .get('/csrf') +// .expect(200, function (err, res) { +// assert(!err); +// request(app.listen()) +// .post('/csrf') +// .set('cookie', res.headers['set-cookie'].join(';')) +// .send({ +// foobar: res.body.token +// }) +// .expect(200, done); +// }); +// }); +// +// it('token can be sent through header instead of post body (session type: {value})', function (done) { +// const router = strapi.middlewares.router(); +// const app = mock({ +// csrf: true +// }); +// +// router.all('/csrf', function * () { +// this.body = { +// token: this.state._csrf +// }; +// }); +// +// app.use(router.routes()); +// app.use(router.allowedMethods()); +// +// request(app.listen()) +// .get('/csrf') +// .expect(200, function (err, res) { +// assert(!err); +// request(app.listen()) +// .post('/csrf') +// .set('cookie', res.headers['set-cookie'].join(';')) +// .set('x-csrf-token', res.body.token) +// .send({ +// name: 'Test' +// }) +// .expect(200, done); +// }); +// }); +// +// it('should allow custom headers (session type: {value})', function (done) { +// const router = strapi.middlewares.router(); +// const app = mock({ +// csrf: { +// header: 'x-xsrf-token', +// secret: 'csrfSecret' +// } +// }); +// +// router.all('/csrf', function * () { +// this.body = { +// token: this.state._csrf +// }; +// }); +// +// app.use(router.routes()); +// app.use(router.allowedMethods()); +// +// request(app.listen()) +// .get('/csrf') +// .expect(200, function (err, res) { +// assert(!err); +// request(app.listen()) +// .post('/csrf') +// .set('cookie', res.headers['set-cookie'].join(';')) +// .set('x-xsrf-token', res.body.token) +// .send({ +// name: 'Test' +// }) +// .expect(200, done); +// }); +// }); +// +// it('should allow custom functions (session type: {value})', function (done) { +// const router = strapi.middlewares.router(); +// const myToken = require('./mocks/token'); +// +// const mockConfig = { +// csrf: { +// impl: myToken +// } +// }; +// +// const app = mock(mockConfig); +// +// router.all('/csrf', function * () { +// this.body = { +// token: this.state._csrf +// }; +// }); +// +// app.use(router.routes()); +// app.use(router.allowedMethods()); +// +// request(app.listen()) +// .get('/csrf') +// .expect(200, function (err, res) { +// assert(!err); +// assert(myToken.value === res.body.token); +// request(app.listen()) +// .post('/csrf') +// .set('cookie', res.headers['set-cookie'].join(';')) +// .send({ +// _csrf: res.body.token +// }) +// .expect(200, done); +// }); +// }); }); diff --git a/test/middlewares/lusca/cto.js b/test/middlewares/lusca/cto.js index d00e960d90..e3b7979646 100755 --- a/test/middlewares/lusca/cto.js +++ b/test/middlewares/lusca/cto.js @@ -29,13 +29,13 @@ describe('x-content-type-options', function () { const app = mock(config); - app.use(router.routes()); - app.use(router.allowedMethods()); - router.get('/', function * () { this.body = 'hello'; }); + app.use(router.routes()); + app.use(router.allowedMethods()); + request(app.listen()) .get('/') .expect('X-Content-Type-Options', config.cto) diff --git a/test/middlewares/lusca/hsts.js b/test/middlewares/lusca/hsts.js index ce9bdca617..dcfb083c79 100755 --- a/test/middlewares/lusca/hsts.js +++ b/test/middlewares/lusca/hsts.js @@ -28,13 +28,13 @@ describe('hsts', function () { const app = mock(config); - app.use(router.routes()); - app.use(router.allowedMethods()); - - router.get('/', function* () { + router.get('/', function * () { this.body = 'hello'; }); + app.use(router.routes()); + app.use(router.allowedMethods()); + request(app.listen()) .get('/') .expect('Strict-Transport-Security', 'max-age=' + config.hsts.maxAge) @@ -52,13 +52,13 @@ describe('hsts', function () { const app = mock(config); - app.use(router.routes()); - app.use(router.allowedMethods()); - - router.get('/', function* () { + router.get('/', function * () { this.body = 'hello'; }); + app.use(router.routes()); + app.use(router.allowedMethods()); + request(app.listen()) .get('/') .expect('Strict-Transport-Security', 'max-age=0') @@ -74,13 +74,13 @@ describe('hsts', function () { const app = mock(config); - app.use(router.routes()); - app.use(router.allowedMethods()); - - router.get('/', function* () { + router.get('/', function * () { this.body = 'hello'; }); + app.use(router.routes()); + app.use(router.allowedMethods()); + request(app.listen()) .get('/') .expect('Strict-Transport-Security', 'max-age=31536000') @@ -99,13 +99,13 @@ describe('hsts', function () { const app = mock(config); - app.use(router.routes()); - app.use(router.allowedMethods()); - - router.get('/', function* () { + router.get('/', function * () { this.body = 'hello'; }); + app.use(router.routes()); + app.use(router.allowedMethods()); + request(app.listen()) .get('/') .expect('Strict-Transport-Security', 'max-age=' + config.hsts.maxAge + '; includeSubDomains') @@ -125,13 +125,13 @@ describe('hsts', function () { const app = mock(config); - app.use(router.routes()); - app.use(router.allowedMethods()); - - router.get('/', function* () { + router.get('/', function * () { this.body = 'hello'; }); + app.use(router.routes()); + app.use(router.allowedMethods()); + request(app.listen()) .get('/') .expect('Strict-Transport-Security', 'max-age=' + config.hsts.maxAge + '; includeSubDomains; preload') diff --git a/test/middlewares/lusca/mocks/app.js b/test/middlewares/lusca/mocks/app.js index db3da7c123..3d91e4434e 100755 --- a/test/middlewares/lusca/mocks/app.js +++ b/test/middlewares/lusca/mocks/app.js @@ -2,8 +2,10 @@ const strapi = require('../../../..'); +const Koa = strapi.server; + module.exports = function (config, disableSession) { - const app = strapi.server(); + const app = new Koa(); const router = strapi.middlewares.router(); app.keys = ['key1', 'key2']; diff --git a/test/middlewares/lusca/p3p.js b/test/middlewares/lusca/p3p.js index b3c2f41a17..fb72ca9931 100755 --- a/test/middlewares/lusca/p3p.js +++ b/test/middlewares/lusca/p3p.js @@ -34,13 +34,13 @@ describe('p3p', function () { const app = mock(config); - app.use(router.routes()); - app.use(router.allowedMethods()); - - router.get('/', function* () { + router.get('/', function * () { this.body = 'hello'; }); + app.use(router.routes()); + app.use(router.allowedMethods()); + request(app.listen()) .get('/') .expect('P3P', config.p3p) diff --git a/test/middlewares/lusca/xframe.js b/test/middlewares/lusca/xframe.js index 096ba67681..8acd79cdc9 100755 --- a/test/middlewares/lusca/xframe.js +++ b/test/middlewares/lusca/xframe.js @@ -45,11 +45,11 @@ describe('xframe', function () { }); it('header (sameorigin) with options.enable true', function (done) { + const router = strapi.middlewares.router(); const enable = function (url) { return url.indexOf('/show') >= 0; }; - const router = strapi.middlewares.router(); const config = { xframe: { value: 'SAMEORIGIN', @@ -59,7 +59,7 @@ describe('xframe', function () { const app = mock(config); - router.get('/show', function* () { + router.get('/show', function * () { this.body = 'show'; }); diff --git a/test/middlewares/proxy/index.js b/test/middlewares/proxy/index.js index c5521b64e0..1346162fac 100755 --- a/test/middlewares/proxy/index.js +++ b/test/middlewares/proxy/index.js @@ -6,12 +6,13 @@ const http = require('http'); const request = require('supertest'); const strapi = require('../../..'); +const Koa = strapi.server; describe('proxy', function () { let server; before(function () { - const app = strapi.server(); + const app = new Koa(); app.use(function * (next) { if (this.path === '/error') { @@ -44,7 +45,7 @@ describe('proxy', function () { }); it('should have option url', function (done) { - const app = strapi.server(); + const app = new Koa(); const router = strapi.middlewares.router(); router.get('/index.js', strapi.middlewares.proxy({ @@ -68,7 +69,7 @@ describe('proxy', function () { }); it('should have option url and host', function (done) { - const app = strapi.server(); + const app = new Koa(); const router = strapi.middlewares.router(); app.use(strapi.middlewares.proxy({ @@ -98,7 +99,7 @@ describe('proxy', function () { }); it('should have option host', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(strapi.middlewares.proxy({ host: 'http://localhost:1234' @@ -118,7 +119,7 @@ describe('proxy', function () { }); it('should have option host and map', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(strapi.middlewares.proxy({ host: 'http://localhost:1234', @@ -141,7 +142,7 @@ describe('proxy', function () { }); it('should have option host and match', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(strapi.middlewares.proxy({ host: 'http://localhost:1234', @@ -166,7 +167,7 @@ describe('proxy', function () { }); it('url not match for url', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(strapi.middlewares.proxy({ url: 'class.js' @@ -190,7 +191,7 @@ describe('proxy', function () { }); it('url not match for map', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(strapi.middlewares.proxy({ map: { @@ -222,7 +223,7 @@ describe('proxy', function () { }); it('encoding', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(strapi.middlewares.proxy({ url: 'http://localhost:1234/index.html', @@ -243,7 +244,7 @@ describe('proxy', function () { }); it('pass query', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(strapi.middlewares.proxy({ url: 'http://localhost:1234/class.js', @@ -264,7 +265,7 @@ describe('proxy', function () { }); it('pass request body', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(strapi.middlewares.proxy({ host: 'http://localhost:1234' @@ -286,7 +287,7 @@ describe('proxy', function () { }); it('pass parsed request body', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(strapi.middlewares.bodyparser()); @@ -310,7 +311,7 @@ describe('proxy', function () { }); it('statusCode', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(strapi.middlewares.proxy({ host: 'http://localhost:1234' diff --git a/test/middlewares/router/layer.js b/test/middlewares/router/layer.js index a7aa2a72a6..3b1be7bc8a 100755 --- a/test/middlewares/router/layer.js +++ b/test/middlewares/router/layer.js @@ -6,10 +6,11 @@ const should = require('should'); const request = require('supertest'); const strapi = require('../../..'); +const Koa = strapi.server; describe('layer', function () { it('composes multiple callbacks/middleware', function (done) { - const app = strapi.server(); + const app = new Koa(); const router = strapi.middlewares.router(); app.use(router.routes()); @@ -38,7 +39,7 @@ describe('layer', function () { describe('layer#match()', function () { it('captures URL path parameters', function (done) { - const app = strapi.server(); + const app = new Koa(); const router = strapi.middlewares.router(); app.use(router.routes()); @@ -63,7 +64,7 @@ describe('layer', function () { }); it('return orginal path parameters when decodeURIComponent throw error', function (done) { - const app = strapi.server(); + const app = new Koa(); const router = strapi.middlewares.router(); app.use(router.routes()); @@ -83,7 +84,7 @@ describe('layer', function () { }); it('populates ctx.captures with regexp captures', function (done) { - const app = strapi.server(); + const app = new Koa(); const router = strapi.middlewares.router(); app.use(router.routes()); @@ -112,7 +113,7 @@ describe('layer', function () { }); it('return orginal ctx.captures when decodeURIComponent throw error', function (done) { - const app = strapi.server(); + const app = new Koa(); const router = strapi.middlewares.router(); app.use(router.routes()); @@ -141,7 +142,7 @@ describe('layer', function () { }); it('populates ctx.captures with regexp captures include undefiend', function (done) { - const app = strapi.server(); + const app = new Koa(); const router = strapi.middlewares.router(); app.use(router.routes()); @@ -170,7 +171,7 @@ describe('layer', function () { }); it('should throw friendly error message when handle not exists', function () { - const app = strapi.server(); + const app = new Koa(); const router = strapi.middlewares.router(); app.use(router.routes()); @@ -193,7 +194,7 @@ describe('layer', function () { describe('layer#param()', function () { it('composes middleware for param fn', function (done) { - const app = strapi.server(); + const app = new Koa(); const router = strapi.middlewares.router(); const route = router.register('/users/:user', ['GET'], [function * (next) { @@ -229,7 +230,7 @@ describe('layer', function () { }); it('ignores params which are not matched', function (done) { - const app = strapi.server(); + const app = new Koa(); const router = strapi.middlewares.router(); const route = router.register('/users/:user', ['GET'], [function * (next) { @@ -279,7 +280,7 @@ describe('layer', function () { describe('layer#url()', function () { it('generates route URL', function () { - const app = strapi.server(); + const app = new Koa(); const router = strapi.middlewares.router(); const route = router.register('/:category/:title', ['get'], [function * () {}], 'books'); @@ -294,7 +295,7 @@ describe('layer', function () { }); it('escapes using encodeURIComponent()', function () { - const app = strapi.server(); + const app = new Koa(); const router = strapi.middlewares.router(); const route = router.register('/:category/:title', ['get'], [function * () {}], 'books'); diff --git a/test/middlewares/router/router.js b/test/middlewares/router/router.js index 8cdb56c683..be30517386 100755 --- a/test/middlewares/router/router.js +++ b/test/middlewares/router/router.js @@ -10,10 +10,11 @@ const should = require('should'); const expect = require('expect.js'); const strapi = require('../../..'); +const Koa = strapi.server; describe('router', function () { it('does not register middleware more than once', function (done) { - const app = strapi.server(); + const app = new Koa(); const parentRouter = strapi.middlewares.router(); const nestedRouter = strapi.middlewares.router(); @@ -50,7 +51,7 @@ describe('router', function () { }); it('exposes middleware factory', function (done) { - const app = strapi.server(); + const app = new Koa(); const router = strapi.middlewares.router(); router.should.have.property('routes'); @@ -64,7 +65,7 @@ describe('router', function () { }); it('supports promises for async/await', function (done) { - const app = strapi.server(); + const app = new Koa(); app.experimental = true; @@ -95,7 +96,7 @@ describe('router', function () { }); it('matches middleware only if route was matched', function (done) { - const app = strapi.server(); + const app = new Koa(); const router = strapi.middlewares.router(); const otherRouter = strapi.middlewares.router(); @@ -125,7 +126,7 @@ describe('router', function () { }); it('matches first to last', function (done) { - const app = strapi.server(); + const app = new Koa(); const router = strapi.middlewares.router(); router @@ -152,7 +153,7 @@ describe('router', function () { }); it('does not run subsequent middleware without yield next', function (done) { - const app = strapi.server(); + const app = new Koa(); const router = strapi.middlewares.router(); router @@ -168,7 +169,7 @@ describe('router', function () { }); it('nests routers with prefixes at root', function (done) { - const app = strapi.server(); + const app = new Koa(); const api = strapi.middlewares.router(); const forums = strapi.middlewares.router({ @@ -228,7 +229,7 @@ describe('router', function () { }); it('nests routers with prefixes at path', function (done) { - const app = strapi.server(); + const app = new Koa(); const api = strapi.middlewares.router(); const forums = strapi.middlewares.router({ @@ -288,7 +289,7 @@ describe('router', function () { }); it('runs subrouter middleware after parent', function (done) { - const app = strapi.server(); + const app = new Koa(); const subrouter = strapi.middlewares.router() .use(function * (next) { this.msg = 'subrouter'; @@ -318,7 +319,7 @@ describe('router', function () { }); it('runs parent middleware for subrouter routes', function (done) { - const app = strapi.server(); + const app = new Koa(); const subrouter = strapi.middlewares.router() .get('/sub', function * () { this.body = { msg: this.msg }; @@ -344,7 +345,7 @@ describe('router', function () { }); it('matches corresponding requests', function (done) { - const app = strapi.server(); + const app = new Koa(); const router = strapi.middlewares.router(); app.use(router.routes()); @@ -397,8 +398,8 @@ describe('router', function () { }); }); - it('executes route middleware using `app.context`', function (done) { - const app = strapi.server(); + it('executes route middleware using app.context', function (done) { + const app = new Koa(); const router = strapi.middlewares.router(); app.use(router.routes()); @@ -432,7 +433,7 @@ describe('router', function () { }); it('supports generators for route middleware', function (done) { - const app = strapi.server(); + const app = new Koa(); const router = strapi.middlewares.router(); app.use(router.routes()); @@ -463,7 +464,7 @@ describe('router', function () { }); it('responds to OPTIONS requests', function (done) { - const app = strapi.server(); + const app = new Koa(); const router = strapi.middlewares.router(); app.use(router.routes()); @@ -486,7 +487,7 @@ describe('router', function () { }); it('responds with 405 Method Not Allowed', function (done) { - const app = strapi.server(); + const app = new Koa(); const router = strapi.middlewares.router(); app.use(router.routes()); @@ -510,7 +511,7 @@ describe('router', function () { }); it('responds with 501 Not Implemented', function (done) { - const app = strapi.server(); + const app = new Koa(); const router = strapi.middlewares.router(); app.use(router.routes()); @@ -531,7 +532,7 @@ describe('router', function () { }); it('does not send 405 if route matched but status is 404', function (done) { - const app = strapi.server(); + const app = new Koa(); const router = strapi.middlewares.router(); app.use(router.routes()); @@ -553,7 +554,7 @@ describe('router', function () { }); it('supports custom routing detect path: ctx.routerPath', function (done) { - const app = strapi.server(); + const app = new Koa(); const router = strapi.middlewares.router(); app.use(function * (next) { @@ -577,7 +578,7 @@ describe('router', function () { describe('router#[verb]()', function () { it('registers route specific to HTTP verb', function () { - const app = strapi.server(); + const app = new Koa(); const router = strapi.middlewares.router(); app.use(router.routes()); @@ -600,7 +601,7 @@ describe('router', function () { }); it('registers routes without params before routes with params', function (done) { - const app = strapi.server(); + const app = new Koa(); const router = strapi.middlewares.router(); router.get('/:parameter', function * (next) { @@ -642,7 +643,7 @@ describe('router', function () { describe('router#use()', function (done) { it('uses router middleware without path', function (done) { - const app = strapi.server(); + const app = new Koa(); const router = strapi.middlewares.router(); router.get('/foo/bar', function * (next) { @@ -676,7 +677,7 @@ describe('router', function () { }); it('uses router middleware at given path', function (done) { - const app = strapi.server(); + const app = new Koa(); const router = strapi.middlewares.router(); router.use('/foo/bar', function * (next) { @@ -706,7 +707,7 @@ describe('router', function () { }); it('runs router middleware before subrouter middleware', function (done) { - const app = strapi.server(); + const app = new Koa(); const router = strapi.middlewares.router(); const subrouter = strapi.middlewares.router(); @@ -744,7 +745,7 @@ describe('router', function () { }); it('assigns middleware to array of paths', function (done) { - const app = strapi.server(); + const app = new Koa(); const router = strapi.middlewares.router(); router.use(['/foo', '/bar'], function * (next) { @@ -793,7 +794,7 @@ describe('router', function () { describe('router#register()', function () { it('registers new routes', function (done) { - const app = strapi.server(); + const app = new Koa(); const router = strapi.middlewares.router(); router.should.have.property('register'); @@ -812,7 +813,7 @@ describe('router', function () { describe('router#redirect()', function () { it('redirects using route names', function (done) { - const app = strapi.server(); + const app = new Koa(); const router = strapi.middlewares.router(); app.use(router.routes()); @@ -836,7 +837,7 @@ describe('router', function () { describe('router#route()', function () { it('inherits routes from nested router', function () { - const app = strapi.server(); + const app = new Koa(); const subrouter = strapi.middlewares.router().get('child', '/hello', function * (next) { this.body = { hello: 'world' }; }); @@ -849,7 +850,7 @@ describe('router', function () { describe('router#url()', function () { it('generates URL for given route', function (done) { - const app = strapi.server(); + const app = new Koa(); const router = strapi.middlewares.router(); app.use(router.routes()); @@ -868,7 +869,7 @@ describe('router', function () { describe('router#param()', function () { it('runs parameter middleware', function (done) { - const app = strapi.server(); + const app = new Koa(); const router = strapi.middlewares.router(); app.use(router.routes()); @@ -900,7 +901,7 @@ describe('router', function () { }); it('runs parameter middleware in order of URL appearance', function (done) { - const app = strapi.server(); + const app = new Koa(); const router = strapi.middlewares.router(); router @@ -946,7 +947,7 @@ describe('router', function () { }); it('runs parent parameter middleware for subrouter', function (done) { - const app = strapi.server(); + const app = new Koa(); const router = strapi.middlewares.router(); const subrouter = strapi.middlewares.router(); @@ -984,7 +985,7 @@ describe('router', function () { describe('router#opts', function () { it('responds with 200', function (done) { - const app = strapi.server(); + const app = new Koa(); const router = strapi.middlewares.router({ strict: true }); @@ -1009,7 +1010,7 @@ describe('router', function () { }); it('should allow setting a prefix', function (done) { - const app = strapi.server(); + const app = new Koa(); const routes = strapi.middlewares.router({ prefix: '/things/:thing_id' }); @@ -1033,7 +1034,7 @@ describe('router', function () { }); it('responds with 404 when has a trailing slash', function (done) { - const app = strapi.server(); + const app = new Koa(); const router = strapi.middlewares.router({ strict: true }); @@ -1059,7 +1060,7 @@ describe('router', function () { describe('use middleware with opts', function () { it('responds with 200', function (done) { - const app = strapi.server(); + const app = new Koa(); const router = strapi.middlewares.router({ strict: true }); @@ -1084,7 +1085,7 @@ describe('router', function () { }); it('responds with 404 when has a trailing slash', function (done) { - const app = strapi.server(); + const app = new Koa(); const router = strapi.middlewares.router({ strict: true }); @@ -1110,7 +1111,7 @@ describe('router', function () { describe('router.routes()', function () { it('should return composed middleware', function (done) { - const app = strapi.server(); + const app = new Koa(); const router = strapi.middlewares.router(); let middlewareCount = 0; @@ -1156,9 +1157,9 @@ describe('router', function () { }); }); - describe('if no `HEAD` method, default to `GET`', function () { + describe('if no HEAD method, default to GET', function () { it('should default to GET', function (done) { - const app = strapi.server(); + const app = new Koa(); const router = strapi.middlewares.router(); router.get('/users/:id', function * () { @@ -1182,7 +1183,7 @@ describe('router', function () { }); it('should work with middleware', function (done) { - const app = strapi.server(); + const app = new Koa(); const router = strapi.middlewares.router(); router.get('/users/:id', function * () { @@ -1241,7 +1242,7 @@ describe('router', function () { let middlewareCount = 0; before(function () { - const app = strapi.server(); + const app = new Koa(); const router = strapi.middlewares.router(); router.get('/', function * () { @@ -1327,7 +1328,7 @@ describe('router', function () { url.should.equal('/programming/how-to-node'); }); - it('escapes using `encodeURIComponent()`', function () { + it('escapes using encodeURIComponent()', function () { const router = strapi.middlewares.router; const url = router.url('/:category/:title', { diff --git a/test/middlewares/session/index.js b/test/middlewares/session/index.js index 6bc352812d..08594b5767 100644 --- a/test/middlewares/session/index.js +++ b/test/middlewares/session/index.js @@ -1,8 +1,10 @@ 'use strict'; const request = require('supertest'); +const should = require('should'); const strapi = require('../../..'); +const Koa = strapi.server; describe('session', function () { let cookie; @@ -10,7 +12,7 @@ describe('session', function () { describe('when options.signed = true', function () { describe('when app.keys are set', function () { it('should work', function (done) { - const app = strapi.server(); + const app = new Koa(); app.keys = ['a', 'b']; @@ -29,7 +31,7 @@ describe('session', function () { describe('when app.keys are not set', function () { it('should throw', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(strapi.middlewares.session(app)); @@ -44,21 +46,21 @@ describe('session', function () { }); }); - // describe('when app not set', function () { - // it('should throw', function () { - // const app = strapi.server(); - // - // (function () { - // app.use(strapi.middlewares.session()); - // });should.throw('app instance required: `session(opts, app)`'); - // }); - // }); + describe('when app not set', function () { + it('should throw', function () { + const app = new Koa(); + + (function () { + app.use(strapi.middlewares.session()); + }).should.throw('app instance required: `session(opts, app)`'); + }); + }); }); describe('when options.signed = false', function () { describe('when app.keys are not set', function () { it('should work', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(strapi.middlewares.session({ signed: false @@ -78,7 +80,7 @@ describe('session', function () { describe('when the session contains a ;', function () { it('should still work', function (done) { - const app = strapi.server(); + const app = new Koa(); app.keys = ['a', 'b']; @@ -113,7 +115,7 @@ describe('session', function () { describe('new session', function () { describe('when not accessed', function () { it('should not Set-Cookie', function (done) { - const app = strapi.server(); + const app = new Koa(); app.keys = ['a', 'b']; @@ -137,7 +139,7 @@ describe('session', function () { describe('when accessed and not populated', function () { it('should not Set-Cookie', function (done) { - const app = strapi.server(); + const app = new Koa(); app.keys = ['a', 'b']; @@ -162,7 +164,7 @@ describe('session', function () { describe('when populated', function () { it('should Set-Cookie', function (done) { - const app = strapi.server(); + const app = new Koa(); app.keys = ['a', 'b']; @@ -186,7 +188,7 @@ describe('session', function () { }); it('should not Set-Cookie', function (done) { - const app = strapi.server(); + const app = new Koa(); app.keys = ['a', 'b']; @@ -212,7 +214,7 @@ describe('session', function () { describe('saved session', function () { describe('when not accessed', function () { it('should not Set-Cookie', function (done) { - const app = strapi.server(); + const app = new Koa(); app.keys = ['a', 'b']; @@ -237,7 +239,7 @@ describe('session', function () { describe('when accessed but not changed', function () { it('should be the same session', function (done) { - const app = strapi.server(); + const app = new Koa(); app.keys = ['a', 'b']; @@ -255,7 +257,7 @@ describe('session', function () { }); it('should not Set-Cookie', function (done) { - const app = strapi.server(); + const app = new Koa(); app.keys = ['a', 'b']; @@ -281,7 +283,7 @@ describe('session', function () { describe('when accessed and changed', function () { it('should Set-Cookie', function (done) { - const app = strapi.server(); + const app = new Koa(); app.keys = ['a', 'b']; @@ -304,7 +306,7 @@ describe('session', function () { describe('when session is', function () { describe('null', function () { it('should expire the session', function (done) { - const app = strapi.server(); + const app = new Koa(); app.keys = ['a', 'b']; @@ -324,7 +326,7 @@ describe('session', function () { describe('an empty object', function () { it('should not Set-Cookie', function (done) { - const app = strapi.server(); + const app = new Koa(); app.keys = ['a', 'b']; @@ -349,7 +351,7 @@ describe('session', function () { describe('an object', function () { it('should create a session', function (done) { - const app = strapi.server(); + const app = new Koa(); app.keys = ['a', 'b']; @@ -371,7 +373,7 @@ describe('session', function () { describe('anything else', function () { it('should throw', function (done) { - const app = strapi.server(); + const app = new Koa(); app.keys = ['a', 'b']; @@ -390,7 +392,7 @@ describe('session', function () { describe('when an error is thrown downstream and caught upstream', function () { it('should still save the session', function (done) { - const app = strapi.server(); + const app = new Koa(); app.keys = ['a', 'b']; @@ -424,7 +426,7 @@ describe('session', function () { describe('when maxAge present', function () { describe('and not expire', function () { it('should not expire the session', function (done) { - const app = strapi.server(); + const app = new Koa(); app.keys = ['a', 'b']; @@ -462,7 +464,7 @@ describe('session', function () { describe('and expired', function () { it('should expire the sess', function (done) { - const app = strapi.server(); + const app = new Koa(); app.keys = ['a', 'b']; @@ -503,7 +505,7 @@ describe('session', function () { describe('ctx.session.maxAge', function () { it('should return opt.maxAge', function (done) { - const app = strapi.server(); + const app = new Koa(); app.keys = ['a', 'b']; @@ -523,7 +525,7 @@ describe('session', function () { describe('ctx.session.maxAge=', function () { it('should set sessionOptions.maxAge', function (done) { - const app = strapi.server(); + const app = new Koa(); app.keys = ['a', 'b']; @@ -542,146 +544,146 @@ describe('session', function () { }); }); - // describe('when get session before enter session middleware', function () { - // it('should work', function (done) { - // const app = strapi.server(); - // - // app.keys = ['a', 'b']; - // app.use(function * (next) { - // this.session.foo = 'hi'; - // yield next; - // }); - // app.use(strapi.middlewares.session({}, app)); - // app.use(function * () { - // this.body = this.session; - // }); - // - // request(app.callback()) - // .get('/') - // .expect(200, function (err, res) { - // should.not.exist(err); - // const cookies = res.headers['set-cookie'].join(';'); - // cookies.should.containEql('koa:sess='); - // - // request(app.callback()) - // .get('/') - // .set('Cookie', cookies) - // .expect(200, done); - // }); - // }); - // }); + describe('when get session before enter session middleware', function () { + it('should work', function (done) { + const app = new Koa(); - // describe('when valid and beforeSave set', function () { - // it('should ignore session when uid changed', function (done) { - // const app = strapi.server(); - // - // app.keys = ['a', 'b']; - // - // app.use(strapi.middlewares.session({ - // valid: function (ctx, sess) { - // return ctx.cookies.get('uid') === sess.uid; - // }, - // beforeSave: function (ctx, sess) { - // sess.uid = ctx.cookies.get('uid'); - // } - // }, app)); - // - // app.use(function * () { - // if (!this.session.foo) { - // this.session.foo = Date.now() + '|uid:' + this.cookies.get('uid'); - // } - // - // this.body = { - // foo: this.session.foo, - // uid: this.cookies.get('uid') - // }; - // }); - // - // request(app.callback()) - // .get('/') - // .set('Cookie', 'uid=123') - // .expect(200, function (err, res) { - // should.not.exist(err); - // const data = res.body; - // const cookies = res.headers['set-cookie'].join(';'); - // cookies.should.containEql('koa:sess='); - // - // request(app.callback()) - // .get('/') - // .set('Cookie', cookies + ';uid=123') - // .expect(200) - // .expect(data, function (err) { - // should.not.exist(err); - // - // request(app.callback()) - // .get('/') - // .set('Cookie', cookies + ';uid=456') - // .expect(200, function (err, res) { - // should.not.exist(err); - // res.body.uid.should.equal('456'); - // res.body.foo.should.not.equal(data.foo); - // done(); - // }); - // }); - // }); - // }); - // }); - // - // describe('when options.encode and options.decode are functions', function () { - // describe('they are used to encode/decode stored cookie values', function () { - // it('should work', function (done) { - // let encodeCallCount = 0; - // let decodeCallCount = 0; - // - // function encode(data) { - // ++encodeCallCount; - // return JSON.stringify({ - // enveloped: data - // }); - // } - // - // function decode(data) { - // ++decodeCallCount; - // return JSON.parse(data).enveloped; - // } - // - // const app = strapi.server(); - // - // app.keys = ['a', 'b']; - // - // app.use(strapi.middlewares.session({ - // encode: encode, - // decode: decode - // }, app)); - // - // app.use(function * () { - // this.session.counter = (this.session.counter || 0) + 1; - // this.body = this.session; - // return; - // }); - // - // request(app.callback()) - // .get('/') - // .expect(function () { - // encodeCallCount.should.above(0, 'encode was not called'); - // }) - // .expect(200, function (err, res) { - // should.not.exist(err); - // res.body.counter.should.equal(1, 'expected body to be equal to session.counter'); - // const cookies = res.headers['set-cookie'].join(';'); - // request(app.callback()) - // .get('/') - // .set('Cookie', cookies) - // .expect(function () { - // decodeCallCount.should.be.above(1, 'decode was not called'); - // }) - // .expect(200, function (err, res) { - // should.not.exist(err); - // res.body.counter.should.equal(2); - // done(); - // }); - // }); - // }); - // }); - // }); + app.keys = ['a', 'b']; + app.use(function * (next) { + this.session.foo = 'hi'; + yield next; + }); + app.use(strapi.middlewares.session({}, app)); + app.use(function * () { + this.body = this.session; + }); + + request(app.callback()) + .get('/') + .expect(200, function (err, res) { + should.not.exist(err); + const cookies = res.headers['set-cookie'].join(';'); + cookies.should.containEql('koa:sess='); + + request(app.callback()) + .get('/') + .set('Cookie', cookies) + .expect(200, done); + }); + }); + }); + + describe('when valid and beforeSave set', function () { + it('should ignore session when uid changed', function (done) { + const app = new Koa(); + + app.keys = ['a', 'b']; + + app.use(strapi.middlewares.session({ + valid: function (ctx, sess) { + return ctx.cookies.get('uid') === sess.uid; + }, + beforeSave: function (ctx, sess) { + sess.uid = ctx.cookies.get('uid'); + } + }, app)); + + app.use(function * () { + if (!this.session.foo) { + this.session.foo = Date.now() + '|uid:' + this.cookies.get('uid'); + } + + this.body = { + foo: this.session.foo, + uid: this.cookies.get('uid') + }; + }); + + request(app.callback()) + .get('/') + .set('Cookie', 'uid=123') + .expect(200, function (err, res) { + should.not.exist(err); + const data = res.body; + const cookies = res.headers['set-cookie'].join(';'); + cookies.should.containEql('koa:sess='); + + request(app.callback()) + .get('/') + .set('Cookie', cookies + ';uid=123') + .expect(200) + .expect(data, function (err) { + should.not.exist(err); + + request(app.callback()) + .get('/') + .set('Cookie', cookies + ';uid=456') + .expect(200, function (err, res) { + should.not.exist(err); + res.body.uid.should.equal('456'); + res.body.foo.should.not.equal(data.foo); + done(); + }); + }); + }); + }); + }); + + describe('when options.encode and options.decode are functions', function () { + describe('they are used to encode/decode stored cookie values', function () { + it('should work', function (done) { + let encodeCallCount = 0; + let decodeCallCount = 0; + + function encode(data) { + ++encodeCallCount; + return JSON.stringify({ + enveloped: data + }); + } + + function decode(data) { + ++decodeCallCount; + return JSON.parse(data).enveloped; + } + + const app = new Koa(); + + app.keys = ['a', 'b']; + + app.use(strapi.middlewares.session({ + encode: encode, + decode: decode + }, app)); + + app.use(function * () { + this.session.counter = (this.session.counter || 0) + 1; + this.body = this.session; + return; + }); + + request(app.callback()) + .get('/') + .expect(function () { + encodeCallCount.should.above(0, 'encode was not called'); + }) + .expect(200, function (err, res) { + should.not.exist(err); + res.body.counter.should.equal(1, 'expected body to be equal to session.counter'); + const cookies = res.headers['set-cookie'].join(';'); + request(app.callback()) + .get('/') + .set('Cookie', cookies) + .expect(function () { + decodeCallCount.should.be.above(1, 'decode was not called'); + }) + .expect(200, function (err, res) { + should.not.exist(err); + res.body.counter.should.equal(2); + done(); + }); + }); + }); + }); + }); }); diff --git a/test/middlewares/static/index.js b/test/middlewares/static/index.js index 3f236c3cd4..dfe8a66ed2 100644 --- a/test/middlewares/static/index.js +++ b/test/middlewares/static/index.js @@ -5,12 +5,13 @@ const path = require('path'); const request = require('supertest'); const strapi = require('../../..'); +const Koa = strapi.server; describe('assets', function () { describe('when defer: false', function () { describe('when root = "."', function () { it('should serve from cwd', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(strapi.middlewares.static('.')); @@ -22,7 +23,7 @@ describe('assets', function () { describe('when path is not a file', function () { it('should 404', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(strapi.middlewares.static(path.resolve(__dirname, 'fixtures'))); @@ -34,7 +35,7 @@ describe('assets', function () { describe('when upstream middleware responds', function () { it('should respond', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(strapi.middlewares.static(path.resolve(__dirname, 'fixtures'))); @@ -52,7 +53,7 @@ describe('assets', function () { describe('the path is valid', function () { it('should serve the file', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(strapi.middlewares.static(path.resolve(__dirname, 'fixtures'))); @@ -66,7 +67,7 @@ describe('assets', function () { describe('.index', function () { describe('when present', function () { it('should alter the index file supported', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(strapi.middlewares.static(path.resolve(__dirname, 'fixtures'), { index: 'index.txt' @@ -82,7 +83,7 @@ describe('assets', function () { describe('when omitted', function () { it('should use index.html', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(strapi.middlewares.static(path.resolve(__dirname, 'fixtures'))); @@ -97,7 +98,7 @@ describe('assets', function () { describe('when method is not `GET` or `HEAD`', function () { it('should 404', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(strapi.middlewares.static(path.resolve(__dirname, 'fixtures'))); @@ -111,7 +112,7 @@ describe('assets', function () { describe('when defer: true', function () { describe('when upstream middleware responds', function () { it('should do nothing', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(strapi.middlewares.static(path.resolve(__dirname, 'fixtures'), { defer: true @@ -131,7 +132,7 @@ describe('assets', function () { describe('the path is valid', function () { it('should serve the file', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(strapi.middlewares.static(path.resolve(__dirname, 'fixtures'), { defer: true @@ -147,7 +148,7 @@ describe('assets', function () { describe('.index', function () { describe('when present', function () { it('should alter the index file supported', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(strapi.middlewares.static(path.resolve(__dirname, 'fixtures'), { defer: true, @@ -164,7 +165,7 @@ describe('assets', function () { describe('when omitted', function () { it('should use index.html', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(strapi.middlewares.static(path.resolve(__dirname, 'fixtures'), { defer: true @@ -181,7 +182,7 @@ describe('assets', function () { describe('when path is not a file', function () { it('should 404', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(strapi.middlewares.static(path.resolve(__dirname, 'fixtures'), { defer: true @@ -195,7 +196,7 @@ describe('assets', function () { describe('it should not handle the request', function () { it('when status=204', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(strapi.middlewares.static(path.resolve(__dirname, 'fixtures'), { defer: true @@ -211,7 +212,7 @@ describe('assets', function () { }); it('when body=""', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(strapi.middlewares.static(path.resolve(__dirname, 'fixtures'), { defer: true @@ -229,7 +230,7 @@ describe('assets', function () { describe('when method is not `GET` or `HEAD`', function () { it('should 404', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(strapi.middlewares.static(path.resolve(__dirname, 'fixtures'), { defer: true diff --git a/test/middlewares/views/index.js b/test/middlewares/views/index.js index ff2edc8965..88df9e5a9f 100755 --- a/test/middlewares/views/index.js +++ b/test/middlewares/views/index.js @@ -1,16 +1,15 @@ 'use strict'; -const fs = require('fs'); const path = require('path'); -const request = require('supertest') -const should = require('should') +const request = require('supertest'); -const strapi = require('../../..') +const strapi = require('../../..'); +const Koa = strapi.server; describe('views', function () { it('have a render method', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(strapi.middlewares.views()); @@ -24,28 +23,28 @@ describe('views', function () { .expect(404, done); }); - it('default to html', function (done) { - const app = strapi.server(); - const router = strapi.middlewares.router(); + // it('default to html', function (done) { + // const app = new Koa(); + // const router = strapi.middlewares.router(); + // + // app.use(strapi.middlewares.views(path.resolve(__dirname, 'fixtures'))); + // + // router.get('/', function * () { + // yield this.render('basic'); + // }); + // + // app.use(router.routes()); + // app.use(router.allowedMethods()); + // + // request(app.listen()) + // .get('/') + // .expect('Content-Type', /html/) + // .expect(/basic:html/) + // .expect(200, done); + // }); - app.use(strapi.middlewares.views(path.resolve(__dirname, 'fixtures'))); - - router.get('/', function * () { - yield this.render('basic'); - }); - - app.use(router.routes()); - app.use(router.allowedMethods()); - - request(app.listen()) - .get('/') - .expect('Content-Type', /html/) - .expect(/basic:html/) - .expect(200, done); - }); - - it('default to [ext] if a default engine is set', function (done) { - const app = strapi.server(); + it('default to ext if a default engine is set', function (done) { + const app = new Koa(); app.use(strapi.middlewares.views(path.resolve(__dirname, 'fixtures'), { default: 'jade' @@ -63,7 +62,7 @@ describe('views', function () { }); it('set and render state', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(strapi.middlewares.views(path.resolve(__dirname, 'fixtures'), { default: 'jade' @@ -82,7 +81,7 @@ describe('views', function () { }); it('set option: root', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(strapi.middlewares.views(path.resolve(__dirname, 'fixtures'), { root: '../../../test', @@ -102,7 +101,7 @@ describe('views', function () { }); it('works with circular references in state', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(strapi.middlewares.views(path.resolve(__dirname, 'fixtures'), { default: 'jade' @@ -130,8 +129,8 @@ describe('views', function () { .expect(200, done); }); - it('`map` given `engine` to given file `ext`', function (done) { - const app = strapi.server(); + it('map given engine to given file ext', function (done) { + const app = new Koa(); app.use(strapi.middlewares.views(path.resolve(__dirname, 'fixtures'), { map: { @@ -152,7 +151,7 @@ describe('views', function () { }); it('merges global and local state ', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(strapi.middlewares.views(path.resolve(__dirname, 'fixtures'), { default: 'jade' @@ -174,7 +173,7 @@ describe('views', function () { }); it('yields to the next middleware if this.render is already defined', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(function * (next) { this.render = true; diff --git a/test/request/host.js b/test/request/host.js index 9f0c14b3af..55724b6ab7 100755 --- a/test/request/host.js +++ b/test/request/host.js @@ -23,7 +23,7 @@ describe('req.host', function () { it('should be ignored', function () { const req = request(); req.header['x-forwarded-host'] = 'bar.com'; - req.header['host'] = 'foo.com'; + req.header.host = 'foo.com'; req.host.should.equal('foo.com'); }); }); @@ -33,7 +33,7 @@ describe('req.host', function () { const req = request(); req.app.proxy = true; req.header['x-forwarded-host'] = 'bar.com, baz.com'; - req.header['host'] = 'foo.com'; + req.header.host = 'foo.com'; req.host.should.equal('bar.com'); }); }); diff --git a/test/request/hostname.js b/test/request/hostname.js index f2c9fba4e8..be64c08410 100755 --- a/test/request/hostname.js +++ b/test/request/hostname.js @@ -23,7 +23,7 @@ describe('req.hostname', function () { it('should be ignored', function () { const req = request(); req.header['x-forwarded-host'] = 'bar.com'; - req.header['host'] = 'foo.com'; + req.header.host = 'foo.com'; req.hostname.should.equal('foo.com'); }); }); @@ -33,7 +33,7 @@ describe('req.hostname', function () { const req = request(); req.app.proxy = true; req.header['x-forwarded-host'] = 'bar.com, baz.com'; - req.header['host'] = 'foo.com'; + req.header.host = 'foo.com'; req.hostname.should.equal('bar.com'); }); }); diff --git a/test/request/href.js b/test/request/href.js index 22d1eac36b..447ad16167 100755 --- a/test/request/href.js +++ b/test/request/href.js @@ -1,11 +1,10 @@ 'use strict'; const http = require('http'); +const context = require('../helpers/context'); const Stream = require('stream'); -const strapi = require('../..'); - -const context = require('../helpers/context'); +const Koa = require('../..').server; describe('ctx.href', function () { it('should return the full request url', function () { @@ -18,6 +17,7 @@ describe('ctx.href', function () { socket: socket, __proto__: Stream.Readable.prototype }; + const ctx = context(req); ctx.href.should.equal('http://localhost/users/1?next=/dashboard'); ctx.url = '/foo/users/1?next=/dashboard'; @@ -25,10 +25,12 @@ describe('ctx.href', function () { }); it('should work with `GET http://example.com/foo`', function (done) { - const app = strapi.server(); + const app = new Koa(); + app.use(function * () { this.body = this.href; }); + app.listen(function () { const address = this.address(); http.get({ diff --git a/test/request/ip.js b/test/request/ip.js index 60f16399e2..f8486b08e6 100755 --- a/test/request/ip.js +++ b/test/request/ip.js @@ -14,10 +14,18 @@ describe('req.ip', function () { }); describe('with no req.ips present', function () { - it('should return req.socket.removeAddress', function () { + it('should return req.socket.remoteAddress', function () { 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 () { + const req = request(); + req.socket.remoteAddress = null; + req.ip.should.equal(''); + }); + }); }); }); diff --git a/test/request/origin.js b/test/request/origin.js new file mode 100755 index 0000000000..98029d3e2d --- /dev/null +++ b/test/request/origin.js @@ -0,0 +1,23 @@ +'use strict'; + +const Stream = require('stream'); + +const context = require('../helpers/context'); + +describe('ctx.origin', function () { + it('should return the origin of url', function () { + const socket = new Stream.Duplex(); + const req = { + url: '/users/1?next=/dashboard', + headers: { + host: 'localhost' + }, + socket: socket, + __proto__: Stream.Readable.prototype + }; + const ctx = context(req); + ctx.origin.should.equal('http://localhost'); + ctx.url = '/foo/users/1?next=/dashboard'; + ctx.origin.should.equal('http://localhost'); + }); +}); diff --git a/test/request/querystring.js b/test/request/querystring.js index 7838343bf9..4cf9a69c15 100755 --- a/test/request/querystring.js +++ b/test/request/querystring.js @@ -2,6 +2,23 @@ const context = require('../helpers/context'); +describe('ctx.querystring', function () { + it('should return the querystring', function () { + 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 () { + const ctx = context(); + ctx.request.req = null; + ctx.querystring.should.equal(''); + }); + }); +}); + describe('ctx.querystring=', function () { it('should replace the querystring', function () { const ctx = context({ diff --git a/test/response/attachment.js b/test/response/attachment.js index f7f8821629..4bb052cd73 100755 --- a/test/response/attachment.js +++ b/test/response/attachment.js @@ -2,10 +2,10 @@ const request = require('supertest'); -const strapi = require('../..'); - const context = require('../helpers/context'); +const Koa = require('../..').server; + describe('ctx.attachment([filename])', function () { describe('when given a filename', function () { it('should set the filename param', function () { @@ -33,7 +33,7 @@ describe('ctx.attachment([filename])', function () { }); it('should work with http client', function (done) { - const app = strapi.server(); + const app = new Koa(); app.use(function * () { this.attachment('path/to/include-no-ascii-char-中文名-ok.json'); diff --git a/test/response/body.js b/test/response/body.js index bebe59c073..48bd74b7d7 100755 --- a/test/response/body.js +++ b/test/response/body.js @@ -1,7 +1,6 @@ 'use strict'; const fs = require('fs'); - const assert = require('assert'); const response = require('../helpers/context').response; diff --git a/test/response/redirect.js b/test/response/redirect.js index 173947b8e6..5d56e850df 100755 --- a/test/response/redirect.js +++ b/test/response/redirect.js @@ -45,7 +45,7 @@ describe('ctx.redirect(url)', function () { ctx.header.accept = 'text/html'; ctx.redirect(url); ctx.response.header['content-type'].should.equal('text/html; charset=utf-8'); - ctx.body.should.equal('Redirecting to ' + url + '.'); + ctx.body.should.equal(`Redirecting to ${url}.`); }); it('should escape the url', function () { @@ -55,7 +55,7 @@ describe('ctx.redirect(url)', function () { ctx.redirect(url); url = escape(url); ctx.response.header['content-type'].should.equal('text/html; charset=utf-8'); - ctx.body.should.equal('Redirecting to ' + url + '.'); + ctx.body.should.equal(`Redirecting to ${url}.`); }); }); @@ -65,7 +65,7 @@ describe('ctx.redirect(url)', function () { const url = 'http://google.com'; ctx.header.accept = 'text/plain'; ctx.redirect(url); - ctx.body.should.equal('Redirecting to ' + url + '.'); + ctx.body.should.equal(`Redirecting to ${url}.`); }); }); @@ -77,7 +77,7 @@ describe('ctx.redirect(url)', function () { ctx.header.accept = 'text/plain'; ctx.redirect('http://google.com'); ctx.status.should.equal(301); - ctx.body.should.equal('Redirecting to ' + url + '.'); + ctx.body.should.equal(`Redirecting to ${url}.`); }); }); @@ -89,7 +89,7 @@ describe('ctx.redirect(url)', function () { ctx.header.accept = 'text/plain'; ctx.redirect('http://google.com'); ctx.status.should.equal(302); - ctx.body.should.equal('Redirecting to ' + url + '.'); + ctx.body.should.equal(`Redirecting to ${url}.`); }); }); @@ -101,14 +101,14 @@ describe('ctx.redirect(url)', function () { ctx.header.accept = 'text/plain'; ctx.redirect('http://google.com'); ctx.status.should.equal(302); - ctx.body.should.equal('Redirecting to ' + url + '.'); + ctx.body.should.equal(`Redirecting to ${url}.`); ctx.type.should.equal('text/plain'); }); }); }); -function escape(html) { - return String(html) +function escape (html) { + return String (html) .replace(/&/g, '&') .replace(/"/g, '"') .replace(/