Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reproduce redirect to file download fails in turbo frame #1348

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/tests/fixtures/frames.html
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ <h2>Frames: #nested-child</h2>
<a id="missing-frame-link" href="/src/tests/fixtures/frames/frame.html">Missing frame</a>
<a id="missing-page-link" href="/missing.html">Missing page</a>
<a id="unvisitable-page-link" href="/src/tests/fixtures/frames/unvisitable.html">Unvisitable page</a>
<a id="non-html" href="/src/tests/fixtures/svg.svg">Non-HTML</a>
<a id="unknown-non-html" href="/__turbo/file.unknown_svg">Unknown non-HTML</a>
<a id="redirect-to-non-html" href="/__turbo/redirect?path=/src/tests/fixtures/svg.svg">Redirect to non-HTML</a>
<a id="redirect-to-unknown-non-html" href="/__turbo/redirect?path=/__turbo/file.unknown_svg">Redirect to unknown non-HTML</a>
</turbo-frame>

<turbo-frame id="body-script" target="body-script">
Expand Down
110 changes: 109 additions & 1 deletion src/tests/functional/frame_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import {
readEventLogs,
scrollPosition,
scrollToSelector,
searchParams
searchParams,
visitAction
} from "../helpers/page"

assert.equalIgnoringWhitespace = function (actual, expected, message) {
Expand Down Expand Up @@ -169,6 +170,113 @@ test("successfully following a link to a page without a matching frame shows an
assert.include(error.message, `The response (200) did not contain the expected <turbo-frame id="missing">`)
})

test("following a link to a non html file changes page src", async ({ page }) => {
await page.goto("/src/tests/fixtures/frames.html")

const requestedUrls = []
page.on('request', (req) => { requestedUrls.push([req.resourceType(), req.url()]) })

const urlBeforeVisit = page.url()

await page.click("#non-html")
await nextBeat()

assert.deepEqual(requestedUrls, [
["document", "http://localhost:9000/src/tests/fixtures/svg.svg"]
])

const urlAfterVisit = page.url()
assert.notEqual(urlBeforeVisit, urlAfterVisit)
assert.equal(await visitAction(page), "load")
})

test("following a link to an unknown non html file changes page src", async ({ page }) => {
await page.goto("/src/tests/fixtures/frames.html")

const requestedUrls = []
page.on('request', (req) => { requestedUrls.push([req.resourceType(), req.url()]) })

const urlBeforeVisit = page.url()

await page.click("#unknown-non-html")
await nextBeat()

// Because the file extension is not a known extension, Turbo will request it first to
// determine the content type and only then refresh the full page to the provided location
assert.deepEqual(requestedUrls, [
["fetch", "http://localhost:9000/__turbo/file.unknown_svg"],
["document", "http://localhost:9000/__turbo/file.unknown_svg"]
])

const urlAfterVisit = page.url()
assert.notEqual(urlBeforeVisit, urlAfterVisit)
assert.equal(await visitAction(page), "load")
})

test("following a link that redirects to a non html file changes page src", async ({ page }) => {
await page.goto("/src/tests/fixtures/frames.html")

const requestedUrls = []
page.on('request', (req) => { requestedUrls.push([req.resourceType(), req.url()]) })

const urlBeforeVisit = page.url()

await page.click("#redirect-to-non-html")
await nextBeat() // 302
await nextBeat() // 200

assert.deepEqual(requestedUrls, [
[
"fetch", "http://localhost:9000/__turbo/redirect?path=%2Fsrc%2Ftests%2Ffixtures%2Fsvg.svg"
],
[
"fetch", "http://localhost:9000/src/tests/fixtures/svg.svg"
],
[
"document", "http://localhost:9000/__turbo/redirect?path=/src/tests/fixtures/svg.svg"
],
[
"document", "http://localhost:9000/src/tests/fixtures/svg.svg"
]
])

const urlAfterVisit = page.url()
assert.notEqual(urlBeforeVisit, urlAfterVisit)
assert.equal(await visitAction(page), "load")
})

