From 59a67f6c93d4b5159a8aa2f83c150879f1710ed0 Mon Sep 17 00:00:00 2001 From: Dan Date: Thu, 9 Nov 2017 14:14:39 -0500 Subject: [PATCH] bug fix https://github.com/inversify/InversifyJS/issues/636 --- .gitignore | 2 +- .vscode/settings.json | 2 +- src/server.ts | 2 ++ test/bugs.test.ts | 71 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 test/bugs.test.ts diff --git a/.gitignore b/.gitignore index 8e5b483..8598bef 100644 --- a/.gitignore +++ b/.gitignore @@ -60,7 +60,7 @@ src/syntax/*.d.ts test/*.js test/**/*.js -src/**/*.js.map +test/**/*.js.map type_definitions/**/*.js type_definitions/*.js diff --git a/.vscode/settings.json b/.vscode/settings.json index edfb99d..fef03b2 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -14,5 +14,5 @@ "**/dist": true, "**/docs": true, "type_definitions/**/*.js": true - }, + } } \ No newline at end of file diff --git a/src/server.ts b/src/server.ts index 8f2974e..278df7d 100644 --- a/src/server.ts +++ b/src/server.ts @@ -117,6 +117,7 @@ export class InversifyRestifyServer { result.then((value: any) => { if (value && !res.headersSent) { res.send(value); + next(); } }) .catch((error: any) => { @@ -125,6 +126,7 @@ export class InversifyRestifyServer { } else if (result && !res.headersSent) { res.send(result); + next(); } }; diff --git a/test/bugs.test.ts b/test/bugs.test.ts new file mode 100644 index 0000000..86c791d --- /dev/null +++ b/test/bugs.test.ts @@ -0,0 +1,71 @@ +import * as request from "supertest"; +import { InversifyRestifyServer } from "../src/server"; +import { interfaces } from "../src/interfaces"; +import { Controller, Get } from "../src/decorators"; +import { Container, injectable } from "inversify"; +import { TYPE } from "../src/constants"; +import * as sinon from "sinon"; +import { expect } from "chai"; + +describe("Unit Test: Bugs", () => { + let container = new Container(); + let server: InversifyRestifyServer; + + it("should fire the 'after' event when the controller function returns a Promise", (done) => { + @injectable() + @Controller("/") + class TestController { + @Get("/promise") public getTest() { + return new Promise(((resolve) => { + setTimeout(resolve, 100, "GET"); + })); + } + } + + let spyA = sinon.spy((req: any, res: any) => null); + + container.bind(TYPE.Controller).to(TestController).whenTargetNamed("TestController"); + + server = new InversifyRestifyServer(container); + server.setConfig((app) => { + app.on("after", spyA); + }); + + request(server.build()) + .get("/noPromise") + .set("Accept", "text/plain") + .expect(200, "GET", () => { + expect(spyA.calledOnce).to.eq(true); + done(); + }); + + }); + + it("should fire the 'after' event when the controller function returns", (done) => { + @injectable() + @Controller("/") + class TestController { + @Get("/noPromise") public getNoPromise() { + return "GET"; + } + } + + let spyA = sinon.spy((req: any, res: any) => null); + + container.bind(TYPE.Controller).to(TestController).whenTargetNamed("TestController"); + + server = new InversifyRestifyServer(container); + server.setConfig((app) => { + app.on("after", spyA); + }); + + request(server.build()) + .get("/") + .set("Accept", "text/plain") + .expect(200, "GET", () => { + expect(spyA.calledOnce).to.eq(true); + done(); + }); + + }); +});