diff --git a/src/link-mobility.service.ts b/src/link-mobility.service.ts index 89258d6..e7dca56 100644 --- a/src/link-mobility.service.ts +++ b/src/link-mobility.service.ts @@ -16,10 +16,12 @@ export class LinkMobilityGateService { constructor(inputs: LinkMobilityGateServiceInputs) { this.username = inputs.username; this.password = inputs.password; - const urlRegex = new RegExp('^(https?|http)://[a-zA-Z0-9-.]+.[a-zA-Z]{2,}(:[0-9]{2,4})?$'); + const regexString = + '^(https?|http)://(w{3}.)?[a-zA-Z0-9-]+.[a-zA-Z]{2,}(:[0-9]{2,4})?(/[a-zA-Z0-9-]+)*$'; + const urlRegex = new RegExp(regexString); if (!urlRegex.test(inputs.url)) { throw new Error( - 'Invalid Link Mobility URL. URL must follow the following pattern: ^(https?|http)://[a-zA-Z0-9-.]+.[a-zA-Z]{2,}(:[0-9]{2,4})?$' + `Invalid Link Mobility URL. URL must follow the following pattern: ${regexString}` ); } this.url = `${inputs.url}/gate/partnergate/platform/${inputs.platform}/partner/${inputs.partner}`; diff --git a/src/tests/link-mobility.provider.test.ts b/src/tests/link-mobility.provider.test.ts index dca6ce6..dae7894 100644 --- a/src/tests/link-mobility.provider.test.ts +++ b/src/tests/link-mobility.provider.test.ts @@ -37,7 +37,7 @@ describe('Test suite for Link Mobility Provider', () => { expect(() => { new LinkMobilityGateDestinationProvider(ctorInput); }).toThrowError( - 'Invalid Link Mobility URL. URL must follow the following pattern: ^(https?|http)://[a-zA-Z0-9-.]+.[a-zA-Z]{2,}(:[0-9]{2,4})?$' + 'Invalid Link Mobility URL. URL must follow the following pattern: ^(https?|http)://(w{3}.)?[a-zA-Z0-9-]+.[a-zA-Z]{2,}(:[0-9]{2,4})?(/[a-zA-Z0-9-]+)*$' ); }); diff --git a/src/tests/link-mobility.service.test.ts b/src/tests/link-mobility.service.test.ts index e290f14..dd03137 100644 --- a/src/tests/link-mobility.service.test.ts +++ b/src/tests/link-mobility.service.test.ts @@ -21,28 +21,35 @@ describe('Test suite for link mobility partner gate service', () => { jest.clearAllMocks(); }); - it('should fail to initialise if url is invalid', async () => { + it.each(['https://some-url.com/ending-with/', 'not-a-real-url'])( + 'should fail to initialise if url is invalid', + async (url) => { + // Arrange + const ctorInput: LinkMobilityGateServiceInputs = { + username: 'username', + password: 'password', + url: url, + platform: 'platform', + partner: 'partner', + }; + + // Act & Assert + expect(() => { + new LinkMobilityGateService(ctorInput); + }).toThrowError(); + } + ); + + it.each([ + 'http://some-url.com', + 'http://www.some-other-url.org', + 'https://actual-url.dk/with-paths', + ])('should initialise if url is valid', async (url) => { // Arrange const ctorInput: LinkMobilityGateServiceInputs = { username: 'username', password: 'password', - url: 'invalid-url', - platform: 'platform', - partner: 'partner', - }; - - // Act & Assert - expect(() => { - new LinkMobilityGateService(ctorInput); - }).toThrowError(); - }); - - it('should initialise if url is valid', async () => { - // Arrange - const ctorInput: LinkMobilityGateServiceInputs = { - username: 'username', - password: 'password', - url: 'http://some-url.com', + url: url, platform: 'platform', partner: 'partner', };