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

fix: handle multipart/form-data submissions #8984

Merged
merged 2 commits into from
Jun 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/brave-shirts-sneeze.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/router": patch
---

fix: properly handle `<Form encType="multipart/form-data">` submissions (#8984)
38 changes: 36 additions & 2 deletions packages/router/__tests__/router-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3868,7 +3868,7 @@ describe("a router", () => {
expect(request.url).toBe("http://localhost/tasks");
expect(request.method).toBe("POST");
expect(request.headers.get("Content-Type")).toBe(
"application/x-www-form-urlencoded"
"application/x-www-form-urlencoded;charset=UTF-8"
);
expect((await request.formData()).get("query")).toBe("params");
});
Expand Down Expand Up @@ -3901,10 +3901,44 @@ describe("a router", () => {
expect(request.url).toBe("http://localhost/tasks?foo=bar");
expect(request.method).toBe("POST");
expect(request.headers.get("Content-Type")).toBe(
"application/x-www-form-urlencoded"
"application/x-www-form-urlencoded;charset=UTF-8"
);
expect((await request.formData()).get("query")).toBe("params");
});

it("handles multipart/form-data submissions", async () => {
let t = setup({
routes: [
{
id: "root",
path: "/",
action: true,
},
],
initialEntries: ["/"],
hydrationData: {
loaderData: {
root: "ROOT_DATA",
},
},
});

let fd = new FormData();
fd.append("key", "value");
fd.append("file", new Blob(["1", "2", "3"]), "file.txt");

let A = await t.navigate("/", {
formMethod: "post",
formEncType: "multipart/form-data",
formData: fd,
});

expect(
A.actions.root.stub.mock.calls[0][0].request.headers.get("Content-Type")
).toMatch(
/^multipart\/form-data; boundary=NodeFetchFormDataBoundary[a-z0-9]+/
);
});
});

describe("scroll restoration", () => {
Expand Down
4 changes: 1 addition & 3 deletions packages/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1846,11 +1846,9 @@ function createRequest(
}
}

// Content-Type is inferred (https://fetch.spec.whatwg.org/#dom-request)
return new Request(url, {
method: formMethod.toUpperCase(),
headers: {
"Content-Type": formEncType,
},
body,
});
}
Expand Down
4 changes: 3 additions & 1 deletion packages/router/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { parsePath } from "./history";
import { DataResult, DataRouteMatch } from "./router";

export type FormMethod = "get" | "post" | "put" | "patch" | "delete";
export type FormEncType = "application/x-www-form-urlencoded";
export type FormEncType =
| "application/x-www-form-urlencoded"
| "multipart/form-data";

/**
* @private
Expand Down