1- import { describe , it , expect , beforeAll , afterAll } from 'vitest' ;
1+ import { describe , it , expect , beforeEach , afterEach } from 'vitest' ;
22import { Server , createServer } from 'http' ;
33import { createServer as createSSLServer } from 'https' ;
44import { readFileSync } from 'fs' ;
@@ -10,27 +10,41 @@ import { CodezeroAgent } from '../src/index.mts';
1010describe ( 'CodezeroAgent' , ( ) => {
1111 describe ( 'constructor' , ( ) => {
1212 it ( 'should not throw given org id, org api key and space id' , ( ) => {
13- expect ( ( ) => new CodezeroAgent ( "orgId" , "orgApiKey" , "spaceId" ) ) . not . toThrow ( "Missing CZ_ORG_ID, CZ_ORG_API_KEY or CZ_SPACE_ID" ) ;
13+ expect ( ( ) => new CodezeroAgent ( { orgID : "orgId" , orgApiKey : "orgApiKey" , spaceID : "spaceId" } ) ) . not . toThrow ( "Missing CZ_ORG_ID, CZ_ORG_API_KEY or CZ_SPACE_ID" ) ;
1414 } ) ;
1515
1616 it ( 'should throw if org id, org api key or space id are missing' , ( ) => {
17- expect ( ( ) => new CodezeroAgent ( "1 ", "2" ) ) . toThrow ( "Missing CZ_ORG_ID, CZ_ORG_API_KEY or CZ_SPACE_ID" ) ;
17+ expect ( ( ) => new CodezeroAgent ( { orgID : "orgId ", orgApiKey : "orgApiKey" } ) ) . toThrow ( "Missing CZ_ORG_ID, CZ_ORG_API_KEY or CZ_SPACE_ID" ) ;
1818 } ) ;
1919 } ) ;
2020
2121 describe ( 'node-fetch' , ( ) => {
2222 let hubServer : Server ;
2323 let hubServerUrl : URL ;
24+ let receivedAuth : string ;
25+ let hubRequestCount = 0 ;
2426
2527 let proxy : ProxyServer ;
2628 let proxyUrl : URL ;
2729
2830 let targetServer : Server ;
2931 let targetServerUrl : URL ;
32+ let spaceCreds : any
33+
34+ const createToken = ( offsetSeconds : number ) => {
35+ const time = Date . now ( ) / 1000 + offsetSeconds ;
36+ const payload = Buffer . from ( JSON . stringify ( { exp : time } ) ) . toString ( 'base64' ) ;
37+ return `header.${ payload } ` ;
38+ }
3039
31- beforeAll ( async ( ) => {
32- hubServer = createServer ( ( _req , res ) => {
33- res . end ( JSON . stringify ( { host : '127.0.0.1' , token : 'token' , cert : readFileSync ( `${ __dirname } /server.crt` ) . toString ( ) } ) ) ;
40+ beforeEach ( async ( ) => {
41+ spaceCreds = { host : '127.0.0.1' , token : createToken ( 3 * 60 ) , cert : readFileSync ( `${ __dirname } /server.crt` ) . toString ( ) } ;
42+
43+ hubRequestCount = 0 ;
44+ hubServer = createServer ( ( req , res ) => {
45+ hubRequestCount ++ ;
46+ receivedAuth = req . headers . authorization ! ;
47+ res . end ( JSON . stringify ( spaceCreds ) ) ;
3448 } ) ;
3549 hubServerUrl = await listen ( hubServer ) ;
3650 process . env . CZ_HUB_SERVER_BASE_URL = hubServerUrl . href ;
@@ -50,7 +64,7 @@ describe('CodezeroAgent', () => {
5064 targetServerUrl = await listen ( targetServer ) ;
5165 } )
5266
53- afterAll ( ( ) => {
67+ afterEach ( ( ) => {
5468 hubServer . close ( ) ;
5569 proxy . close ( ) ;
5670 targetServer . close ( ) ;
@@ -59,11 +73,33 @@ describe('CodezeroAgent', () => {
5973 } ) ;
6074
6175 it ( 'should forward fetch requests via a proxy' , async ( ) => {
62- const agent = new CodezeroAgent ( "orgId" , "orgApiKey" , "spaceId" ) ;
76+ const agent = new CodezeroAgent ( { orgID : "orgId" , orgApiKey : "orgApiKey" , spaceID : "spaceId" } ) ;
6377 const response = await fetch ( targetServerUrl . href , { agent } ) ;
6478
6579 expect ( response . status ) . toBe ( 200 ) ;
6680 expect ( await response . text ( ) ) . toBe ( 'Hello!' ) ;
81+
82+ expect ( receivedAuth ) . toBe ( 'orgId:orgApiKey' ) ;
83+ } ) ;
84+
85+ it ( 'should cache credentials in the agent instance' , async ( ) => {
86+ const agent = new CodezeroAgent ( { orgID : "orgId" , orgApiKey : "orgApiKey" , spaceID : "spaceId" } ) ;
87+ await fetch ( targetServerUrl . href , { agent } ) ;
88+ await fetch ( targetServerUrl . href , { agent } ) ;
89+
90+ expect ( hubRequestCount ) . toBe ( 1 ) ;
91+ } ) ;
92+
93+ it ( 'should refetch credentials if token is expired' , async ( ) => {
94+ const agent = new CodezeroAgent ( { orgID : "orgId" , orgApiKey : "orgApiKey" , spaceID : "spaceId" } ) ;
95+
96+ await fetch ( targetServerUrl . href , { agent } ) ;
97+ expect ( hubRequestCount ) . toBe ( 1 ) ;
98+
99+ agent [ '_credentials' ] ! [ 'token' ] = createToken ( - 3 * 60 ) ;
100+ await fetch ( targetServerUrl . href , { agent } ) ;
101+
102+ expect ( hubRequestCount ) . toBe ( 2 ) ;
67103 } ) ;
68104 } ) ;
69105} ) ;
0 commit comments