Skip to content

Commit

Permalink
Port FormData tests from internal to workerd (cloudflare#1355)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasnell authored Oct 27, 2023
1 parent 7448549 commit 875a517
Show file tree
Hide file tree
Showing 4 changed files with 949 additions and 0 deletions.
58 changes: 58 additions & 0 deletions src/workerd/api/tests/form-data-legacy-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
export const sendFilesInFormdata = {
async test() {
const INPUT = `--2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a
Content-Disposition: form-data; name="foo"; filename="foo.txt"
Content-Type: application/octet-stream
foo-content
--2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a
Content-Disposition: form-data; name="bar"; filename="bar-renamed.txt"
Content-Type: application/octet-stream
bar1-content
--2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a
Content-Disposition: form-data; name="bar"; filename="bar2.txt"
Content-Type: text/bary
bar2-content
--2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a
Content-Disposition: form-data; name="baz"; filename="baz"
Content-Type: text/bazzy
baz-content
--2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a
Content-Disposition: form-data; name="qux"; filename="qux%0A%22\\.txt"
Content-Type: application/octet-stream
qux-content
--2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a--
`;

const req = new Request('https://example.org', {
method: 'POST',
body: INPUT,
headers: {
'content-type': 'multipart/form-data;boundary=2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a'
}
});

const form = await req.formData();

form.set("foo", new File(["foo-content"], "foo.txt"));
form.append("bar", new File(["bar1-content"], "bar1.txt"), "bar-renamed.txt");
form.append("bar", new File(["bar2-content"], "bar2.txt", {type: "text/bary"}));
form.append("baz", new Blob(["baz-content"], {type: "text/bazzy"}));
form.set("qux", new Blob(["qux-content"]), "qux\n\"\\.txt");

{
let resp = new Response(form);
let text = await resp.text();
let roundtrip = await new Response(text, {headers: resp.headers}).formData();
if (roundtrip.get("foo") != "foo-content") {
throw new Error("expected round-trip turns into string (wrong, but backwards-compatible)");
}
}

}
};

19 changes: 19 additions & 0 deletions src/workerd/api/tests/form-data-legacy-test.wd-test
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Workerd = import "/workerd/workerd.capnp";

const unitTestsLegacy :Workerd.Config = (
services = [
( name = "form-data-legacy-test",
worker = (
modules = [
(name = "worker", esModule = embed "form-data-legacy-test.js")
],
compatibilityDate = "2023-01-15",
compatibilityFlags = [
"nodejs_compat",
"formdata_parser_converts_files_to_strings"
],
)
),
],
);

Loading

0 comments on commit 875a517

Please sign in to comment.