1
1
/* eslint-disable sonarjs/no-duplicate-string */
2
2
import { cacheInterceptor } from "@opennextjs/aws/core/routing/cacheInterceptor.js" ;
3
3
import { 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" ;
5
5
import type { Queue } from "@opennextjs/aws/types/overrides.js" ;
6
6
import { fromReadableStream } from "@opennextjs/aws/utils/stream.js" ;
7
7
import { vi } from "vitest" ;
@@ -26,14 +26,14 @@ vi.mock("@opennextjs/aws/adapters/config/index.js", () => ({
26
26
} ) ) ;
27
27
28
28
vi . mock ( "@opennextjs/aws/core/routing/i18n/index.js" , ( ) => ( {
29
- localizePath : ( event : InternalEvent ) => event . rawPath ,
29
+ localizePath : ( event : MiddlewareEvent ) => event . rawPath ,
30
30
} ) ) ;
31
31
32
32
type PartialEvent = Partial <
33
- Omit < InternalEvent , "body" | "rawPath" | "query" >
33
+ Omit < MiddlewareEvent , "body" | "rawPath" | "query" >
34
34
> & { body ?: string } ;
35
35
36
- function createEvent ( event : PartialEvent ) : InternalEvent {
36
+ function createEvent ( event : PartialEvent ) : MiddlewareEvent {
37
37
const [ rawPath , qs ] = ( event . url ?? "/" ) . split ( "?" , 2 ) ;
38
38
return {
39
39
type : "core" ,
@@ -45,6 +45,7 @@ function createEvent(event: PartialEvent): InternalEvent {
45
45
query : convertFromQueryString ( qs ?? "" ) ,
46
46
cookies : event . cookies ?? { } ,
47
47
remoteAddress : event . remoteAddress ?? "::1" ,
48
+ rewriteStatusCode : event . rewriteStatusCode ,
48
49
} ;
49
50
}
50
51
@@ -469,4 +470,72 @@ describe("cacheInterceptor", () => {
469
470
} ) ,
470
471
) ;
471
472
} ) ;
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
+ } ) ;
472
541
} ) ;
0 commit comments