1- import { test , expect } from "@playwright/test" ;
1+ import { test , expect , APIResponse } from "@playwright/test" ;
2+ import { toPath } from "../../fake-backend/page-builder" ;
23
34test . describe ( "requests" , ( ) => {
45 test ( "it proxies non-html requests" , async ( { request } ) => {
@@ -9,55 +10,111 @@ test.describe("requests", () => {
910 ) ;
1011 } ) ;
1112
12- test . describe ( "HEAD request" , ( ) => {
13- test ( "returns headers for vite page" , async ( { request } ) => {
14- const req = await request . head ( "./vite-page" ) ;
15- expect ( req . headers ( ) ) . toMatchObject ( {
16- "x-test-pageid" : "/pages/vite-page" ,
17- } ) ;
18- expect ( req . headers ( ) ) . not . toMatchObject ( {
19- "x-test-fake-backend" : "1" ,
20- } ) ;
21- } ) ;
22-
23- test ( "returns headers for proxied page" , async ( { request } ) => {
24- const req = await request . head ( "./custom-incorrect" ) ;
25- expect ( req . headers ( ) ) . toMatchObject ( {
26- "x-test-pageid" : "/pages/proxy/passthru" ,
27- // hits old backend
28- "x-test-fake-backend" : "1" ,
29- } ) ;
30- } ) ;
31- } ) ;
32-
3313 test . describe ( "onError" , ( ) => {
3414 test ( "returns header that we set in onError" , async ( { request } ) => {
3515 const req = await request . get ( "./broken-page" ) ;
3616 expect ( req . headers ( ) [ "x-test-onerror" ] ) . toBe ( "true" ) ;
3717 } ) ;
3818 } ) ;
3919
40- test . describe ( "req.bifrostPageId" , ( ) => {
41- test ( "returns page id" , async ( { request } ) => {
20+ test . describe ( "diagnostics attached to req" , ( ) => {
21+ function diagnostics ( req : APIResponse ) {
22+ return {
23+ status : req . status ( ) ,
24+ pageId : req . headers ( ) [ "x-test-pageid" ] ,
25+ layout :
26+ req . headers ( ) [ "x-test-layout" ] ?. split ( "," ) . filter ( Boolean ) || [ ] ,
27+ proxyMode :
28+ req . headers ( ) [ "x-test-proxymode" ] &&
29+ JSON . parse ( req . headers ( ) [ "x-test-proxymode" ] ) ,
30+ sentProxyHeaders : req . headers ( ) [ "x-test-sent-proxy-headers" ] === "1" ,
31+ } ;
32+ }
33+
34+ test ( "vite page sets pageId" , async ( { request } ) => {
4235 const req = await request . get ( "./vite-page" ) ;
43- expect ( req . headers ( ) [ "x-test-pageid" ] ) . toBe ( "/pages/vite-page" ) ;
36+ expect ( diagnostics ( req ) ) . toEqual ( {
37+ status : 200 ,
38+ pageId : "/pages/vite-page" ,
39+ layout : [ ] ,
40+ proxyMode : false ,
41+ sentProxyHeaders : false ,
42+ } ) ;
4443 } ) ;
4544
46- test ( "returns undefined when no route matches at all" , async ( {
47- request,
48- } ) => {
45+ test ( "when no route matches at all" , async ( { request } ) => {
4946 const req = await request . get ( "./jsaidofjasidofjasoidf" ) ;
50- expect ( req . headers ( ) [ "x-test-pageid" ] ) . toBe ( undefined ) ;
47+ expect ( diagnostics ( req ) ) . toEqual ( {
48+ status : 404 ,
49+ pageId : undefined ,
50+ layout : [ ] ,
51+ proxyMode : undefined ,
52+ sentProxyHeaders : false ,
53+ } ) ;
5154 } ) ;
5255
53- test ( "returns wrapped proxy route when hit" , async ( { request } ) => {
54- const req = await request . get ( "./json-route" ) ;
55- expect ( req . headers ( ) [ "x-test-pageid" ] ) . toBe ( "/pages/proxy/wrapped" ) ;
56+ test ( "on passthru, undefined pageId and layout" , async ( { request } ) => {
57+ const req = await request . get (
58+ toPath ( { endpoint : "custom-incorrect" , title : "a" } )
59+ ) ;
60+ expect ( diagnostics ( req ) ) . toEqual ( {
61+ status : 200 ,
62+ pageId : undefined ,
63+ layout : [ ] ,
64+ proxyMode : "passthru" ,
65+ sentProxyHeaders : false ,
66+ } ) ;
5667 } ) ;
5768
58- test ( "returns passthru proxy route when hit" , async ( { request } ) => {
59- const req = await request . get ( "./custom-incorrect" ) ;
60- expect ( req . headers ( ) [ "x-test-pageid" ] ) . toBe ( "/pages/proxy/passthru" ) ;
69+ test ( "wrapped with layout" , async ( { request } ) => {
70+ const req = await request . get ( toPath ( { title : "a" } ) ) ;
71+ expect ( diagnostics ( req ) ) . toEqual ( {
72+ status : 200 ,
73+ pageId : "/pages/proxy/wrapped" ,
74+ layout : [ "main_nav" ] ,
75+ proxyMode : "wrapped" ,
76+ sentProxyHeaders : true ,
77+ } ) ;
78+ } ) ;
79+
80+ test ( "wrapped with no layout" , async ( { request } ) => {
81+ const req = await request . get ( toPath ( { title : "a" , layout : "" } ) ) ;
82+ expect ( diagnostics ( req ) ) . toEqual ( {
83+ status : 200 ,
84+ pageId : undefined ,
85+ layout : [ ] ,
86+ proxyMode : "passthru" ,
87+ sentProxyHeaders : true ,
88+ } ) ;
89+ } ) ;
90+
91+ test ( "HEAD request on vite-page" , async ( { request } ) => {
92+ const req = await request . head ( "./vite-page" ) ;
93+ expect ( diagnostics ( req ) ) . toEqual ( {
94+ status : 200 ,
95+ pageId : "/pages/vite-page" ,
96+ layout : [ ] ,
97+ proxyMode : false ,
98+ sentProxyHeaders : false ,
99+ } ) ;
100+ expect ( req . headers ( ) ) . not . toMatchObject ( {
101+ "x-test-fake-backend" : "1" ,
102+ } ) ;
103+ } ) ;
104+
105+ test ( "HEAD request on proxied page" , async ( { request } ) => {
106+ const req = await request . head (
107+ toPath ( { endpoint : "custom-incorrect" , title : "a" } )
108+ ) ;
109+ expect ( diagnostics ( req ) ) . toEqual ( {
110+ status : 200 ,
111+ pageId : undefined ,
112+ layout : [ ] ,
113+ proxyMode : "passthru" ,
114+ sentProxyHeaders : false ,
115+ } ) ;
116+ // hits old backend
117+ expect ( req . headers ( ) [ "x-test-fake-backend" ] ) . toBe ( "1" ) ;
61118 } ) ;
62119
63120 test . skip ( "returns original page id on error pages" , async ( {
@@ -69,9 +126,7 @@ test.describe("requests", () => {
69126 } ) ;
70127
71128 test . describe ( "wrapped xhr" , ( ) => {
72- test ( "passes through script response" , async ( {
73- request,
74- } ) => {
129+ test ( "passes through script response" , async ( { request } ) => {
75130 const req = await request . get ( "/script-wrapped" , {
76131 headers : { Accept : "text/html" } ,
77132 } ) ;
@@ -81,15 +136,13 @@ test.describe("requests", () => {
81136 ) ;
82137 } ) ;
83138
84- test ( "passes through JSON response" , async ( {
85- request,
86- } ) => {
139+ test ( "passes through JSON response" , async ( { request } ) => {
87140 // important to set accept */* to allow the wrapped proxy
88141 const req = await request . get ( "/json-wrapped" , {
89142 headers : { Accept : "application/json */*" } ,
90143 } ) ;
91144 expect ( req . status ( ) ) . toBe ( 200 ) ;
92145 expect ( await req . json ( ) ) . toEqual ( { data : true } ) ;
93146 } ) ;
94- } )
147+ } ) ;
95148} ) ;
0 commit comments