Skip to content

Commit

Permalink
~80% coverage test suits, need more later
Browse files Browse the repository at this point in the history
  • Loading branch information
joesonw committed Mar 1, 2016
1 parent b7ad8cb commit e9eb671
Show file tree
Hide file tree
Showing 13 changed files with 493 additions and 141 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ node_modules/
builds/
dist/
npm-debug.log
coverage/
.DS_Storage
141 changes: 0 additions & 141 deletions declaration.d.ts

This file was deleted.

61 changes: 61 additions & 0 deletions test/after.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/// <reference path="../typings/mocha/mocha.d.ts"/>
/// <reference path="../typings/chai/chai.d.ts"/>
/// <reference path="../typings/supertest/supertest.d.ts"/>
/// <reference path="../typings/koa/koa.d.ts"/>

import * as tsRouter from '../src';
import * as chai from 'chai';
import * as request from 'supertest';
import * as Koa from 'koa';

@tsRouter.Path('/test')
class TestController extends tsRouter.Controller {
@tsRouter.Path('/:v1/:v2')
@tsRouter.GET
@tsRouter.Produce(tsRouter.MediaType.JSON)
async index(
@tsRouter.Params params:Object,
@tsRouter.PathParam('v1') v1:string,
@tsRouter.PathParam('v2') v2:string
):Promise<tsRouter.Response> {
return tsRouter.Response.status(200).body({v1, v2, params}).build();
}

@tsRouter.After
async after(
@tsRouter.Params params:Object,
@tsRouter.PathParam('v1') v1:string,
@tsRouter.PathParam('v2') v2:string,
@tsRouter.AppContext context:tsRouter.Context,
@tsRouter.RouteResponse res:tsRouter.Response
) {
res.body = JSON.stringify({v1, v2, params, v3: v1 + v2});
res.send(context);
}
}

const app = new Koa();
const router = new tsRouter.Router();
router.use(TestController);
app.use(router.routes());
let server = app.listen();
describe('GET with path paramters with afterwares', () => {
after(() => {
server.close();
})
it('response paramters in back in json', function (done) {
request(app.listen())
.get('/test/hello/world')
.expect('Content-Type', 'application/json')
.expect({
params: {
v1: 'hello',
v2: 'world'
},
v1: 'hello',
v2: 'world',
v3: 'helloworld'
})
.expect(200, done);
});
})
58 changes: 58 additions & 0 deletions test/before.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/// <reference path="../typings/mocha/mocha.d.ts"/>
/// <reference path="../typings/chai/chai.d.ts"/>
/// <reference path="../typings/supertest/supertest.d.ts"/>
/// <reference path="../typings/koa/koa.d.ts"/>

import * as tsRouter from '../src';
import * as chai from 'chai';
import * as request from 'supertest';
import * as Koa from 'koa';

@tsRouter.Path('/test')
class TestController extends tsRouter.Controller {
@tsRouter.PathParam('v1') private v1:string;
private v2:string;
constructor() {
super();
this.v1 = this.v1 + this.v1;
}

@tsRouter.Path('/:v1/:v2')
@tsRouter.GET
@tsRouter.Produce(tsRouter.MediaType.JSON)
async index(@tsRouter.Params params:Object):Promise<tsRouter.Response> {
let v1 = this.v1;
let v2 = this.v2;
return tsRouter.Response.status(200).body({v1, v2, params}).build();
}

@tsRouter.Before
async before(@tsRouter.PathParam('v2') v2:string) {
this.v2 = v2;
}
}

const app = new Koa();
const router = new tsRouter.Router();
router.use(TestController);
app.use(router.routes());
let server = app.listen();
describe('GET with path paramters with beforewares', () => {
after(() => {
server.close();
})
it('response paramters in back in json', function (done) {
request(app.listen())
.get('/test/hello/world')
.expect('Content-Type', 'application/json')
.expect({
params: {
v1: 'hello',
v2: 'world'
},
v1: 'hellohello',
v2: 'world',
})
.expect(200, done);
});
})
58 changes: 58 additions & 0 deletions test/body.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/// <reference path="../typings/mocha/mocha.d.ts"/>
/// <reference path="../typings/chai/chai.d.ts"/>
/// <reference path="../typings/supertest/supertest.d.ts"/>
/// <reference path="../typings/koa/koa.d.ts"/>

import * as tsRouter from '../src';
import * as chai from 'chai';
import * as request from 'supertest';
import * as Koa from 'koa';

@tsRouter.Path('/test')
class TestController extends tsRouter.Controller {
@tsRouter.Path('')
@tsRouter.POST
@tsRouter.Consume(tsRouter.MediaType.JSON)
@tsRouter.Produce(tsRouter.MediaType.JSON)
async index(
@tsRouter.Body body:Object,
@tsRouter.BodyParam('v1') v1:string,
@tsRouter.BodyParam('v2') v2:string
):Promise<tsRouter.Response> {
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 = app.listen();
describe('POST with body', () => {
after(() => {
server.close();
})
it('response body back in json', function (done) {
request(app.listen())
.post('/test')
.send({
v1: 'hello',
v2: 'world'
})
.set('Content-Type', 'application/json')
.expect('Content-Type', 'application/json')
.expect({
body: {
v1: 'hello',
v2: 'world'
},
hello: 'hello',
world: 'world'
})
.expect(200, done);
});
})
36 changes: 36 additions & 0 deletions test/delete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/// <reference path="../typings/mocha/mocha.d.ts"/>
/// <reference path="../typings/chai/chai.d.ts"/>
/// <reference path="../typings/supertest/supertest.d.ts"/>
/// <reference path="../typings/koa/koa.d.ts"/>

import * as tsRouter from '../src';
import * as chai from 'chai';
import * as request from 'supertest';
import * as Koa from 'koa';

@tsRouter.Path('/test')
class TestController extends tsRouter.Controller {
@tsRouter.Path('')
@tsRouter.DELETE
async index():Promise<tsRouter.Response> {
return tsRouter.Response.status(200).body('hello').build();
}
}

const app = new Koa();
const router = new tsRouter.Router();
router.use(TestController);
app.use(router.routes());
let server = app.listen();
describe('DELETE plain text', () => {
after(() => {
server.close();
})
it('response plain text', function (done) {
request(app.listen())
.del('/test')
.expect('Content-Type', 'text/plain')
.expect('hello')
.expect(200, done);
});
})
Loading

0 comments on commit e9eb671

Please sign in to comment.