From a4ba2a2a1ef7de49e196c01447fd73ab22ed6d34 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Mon, 19 Jun 2017 13:47:57 +0800 Subject: [PATCH] feat: enable overrideMethod middleware by default (#1069) --- app/middleware/override_method.js | 3 + config/config.default.js | 1 + package.json | 9 +-- test/app/middleware/override_method.test.js | 55 +++++++++++++++++++ .../apps/override_method/app/router.js | 17 ++++++ .../override_method/config/config.default.js | 1 + .../apps/override_method/package.json | 3 + test/lib/core/loader/config_loader.test.js | 3 +- 8 files changed, 87 insertions(+), 5 deletions(-) create mode 100644 app/middleware/override_method.js create mode 100644 test/app/middleware/override_method.test.js create mode 100644 test/fixtures/apps/override_method/app/router.js create mode 100644 test/fixtures/apps/override_method/config/config.default.js create mode 100644 test/fixtures/apps/override_method/package.json diff --git a/app/middleware/override_method.js b/app/middleware/override_method.js new file mode 100644 index 0000000000..bf5f7ef0f4 --- /dev/null +++ b/app/middleware/override_method.js @@ -0,0 +1,3 @@ +'use strict'; + +module.exports = require('koa-override'); diff --git a/config/config.default.js b/config/config.default.js index 9a402c3688..fbc88766e6 100644 --- a/config/config.default.js +++ b/config/config.default.js @@ -254,6 +254,7 @@ module.exports = appInfo => { 'siteFile', 'notfound', 'bodyParser', + 'overrideMethod', ]; /** diff --git a/package.json b/package.json index 3cb91bbb68..8b762985d6 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,7 @@ "co": "^4.6.0", "debug": "^2.6.8", "delegates": "^1.0.0", - "egg-cluster": "^1.7.0", + "egg-cluster": "^1.8.0", "egg-cookies": "^2.2.1", "egg-core": "^3.10.0", "egg-development": "^1.3.1", @@ -30,7 +30,7 @@ "egg-multipart": "^1.5.0", "egg-onerror": "^1.4.4", "egg-schedule": "^2.4.1", - "egg-security": "^1.10.1", + "egg-security": "^1.10.2", "egg-session": "^2.1.1", "egg-static": "^1.4.1", "egg-view": "^1.1.1", @@ -40,6 +40,7 @@ "is-type-of": "^1.0.0", "koa-bodyparser": "^2.5.0", "koa-is-json": "^1.0.0", + "koa-override": "^2.0.0", "mime-types": "^2.1.15", "sendmessage": "^1.1.0", "urllib": "^2.22.0", @@ -51,13 +52,13 @@ "autod-egg": "^1.0.0", "coffee": "^3.3.2", "egg-alinode": "^1.0.3", - "egg-bin": "^3.4.2", + "egg-bin": "^3.6.0", "egg-doctools": "^2.0.0", "egg-mock": "^3.7.2", "egg-plugin-puml": "^2.4.0", "egg-view-nunjucks": "^2.1.2", "eslint": "^3.19.0", - "eslint-config-egg": "^4.2.0", + "eslint-config-egg": "^4.2.1", "findlinks": "^1.1.0", "formstream": "^1.1.0", "glob": "^7.1.2", diff --git a/test/app/middleware/override_method.test.js b/test/app/middleware/override_method.test.js new file mode 100644 index 0000000000..80622e43ff --- /dev/null +++ b/test/app/middleware/override_method.test.js @@ -0,0 +1,55 @@ +'use strict'; + +const utils = require('../../utils'); + +describe('test/app/middleware/override_method.test.js', () => { + let app; + before(() => { + app = utils.app('apps/override_method'); + return app.ready(); + }); + after(() => app.close()); + + it('should put', () => { + app.mockCsrf(); + return app.httpRequest() + .post('/test') + .send({ _method: 'PUT' }) + .expect(200) + .expect('test-put'); + }); + + it('should patch', () => { + app.mockCsrf(); + return app.httpRequest() + .post('/test') + .send({ _method: 'PATCH' }) + .expect(200) + .expect('test-patch'); + }); + + it('should delete', () => { + return app.httpRequest() + .post('/test') + .send({ _method: 'DELETE' }) + .expect(200) + .expect('test-delete'); + }); + + it('should not work on PUT request', () => { + app.mockCsrf(); + return app.httpRequest() + .put('/test') + .send({ _method: 'DELETE' }) + .expect(200) + .expect('test-put'); + }); + + it('should not work on GET request', () => { + return app.httpRequest() + .get('/test') + .set('x-http-method-override', 'DELETE') + .expect(200) + .expect('test-get'); + }); +}); diff --git a/test/fixtures/apps/override_method/app/router.js b/test/fixtures/apps/override_method/app/router.js new file mode 100644 index 0000000000..45e07cf65f --- /dev/null +++ b/test/fixtures/apps/override_method/app/router.js @@ -0,0 +1,17 @@ +module.exports = app => { + app.get('/test', function* () { + this.body = "test-get"; + }); + + app.put('/test', function* () { + this.body = "test-put"; + }); + + app.delete('/test', function* () { + this.body = 'test-delete'; + }); + + app.patch('/test', function* () { + this.body = 'test-patch'; + }); +}; diff --git a/test/fixtures/apps/override_method/config/config.default.js b/test/fixtures/apps/override_method/config/config.default.js new file mode 100644 index 0000000000..b6cba897c8 --- /dev/null +++ b/test/fixtures/apps/override_method/config/config.default.js @@ -0,0 +1 @@ +exports.keys = 'foo'; diff --git a/test/fixtures/apps/override_method/package.json b/test/fixtures/apps/override_method/package.json new file mode 100644 index 0000000000..73b8ebb113 --- /dev/null +++ b/test/fixtures/apps/override_method/package.json @@ -0,0 +1,3 @@ +{ + "name": "override_method" +} diff --git a/test/lib/core/loader/config_loader.test.js b/test/lib/core/loader/config_loader.test.js index e0967c08c4..40a9978930 100644 --- a/test/lib/core/loader/config_loader.test.js +++ b/test/lib/core/loader/config_loader.test.js @@ -14,12 +14,13 @@ describe('test/lib/core/loader/config_loader.test.js', () => { it('should get middlewares', function* () { app = utils.app('apps/demo'); yield app.ready(); - assert.deepEqual(app.config.coreMiddleware.slice(0, 6), [ + assert.deepEqual(app.config.coreMiddleware.slice(0, 7), [ 'meta', 'siteFile', 'notfound', 'static', 'bodyParser', + 'overrideMethod', 'session', ]); });