Skip to content

Commit

Permalink
Add bodyParser
Browse files Browse the repository at this point in the history
Add bodyParser Module and Test
It is needed for handling a request as post

ISSUE=lunchclass#458
  • Loading branch information
hyungheo committed Nov 16, 2017
1 parent aecd7a9 commit e7a580c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
9 changes: 7 additions & 2 deletions server/base/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import * as bodyParser from 'body-parser';
import * as express from 'express';
import * as path from 'path';

Expand All @@ -24,14 +25,18 @@ export class Application {
private static app: express.Application = express();

public static async START(): Promise<void> {
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<express.Application> {
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;
}
Expand Down
11 changes: 11 additions & 0 deletions server/example/example.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
12 changes: 12 additions & 0 deletions server/example/example.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

0 comments on commit e7a580c

Please sign in to comment.