import { Request , Response , NextFunction } from 'express' ; import { request , Dino , Controller , ApiController , HttpGet , SendsResponse , RequestEndMiddleware , ErrorMiddleware , Injectable , ReflectiveInjector } from './index' ; import { initializeTests } from './init' ; class JsonEnd extends RequestEndMiddleware { invoke ( request , response , next , result ) { if ( result === undefined ) response . status ( 204 ) . end ( ) ; else response . status ( 200 ) . json ( result ) ; } } class ErrEnd extends ErrorMiddleware { invoke ( err , request , response , next ) { response . status ( 500 ) . json ( { data : err . message } ) ; } } @ Injectable ( ) class TestService { name ( ) { return 'TestService' ; } } @ Controller ( '/test' ) class DIController extends ApiController { constructor ( private test ) { super ( ) ; } @ HttpGet ( '/get' ) get ( ) { return { data : this . test . name ( ) } ; } } @ Controller ( '/test' ) class TestController extends ApiController { @ SendsResponse ( ) @ HttpGet ( '/proceedCb' ) proceedCb ( ) { setTimeout ( ( ) => { this . dino . proceed ( { data : 'Response_End' } ) ; } , 2 ) ; } @ SendsResponse ( ) @ HttpGet ( '/errCb' ) errCb ( ) { setTimeout ( ( ) => { this . dino . throw ( new Error ( 'TestError' ) ) ; } , 2 ) ; } @ SendsResponse ( ) @ HttpGet ( '/nextCb' ) netWithCallback ( ) { setTimeout ( ( ) => { return this . next ( ) ; } , 2 ) ; } @ HttpGet ( '/query' ) query ( ) { return { data : this . request . query . id } ; } } describe ( 'api.controller.e2e.spec' , ( ) => { const baseRoute = '/api/test' ; function register ( dino ) { dino . registerController ( TestController ) ; dino . serverError ( ErrEnd ) ; dino . requestEnd ( JsonEnd ) ; dino . bind ( ) ; } it ( 'dino.proceed_with_cb_$Get_returns_200_and_data' , done => { const x = initializeTests ( ) ; const app = x . app ; const dino = x . dino ; register ( dino ) ; request ( app ) . get ( `${ baseRoute }/proceedCb` ) . expect ( 'Content-Type' , /json/ ) . expect ( 200 , { data : 'Response_End' } , done ) ; } ) ; it ( 'dino.throw_with_cb_$Get_returns_500_and_data' , done => { const x = initializeTests ( ) ; const app = x . app ; const dino = x . dino ; register ( dino ) ; request ( app ) . get ( `${ baseRoute }/errCb` ) . expect ( 'Content-Type' , /json/ ) . expect ( 500 , { data : 'TestError' } , done ) ; } ) ; it ( 'this.next_with_cb_and_without_result_returns_204' , done => { const x = initializeTests ( ) ; const app = x . app ; const dino = x . dino ; register ( dino ) ; request ( app ) . get ( `${ baseRoute }/nextCb` ) . expect ( 204 , done ) ; } ) ; it ( 'this.request.query.id_with_query_string_returns_200' , done => { const x = initializeTests ( ) ; const app = x . app ; const dino = x . dino ; register ( dino ) ; request ( app ) . get ( `${ baseRoute }/query?id=42` ) . expect ( 'Content-Type' , /json/ ) . expect ( 200 , { data : '42' } , done ) ; } ) ; it ( 'dependencyResolver.verify_dependency_injection' , done => { const x = initializeTests ( ) ; const app = x . app ; const dino = x . dino ; dino . registerController ( DIController ) ; const injector = ReflectiveInjector . resolveAndCreate ( [ { provide : DIController , useClass : DIController } , { provide : TestService , useClass : TestService } ] ) ; dino . dependencyResolver ( injector , ( i , type ) => i . get ( type ) ) ; dino . bind ( ) ; request ( app ) . get ( `${ baseRoute }/get` ) . expect ( 'Content-Type' , /json/ ) . expect ( 200 , { data : 'TestService' } , done ) ; } ) ; } ) ;