test("following a link that redirects to an unknown non html file changes page src", async ({ page }) => {
await page.goto("/src/tests/fixtures/frames.html")

const requestedUrls = []
page.on('request', (req) => { requestedUrls.push([req.resourceType(), req.url()]) })

const urlBeforeVisit = page.url()

await page.click("#redirect-to-unknown-non-html")
await nextBeat() // 302
await nextBeat() // 200

assert.deepEqual(requestedUrls, [
[
"fetch", "http://localhost:9000/__turbo/redirect?path=%2F__turbo%2Ffile.unknown_svg"
],
[
"fetch", "http://localhost:9000/__turbo/file.unknown_svg"
],
[
"document", "http://localhost:9000/__turbo/redirect?path=/__turbo/file.unknown_svg"
],
[
"document", "http://localhost:9000/__turbo/file.unknown_svg"
]
])

const urlAfterVisit = page.url()
assert.notEqual(urlBeforeVisit, urlAfterVisit)
assert.equal(await visitAction(page), "load")
})

test("successfully following a link to a page with `turbo-visit-control` `reload` performs a full page reload", async ({
page
}) => {
Expand Down
62 changes: 62 additions & 0 deletions src/tests/functional/visit_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,68 @@ test("visiting a location served with an unknown non-HTML content type", async (
assert.equal(await visitAction(page), "load")
})

test("being redirected to a location served with a known non-HTML content type", async ({ page }) => {
const requestedUrls = []
page.on('request', (req) => { requestedUrls.push([req.resourceType(), req.url()]) })

const urlBeforeVisit = page.url()
await visitLocation(page, "/__turbo/redirect?path=/src/tests/fixtures/svg.svg")
await nextBeat() // 302
await nextBeat() // 200

const url = page.url()
const contentType = await contentTypeOfURL(url)
assert.equal(contentType, "image/svg+xml")

assert.deepEqual(requestedUrls, [
[
"fetch", "http://localhost:9000/__turbo/redirect?path=%2Fsrc%2Ftests%2Ffixtures%2Fsvg.svg"
],
[
"fetch", "http://localhost:9000/src/tests/fixtures/svg.svg"
],
[
"document", "http://localhost:9000/__turbo/redirect?path=/src/tests/fixtures/svg.svg"
],
[
"document", "http://localhost:9000/src/tests/fixtures/svg.svg"
]
])

const urlAfterVisit = page.url()
assert.notEqual(urlBeforeVisit, urlAfterVisit)
assert.equal(await visitAction(page), "load")
})

test("being redirected to a location served with an unknown non-HTML content type", async ({ page }) => {
const requestedUrls = []
page.on('request', (req) => { requestedUrls.push([req.resourceType(), req.url()]) })

const urlBeforeVisit = page.url()
await visitLocation(page, "/__turbo/redirect?path=/__turbo/file.unknown_svg")
await nextBeat() // 302
await nextBeat() // 200

assert.deepEqual(requestedUrls, [
[
"fetch", "http://localhost:9000/__turbo/redirect?path=%2F__turbo%2Ffile.unknown_svg"
],
[
"fetch", "http://localhost:9000/__turbo/file.unknown_svg"
],
[
"document", "http://localhost:9000/__turbo/redirect?path=/__turbo/file.unknown_svg"
],
[
"document", "http://localhost:9000/__turbo/file.unknown_svg"
]
])

const urlAfterVisit = page.url()
assert.notEqual(urlBeforeVisit, urlAfterVisit)
assert.equal(await visitAction(page), "load")
})

test("visiting a location served with an unknown non-HTML content type added to the unvisitableExtensions set", async ({ page }) => {
const requestedUrls = []
page.on('request', (req) => { requestedUrls.push([req.resourceType(), req.url()]) })
Expand Down