@@ -4,7 +4,7 @@ import { HttpServer, HttpServerError, HttpServerResponse } from "effect/unstable
44import { it } from "../../core/test/lib/effect"
55import { ServerProcess } from "../src/process"
66
7- it . live ( "allows browser preflight requests without credentials " , ( ) =>
7+ it . live ( "authenticates API and frontend requests while allowing browser preflight " , ( ) =>
88 Effect . gen ( function * ( ) {
99 const fallback = "fallback" . repeat ( 256 )
1010 const server = yield * ServerProcess . start < never , never > (
@@ -130,6 +130,36 @@ it.live("allows browser preflight requests without credentials", () =>
130130 expect ( yield * Effect . promise ( ( ) => response . text ( ) ) ) . toBe ( "" )
131131 } ) ,
132132 )
133+
134+ yield * Effect . forEach ( [ "/" , "/workspace/example" , "/_assets/app.js" , "/icons/icon.svg" , "/sw.js" ] , ( pathname ) =>
135+ Effect . gen ( function * ( ) {
136+ yield * Effect . forEach ( [ "GET" , "HEAD" ] , ( method ) =>
137+ Effect . gen ( function * ( ) {
138+ yield * Effect . forEach ( [ undefined , `Basic ${ btoa ( "opencode:wrong" ) } ` ] , ( authorization ) =>
139+ Effect . gen ( function * ( ) {
140+ const response = yield * Effect . promise ( ( ) =>
141+ fetch ( new URL ( pathname , HttpServer . formatAddress ( server . address ) ) , {
142+ method,
143+ headers : authorization ? { authorization } : undefined ,
144+ } ) ,
145+ )
146+ expect ( response . status ) . toBe ( 401 )
147+ expect ( response . headers . get ( "www-authenticate" ) ) . toBe ( 'Basic realm="Secure Area"' )
148+ expect ( yield * Effect . promise ( ( ) => response . text ( ) ) ) . toBe ( "" )
149+ } ) ,
150+ )
151+ const response = yield * Effect . promise ( ( ) =>
152+ fetch ( new URL ( pathname , HttpServer . formatAddress ( server . address ) ) , {
153+ method,
154+ headers : { authorization : `Basic ${ btoa ( "opencode:secret" ) } ` } ,
155+ } ) ,
156+ )
157+ expect ( response . status ) . toBe ( 200 )
158+ expect ( yield * Effect . promise ( ( ) => response . text ( ) ) ) . toBe ( method === "HEAD" ? "" : fallback )
159+ } ) ,
160+ )
161+ } ) ,
162+ )
133163 } ) ,
134164)
135165
0 commit comments