Skip to content

Commit

Permalink
Add bodyParser
Browse files Browse the repository at this point in the history
Add bodyParser Module and Test

ISSUE=lunchclass#458
  • Loading branch information
hyungheo committed Nov 9, 2017
1 parent 3cc0f26 commit f744a29
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
8 changes: 7 additions & 1 deletion server/base/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,21 @@
* limitations under the License.
*/

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

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);
}

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 @@ -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.status(200);
} else {
response.sendStatus(400);
}
} else {
response.sendStatus(501);
}
}
}
9 changes: 9 additions & 0 deletions server/example/example.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,12 @@ test('GET /example', async() => {
expect(response.statusCode).toBe(200);
expect(response.text).toBe('hello world');
});

test('POST /example', async() => {
const request: {} = supertest(await Application.START_FOR_TESTING());
const response: {} = await request.get('/example')
.set('Content-Type', 'application/x-www-form-urlencoded')
.send('userId=absolute');
expect(response.statusCode).toBe(200);
expect(response.text).toBe('hello world');
});

0 comments on commit f744a29

Please sign in to comment.