1
+ import "reflect-metadata" ;
2
+ import { createExpressServer , getMetadataArgsStorage } from "../../src/index" ;
3
+ import { ExpressMiddlewareInterface } from "../../src/driver/express/ExpressMiddlewareInterface" ;
4
+ import { Controller } from "../../src/decorator/Controller" ;
5
+ import { Get } from "../../src/decorator/Get" ;
6
+ import { UseBefore } from "../../src/decorator/UseBefore" ;
7
+ import { Middleware } from "../../src/decorator/Middleware" ;
8
+ import { UseAfter } from "../../src/decorator/UseAfter" ;
9
+ import { NotAcceptableError } from "./../../src/http-error/NotAcceptableError" ;
10
+ import { ExpressErrorMiddlewareInterface } from "./../../src/driver/express/ExpressErrorMiddlewareInterface" ;
11
+ import { QueryParam } from '../../src/decorator/QueryParam' ;
12
+ import { OnUndefined } from '../../src/decorator/OnUndefined' ;
13
+ const chakram = require ( "chakram" ) ;
14
+ const expect = chakram . expect ;
15
+
16
+ describe ( "express middlewares" , ( ) => {
17
+
18
+ before ( ( ) => {
19
+
20
+ // reset metadata args storage
21
+ getMetadataArgsStorage ( ) . reset ( ) ;
22
+
23
+ @Controller ( )
24
+ class ExpressController {
25
+
26
+ @Get ( "/voidfunc" )
27
+ voidfunc ( ) { }
28
+
29
+ @Get ( "/promisevoidfunc" )
30
+ promisevoidfunc ( ) {
31
+ return Promise . resolve ( ) ;
32
+ }
33
+
34
+ @Get ( "/paramfunc" )
35
+ paramfunc ( @QueryParam ( 'x' ) x : number ) {
36
+ return { foo : 'bar' } ;
37
+ }
38
+
39
+ @Get ( "/nullfunc" )
40
+ nullfunc ( ) : string {
41
+ return null ;
42
+ }
43
+
44
+ @Get ( "/overridefunc" )
45
+ @OnUndefined ( 404 )
46
+ overridefunc ( ) { }
47
+
48
+ @Get ( "/overrideparamfunc" )
49
+ overrideparamfunc ( @QueryParam ( 'x' , { required : false } ) x : number ) {
50
+ return { foo : 'bar' } ;
51
+ }
52
+ }
53
+ } ) ;
54
+
55
+ let defaultUndefinedResultCode = 204 ;
56
+ let defaultNullResultCode = 404 ;
57
+ let app : any ;
58
+ before ( done => app = createExpressServer ( {
59
+ defaults : {
60
+ nullResultCode : defaultNullResultCode ,
61
+ undefinedResultCode : defaultUndefinedResultCode ,
62
+ paramOptions : {
63
+ required : true
64
+ }
65
+ }
66
+ } ) . listen ( 3001 , done ) ) ;
67
+ after ( done => app . close ( done ) ) ;
68
+
69
+ it ( "should return undefinedResultCode from defaults config for void function" , async ( ) => {
70
+ let res = await chakram . get ( "http://127.0.0.1:3001/voidfunc" ) ;
71
+ expect ( res ) . to . have . status ( defaultUndefinedResultCode ) ;
72
+ } ) ;
73
+
74
+ it ( "should return undefinedResultCode from defaults config for promise void function" , async ( ) => {
75
+ let res = await chakram . get ( "http://127.0.0.1:3001/promisevoidfunc" ) ;
76
+ expect ( res ) . to . have . status ( defaultUndefinedResultCode ) ;
77
+ } ) ;
78
+
79
+ it ( "should return 400 from required paramOptions" , async ( ) => {
80
+ let res = await chakram . get ( "http://127.0.0.1:3001/paramfunc" ) ;
81
+ expect ( res ) . to . have . status ( 400 ) ;
82
+ } ) ;
83
+
84
+ it ( "should return nullResultCode from defaults config" , async ( ) => {
85
+ let res = await chakram . get ( "http://127.0.0.1:3001/nullfunc" ) ;
86
+ expect ( res ) . to . have . status ( defaultNullResultCode ) ;
87
+ } ) ;
88
+
89
+ it ( "should return status code from OnUndefined annotation" , async ( ) => {
90
+ let res = await chakram . get ( "http://127.0.0.1:3001/overridefunc" ) ;
91
+ expect ( res ) . to . have . status ( 404 ) ;
92
+ } ) ;
93
+
94
+ it ( "should mark arg optional from QueryParam annotation" , async ( ) => {
95
+ let res = await chakram . get ( "http://127.0.0.1:3001/overrideparamfunc" ) ;
96
+ expect ( res ) . to . have . status ( 200 ) ;
97
+ } ) ;
98
+
99
+ } ) ;
0 commit comments