@@ -17,6 +17,7 @@ import { createTempDir } from '../../stx/src/temp-dir'
1717setDefaultTimeout ( 60_000 )
1818
1919const PORT = freePort ( )
20+ const API_PORT = freePort ( )
2021const BASE = `http://127.0.0.1:${ PORT } `
2122const SERVE_SRC = path . join ( import . meta. dir , '..' , 'src' , 'serve.ts' )
2223
@@ -29,10 +30,25 @@ beforeAll(async () => {
2930 await Bun . write ( path . join ( dir , 'views' , 'index.stx' ) , '<h1>home</h1>' )
3031 await Bun . write ( path . join ( dir , 'driver.ts' ) , `import { serve } from ${ JSON . stringify ( SERVE_SRC ) }
3132
33+ Bun.serve({
34+ port: ${ API_PORT } ,
35+ fetch(request) {
36+ if (request.headers.get('origin') !== 'https://lsu.example')
37+ return new Response('Origin refused', { status: 403 })
38+ return new Response(null, { status: 204, headers: {
39+ 'Access-Control-Allow-Origin': 'https://lsu.example',
40+ 'Access-Control-Allow-Methods': 'GET, OPTIONS',
41+ 'Access-Control-Allow-Headers': 'Authorization, X-HQ-Day, X-Org-Id',
42+ 'Vary': 'Origin',
43+ } })
44+ },
45+ })
3246serve({
3347 patterns: ['views'],
3448 port: ${ PORT } ,
35- onRequest(req, server) {
49+ async onRequest(req, server) {
50+ if (new URL(req.url).pathname.startsWith('/api/'))
51+ return fetch('http://127.0.0.1:${ API_PORT } ' + new URL(req.url).pathname, { method: req.method, headers: req.headers })
3652 if (new URL(req.url).pathname !== '/__peer')
3753 return null
3854 return Response.json({ peer: server.requestIP(req)?.address ?? null })
@@ -75,4 +91,29 @@ describe('serve onRequest', () => {
7591
7692 expect ( await res . text ( ) ) . toContain ( 'home' )
7793 } )
94+
95+ it ( 'forwards API preflight to its proxy with the configured origin and headers' , async ( ) => {
96+ const res = await fetch ( `${ BASE } /api/health/metrics` , { method : 'OPTIONS' , headers : {
97+ Origin : 'https://lsu.example' ,
98+ 'Access-Control-Request-Method' : 'GET' ,
99+ 'Access-Control-Request-Headers' : 'authorization,x-hq-day,x-org-id' ,
100+ } } )
101+ expect ( res . status ) . toBe ( 204 )
102+ expect ( res . headers . get ( 'access-control-allow-origin' ) ) . toBe ( 'https://lsu.example' )
103+ expect ( res . headers . get ( 'access-control-allow-headers' ) ) . toContain ( 'X-HQ-Day' )
104+ expect ( res . headers . get ( 'vary' ) ) . toBe ( 'Origin' )
105+ } )
106+
107+ it ( 'keeps the upstream refusal instead of granting a wildcard preflight' , async ( ) => {
108+ const res = await fetch ( `${ BASE } /api/health/metrics` , { method : 'OPTIONS' , headers : { Origin : 'https://foreign.example' } } )
109+ expect ( res . status ) . toBe ( 403 )
110+ expect ( res . headers . get ( 'access-control-allow-origin' ) ) . toBeNull ( )
111+ } )
112+
113+ it ( 'retains the generic asset preflight when the hook does not answer' , async ( ) => {
114+ const res = await fetch ( `${ BASE } /` , { method : 'OPTIONS' } )
115+ expect ( res . status ) . toBe ( 200 )
116+ expect ( res . headers . get ( 'access-control-allow-origin' ) ) . toBe ( '*' )
117+ } )
118+
78119} )
0 commit comments