From e7a580c6aba08e3c50aa81694832634c619353e7 Mon Sep 17 00:00:00 2001 From: HyungHeo Date: Thu, 9 Nov 2017 10:41:47 +0900 Subject: [PATCH] Add bodyParser Add bodyParser Module and Test It is needed for handling a request as post ISSUE=#458 --- server/base/application.ts | 9 +++++++-- server/example/example.router.ts | 11 +++++++++++ server/example/example.test.ts | 12 ++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/server/base/application.ts b/server/base/application.ts index 84903cc4..ca1a1a2d 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'; @@ -24,14 +25,18 @@ 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()); + this.app.use(bodyParser.urlencoded({ 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()); + this.app.use(bodyParser.urlencoded({ 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 1d5e525b..1b7c856e 100644 --- a/server/example/example.router.ts +++ b/server/example/example.router.ts @@ -25,4 +25,15 @@ export class ExampleRouter { 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.exampleParam === 'example') { + 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 a532fde6..ac004bb1 100644 --- a/server/example/example.test.ts +++ b/server/example/example.test.ts @@ -24,3 +24,15 @@ 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('exampleParam=example'); + 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('{"exampleParam": "example"}'); + expect(response.statusCode).toBe(200); +});