Skip to content

Commit

Permalink
Recursively prepare fetch headers
Browse files Browse the repository at this point in the history
Closes hotwired#86
Closes hotwired#110

Recursively walk the tree of dependent FetchRequestDelegates so that
they all have an opportunity to modify the fetch request headers.

Testing adds listeners for `turbo:before-fetch-request` and
`turbo:before-fetch-response` so that the event logs can drain.

Co-authored-by: tleish <tleish@users.noreply.github.com>
  • Loading branch information
seanpdoyle and tleish committed Feb 7, 2021
1 parent 57a118e commit 1fa60a6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/http/fetch_request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,12 @@ export class FetchRequest {

get headers() {
const headers = { ...this.defaultHeaders }
if (typeof this.delegate.prepareHeadersForRequest == "function") {
this.delegate.prepareHeadersForRequest(headers, this)

let delegate: any = this.delegate;
while(delegate = delegate.delegate) {
if (typeof delegate.prepareHeadersForRequest == "function") {
delegate.prepareHeadersForRequest(headers, this);
}
}
return headers
}
Expand Down
5 changes: 3 additions & 2 deletions src/tests/fixtures/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"turbo:before-visit",
"turbo:load",
"turbo:render",
"turbo:request-end",
"turbo:visit"
"turbo:visit",
"turbo:before-fetch-request",
"turbo:before-fetch-response",
])
16 changes: 16 additions & 0 deletions src/tests/functional/form_submission_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,14 @@ export class FormSubmissionTests extends TurboDriveTestCase {
this.assert.equal(htmlAfter, htmlBefore)
}

async "test frame form submission within a frame submits the Turbo-Frame header"() {
await this.clickSelector("#frame form.redirect input[type=submit]")

const { fetchOptions } = await this.nextEventNamed("turbo:before-fetch-request")

this.assert.ok(fetchOptions.headers["Turbo-Frame"], "submits with the Turbo-Frame header")
}

async "test invalid frame form submission with unprocessable entity status"() {
await this.clickSelector("#frame form.unprocessable_entity input[type=submit]")
await this.nextBeat
Expand Down Expand Up @@ -260,6 +268,14 @@ export class FormSubmissionTests extends TurboDriveTestCase {
this.assert.equal(await this.pathname, "/src/tests/fixtures/one.html")
}

async "test form submission targeting a frame submits the Turbo-Frame header"() {
await this.clickSelector('#targets-frame [type="submit"]')

const { fetchOptions } = await this.nextEventNamed("turbo:before-fetch-request")

this.assert.ok(fetchOptions.headers["Turbo-Frame"], "submits with the Turbo-Frame header")
}

get formSubmitted(): Promise<boolean> {
return this.hasSelector("html[data-form-submitted]")
}
Expand Down

0 comments on commit 1fa60a6

Please sign in to comment.