Skip to content
This repository has been archived by the owner on Dec 16, 2023. It is now read-only.

Commit

Permalink
Merge pull request #1090 from djanowski/meta-refresh-timeout
Browse files Browse the repository at this point in the history
Honor timeout in refresh <meta> tag.
  • Loading branch information
assaf committed Nov 9, 2016
2 parents 1380008 + 13660e0 commit 0a18561
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 14 deletions.
35 changes: 21 additions & 14 deletions src/document.js
Expand Up @@ -501,8 +501,8 @@ function createDocument(args) {
}


// Get refresh URL from <meta> tag
function getMetaRefreshURL(document) {
// Get refresh URL and timeout from <meta> tag
function getMetaRefresh(document) {
const refresh = document.querySelector('meta[http-equiv="refresh"]');
if (refresh) {
const content = refresh.getAttribute('content');
Expand All @@ -511,7 +511,7 @@ function getMetaRefreshURL(document) {
const refreshTimeout = parseInt(match[1], 10);
const refreshURL = match[2] || document.location.href;
if (refreshTimeout >= 0)
return refreshURL;
return { refreshTimeout, refreshURL };
}
}
return null;
Expand Down Expand Up @@ -633,20 +633,27 @@ function parseResponse({ browser, history, document, response }) {
// return browser.query('meta[http-equiv="refresh"]');
// }
// });
const refreshURL = getMetaRefreshURL(document);
if (refreshURL)
// Allow completion function to run
window._eventQueue.enqueue(function() {
const refresh = getMetaRefresh(document);

if (refresh) {
const { refreshTimeout } = refresh;
const { refreshURL } = refresh;

window._eventQueue.setTimeout(function() {
// Allow completion function to run
window._eventQueue.enqueue(function() {
// Count a meta-refresh in the redirects count.
history.replace(refreshURL || document.location.href);
// This results in a new window getting loaded
const newWindow = history.current.window;
newWindow.addEventListener('load', function() {
++newWindow._request._redirectCount;
window._eventQueue.enqueue(function() {
// Count a meta-refresh in the redirects count.
history.replace(refreshURL);
// This results in a new window getting loaded
const newWindow = history.current.window;
newWindow.addEventListener('load', function() {
++newWindow._request._redirectCount;
});
});
});
});
}, refreshTimeout * 1000);
}

})
.catch(function(error) {
Expand Down
45 changes: 45 additions & 0 deletions test/window_test.js
Expand Up @@ -341,6 +341,51 @@ describe('Window', function() {
});


describe('refresh with timeout', function() {
before(function() {
brains.static('/windows/foo', `
<html>
<title>Foo</title>
</html>
`);
brains.static('/windows/bar', `
<html>
<title>Bar</title>
</html>
`);
brains.get('/windows/refresh-with-timeout', function(req, res) {
res.send(`
<html>
<head>
<title>Refresh</title>
<meta http-equiv="refresh" content="1; url=/windows/foo">
</head>
<body>
<script>
setTimeout(function() {
window.location = '/windows/bar';
}, 10);
</script>
</body>
</html>
`);
});
});


it('should honor the timeout', async function() {
await browser.visit('/windows/refresh-with-timeout');
browser.assert.url('/windows/bar');
});


afterEach(function() {
browser.deleteCookies();
});

});


describe('resize', function() {
it('should change window dimensions', function() {
const window = browser.open();
Expand Down

0 comments on commit 0a18561

Please sign in to comment.