@@ -238,6 +238,65 @@ describe('journey-client', () => {
238238 } ) ;
239239 } ) ;
240240
241+ describe ( 'baseUrl normalization' , ( ) => {
242+ test ( 'should add trailing slash to baseUrl without one' , async ( ) => {
243+ const configWithoutSlash : JourneyClientConfig = {
244+ serverConfig : {
245+ baseUrl : 'http://localhost:9443/am' ,
246+ } ,
247+ realmPath : 'root' ,
248+ } ;
249+
250+ const mockStepResponse : Step = { authId : 'test-auth-id' , callbacks : [ ] } ;
251+ mockFetch . mockResolvedValue ( new Response ( JSON . stringify ( mockStepResponse ) ) ) ;
252+
253+ const client = await journey ( { config : configWithoutSlash } ) ;
254+ await client . start ( ) ;
255+
256+ expect ( mockFetch ) . toHaveBeenCalledTimes ( 1 ) ;
257+ const request = mockFetch . mock . calls [ 0 ] [ 0 ] as Request ;
258+ expect ( request . url ) . toBe ( 'http://localhost:9443/am/json/realms/root/authenticate' ) ;
259+ } ) ;
260+
261+ test ( 'should preserve trailing slash if already present' , async ( ) => {
262+ const configWithSlash : JourneyClientConfig = {
263+ serverConfig : {
264+ baseUrl : 'http://localhost:9443/am/' ,
265+ } ,
266+ realmPath : 'root' ,
267+ } ;
268+
269+ const mockStepResponse : Step = { authId : 'test-auth-id' , callbacks : [ ] } ;
270+ mockFetch . mockResolvedValue ( new Response ( JSON . stringify ( mockStepResponse ) ) ) ;
271+
272+ const client = await journey ( { config : configWithSlash } ) ;
273+ await client . start ( ) ;
274+
275+ expect ( mockFetch ) . toHaveBeenCalledTimes ( 1 ) ;
276+ const request = mockFetch . mock . calls [ 0 ] [ 0 ] as Request ;
277+ expect ( request . url ) . toBe ( 'http://localhost:9443/am/json/realms/root/authenticate' ) ;
278+ } ) ;
279+
280+ test ( 'should work with baseUrl without context path' , async ( ) => {
281+ const configNoContext : JourneyClientConfig = {
282+ serverConfig : {
283+ baseUrl : 'http://localhost:9443' ,
284+ } ,
285+ realmPath : 'root' ,
286+ } ;
287+
288+ const mockStepResponse : Step = { authId : 'test-auth-id' , callbacks : [ ] } ;
289+ mockFetch . mockResolvedValue ( new Response ( JSON . stringify ( mockStepResponse ) ) ) ;
290+
291+ const client = await journey ( { config : configNoContext } ) ;
292+ await client . start ( ) ;
293+
294+ expect ( mockFetch ) . toHaveBeenCalledTimes ( 1 ) ;
295+ const request = mockFetch . mock . calls [ 0 ] [ 0 ] as Request ;
296+ expect ( request . url ) . toBe ( 'http://localhost:9443/json/realms/root/authenticate' ) ;
297+ } ) ;
298+ } ) ;
299+
241300 // TODO: Add tests for endSession when the test environment AbortSignal issue is resolved
242301 // test('endSession() should call the sessions endpoint with DELETE method', async () => {
243302 // mockFetch.mockResolvedValue(new Response('', { status: 200 }));
0 commit comments