Skip to content

Commit

Permalink
feat: enable overrideMethod middleware by default
Browse files Browse the repository at this point in the history
only allow override method on POST request by default

#324
  • Loading branch information
fengmk2 committed Jun 19, 2017
1 parent bfb8df5 commit 81405f1
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 1 deletion.
3 changes: 3 additions & 0 deletions app/middleware/override_method.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

module.exports = require('koa-override');
1 change: 1 addition & 0 deletions config/config.default.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ module.exports = appInfo => {
'siteFile',
'notfound',
'bodyParser',
'overrideMethod',
];

/**
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
55 changes: 55 additions & 0 deletions test/app/middleware/override_method.test.js
Original file line number Diff line number Diff line change
@@ -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');
});
});
17 changes: 17 additions & 0 deletions test/fixtures/apps/override_method/app/router.js
Original file line number Diff line number Diff line change
@@ -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';
});
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports.keys = 'foo';
3 changes: 3 additions & 0 deletions test/fixtures/apps/override_method/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "override_method"
}
3 changes: 2 additions & 1 deletion test/lib/core/loader/config_loader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
]);
});
Expand Down

0 comments on commit 81405f1

Please sign in to comment.