Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
dcavanagh committed Nov 9, 2017
1 parent c491f6b commit 59a67f6
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ src/syntax/*.d.ts

test/*.js
test/**/*.js
src/**/*.js.map
test/**/*.js.map

type_definitions/**/*.js
type_definitions/*.js
Expand Down
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
"**/dist": true,
"**/docs": true,
"type_definitions/**/*.js": true
},
}
}
2 changes: 2 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ export class InversifyRestifyServer {
result.then((value: any) => {
if (value && !res.headersSent) {
res.send(value);
next();
}
})
.catch((error: any) => {
Expand All @@ -125,6 +126,7 @@ export class InversifyRestifyServer {

} else if (result && !res.headersSent) {
res.send(result);
next();
}

};
Expand Down
71 changes: 71 additions & 0 deletions test/bugs.test.ts
Original file line number Diff line number Diff line change
@@ -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<interfaces.Controller>(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<interfaces.Controller>(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();
});

});
});

0 comments on commit 59a67f6

Please sign in to comment.