ts-router
Version:
koa router middleware, typescript fully supported, jersey like coding
89 lines (85 loc) • 2.69 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('/:v1/:v2')
.GET
.Produce(tsRouter.MediaType.JSON)
async index(
.Params params:Object,
.PathParam('v1') v1:string,
.PathParam('v2') v2:string
):Promise<tsRouter.Response> {
return tsRouter.Response.status(200).body({v1, v2, params}).build();
}
.Params params:Object;
.PathParam('v1') v1:string;
.PathParam('v2') v2:string;
.Path('/2/:v1/:v2')
.Path('/3/:v1/:v2')
.GET
.Produce(tsRouter.MediaType.JSON)
async index2():Promise<tsRouter.Response> {
let params = this.params;
let v1 = this.v1;
let v2 = this.v2;
return tsRouter.Response.status(200).body({v1, v2, params}).build();
}
}
const app = new Koa();
const router = new tsRouter.Router();
router.use(TestController);
app.use(router.routes());
let server;
describe('GET with path paramters', () => {
before(() => {
server = app.listen(3000)
})
after(() => {
server.close();
});
it('should respond paramters in back in json', function (done) {
request(server)
.get('/test/hello/world')
.expect('Content-Type', 'application/json')
.expect({
params: {
v1: 'hello',
v2: 'world'
},
v1: 'hello',
v2: 'world'
})
.expect(200, done);
});
it('should route to correct subroute respond paramters in back in json', function (done) {
request(server)
.get('/test/2/hello/world')
.expect('Content-Type', 'application/json')
.expect({
params: {
v1: 'hello',
v2: 'world'
},
v1: 'hello',
v2: 'world'
})
.expect(200, done);
});
it('should route to correct subroute respond paramters in back in json', function (done) {
request(server)
.get('/test/3/hello/world')
.expect('Content-Type', 'application/json')
.expect({
params: {
v1: 'hello',
v2: 'world'
},
v1: 'hello',
v2: 'world'
})
.expect(200, done);
});
})