From 185fccad65b921e3e2bd812b19cf6d817a2d2402 Mon Sep 17 00:00:00 2001 From: ChengLei Shao Date: Mon, 4 Sep 2023 22:21:31 +0800 Subject: [PATCH] chore: incr bodyParser body limit (#2591) --- .../server/src/__tests__/application.test.ts | 18 ++++++++++++++++++ .../server/src/__tests__/fixtures/long-json.ts | 11 +++++++++++ packages/core/server/src/helper.ts | 4 ++++ 3 files changed, 33 insertions(+) create mode 100644 packages/core/server/src/__tests__/fixtures/long-json.ts diff --git a/packages/core/server/src/__tests__/application.test.ts b/packages/core/server/src/__tests__/application.test.ts index 31fb19e34..ef361e7c5 100644 --- a/packages/core/server/src/__tests__/application.test.ts +++ b/packages/core/server/src/__tests__/application.test.ts @@ -1,6 +1,7 @@ import supertest from 'supertest'; import { Application } from '../application'; import { Plugin } from '../plugin'; +import longJson from './fixtures/long-json'; class MyPlugin extends Plugin { async load() {} @@ -29,6 +30,7 @@ describe('application', () => { dataWrapping: false, registerActions: false, }); + app.resourcer.registerActionHandlers({ list: async (ctx, next) => { ctx.body = [1, 2]; @@ -50,6 +52,22 @@ describe('application', () => { return app.destroy(); }); + it('should request long json', async () => { + app.resourcer.define({ + name: 'test', + actions: { + test: async (ctx, next) => { + ctx.body = ctx.request.body; + await next(); + }, + }, + }); + + const response = await agent.post('/api/test:test').send(longJson).set('Content-Type', 'application/json'); + + expect(response.statusCode).toBe(200); + }); + it('resourcer.define', async () => { app.resourcer.define({ name: 'test', diff --git a/packages/core/server/src/__tests__/fixtures/long-json.ts b/packages/core/server/src/__tests__/fixtures/long-json.ts new file mode 100644 index 000000000..5f1185eb1 --- /dev/null +++ b/packages/core/server/src/__tests__/fixtures/long-json.ts @@ -0,0 +1,11 @@ +export default { + title: 'title', + content: getLongString(), +}; + +function getLongString() { + const size = 2 * 1024 * 1024; + const buffer = Buffer.alloc(size, 'a'); + const str = buffer.toString('utf-8'); + return str; +} diff --git a/packages/core/server/src/helper.ts b/packages/core/server/src/helper.ts index 070737c25..a67dc7670 100644 --- a/packages/core/server/src/helper.ts +++ b/packages/core/server/src/helper.ts @@ -48,8 +48,12 @@ export function registerMiddlewares(app: Application, options: ApplicationOption ); if (options.bodyParser !== false) { + const bodyLimit = '10mb'; app.use( bodyParser({ + jsonLimit: bodyLimit, + formLimit: bodyLimit, + textLimit: bodyLimit, ...options.bodyParser, }), {