11import { describe , before , after , it } from "node:test" ;
22import assert from "node:assert" ;
3- import { GenericContainer , StartedTestContainer , Wait } from "testcontainers" ;
3+ import {
4+ GenericContainer ,
5+ Network ,
6+ StartedNetwork ,
7+ StartedTestContainer ,
8+ Wait ,
9+ } from "testcontainers" ;
410import { OPAClient , ToInput , Input , Result } from "../src/" ;
511import { HTTPClient } from "../src/lib/http" ;
612
@@ -53,8 +59,11 @@ allow if {
5359` ;
5460
5561 let container : StartedTestContainer ;
62+ let network : StartedNetwork ;
63+ let proxy : StartedTestContainer ;
5664 let serverURL : string ;
5765 before ( async ( ) => {
66+ network = await new Network ( ) . start ( ) ;
5867 container = await new GenericContainer ( "openpolicyagent/opa:latest" )
5968 . withCommand ( [
6069 "run" ,
@@ -66,6 +75,8 @@ allow if {
6675 "--set=default_decision=system/main/main" ,
6776 "/authz.rego" ,
6877 ] )
78+ . withName ( "opa" )
79+ . withNetwork ( network )
6980 . withExposedPorts ( 8181 )
7081 . withWaitStrategy ( Wait . forHttp ( "/health" , 8181 ) . forStatusCode ( 200 ) )
7182 . withCopyContentToContainer ( [
@@ -75,6 +86,23 @@ allow if {
7586 } ,
7687 ] )
7788 . start ( ) ;
89+
90+ proxy = await new GenericContainer ( "caddy:latest" )
91+ . withNetwork ( network )
92+ . withExposedPorts ( 8000 )
93+ . withWaitStrategy ( Wait . forHttp ( "/opa/health" , 8000 ) . forStatusCode ( 200 ) )
94+ . withCopyContentToContainer ( [
95+ {
96+ content : `
97+ :8000 {
98+ handle_path /opa/* {
99+ reverse_proxy http://opa:8181
100+ }
101+ }` ,
102+ target : "/etc/caddy/Caddyfile" ,
103+ } ,
104+ ] )
105+ . start ( ) ;
78106 serverURL = `http://${ container . getHost ( ) } :${ container . getMappedPort ( 8181 ) } ` ;
79107
80108 for ( const [ id , body ] of Object . entries ( policies ) ) {
@@ -228,5 +256,17 @@ allow if {
228256 assert . strictEqual ( res , true ) ;
229257 } ) ;
230258
231- after ( async ( ) => await container . stop ( ) ) ;
259+ it ( "supports rules with slashes when proxied" , async ( ) => {
260+ const serverURL = `http://${ proxy . getHost ( ) } :${ proxy . getMappedPort ( 8000 ) } /opa` ;
261+ const res = await new OPAClient ( serverURL ) . evaluate (
262+ "has/weird%2fpackage/but/it_is" ,
263+ ) ;
264+ assert . strictEqual ( res , true ) ;
265+ } ) ;
266+
267+ after ( async ( ) => {
268+ await container . stop ( ) ;
269+ await proxy . stop ( ) ;
270+ await network . stop ( ) ;
271+ } ) ;
232272} ) ;
0 commit comments