@@ -45,6 +45,7 @@ function createEvent(event: PartialEvent): InternalEvent {
4545 query : convertFromQueryString ( qs ?? "" ) ,
4646 cookies : event . cookies ?? { } ,
4747 remoteAddress : event . remoteAddress ?? "::1" ,
48+ rewriteStatusCode : event . rewriteStatusCode ,
4849 } ;
4950}
5051
@@ -452,4 +453,72 @@ describe("cacheInterceptor", () => {
452453 } ) ,
453454 ) ;
454455 } ) ;
456+
457+ it ( "should return the rewrite status code when there is active cache" , async ( ) => {
458+ const event = createEvent ( {
459+ url : "/albums" ,
460+ rewriteStatusCode : 403 ,
461+ } ) ;
462+ incrementalCache . get . mockResolvedValueOnce ( {
463+ value : {
464+ type : "app" ,
465+ html : "Hello, world!" ,
466+ } ,
467+ } ) ;
468+
469+ const result = await cacheInterceptor ( event ) ;
470+ expect ( result . statusCode ) . toBe ( 403 ) ;
471+ } ) ;
472+
473+ it ( "should return the rewriteStatusCode if there is a cached status code" , async ( ) => {
474+ const event = createEvent ( {
475+ url : "/albums" ,
476+ rewriteStatusCode : 203 ,
477+ } ) ;
478+ incrementalCache . get . mockResolvedValueOnce ( {
479+ value : {
480+ type : "app" ,
481+ html : "Hello, world!" ,
482+ meta : {
483+ status : 404 ,
484+ } ,
485+ } ,
486+ } ) ;
487+
488+ const result = await cacheInterceptor ( event ) ;
489+ expect ( result . statusCode ) . toBe ( 203 ) ;
490+ } ) ;
491+
492+ it ( "should return the cached status code if there is one" , async ( ) => {
493+ const event = createEvent ( {
494+ url : "/albums" ,
495+ } ) ;
496+ incrementalCache . get . mockResolvedValueOnce ( {
497+ value : {
498+ type : "app" ,
499+ html : "Hello, world!" ,
500+ meta : {
501+ status : 405 ,
502+ } ,
503+ } ,
504+ } ) ;
505+
506+ const result = await cacheInterceptor ( event ) ;
507+ expect ( result . statusCode ) . toBe ( 405 ) ;
508+ } ) ;
509+
510+ it ( "should return 200 if there is no cached status code, nor a rewriteStatusCode" , async ( ) => {
511+ const event = createEvent ( {
512+ url : "/albums" ,
513+ } ) ;
514+ incrementalCache . get . mockResolvedValueOnce ( {
515+ value : {
516+ type : "app" ,
517+ html : "Hello, world!" ,
518+ } ,
519+ } ) ;
520+
521+ const result = await cacheInterceptor ( event ) ;
522+ expect ( result . statusCode ) . toBe ( 200 ) ;
523+ } ) ;
455524} ) ;
0 commit comments