Update to Koa v1.1.0

This commit is contained in:
loicsaintroch 2015-10-16 15:47:17 +02:00
parent 809580df97
commit a1a85a557f
47 changed files with 1221 additions and 993 deletions

9
.eslintrc Normal file
View File

@ -0,0 +1,9 @@
parser: babel-eslint
extends: standard
rules:
eqeqeq: 0
semi: [2, always]
space-before-function-paren: [2, never]
yoda: 0

View File

@ -1,10 +1,6 @@
language: node_js
node_js:
- "0.12"
- "1"
- "2"
- "3"
- "4"
sudo: false

View File

@ -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

View File

@ -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'));

View File

@ -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",

35
test/application/context.js Executable file
View File

@ -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);
});
});

54
test/application/index.js Executable file
View File

@ -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');
});
});

11
test/application/inspect.js Executable file
View File

@ -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);
});
});

82
test/application/onerror.js Executable file
View File

@ -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();
});
});

35
test/application/request.js Executable file
View File

@ -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);
});
});

File diff suppressed because it is too large Load Diff

35
test/application/response.js Executable file
View File

@ -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);
});
});

15
test/application/toJSON.js Executable file
View File

@ -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'
});
});
});

59
test/application/use.js Executable file
View File

@ -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 () {});
});
});

View File

@ -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) {

View File

@ -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')

View File

@ -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);

View File

@ -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();
}
});
});

21
test/experimental/async.js Executable file
View File

@ -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);
});
});

6
test/experimental/index.js Executable file
View File

@ -0,0 +1,6 @@
'use strict';
require('babel/register')({
optional: ['asyncToGenerator']
});
require('./async');

View File

@ -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;
};

View File

@ -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'];

View File

@ -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
}));

View File

@ -6,6 +6,7 @@ const should = require('should');
const request = require('supertest');
const strapi = require('../../..');
const Koa = strapi.server;
describe('gzip', function () {
const options = {};

View File

@ -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);

View File

@ -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);
// });
// });
});

View File

@ -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)

View File

@ -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')

View File

@ -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'];

View File

@ -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)

View File

@ -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';
});

View File

@ -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'

View File

@ -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');

View File

@ -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', {

View File

@ -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();
});
});
});
});
});
});

View File

@ -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

View File

@ -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;

View File

@ -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');
});
});

View File

@ -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');
});
});

View File

@ -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({

View File

@ -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('');
});
});
});
});

23
test/request/origin.js Executable file
View File

@ -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');
});
});

View File

@ -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({

View File

@ -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');

View File

@ -1,7 +1,6 @@
'use strict';
const fs = require('fs');
const assert = require('assert');
const response = require('../helpers/context').response;

View File

@ -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 <a href="' + url + '">' + url + '</a>.');
ctx.body.should.equal(`Redirecting to <a href="${url}">${url}</a>.`);
});
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 <a href="' + url + '">' + url + '</a>.');
ctx.body.should.equal(`Redirecting to <a href="${url}">${url}</a>.`);
});
});
@ -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, '&amp;')
.replace(/"/g, '&quot;')
.replace(/</g, '&lt;')

View File

@ -2,11 +2,12 @@
const assert = require('assert');
const request = require('supertest');
const strapi = require('../..');
const statuses = require('statuses');
const response = require('../helpers/context').response;
const Koa = require('../..').server;
describe('res.status=', function () {
describe('when a status code', function () {
describe('and valid', function () {
@ -31,23 +32,23 @@ describe('res.status=', function () {
});
});
// describe('and custom status', function () {
// before(function () {
// statuses['700'] = 'custom status';
// });
//
// it('should set the status', function () {
// const res = response();
// res.status = 700;
// res.status.should.equal(700);
// });
//
// it('should not throw', function() {
// assert.doesNotThrow(function() {
// response().status = 700;
// });
// });
// });
describe('and custom status', function () {
before(function () {
statuses['700'] = 'custom status';
});
it('should set the status', function () {
const res = response();
res.status = 700;
res.status.should.equal(700);
});
it('should not throw', function () {
assert.doesNotThrow(function () {
response().status = 700;
});
});
});
});
describe('when a status string', function () {
@ -58,9 +59,9 @@ describe('res.status=', function () {
});
});
function strip(status) {
function strip (status) {
it('should strip content related header fields', function (done) {
const app = strapi.server();
const app = new Koa();
app.use(function * () {
this.body = {
@ -88,7 +89,7 @@ describe('res.status=', function () {
});
it('should strip content releated header fields after status set', function (done) {
const app = strapi.server();
const app = new Koa();
app.use(function * () {
this.status = status;