Skip to content

Commit

Permalink
fix(webex-core): fix findServiceUrlFromUrl (webex#3725)
Browse files Browse the repository at this point in the history
  • Loading branch information
Coread authored Jul 22, 2024
1 parent 45d5cc6 commit 9f0f20e
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 5 deletions.
16 changes: 12 additions & 4 deletions packages/@webex/webex-core/src/lib/services/service-catalog.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,6 @@ const ServiceCatalog = AmpState.extend({
* @returns {serviceUrl} - ServiceUrl assocated with provided url
*/
findServiceUrlFromUrl(url) {
const incomingUrlObj = Url.parse(url);
const serviceUrls = [
...this.serviceGroups.discovery,
...this.serviceGroups.preauth,
Expand All @@ -247,12 +246,21 @@ const ServiceCatalog = AmpState.extend({
];

return serviceUrls.find((serviceUrl) => {
if (incomingUrlObj.hostname === Url.parse(serviceUrl.defaultUrl).hostname) {
// Check to see if the URL we are checking starts with the default URL
if (url.startsWith(serviceUrl.defaultUrl)) {
return true;
}

if (serviceUrl.hosts.find((host) => host.host === incomingUrlObj.hostname)) {
return true;
// If not, we check to see if the alternate URLs match
// These are made by swapping the host of the default URL
// with that of an alternate host
for (const host of serviceUrl.hosts) {
const alternateUrl = new URL(serviceUrl.defaultUrl);
alternateUrl.host = host.host;

if (url.startsWith(alternateUrl.toString())) {
return true;
}
}

return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('webex-core', () => {
let services;
let catalog;

beforeAll(() => {
beforeEach(() => {
webex = new MockWebex();
services = new Services(undefined, {parent: webex});
catalog = services._getCatalog();
Expand Down Expand Up @@ -201,6 +201,56 @@ describe('webex-core', () => {
assert.match(['example-a', 'example-b', 'example-c', 'example-e', 'example-f'], list);
});
});

describe('findServiceUrlFromUrl()', () => {
const otherService = {
defaultUrl: 'https://example.com/differentresource',
hosts: [{host: 'example1.com'}, {host: 'example2.com'}],
};

it.each([
'discovery',
'preauth',
'signin',
'postauth',
'override'
])('matches a default url correctly', (serviceGroup) => {
const url = 'https://example.com/resource/id';


const exampleService = {
defaultUrl: 'https://example.com/resource',
hosts: [{host: 'example1.com'}, {host: 'example2.com'}],
};

catalog.serviceGroups[serviceGroup].push(otherService, exampleService);

const service = catalog.findServiceUrlFromUrl(url);

assert.equal(service, exampleService);
});

it.each([
'discovery',
'preauth',
'signin',
'postauth',
'override'
])('matches an alternate host url', (serviceGroup) => {
const url = 'https://example2.com/resource/id';

const exampleService = {
defaultUrl: 'https://example.com/resource',
hosts: [{host: 'example1.com'}, {host: 'example2.com'}],
};

catalog.serviceGroups[serviceGroup].push(otherService, exampleService);

const service = catalog.findServiceUrlFromUrl(url);

assert.equal(service, exampleService);
});
});
});
});
/* eslint-enable no-underscore-dangle */

0 comments on commit 9f0f20e

Please sign in to comment.