11import { ForbiddenException } from '@nestjs/common/exceptions/forbidden.exception' ;
22import { expect } from 'chai' ;
3+ import { of } from 'rxjs' ;
34import * as sinon from 'sinon' ;
45import { HttpException , HttpStatus , RouteParamMetadata } from '../../../common' ;
56import { CUSTOM_ROUTE_AGRS_METADATA } from '../../../common/constants' ;
@@ -17,6 +18,7 @@ import { PipesContextCreator } from '../../pipes/pipes-context-creator';
1718import { RouteParamsFactory } from '../../router/route-params-factory' ;
1819import { RouterExecutionContext } from '../../router/router-execution-context' ;
1920import { NoopHttpAdapter } from '../utils/noop-adapter.spec' ;
21+ import { PassThrough } from 'stream' ;
2022
2123describe ( 'RouterExecutionContext' , ( ) => {
2224 let contextCreator : RouterExecutionContext ;
@@ -326,6 +328,7 @@ describe('RouterExecutionContext', () => {
326328
327329 sinon . stub ( contextCreator , 'reflectResponseHeaders' ) . returns ( [ ] ) ;
328330 sinon . stub ( contextCreator , 'reflectRenderTemplate' ) . returns ( undefined ) ;
331+ sinon . stub ( contextCreator , 'reflectSse' ) . returns ( undefined ) ;
329332
330333 const handler = contextCreator . createHandleResponseFn (
331334 null ,
@@ -377,6 +380,7 @@ describe('RouterExecutionContext', () => {
377380
378381 sinon . stub ( contextCreator , 'reflectResponseHeaders' ) . returns ( [ ] ) ;
379382 sinon . stub ( contextCreator , 'reflectRenderTemplate' ) . returns ( undefined ) ;
383+ sinon . stub ( contextCreator , 'reflectSse' ) . returns ( undefined ) ;
380384
381385 const handler = contextCreator . createHandleResponseFn (
382386 null ,
@@ -396,6 +400,7 @@ describe('RouterExecutionContext', () => {
396400 const response = { } ;
397401
398402 sinon . stub ( contextCreator , 'reflectRenderTemplate' ) . returns ( undefined ) ;
403+ sinon . stub ( contextCreator , 'reflectSse' ) . returns ( undefined ) ;
399404
400405 const handler = contextCreator . createHandleResponseFn (
401406 null ,
@@ -414,5 +419,53 @@ describe('RouterExecutionContext', () => {
414419 ) . to . be . true ;
415420 } ) ;
416421 } ) ;
422+
423+ describe ( 'when "isSse" is enabled' , ( ) => {
424+ it ( 'should use sse-stream.service' , async ( ) => {
425+ const result = of ( 'test' ) ;
426+ const response = new PassThrough ( ) ;
427+ response . write = sinon . spy ( ) ;
428+ const request = new PassThrough ( ) ;
429+ request . on = sinon . spy ( ) ;
430+
431+ sinon . stub ( contextCreator , 'reflectRenderTemplate' ) . returns ( undefined ) ;
432+ sinon . stub ( contextCreator , 'reflectSse' ) . returns ( '/' ) ;
433+
434+ const handler = contextCreator . createHandleResponseFn (
435+ null ,
436+ true ,
437+ undefined ,
438+ 200 ,
439+ ) ;
440+ await handler ( result , response , request ) ;
441+
442+ expect ( ( response . write as any ) . called ) . to . be . true ;
443+ expect ( ( request . on as any ) . called ) . to . be . true ;
444+ } ) ;
445+
446+ it ( 'should not allow a non-observable result' , async ( ) => {
447+ const result = Promise . resolve ( 'test' ) ;
448+ const response = new PassThrough ( ) ;
449+ const request = new PassThrough ( ) ;
450+
451+ sinon . stub ( contextCreator , 'reflectRenderTemplate' ) . returns ( undefined ) ;
452+ sinon . stub ( contextCreator , 'reflectSse' ) . returns ( '/' ) ;
453+
454+ const handler = contextCreator . createHandleResponseFn (
455+ null ,
456+ true ,
457+ undefined ,
458+ 200 ,
459+ ) ;
460+
461+ try {
462+ await handler ( result , response , request ) ;
463+ } catch ( e ) {
464+ expect ( e . message ) . to . equal (
465+ 'You should use an observable to use server-sent events.' ,
466+ ) ;
467+ }
468+ } ) ;
469+ } ) ;
417470 } ) ;
418471} ) ;
0 commit comments