@@ -7,7 +7,7 @@ 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 } from "../../src/index" ;
10
+ import { createExpressServer , createKoaServer , getMetadataArgsStorage , JsonController } from "../../src/index" ;
11
11
import { assertRequest } from "./test-utils" ;
12
12
const chakram = require ( "chakram" ) ;
13
13
const expect = chakram . expect ;
@@ -83,6 +83,30 @@ describe("controller methods", () => {
83
83
}
84
84
}
85
85
86
+ @JsonController ( "/return/json" )
87
+ class ReturnJsonController {
88
+ @Get ( "/undefined" )
89
+ returnUndefined ( ) : undefined {
90
+ return undefined ;
91
+ }
92
+ @Get ( "/null" )
93
+ returnNull ( ) : null {
94
+ return null ;
95
+ }
96
+ }
97
+
98
+ @Controller ( "/return/normal" )
99
+ class ReturnNormalController {
100
+ @Get ( "/undefined" )
101
+ returnUndefined ( ) : undefined {
102
+ return undefined ;
103
+ }
104
+ @Get ( "/null" )
105
+ returnNull ( ) : null {
106
+ return null ;
107
+ }
108
+ }
109
+
86
110
} ) ;
87
111
88
112
let expressApp : any , koaApp : any ;
@@ -207,4 +231,36 @@ describe("controller methods", () => {
207
231
} ) ;
208
232
} ) ;
209
233
234
+ describe ( "should respond with 204 No Content when null returned in action" , ( ) => {
235
+ assertRequest ( [ 3001 , 3002 ] , "get" , "return/normal/null" , response => {
236
+ expect ( response ) . to . have . status ( 204 ) ;
237
+ expect ( response ) . to . not . have . header ( "content-type" ) ;
238
+ expect ( response . body ) . to . not . exist ;
239
+ } ) ;
240
+ assertRequest ( [ 3001 , 3002 ] , "get" , "return/json/null" , response => {
241
+ expect ( response ) . to . have . status ( 204 ) ;
242
+ expect ( response ) . to . not . have . header ( "content-type" ) ;
243
+ expect ( response . body ) . to . not . exist ;
244
+ } ) ;
245
+ } ) ;
246
+
247
+ describe ( "should respond with 404 Not Found text when undefined returned in action" , ( ) => {
248
+ assertRequest ( [ 3001 , 3002 ] , "get" , "return/normal/undefined" , response => {
249
+ expect ( response ) . to . have . status ( 404 ) ;
250
+ expect ( response ) . to . have . header ( "content-type" , ( contentType : string ) => {
251
+ expect ( contentType ) . to . match ( / t e x t / ) ;
252
+ } ) ;
253
+ } ) ;
254
+ } ) ;
255
+
256
+ describe ( "should respond with 404 Not Found JSON when undefined returned in action" , ( ) => {
257
+ assertRequest ( [ 3001 , 3002 ] , "get" , "return/json/undefined" , response => {
258
+ expect ( response ) . to . have . status ( 404 ) ;
259
+ expect ( response ) . to . have . header ( "content-type" , ( contentType : string ) => {
260
+ expect ( contentType ) . to . match ( / a p p l i c a t i o n \/ j s o n / ) ;
261
+ } ) ;
262
+ } ) ;
263
+ } ) ;
264
+
265
+
210
266
} ) ;
0 commit comments