ts-router
Version:
koa router middleware, typescript fully supported, jersey like coding
96 lines (90 loc) • 2.66 kB
text/typescript
import * as tsRouter from '../src';
import * as chai from 'chai';
import * as request from 'supertest';
import * as Koa from 'koa';
.Path('/test')
class TestController extends tsRouter.Controller {
.Path('')
.POST
.Consume(tsRouter.MediaType.JSON)
.Produce(tsRouter.MediaType.JSON)
async index(
.Body body:Object,
.BodyParam('v1') v1:string,
.BodyParam('v2') v2:string
):Promise<tsRouter.Response> {
return tsRouter.Response.status(200).body({
hello: v1,
world: v2,
body
}).build();
}
.Body body:Object;
.BodyParam('v1') v1:string;
.BodyParam('v2') v2:string;
.Path('/2')
.POST
.Consume(tsRouter.MediaType.JSON)
.Produce(tsRouter.MediaType.JSON)
async index2():Promise<tsRouter.Response> {
let body = this.body;
let v1 = this.v1;
let v2 = this.v2;
return tsRouter.Response.status(200).body({
hello: v1,
world: v2,
body
}).build();
}
}
const app = new Koa();
const router = new tsRouter.Router();
router.use(TestController);
app.use(router.routes());
let server;
describe('POST with body', () => {
before(() => {
server = app.listen(3000)
})
after(() => {
server.close();
});
it('should respond body back in json', function (done) {
request(server)
.post('/test')
.send({
v1: 'hello',
v2: 'world'
})
.set('Content-Type', 'application/json;charset=utf-8')
.expect('Content-Type', 'application/json')
.expect({
body: {
v1: 'hello',
v2: 'world'
},
hello: 'hello',
world: 'world'
})
.expect(200, done);
});
it('should route to correct subroute and respond body back in json', function (done) {
request(server)
.post('/test/2')
.send({
v1: 'hello',
v2: 'world'
})
.set('Content-Type', 'application/json;charset=utf-8')
.expect('Content-Type', 'application/json')
.expect({
body: {
v1: 'hello',
v2: 'world'
},
hello: 'hello',
world: 'world'
})
.expect(200, done);
});
})