@@ -7,7 +7,14 @@ import {Head} from "../../src/decorator/Head";
77import { Delete } from "../../src/decorator/Delete" ;
88import { Patch } from "../../src/decorator/Patch" ;
99import { 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" ;
1118import { assertRequest } from "./test-utils" ;
1219const chakram = require ( "chakram" ) ;
1320const 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 ( / t e x t \/ h t m l / ) ;
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 ( / t e x t \/ p l a i n / ) ;
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 ( / t e x t \/ p l a i n / ) ;
318+ } ) ;
319+ expect ( response . body ) . to . match ( / U n a u t h o r i z e d E r r o r .H t t p E r r o r / ) ;
320+ } ) ;
321+ } ) ;
322+
265323
266324} ) ;
0 commit comments