Skip to content

Commit

Permalink
test(hostrouter): adding test and more documentation
Browse files Browse the repository at this point in the history
These changes fix a bug where routes were grabbing the routes with less matching path segments,
causing some routes to return the wrong client

BREAKING CHANGE: If apps made workarounds for this bug, then there may be a possibility that the
wrong client is returned for them

COMUI-2914
  • Loading branch information
jordanstith15 committed Jul 5, 2024
1 parent 23140c0 commit 4714ce2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
16 changes: 11 additions & 5 deletions packages/iframe-coordinator/src/HostRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {

/**
* HostRouter is responsible for mapping route paths to
* corresponding client URls.
* corresponding client URLs.
*/
export class HostRouter {
private _clients: ClientInfo[];
Expand All @@ -17,14 +17,13 @@ export class HostRouter {
.map(([id, data]) => {
return parseRegistration(id, data);
})
.sort(
(a, b) =>
b.assignedRoute.split("/").length - a.assignedRoute.split("/").length,
);
// Sorts by most path segments in url descending to make sure more specific paths are matched first
.sort(byMostPathSegments);
}

/**
* Gets the client id and url for the provided route.
* Because of the sorting done when instantiated, this will return the target client with the most matching path segments
*
* @param route The route to lookup, such as '/foo/bar/baz'
*/
Expand Down Expand Up @@ -187,3 +186,10 @@ function applyRoute(urlStr: string, route: string): string {
}
return newUrl.toString();
}

/**
* Helper function for sorting clients by length of url path segments descending
*/
function byMostPathSegments(a: ClientInfo, b: ClientInfo): number {
return b.assignedRoute.split("/").length - a.assignedRoute.split("/").length;
}
18 changes: 17 additions & 1 deletion packages/iframe-coordinator/src/specs/HostRouter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe("HostRouter", () => {
hostRouter = new HostRouter({
route1: {
url: clientUrl,
assignedRoute: "route/one",
assignedRoute: "route/one/",
},
withRouteSlashes: {
url: clientUrl,
Expand All @@ -23,6 +23,10 @@ describe("HostRouter", () => {
sandbox: "allow-scripts",
allow: "microphone *; camera *;",
},
withMoreSpecificity: {
url: clientUrl + "/specific",
assignedRoute: "route/one/specific",
},
});
});

Expand Down Expand Up @@ -92,5 +96,17 @@ describe("HostRouter", () => {
expect(clientInfo.sandbox).toBe("allow-scripts");
expect(clientInfo.allow).toBe("microphone *; camera *;");
});

it("should return route with more specificity", () => {
const clientInfo = hostRouter.getClientTarget("route/one/specific/route");
if (!clientInfo) {
fail();
return;
}
expect(clientInfo.url).toBe(
"http://example.com/#/test/one/specific/route",
);
expect(clientInfo.id).toBe("withMoreSpecificity");
});
});
});

0 comments on commit 4714ce2

Please sign in to comment.