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
@@ -469,4 +470,72 @@ describe("cacheInterceptor", () => {
469470 } ) ,
470471 ) ;
471472 } ) ;
473+
474+ it ( "should return the rewrite status code when there is active cache" , async ( ) => {
475+ const event = createEvent ( {
476+ url : "/albums" ,
477+ rewriteStatusCode : 403 ,
478+ } ) ;
479+ incrementalCache . get . mockResolvedValueOnce ( {
480+ value : {
481+ type : "app" ,
482+ html : "Hello, world!" ,
483+ } ,
484+ } ) ;
485+
486+ const result = await cacheInterceptor ( event ) ;
487+ expect ( result . statusCode ) . toBe ( 403 ) ;
488+ } ) ;
489+
490+ it ( "should return the rewriteStatusCode if there is a cached status code" , async ( ) => {
491+ const event = createEvent ( {
492+ url : "/albums" ,
493+ rewriteStatusCode : 203 ,
494+ } ) ;
495+ incrementalCache . get . mockResolvedValueOnce ( {
496+ value : {
497+ type : "app" ,
498+ html : "Hello, world!" ,
499+ meta : {
500+ status : 404 ,
501+ } ,
502+ } ,
503+ } ) ;
504+
505+ const result = await cacheInterceptor ( event ) ;
506+ expect ( result . statusCode ) . toBe ( 203 ) ;
507+ } ) ;
508+
509+ it ( "should return the cached status code if there is one" , async ( ) => {
510+ const event = createEvent ( {
511+ url : "/albums" ,
512+ } ) ;
513+ incrementalCache . get . mockResolvedValueOnce ( {
514+ value : {
515+ type : "app" ,
516+ html : "Hello, world!" ,
517+ meta : {
518+ status : 405 ,
519+ } ,
520+ } ,
521+ } ) ;
522+
523+ const result = await cacheInterceptor ( event ) ;
524+ expect ( result . statusCode ) . toBe ( 405 ) ;
525+ } ) ;
526+
527+ it ( "should return 200 if there is no cached status code, nor a rewriteStatusCode" , async ( ) => {
528+ const event = createEvent ( {
529+ url : "/albums" ,
530+ } ) ;
531+ incrementalCache . get . mockResolvedValueOnce ( {
532+ value : {
533+ type : "app" ,
534+ html : "Hello, world!" ,
535+ } ,
536+ } ) ;
537+
538+ const result = await cacheInterceptor ( event ) ;
539+ expect ( result . statusCode ) . toBe ( 200 ) ;
540+ } ) ;
472541} ) ;
0 commit comments