Skip to content

Commit d61c165

Browse files
committed
add unit test and add example in README.md
1 parent a395f9c commit d61c165

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ You can use routing-controllers with [express.js][1] or [koa.js][2].
4141
- [Render templates](#render-templates)
4242
- [Throw HTTP errors](#throw-http-errors)
4343
- [Enable CORS](#enable-cors)
44+
- [Default settings](#default-settings)
4445
* [Using middlewares](#using-middlewares)
4546
+ [Use exist middleware](#use-exist-middleware)
4647
+ [Creating your own express middleware](#creating-your-own-express-middleware)
@@ -728,6 +729,34 @@ const app = createExpressServer({
728729
app.listen(3000);
729730
```
730731

732+
#### Default settings
733+
734+
You can override default status code in routing-controllers options.
735+
736+
```typescript
737+
import "reflect-metadata";
738+
import {createExpressServer} from "routing-controllers";
739+
import {UserController} from "./UserController";
740+
741+
const app = createExpressServer({
742+
defaults: {
743+
744+
//with this option, null will return 404 by default
745+
nullResultCode: 404,
746+
747+
//with this option, void or Promise<void> will return 204 by default
748+
undefinedResultCode: 204,
749+
750+
paramOptions: {
751+
//with this option, argument will be required by default
752+
required: true
753+
}
754+
}
755+
});
756+
757+
app.listen(3000);
758+
```
759+
731760
## Using middlewares
732761

733762
You can use any exist express / koa middleware, or create your own.
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import "reflect-metadata";
2+
import {createExpressServer, getMetadataArgsStorage} from "../../src/index";
3+
import {ExpressMiddlewareInterface} from "../../src/driver/express/ExpressMiddlewareInterface";
4+
import {Controller} from "../../src/decorator/Controller";
5+
import {Get} from "../../src/decorator/Get";
6+
import {UseBefore} from "../../src/decorator/UseBefore";
7+
import {Middleware} from "../../src/decorator/Middleware";
8+
import {UseAfter} from "../../src/decorator/UseAfter";
9+
import {NotAcceptableError} from "./../../src/http-error/NotAcceptableError";
10+
import {ExpressErrorMiddlewareInterface} from "./../../src/driver/express/ExpressErrorMiddlewareInterface";
11+
import { QueryParam } from '../../src/decorator/QueryParam';
12+
import { OnUndefined } from '../../src/decorator/OnUndefined';
13+
const chakram = require("chakram");
14+
const expect = chakram.expect;
15+
16+
describe("express middlewares", () => {
17+
18+
before(() => {
19+
20+
// reset metadata args storage
21+
getMetadataArgsStorage().reset();
22+
23+
@Controller()
24+
class ExpressController {
25+
26+
@Get("/voidfunc")
27+
voidfunc() { }
28+
29+
@Get("/promisevoidfunc")
30+
promisevoidfunc() {
31+
return Promise.resolve();
32+
}
33+
34+
@Get("/paramfunc")
35+
paramfunc(@QueryParam('x') x: number) {
36+
return { foo: 'bar' };
37+
}
38+
39+
@Get("/nullfunc")
40+
nullfunc(): string {
41+
return null;
42+
}
43+
44+
@Get("/overridefunc")
45+
@OnUndefined(404)
46+
overridefunc() { }
47+
48+
@Get("/overrideparamfunc")
49+
overrideparamfunc(@QueryParam('x', { required: false }) x: number) {
50+
return { foo: 'bar' };
51+
}
52+
}
53+
});
54+
55+
let defaultUndefinedResultCode = 204;
56+
let defaultNullResultCode = 404;
57+
let app: any;
58+
before(done => app = createExpressServer({
59+
defaults: {
60+
nullResultCode: defaultNullResultCode,
61+
undefinedResultCode: defaultUndefinedResultCode,
62+
paramOptions: {
63+
required: true
64+
}
65+
}
66+
}).listen(3001, done));
67+
after(done => app.close(done));
68+
69+
it("should return undefinedResultCode from defaults config for void function", async () => {
70+
let res = await chakram.get("http://127.0.0.1:3001/voidfunc");
71+
expect(res).to.have.status(defaultUndefinedResultCode);
72+
});
73+
74+
it("should return undefinedResultCode from defaults config for promise void function", async () => {
75+
let res = await chakram.get("http://127.0.0.1:3001/promisevoidfunc");
76+
expect(res).to.have.status(defaultUndefinedResultCode);
77+
});
78+
79+
it("should return 400 from required paramOptions", async () => {
80+
let res = await chakram.get("http://127.0.0.1:3001/paramfunc");
81+
expect(res).to.have.status(400);
82+
});
83+
84+
it("should return nullResultCode from defaults config", async () => {
85+
let res = await chakram.get("http://127.0.0.1:3001/nullfunc");
86+
expect(res).to.have.status(defaultNullResultCode);
87+
});
88+
89+
it("should return status code from OnUndefined annotation", async () => {
90+
let res = await chakram.get("http://127.0.0.1:3001/overridefunc");
91+
expect(res).to.have.status(404);
92+
});
93+
94+
it("should mark arg optional from QueryParam annotation", async () => {
95+
let res = await chakram.get("http://127.0.0.1:3001/overrideparamfunc");
96+
expect(res).to.have.status(200);
97+
});
98+
99+
});

0 commit comments

Comments
 (0)