@@ -2,7 +2,7 @@ import { isNil, isObject } from '@nestjs/common/utils/shared.utils';
22import { expect } from 'chai' ;
33import { of } from 'rxjs' ;
44import * as sinon from 'sinon' ;
5- import { RequestMethod } from '../../../common' ;
5+ import { RequestMethod , HttpStatus } from '../../../common' ;
66import { RouterResponseController } from '../../router/router-response-controller' ;
77import { NoopHttpAdapter } from '../utils/noop-adapter.spec' ;
88
@@ -170,4 +170,81 @@ describe('RouterResponseController', () => {
170170 expect ( statusStub . calledWith ( response , statusCode ) ) . to . be . true ;
171171 } ) ;
172172 } ) ;
173+
174+ describe ( 'redirect should HttpServer.redirect' , ( ) => {
175+ it ( 'should transformToResult' , async ( ) => {
176+ const transformToResultSpy = sinon
177+ . stub ( routerResponseController , 'transformToResult' )
178+ . returns ( Promise . resolve ( { statusCode : 123 , url : 'redirect url' } ) ) ;
179+ const result = { } ;
180+ await routerResponseController . redirect ( result , null , null ) ;
181+ expect ( transformToResultSpy . firstCall . args [ 0 ] ) . to . be . equal ( result ) ;
182+ } ) ;
183+ it ( 'should pass the response to redirect' , async ( ) => {
184+ sinon
185+ . stub ( routerResponseController , 'transformToResult' )
186+ . returns ( Promise . resolve ( { statusCode : 123 , url : 'redirect url' } ) ) ;
187+ const redirectSpy = sinon . spy ( adapter , 'redirect' ) ;
188+ const response = { } ;
189+ await routerResponseController . redirect ( null , response , null ) ;
190+ expect ( redirectSpy . firstCall . args [ 0 ] ) . to . be . equal ( response ) ;
191+ } ) ;
192+ describe ( 'status code' , ( ) => {
193+ it ( 'should come from the transformed result if present' , async ( ) => {
194+ sinon
195+ . stub ( routerResponseController , 'transformToResult' )
196+ . returns ( Promise . resolve ( { statusCode : 123 , url : 'redirect url' } ) ) ;
197+ const redirectSpy = sinon . spy ( adapter , 'redirect' ) ;
198+ await routerResponseController . redirect ( null , null , {
199+ statusCode : 999 ,
200+ url : 'not form here' ,
201+ } ) ;
202+ expect ( redirectSpy . firstCall . args [ 1 ] ) . to . be . eql ( 123 ) ;
203+ } ) ;
204+ it ( 'should come from the redirectResponse if not on the transformed result' , async ( ) => {
205+ sinon
206+ . stub ( routerResponseController , 'transformToResult' )
207+ . returns ( Promise . resolve ( { } ) ) ;
208+ const redirectSpy = sinon . spy ( adapter , 'redirect' ) ;
209+ await routerResponseController . redirect ( null , null , {
210+ statusCode : 123 ,
211+ url : 'redirect url' ,
212+ } ) ;
213+ expect ( redirectSpy . firstCall . args [ 1 ] ) . to . be . eql ( 123 ) ;
214+ } ) ;
215+ it ( 'should default to HttpStatus.FOUND' , async ( ) => {
216+ sinon
217+ . stub ( routerResponseController , 'transformToResult' )
218+ . returns ( Promise . resolve ( { } ) ) ;
219+ const redirectSpy = sinon . spy ( adapter , 'redirect' ) ;
220+ await routerResponseController . redirect ( null , null , {
221+ url : 'redirect url' ,
222+ } ) ;
223+ expect ( redirectSpy . firstCall . args [ 1 ] ) . to . be . eql ( HttpStatus . FOUND ) ;
224+ } ) ;
225+ } ) ;
226+ describe ( 'url' , ( ) => {
227+ it ( 'should come from the transformed result if present' , async ( ) => {
228+ sinon
229+ . stub ( routerResponseController , 'transformToResult' )
230+ . returns ( Promise . resolve ( { statusCode : 123 , url : 'redirect url' } ) ) ;
231+ const redirectSpy = sinon . spy ( adapter , 'redirect' ) ;
232+ await routerResponseController . redirect ( null , null , {
233+ url : 'not from here' ,
234+ } ) ;
235+ expect ( redirectSpy . firstCall . args [ 2 ] ) . to . be . eql ( 'redirect url' ) ;
236+ } ) ;
237+ it ( 'should come from the redirectResponse if not on the transformed result' , async ( ) => {
238+ sinon
239+ . stub ( routerResponseController , 'transformToResult' )
240+ . returns ( Promise . resolve ( { } ) ) ;
241+ const redirectSpy = sinon . spy ( adapter , 'redirect' ) ;
242+ await routerResponseController . redirect ( null , null , {
243+ statusCode : 123 ,
244+ url : 'redirect url' ,
245+ } ) ;
246+ expect ( redirectSpy . firstCall . args [ 2 ] ) . to . be . eql ( 'redirect url' ) ;
247+ } ) ;
248+ } ) ;
249+ } ) ;
173250} ) ;
0 commit comments