@@ -7,7 +7,14 @@ import {Head} from "../../src/decorator/Head";
7
7
import { Delete } from "../../src/decorator/Delete" ;
8
8
import { Patch } from "../../src/decorator/Patch" ;
9
9
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" ;
11
18
import { assertRequest } from "./test-utils" ;
12
19
const chakram = require ( "chakram" ) ;
13
20
const expect = chakram . expect ;
@@ -107,6 +114,27 @@ describe("controller methods", () => {
107
114
}
108
115
}
109
116
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
+
110
138
} ) ;
111
139
112
140
let expressApp : any , koaApp : any ;
@@ -262,5 +290,35 @@ describe("controller methods", () => {
262
290
} ) ;
263
291
} ) ;
264
292
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
+
265
323
266
324
} ) ;
0 commit comments