1- import { expect , test } from "@playwright/test" ;
1+ import { expect , test , Response as PwResponse } from "@playwright/test" ;
22import { validateMd5 } from "../../utils" ;
33
44/*
@@ -23,26 +23,37 @@ test("Middleware Rewrite", async ({ page }) => {
2323} ) ;
2424
2525test ( "Middleware Rewrite External Image" , async ( { page } ) => {
26- page . on ( "response" , async ( response ) => {
27- expect ( response . status ( ) ) . toBe ( 200 ) ;
28- expect ( response . headers ( ) [ "content-type" ] ) . toBe ( "image/png" ) ;
29- expect ( response . headers ( ) [ "cache-control" ] ) . toBe ( "max-age=600" ) ;
30- const bodyBuffer = await response . body ( ) ;
31- expect ( validateMd5 ( bodyBuffer , OPENNEXT_PNG_MD5 ) ) . toBe ( true ) ;
26+ let responsePromise = new Promise < PwResponse > ( ( resolve ) => {
27+ page . on ( "response" , async ( resp ) => {
28+ resolve ( resp ) ;
29+ } ) ;
3230 } ) ;
31+
3332 await page . goto ( "/rewrite-external" ) ;
33+
34+ const response = await responsePromise ;
35+
36+ expect ( response . status ( ) ) . toBe ( 200 ) ;
37+ expect ( response . headers ( ) [ "content-type" ] ) . toBe ( "image/png" ) ;
38+ expect ( response . headers ( ) [ "cache-control" ] ) . toBe ( "max-age=600" ) ;
39+ const bodyBuffer = await response . body ( ) ;
40+ expect ( validateMd5 ( bodyBuffer , OPENNEXT_PNG_MD5 ) ) . toBe ( true ) ;
3441} ) ;
3542
3643test ( "Middleware Rewrite Status Code" , async ( { page } ) => {
3744 // Need to set up the event before navigating to the page to avoid missing it
3845 // 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- }
46+ const statusPromise = new Promise < number > ( ( resolve ) => {
47+ page . on ( "response" , async ( response ) => {
48+ // `response.url()` will be the full URL including the host, so we need to check the pathname
49+ if ( new URL ( response . url ( ) ) . pathname === "/rewrite-status-code" ) {
50+ resolve ( response . status ( ) ) ;
51+ }
52+ } ) ;
4453 } ) ;
54+
4555 await page . goto ( "/rewrite-status-code" ) ;
4656 const el = page . getByText ( "Rewritten Destination" , { exact : true } ) ;
4757 await expect ( el ) . toBeVisible ( ) ;
58+ expect ( statusPromise ) . resolves . toEqual ( 403 ) ;
4859} ) ;
0 commit comments