Skip to content

Commit

Permalink
Preserve URL fragments across redirects in jsdom.env
Browse files Browse the repository at this point in the history
This may need a more extensive fix for other cases, e.g. iframes, but for now this will help people.
  • Loading branch information
josephfrazier authored and domenic committed Mar 12, 2017
1 parent 0454f04 commit 7f148e8
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
5 changes: 5 additions & 0 deletions lib/jsdom.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,8 @@ exports.env = exports.jsdom.env = function () {
agentOptions: config.agentOptions
};

const fragment = whatwgURL.parseURL(config.url).fragment;

return resourceLoader.download(config.url, options, (err, responseText, res) => {
if (err) {
reportInitError(err, config);
Expand All @@ -262,6 +264,9 @@ exports.env = exports.jsdom.env = function () {
// is updated when `request` follows redirects.
config.html = responseText;
config.url = res.request.uri.href;
if (fragment) {
config.url += `#${fragment}`;
}

if (res.headers["last-modified"]) {
config.lastModified = new Date(res.headers["last-modified"]);
Expand Down
4 changes: 2 additions & 2 deletions test/jsdom/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -1180,14 +1180,14 @@ describe("jsdom/miscellaneous", () => {

server.listen(8001, "127.0.0.1", () => {
jsdom.env({
url: "http://127.0.0.1:8001",
url: "http://127.0.0.1:8001#hash",
done(errors, window) {
server.close();
if (errors) {
assert.ok(false, errors.message);
} else {
assert.equal(window.document.body.innerHTML, html, "root page should be redirected");
assert.equal(window.location.href, "http://127.0.0.1:8001/redir",
assert.equal(window.location.href, "http://127.0.0.1:8001/redir#hash",
"window.location.href should equal to redirected url");
}
t.done();
Expand Down

0 comments on commit 7f148e8

Please sign in to comment.