Skip to content

Commit

Permalink
more test cases for regex
Browse files Browse the repository at this point in the history
  • Loading branch information
xShteff committed Apr 17, 2024
1 parent 6e8bd31 commit 1c06a14
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 21 deletions.
6 changes: 4 additions & 2 deletions src/link-mobility.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
Expand Down
2 changes: 1 addition & 1 deletion src/tests/link-mobility.provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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-]+)*$'
);
});

Expand Down
43 changes: 25 additions & 18 deletions src/tests/link-mobility.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
};
Expand Down

0 comments on commit 1c06a14

Please sign in to comment.