@@ -2,6 +2,7 @@ import { describe, before, after, it } from "node:test";
22import assert from "node:assert" ;
33import { GenericContainer , StartedTestContainer , Wait } from "testcontainers" ;
44import { OPAClient , ToInput , Input , Result } from "../src/porcelain" ;
5+ import { HTTPClient } from "../src/lib/http" ;
56
67// Run these locally, with debug output from testcontainers, like this:
78// DEBUG='testcontainers*' node --require ts-node/register --test tests/**/*.ts
@@ -24,9 +25,25 @@ compound_result.allowed := true
2425 slash : `package has["weird/package"].but
2526import rego.v1
2627
27- it_is := true
28+ it_is := true` ,
29+ token : `package token
30+ import rego.v1
31+ p := true
2832` ,
2933 } ;
34+ const authzPolicy = `package system.authz
35+ import rego.v1
36+
37+ default allow := false
38+ allow if input.method == "PUT"
39+ allow if input.path[0] == "health"
40+ allow if input.path[2] == "test"
41+ allow if input.path[2] == "has"
42+ allow if {
43+ input.path[2] = "token"
44+ input.identity = "opensesame"
45+ }
46+ ` ;
3047
3148 let container : StartedTestContainer ;
3249 let serverURL : string ;
@@ -37,9 +54,18 @@ it_is := true
3754 "--server" ,
3855 "--disable-telemetry" ,
3956 "--log-level=debug" ,
57+ "--authentication=token" ,
58+ "--authorization=basic" ,
59+ "/authz.rego" ,
4060 ] )
4161 . withExposedPorts ( 8181 )
4262 . withWaitStrategy ( Wait . forHttp ( "/health" , 8181 ) . forStatusCode ( 200 ) )
63+ . withCopyContentToContainer ( [
64+ {
65+ content : authzPolicy ,
66+ target : "/authz.rego" ,
67+ } ,
68+ ] )
4369 . start ( ) ;
4470 serverURL = `http://${ container . getHost ( ) } :${ container . getMappedPort ( 8181 ) } ` ;
4571
@@ -160,5 +186,27 @@ it_is := true
160186 assert . deepStrictEqual ( res , true ) ;
161187 } ) ;
162188
189+ it ( "allows custom low-level SDKOptions' HTTPClient" , async ( ) => {
190+ const httpClient = new HTTPClient ( { } ) ;
191+ let called = false ;
192+ httpClient . addHook ( "beforeRequest" , ( req ) => {
193+ called = true ;
194+ return req ;
195+ } ) ;
196+ const res = await new OPAClient ( serverURL , {
197+ sdk : { httpClient } ,
198+ } ) . authorize ( "test/p_bool" ) ;
199+ assert . strictEqual ( res , true ) ;
200+ assert . strictEqual ( called , true ) ;
201+ } ) ;
202+
203+ it ( "allows custom headers" , async ( ) => {
204+ const authorization = "Bearer opensesame" ;
205+ const res = await new OPAClient ( serverURL , {
206+ headers : { authorization } ,
207+ } ) . authorize ( "token/p" ) ;
208+ assert . strictEqual ( res , true ) ;
209+ } ) ;
210+
163211 after ( async ( ) => await container . stop ( ) ) ;
164212} ) ;
0 commit comments