@@ -23,21 +23,26 @@ test("Middleware Rewrite", async ({ page }) => {
23
23
} ) ;
24
24
25
25
test ( "Middleware Rewrite External Image" , async ( { page } ) => {
26
- await page . goto ( "/rewrite-external" ) ;
27
26
page . on ( "response" , async ( response ) => {
28
27
expect ( response . status ( ) ) . toBe ( 200 ) ;
29
28
expect ( response . headers ( ) [ "content-type" ] ) . toBe ( "image/png" ) ;
30
29
expect ( response . headers ( ) [ "cache-control" ] ) . toBe ( "max-age=600" ) ;
31
30
const bodyBuffer = await response . body ( ) ;
32
31
expect ( validateMd5 ( bodyBuffer , OPENNEXT_PNG_MD5 ) ) . toBe ( true ) ;
33
32
} ) ;
33
+ await page . goto ( "/rewrite-external" ) ;
34
34
} ) ;
35
35
36
36
test ( "Middleware Rewrite Status Code" , async ( { page } ) => {
37
+ // Need to set up the event before navigating to the page to avoid missing it
38
+ // We need to check the URL here also cause there will be multiple responses (i.e the fonts, css, js, etc)
39
+ page . on ( "response" , async ( response ) => {
40
+ // `response.url()` will be the full URL including the host, so we need to check the pathname
41
+ if ( new URL ( response . url ( ) ) . pathname === "/rewrite-status-code" ) {
42
+ expect ( response . status ( ) ) . toBe ( 403 ) ;
43
+ }
44
+ } ) ;
37
45
await page . goto ( "/rewrite-status-code" ) ;
38
46
const el = page . getByText ( "Rewritten Destination" , { exact : true } ) ;
39
47
await expect ( el ) . toBeVisible ( ) ;
40
- page . on ( "response" , async ( response ) => {
41
- expect ( response . status ( ) ) . toBe ( 403 ) ;
42
- } ) ;
43
48
} ) ;
0 commit comments