Skip to content

Commit

Permalink
fix: [#1627] Fixes bug where the incorrect location is set after a re…
Browse files Browse the repository at this point in the history
…direct in BrowserFrame.goto and DetachedBrowserFrame.goto (#1651)
  • Loading branch information
capricorn86 authored Dec 30, 2024
1 parent f41ad67 commit 758e9ff
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/happy-dom/src/browser/BrowserFrame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export default class BrowserFrame implements IBrowserFrame {
* @param url URL.
*/
public set url(url) {
this.window.location[PropertySymbol.setURL](
this.window[PropertySymbol.location][PropertySymbol.setURL](
this,
BrowserFrameURL.getRelativeURL(this, url).href
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export default class DetachedBrowserFrame implements IBrowserFrame {
if (!this.window) {
throw new Error('The frame has been destroyed, the "window" property is not set.');
}
this.window.location[PropertySymbol.setURL](
this.window[PropertySymbol.location][PropertySymbol.setURL](
this,
BrowserFrameURL.getRelativeURL(this, url).href
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@ export default class BrowserFrameNavigator {
throw error;
}

if (response.url) {
frame.window[PropertySymbol.location][PropertySymbol.setURL](frame, response.url);
}

if (!response.ok) {
frame.page.console.error(`GET ${targetURL.href} ${response.status} (${response.statusText})`);
}
Expand Down
20 changes: 20 additions & 0 deletions packages/happy-dom/test/browser/BrowserFrame.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,26 @@ describe('BrowserFrame', () => {
expect(page.mainFrame.url).toBe('http://localhost:9999/');
expect(page.mainFrame.window === oldWindow).toBe(false);
});

it('Sets the correct location after being redirected.', async () => {
vi.spyOn(Fetch.prototype, 'send').mockImplementation(function (): Promise<Response> {
return Promise.resolve(<Response>{
status: 200,
url: 'http://localhost:3000/redirected/',
text: () => Promise.resolve('<b>Redirected</b>')
});
});

const browser = new Browser();
const page = browser.newPage();

await page.mainFrame.goto('http://localhost:3000');

expect(page.mainFrame.content).toBe(
'<html><head></head><body><b>Redirected</b></body></html>'
);
expect(page.mainFrame.url).toBe('http://localhost:3000/redirected/');
});
});

describe('goBack()', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,32 @@ describe('DetachedBrowserFrame', () => {
expect(page.mainFrame.url).toBe('http://localhost:9999/');
expect(page.mainFrame.window === oldWindow).toBe(true);
});

it('Sets the correct location after being redirected.', async () => {
vi.spyOn(Fetch.prototype, 'send').mockImplementation(function (): Promise<Response> {
return Promise.resolve(<Response>{
status: 200,
url: 'http://localhost:3000/redirected/',
text: () => Promise.resolve('<b>Redirected</b>')
});
});

const browser = new DetachedBrowser(BrowserWindow, {
settings: {
navigation: {
disableFallbackToSetURL: false
}
}
});
const page = browser.newPage();

await page.mainFrame.goto('http://localhost:3000');

expect(page.mainFrame.content).toBe(
'<html><head></head><body><b>Redirected</b></body></html>'
);
expect(page.mainFrame.url).toBe('http://localhost:3000/redirected/');
});
});

describe('goBack()', () => {
Expand Down

0 comments on commit 758e9ff

Please sign in to comment.