@@ -12,6 +12,7 @@ import '@angular/compiler';
1212/* eslint-enable import/no-unassigned-import */
1313
1414import { APP_BASE_HREF } from '@angular/common' ;
15+ import { HttpClient , provideHttpClient } from '@angular/common/http' ;
1516import {
1617 Component ,
1718 DestroyRef ,
@@ -20,12 +21,14 @@ import {
2021 REQUEST ,
2122 RESPONSE_INIT ,
2223 inject ,
24+ signal ,
2325} from '@angular/core' ;
2426import { ActivatedRoute , CanActivateFn , Router } from '@angular/router' ;
2527import { setTimeout } from 'node:timers/promises' ;
2628import { AngularServerApp } from '../src/app' ;
2729import { Hooks } from '../src/hooks' ;
2830import { RenderMode } from '../src/routes/route-config' ;
31+ import { SSR_REQUEST_HEADER } from '../src/utils/ssr-request' ;
2932import { setAngularAppTestingManifest } from './testing-utils' ;
3033
3134describe ( 'AngularServerApp' , ( ) => {
@@ -818,5 +821,113 @@ describe('AngularServerApp', () => {
818821 } ) ;
819822 } ) ;
820823 } ) ;
824+
825+ describe ( 'requests sent while rendering on the server' , ( ) => {
826+ @Component ( {
827+ selector : 'app-request' ,
828+ template : `{{ response() }}` ,
829+ } )
830+ class RequestComponent {
831+ protected readonly response = signal ( '' ) ;
832+
833+ constructor ( ) {
834+ inject ( HttpClient )
835+ . get ( inject ( ActivatedRoute ) . snapshot . data [ 'url' ] , { responseType : 'text' } )
836+ . subscribe ( {
837+ next : ( value ) => this . response . set ( value ) ,
838+ error : ( ) => this . response . set ( 'Request failed' ) ,
839+ } ) ;
840+ }
841+ }
842+
843+ /** Creates an application whose pages request the given URL while they are rendered. */
844+ function createAppRequesting ( url : string ) : AngularServerApp {
845+ setAngularAppTestingManifest (
846+ [ { path : '**' , component : RequestComponent , data : { url } } ] ,
847+ [ { path : '**' , renderMode : RenderMode . Server } ] ,
848+ undefined ,
849+ undefined ,
850+ undefined ,
851+ undefined ,
852+ [ provideHttpClient ( ) ] ,
853+ ) ;
854+
855+ return new AngularServerApp ( ) ;
856+ }
857+
858+ beforeEach ( ( ) => {
859+ setupManifest ( ) ;
860+ } ) ;
861+
862+ it ( 'should return a 400 response that is not stored for routes requested while rendering' , async ( ) => {
863+ const response = await app . handle (
864+ new Request ( 'http://localhost/page-with-headers' , {
865+ headers : { [ SSR_REQUEST_HEADER ] : '' } ,
866+ } ) ,
867+ ) ;
868+
869+ expect ( response ?. status ) . toBe ( 400 ) ;
870+ expect ( Object . fromEntries ( response ?. headers . entries ( ) ?? [ ] ) ) . toEqual ( {
871+ 'cache-control' : 'no-store' ,
872+ 'content-type' : 'text/plain' ,
873+ } ) ;
874+ } ) ;
875+
876+ it ( 'should serve prerendered pages, redirects and client-side rendered routes requested while rendering' , async ( ) => {
877+ const headers = { [ SSR_REQUEST_HEADER ] : '1' } ;
878+
879+ const redirect = await app . handle ( new Request ( 'http://localhost/redirect' , { headers } ) ) ;
880+ expect ( redirect ?. headers . get ( 'location' ) ) . toBe ( '/home' ) ;
881+
882+ const prerendered = await app . handle ( new Request ( 'http://localhost/home-ssg' , { headers } ) ) ;
883+ expect ( await prerendered ?. text ( ) ) . toContain ( 'Home SSG works' ) ;
884+
885+ const clientSide = await app . handle ( new Request ( 'http://localhost/home-csr' , { headers } ) ) ;
886+ expect ( await clientSide ?. text ( ) ) . toContain ( '<title>CSR page</title>' ) ;
887+ } ) ;
888+
889+ it ( 'should not render the requests that a page sends to its own host' , async ( ) => {
890+ const requestingApp = createAppRequesting ( '/api' ) ;
891+ let requestCount = 0 ;
892+ let requestStatus : number | undefined ;
893+ spyOn ( globalThis , 'fetch' ) . and . callFake ( async ( input , init ) => {
894+ // Stop the loop that renders the page again for each of its own requests.
895+ if ( ++ requestCount > 5 ) {
896+ return new Response ( null , { status : 508 } ) ;
897+ }
898+
899+ const response =
900+ ( await requestingApp . handle ( new Request ( input , init ) ) ) ??
901+ new Response ( null , { status : 404 } ) ;
902+ requestStatus = response . status ;
903+
904+ return response ;
905+ } ) ;
906+
907+ const response = await requestingApp . handle ( new Request ( 'http://localhost/page' ) ) ;
908+ expect ( await response ?. text ( ) ) . toContain ( 'Request failed' ) ;
909+ expect ( requestCount ) . toBe ( 1 ) ;
910+ expect ( requestStatus ) . toBe ( 400 ) ;
911+ } ) ;
912+
913+ it ( `should add the '${ SSR_REQUEST_HEADER } ' header only to requests sent to the host of the page` , async ( ) => {
914+ const fetchSpy = spyOn ( globalThis , 'fetch' ) . and . callFake (
915+ async ( ) => new Response ( 'Request works' ) ,
916+ ) ;
917+
918+ for ( const url of [ 'http://example.com/api' , 'https://localhost:8443/api' ] ) {
919+ const response = await createAppRequesting ( url ) . handle (
920+ new Request ( 'http://localhost/page' ) ,
921+ ) ;
922+ expect ( await response ?. text ( ) ) . toContain ( 'Request works' ) ;
923+ }
924+
925+ const requests = fetchSpy . calls . allArgs ( ) . map ( ( [ input , init ] ) => new Request ( input , init ) ) ;
926+ expect ( requests . map ( ( { url, headers } ) => [ url , headers . has ( SSR_REQUEST_HEADER ) ] ) ) . toEqual ( [
927+ [ 'http://example.com/api' , false ] ,
928+ [ 'https://localhost:8443/api' , true ] ,
929+ ] ) ;
930+ } ) ;
931+ } ) ;
821932 } ) ;
822933} ) ;
0 commit comments