Skip to content

Commit 77f71b2

Browse files
committed
New tests for writing responses directly
1 parent b6ff09d commit 77f71b2

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

test/functional/action-params.spec.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {assertRequest} from "./test-utils";
66
import {User} from "../fakes/global-options/User";
77
import {Controller} from "../../src/decorator/Controller";
88
import {Get} from "../../src/decorator/Get";
9+
import {Ctx} from "../../src/decorator/Ctx";
910
import {Req} from "../../src/decorator/Req";
1011
import {Res} from "../../src/decorator/Res";
1112
import {Param} from "../../src/decorator/Param";
@@ -88,6 +89,24 @@ describe("action parameters", () => {
8889
return "<html><body>hello</body></html>";
8990
}
9091

92+
@Get("/users/direct")
93+
getUsersDirectExpress(@Res() response: any): any {
94+
if (typeof response.send === "function")
95+
response.status(201).contentType("custom/x-sample").send("hi, I was written directly to the response");
96+
else {
97+
response.status = 201;
98+
response.type = "custom/x-sample";
99+
response.body = "hi, I was written directly to the response";
100+
}
101+
}
102+
103+
@Get("/users/direct/ctx")
104+
getUsersDirectKoa(@Ctx() ctx: any): any {
105+
ctx.response.status = 201;
106+
ctx.response.type = "custom/x-sample";
107+
ctx.response.body = "hi, I was written directly to the response using Koa Ctx";
108+
}
109+
91110
@Get("/users/:userId")
92111
getUser(@Param("userId") userId: number) {
93112
paramUserId = userId;
@@ -346,6 +365,22 @@ describe("action parameters", () => {
346365
});
347366
});
348367

368+
describe("writing directly to the response using @Res should work", () => {
369+
assertRequest([3001, 3002], "get", "users/direct", response => {
370+
expect(response).to.be.status(201);
371+
expect(response.body).to.be.equal("hi, I was written directly to the response");
372+
expect(response).to.have.header("content-type", "custom/x-sample; charset=utf-8");
373+
});
374+
});
375+
376+
describe("writing directly to the response using @Ctx should work", () => {
377+
assertRequest([3002], "get", "users/direct/ctx", response => {
378+
expect(response).to.be.status(201);
379+
expect(response.body).to.be.equal("hi, I was written directly to the response using Koa Ctx");
380+
expect(response).to.have.header("content-type", "custom/x-sample; charset=utf-8");
381+
});
382+
});
383+
349384
describe("@Param should give a param from route", () => {
350385
assertRequest([3001, 3002], "get", "users/1", response => {
351386
expect(paramUserId).to.be.equal(1);

0 commit comments

Comments
 (0)