Skip to content

Commit cf0f884

Browse files
refactor: added new test cases for getEndpoint()
1 parent e74215e commit cf0f884

File tree

2 files changed

+85
-1
lines changed

2 files changed

+85
-1
lines changed

packages/core/src/plugins/SegmentDestination.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ export class SegmentDestination extends DestinationPlugin {
9898
let endpoint = '';
9999

100100
if (hasProxy) {
101+
if (baseURL.endsWith('/') || baseURL.includes('?')) {
102+
throw new Error('Invalid proxy url has been passed');
103+
}
101104
endpoint = useSegmentEndpoints ? '/b' : '';
102105
} else {
103106
// Check if baseURL ends with '/b', if so, do not append '/b'

packages/core/src/plugins/__tests__/SegmentDestination.test.ts

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)