Skip to content

Commit 801a233

Browse files
sameeragCopilot
andcommitted
test: add URL-as-title test cases for redirect URI without title element
Cover the scenario where document.title defaults to the raw redirect URL (browser behavior when no <title> element is set on the page). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 5ae5d65 commit 801a233

3 files changed

Lines changed: 58 additions & 0 deletions

File tree

lib/msal-browser/test/interaction_client/PopupClient.spec.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2365,6 +2365,33 @@ describe("PopupClient", () => {
23652365
).toBe("Microsoft Authentication");
23662366
});
23672367

2368+
it("replaces URL-based document.title on popup window when no title is set", () => {
2369+
const mockPopupWindow = {
2370+
...window,
2371+
document: {
2372+
title: "https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=test",
2373+
},
2374+
focus: jest.fn(),
2375+
};
2376+
jest.spyOn(window, "open").mockReturnValue(
2377+
mockPopupWindow as unknown as Window
2378+
);
2379+
2380+
const popupWindow = popupClient.initiateAuthRequest(
2381+
"http://localhost/#/code=hello",
2382+
{
2383+
popupName: "name",
2384+
popupWindowAttributes: {},
2385+
popupWindowParent: window,
2386+
}
2387+
);
2388+
2389+
expect(
2390+
(popupWindow as unknown as { document: { title: string } })
2391+
.document.title
2392+
).toBe("Microsoft Authentication");
2393+
});
2394+
23682395
it("does not throw when setting document.title on cross-origin popup fails", () => {
23692396
const mockPopupWindow = {
23702397
focus: jest.fn(),

lib/msal-browser/test/interaction_client/RedirectClient.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,27 @@ describe("RedirectClient", () => {
247247
});
248248
});
249249

250+
it("restores URL-based document.title when redirect URI page has no title element", (done) => {
251+
document.title =
252+
"https://localhost:3000/redirect#code=authCode123&state=abc";
253+
browserStorage.setInteractionInProgress(true);
254+
255+
redirectClient
256+
.handleRedirectPromise(
257+
testRequest,
258+
TEST_CONFIG.TEST_VERIFIER,
259+
rootMeasurement,
260+
{ hash: "" }
261+
)
262+
.then(() => {
263+
// Restores to the URL-based title since that was the original value
264+
expect(document.title).toBe(
265+
"https://localhost:3000/redirect#code=authCode123&state=abc"
266+
);
267+
done();
268+
});
269+
});
270+
250271
it("does nothing if no hash is detected", (done) => {
251272
browserStorage.setInteractionInProgress(true);
252273

lib/msal-browser/test/redirect_bridge/broadcastResponseToMainFrame.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,16 @@ describe("broadcastResponseToMainFrame", () => {
145145
expect(document.title).toBe("Microsoft Authentication");
146146
});
147147

148+
it("replaces URL-based document.title when redirect URI has no title set", async () => {
149+
document.title =
150+
"https://localhost:3000/redirect#code=testCode&state=testState";
151+
window.location.hash = TEST_HASHES.TEST_SUCCESS_CODE_HASH_POPUP;
152+
153+
await broadcastResponseToMainFrame();
154+
155+
expect(document.title).toBe("Microsoft Authentication");
156+
});
157+
148158
it("broadcasts response for popup flow from hash", async () => {
149159
window.location.hash = TEST_HASHES.TEST_SUCCESS_CODE_HASH_POPUP;
150160

0 commit comments

Comments
 (0)