Skip to content

Commit 9a7b3e4

Browse files
Merge pull request #277 from 19majkel94/feature/content-typed-response
Handle different content-type responses in JsonController
2 parents 6530cb2 + 3115583 commit 9a7b3e4

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed

src/metadata/ActionMetadata.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ export class ActionMetadata {
174174
const redirectHandler = responseHandlers.find(handler => handler.type === "redirect");
175175
const renderedTemplateHandler = responseHandlers.find(handler => handler.type === "rendered-template");
176176
const authorizedHandler = responseHandlers.find(handler => handler.type === "authorized");
177+
const contentTypeHandler = responseHandlers.find(handler => handler.type === "content-type");
177178
const bodyParam = this.params.find(param => param.type === "body");
178179

179180
if (classTransformerResponseHandler)
@@ -198,7 +199,10 @@ export class ActionMetadata {
198199
this.isBodyUsed = !!this.params.find(param => param.type === "body" || param.type === "body-param");
199200
this.isFilesUsed = !!this.params.find(param => param.type === "files");
200201
this.isFileUsed = !!this.params.find(param => param.type === "file");
201-
this.isJsonTyped = this.controllerMetadata.type === "json";
202+
this.isJsonTyped = (contentTypeHandler !== undefined
203+
? /json/.test(contentTypeHandler.value)
204+
: this.controllerMetadata.type === "json"
205+
);
202206
this.fullRoute = this.buildFullRoute();
203207
this.headers = this.buildHeaders(responseHandlers);
204208

test/functional/controller-methods.spec.ts

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,14 @@ import {Head} from "../../src/decorator/Head";
77
import {Delete} from "../../src/decorator/Delete";
88
import {Patch} from "../../src/decorator/Patch";
99
import {Put} from "../../src/decorator/Put";
10-
import { createExpressServer, createKoaServer, getMetadataArgsStorage, JsonController } from "../../src/index";
10+
import {ContentType} from "../../src/decorator/ContentType";
11+
import {JsonController} from "../../src/decorator/JsonController";
12+
import {UnauthorizedError} from "../../src/http-error/UnauthorizedError";
13+
import {
14+
createExpressServer,
15+
createKoaServer,
16+
getMetadataArgsStorage,
17+
} from "../../src/index";
1118
import {assertRequest} from "./test-utils";
1219
const chakram = require("chakram");
1320
const expect = chakram.expect;
@@ -107,6 +114,27 @@ describe("controller methods", () => {
107114
}
108115
}
109116

117+
@JsonController("/json-controller")
118+
class ContentTypeController {
119+
@Get("/text-html")
120+
@ContentType("text/html")
121+
returnHtml(): string {
122+
return "<html>Test</html>";
123+
}
124+
125+
@Get("/text-plain")
126+
@ContentType("text/plain")
127+
returnString(): string {
128+
return "Test";
129+
}
130+
131+
@Get("/text-plain-error")
132+
@ContentType("text/plain")
133+
error(): never {
134+
throw new UnauthorizedError();
135+
}
136+
}
137+
110138
});
111139

112140
let expressApp: any, koaApp: any;
@@ -262,5 +290,35 @@ describe("controller methods", () => {
262290
});
263291
});
264292

293+
describe("should respond with 200 and text/html even in json controller's method", () => {
294+
assertRequest([3001, 3002], "get", "json-controller/text-html", response => {
295+
expect(response).to.have.status(200);
296+
expect(response).to.have.header("content-type", (contentType: string) => {
297+
expect(contentType).to.match(/text\/html/);
298+
});
299+
expect(response.body).to.equals("<html>Test</html>");
300+
});
301+
});
302+
303+
describe("should respond with 200 and text/plain even in json controller's method", () => {
304+
assertRequest([3001, 3002], "get", "json-controller/text-plain", response => {
305+
expect(response).to.have.status(200);
306+
expect(response).to.have.header("content-type", (contentType: string) => {
307+
expect(contentType).to.match(/text\/plain/);
308+
});
309+
expect(response.body).to.equals("Test");
310+
});
311+
});
312+
313+
describe("should respond with 401 and text/html when UnauthorizedError throwed even in json controller's method", () => {
314+
assertRequest([3001, 3002], "get", "json-controller/text-plain-error", response => {
315+
expect(response).to.have.status(401);
316+
expect(response).to.have.header("content-type", (contentType: string) => {
317+
expect(contentType).to.match(/text\/plain/);
318+
});
319+
expect(response.body).to.match(/UnauthorizedError.HttpError/);
320+
});
321+
});
322+
265323

266324
});

0 commit comments

Comments
 (0)