diff --git a/server/base/application.ts b/server/base/application.ts index 70b54a6f..cdfedf9b 100644 --- a/server/base/application.ts +++ b/server/base/application.ts @@ -14,6 +14,7 @@ * limitations under the License. */ +import * as bodyParser from 'body-parser'; import * as express from 'express'; import * as path from 'path'; @@ -21,14 +22,24 @@ export class Application { private static app: express.Application = express(); public static async START(): Promise { - await import('../example/example.router'); this.app.use(express.static(path.join(__dirname, '../client'))); + this.app.use(bodyParser.json({limit: '10mb'})); + this.app.use(bodyParser.urlencoded({ + limit: '10mb', + extended: true, + })); + await import('../example/example.router'); this.app.listen(8090); } public static async START_FOR_TESTING(): Promise { - await import('../example/example.router'); this.app.use(express.static(path.join(__dirname, '../../out/client'))); + this.app.use(bodyParser.json({limit: '10mb'})); + this.app.use(bodyParser.urlencoded({ + limit: '10mb', + extended: true, + })); + await import('../example/example.router'); return this.app; } diff --git a/server/example/example.router.ts b/server/example/example.router.ts index 0137dfd8..007030ec 100644 --- a/server/example/example.router.ts +++ b/server/example/example.router.ts @@ -22,4 +22,15 @@ export class Test { public get(request: express.Request, response: express.Response): void { response.send('hello world'); } + public post(request: express.Request, response: express.Response): void { + if (request.body) { + if (request.body.userId === 'absolute') { + response.sendStatus(200); + } else { + response.sendStatus(400); + } + } else { + response.sendStatus(501); + } + } } diff --git a/server/example/example.test.ts b/server/example/example.test.ts index a1d087c7..b525d87f 100644 --- a/server/example/example.test.ts +++ b/server/example/example.test.ts @@ -24,3 +24,19 @@ test('GET /example', async() => { expect(response.statusCode).toBe(200); expect(response.text).toBe('hello world'); }); + +test('POST x-www-form-urlencoded', async() => { + const request: {} = supertest(await Application.START_FOR_TESTING()); + const response: {} = await request.post('/example') + .set('Content-Type', 'application/x-www-form-urlencoded') + .send('userId=absolute'); + expect(response.statusCode).toBe(200); +}); + +test('POST json', async() => { + const request: {} = supertest(await Application.START_FOR_TESTING()); + const response: {} = await request.post('/example') + .set('Content-Type', 'application/json') + .send('{"userId": "absolute"}') + expect(response.statusCode).toBe(200); +})