Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Controllers should not be forced to send a response for falsey return values #126

Merged
merged 1 commit into from
Jan 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,6 @@ test/**/*.js.map
type_definitions/**/*.js
type_definitions/*.js
package-lock.json

# JetBrains IDE
.idea
7 changes: 6 additions & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,12 @@ export class InversifyExpressServer {
let result = childContainer.getNamed<any>(TYPE.Controller, controllerName)[key](...args);
Promise.resolve(result)
.then((value: any) => {
if (value && !res.headersSent) {
if (value instanceof Function) {
value();
} else if (!res.headersSent) {
if (value === undefined) {
res.status(204);
}
res.send(value);
}
})
Expand Down
22 changes: 18 additions & 4 deletions test/framework.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ describe("Integration Tests:", () => {
.expect(500, done);
});

it ("should work for methods which call nextFunc()", (done) => {
it("should work for methods which call nextFunc()", (done) => {

@controller("/")
class TestController {
Expand All @@ -118,7 +118,7 @@ describe("Integration Tests:", () => {
});


it ("should work for async methods which call nextFunc()", (done) => {
it("should work for async methods which call nextFunc()", (done) => {

@controller("/")
class TestController {
Expand All @@ -143,12 +143,12 @@ describe("Integration Tests:", () => {
});


it ("should work for async methods called by nextFunc()", (done) => {
it("should work for async methods called by nextFunc()", (done) => {

@controller("/")
class TestController {
@httpGet("/") public getTest(req: express.Request, res: express.Response, nextFunc: express.NextFunction) {
nextFunc();
return nextFunc;
}

@httpGet("/") public getTest2(req: express.Request, res: express.Response) {
Expand Down Expand Up @@ -269,6 +269,20 @@ describe("Integration Tests:", () => {
.get("/api/v1/ping/endpoint")
.expect(200, "pong");
});

it("should work for controller methods who's return value is falsey", (done) => {
@controller("/user")
class TestController {
@httpDelete("/") private async delete(): Promise<void> {
return;
}
}

server = new InversifyExpressServer(container);
supertest(server.build())
.delete("/user")
.expect(204, "", done);
});
});


Expand Down