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 813e03e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
16 changes: 13 additions & 3 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,15 +25,24 @@ 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({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<express.Application> {
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;
}

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.userId === 'absolute') {
response.sendStatus(200);
} else {
response.sendStatus(400);
}
} else {
response.sendStatus(501);
}
}
}
16 changes: 16 additions & 0 deletions server/example/example.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
})

0 comments on commit 813e03e

Please sign in to comment.