11/* eslint-disable sonarjs/no-duplicate-string */
22import { cacheInterceptor } from "@opennextjs/aws/core/routing/cacheInterceptor.js" ;
33import { convertFromQueryString } from "@opennextjs/aws/core/routing/util.js" ;
4- import type { InternalEvent } from "@opennextjs/aws/types/open-next.js" ;
4+ import type { MiddlewareEvent } from "@opennextjs/aws/types/open-next.js" ;
55import type { Queue } from "@opennextjs/aws/types/overrides.js" ;
66import { fromReadableStream } from "@opennextjs/aws/utils/stream.js" ;
77import { vi } from "vitest" ;
@@ -26,14 +26,14 @@ vi.mock("@opennextjs/aws/adapters/config/index.js", () => ({
2626} ) ) ;
2727
2828vi . mock ( "@opennextjs/aws/core/routing/i18n/index.js" , ( ) => ( {
29- localizePath : ( event : InternalEvent ) => event . rawPath ,
29+ localizePath : ( event : MiddlewareEvent ) => event . rawPath ,
3030} ) ) ;
3131
3232type PartialEvent = Partial <
33- Omit < InternalEvent , "body" | "rawPath" | "query" >
33+ Omit < MiddlewareEvent , "body" | "rawPath" | "query" >
3434> & { body ?: string } ;
3535
36- function createEvent ( event : PartialEvent ) : InternalEvent {
36+ function createEvent ( event : PartialEvent ) : MiddlewareEvent {
3737 const [ rawPath , qs ] = ( event . url ?? "/" ) . split ( "?" , 2 ) ;
3838 return {
3939 type : "core" ,
@@ -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