@@ -396,7 +396,6 @@ describe('SegmentDestination', () => {
396396 expectedUrl = getURL ( customEndpoint , '/b' ) ;
397397 } else {
398398 expectedUrl = getURL ( customEndpoint , '' ) ;
399- console . log ( 'expected URL---->' , expectedUrl ) ;
400399 }
401400 } else {
402401 expectedUrl = getURL ( 'events.eu1.segmentapis.com' , '/b' ) ;
@@ -419,4 +418,86 @@ describe('SegmentDestination', () => {
419418 }
420419 ) ;
421420 } ) ;
421+ describe ( 'getEndpoint' , ( ) => {
422+ it ( 'should throw an error when proxy ends with "/" and useSegmentEndpoints is true' , ( ) => {
423+ const plugin = new SegmentDestination ( ) ;
424+ // Set up config dynamically
425+ const config = {
426+ ...clientArgs . config ,
427+ useSegmentEndpoints : true ,
428+ proxy : 'example.com/v1/' ,
429+ } ;
430+ plugin . analytics = new SegmentClient ( {
431+ ...clientArgs ,
432+ config,
433+ } ) ;
434+
435+ // Create a spy for the private method
436+ const spy = jest . spyOn (
437+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
438+ Object . getPrototypeOf ( plugin ) as any ,
439+ 'getEndpoint'
440+ ) ;
441+ // Expect the private method to throw an error
442+ expect ( ( ) => plugin [ 'getEndpoint' ] ( ) ) . toThrow (
443+ 'Invalid proxy url has been passed'
444+ ) ;
445+ // Ensure the spy was called
446+ expect ( spy ) . toHaveBeenCalled ( ) ;
447+ } ) ;
448+ } ) ;
449+ it ( 'should throw an error when proxy contains a query parameter' , ( ) => {
450+ const plugin = new SegmentDestination ( ) ;
451+ // Set up config dynamically
452+ const config = {
453+ ...clientArgs . config ,
454+ useSegmentEndpoints : false , // Irrelevant for this test
455+ proxy : 'example.com/v1?query=1' , // Invalid proxy (contains '?')
456+ } ;
457+ plugin . analytics = new SegmentClient ( {
458+ ...clientArgs ,
459+ config,
460+ } ) ;
461+
462+ // Create a spy for the private method
463+ const spy = jest . spyOn (
464+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
465+ Object . getPrototypeOf ( plugin ) as any ,
466+ 'getEndpoint'
467+ ) ;
468+
469+ // Expect the private method to throw an error
470+ expect ( ( ) => plugin [ 'getEndpoint' ] ( ) ) . toThrow (
471+ 'Invalid proxy url has been passed'
472+ ) ;
473+
474+ // Ensure the spy was called
475+ expect ( spy ) . toHaveBeenCalled ( ) ;
476+ } ) ;
477+ it ( 'should append "/b" when proxy ends with "/b" and useSegmentEndpoints is true' , ( ) => {
478+ const plugin = new SegmentDestination ( ) ;
479+ // Set up config dynamically
480+ const config = {
481+ ...clientArgs . config ,
482+ useSegmentEndpoints : true , // Should append "/b"
483+ proxy : 'example.com/v1/b' , // Already ends with "/b"
484+ } ;
485+ plugin . analytics = new SegmentClient ( {
486+ ...clientArgs ,
487+ config,
488+ } ) ;
489+
490+ // Spy on the private method
491+ const spy = jest . spyOn (
492+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
493+ Object . getPrototypeOf ( plugin ) as any ,
494+ 'getEndpoint'
495+ ) ;
496+
497+ // Expect the private method to return "example.com/v1/b/b"
498+ expect ( plugin [ 'getEndpoint' ] ( ) ) . toBe ( 'https://example.com/v1/b/b' ) ;
499+
500+ // Ensure the spy was called
501+ expect ( spy ) . toHaveBeenCalled ( ) ;
502+ } ) ;
422503} ) ;
0 commit comments