Debian Codegraph @

testing/web-platform/mozilla/tests/html/semantics/forms/form-submission-0/non-usv-filenames.window.js

raw
1 // META: script=/html/semantics/forms/form-submission-0/enctypes-helper.js
2
3 // This test is built on the same infrastructure as the WPT tests
4 // urlencoded2.window.js, multipart-formdata.window.js and text-plain.window.js,
5 // except modified because this file only tests the serialization of filenames.
6 // See the enctypes-helper.js file in the regular WPT test suite for more info.
7
8 // The `urlencoded`, `multipart` and `textPlain` functions take a `file`
9 // property rather than `name` and `value` properties, and the value of
10 // `expected` is the serialization of the filename in the given encoding.
11
12 function formSubmissionTemplate2(enctype, expectedBuilder) {
13 const formTestFn = formSubmissionTemplate(enctype, expectedBuilder);
14 return ({ file, formEncoding, expected, description }) =>
15 formTestFn({ name: "a", value: file, formEncoding, expected, description });
16 }
17
18 const urlencoded = formSubmissionTemplate2(
19 "application/x-www-form-urlencoded",
20 filename => `a=${filename}`
21 );
22 const multipart = formSubmissionTemplate2(
23 "multipart/form-data",
24 (filename, serialized) => {
25 const boundary = serialized.split("\r\n")[0];
26 return [
27 boundary,
28 `Content-Disposition: form-data; name="a"; filename="${filename}"`,
29 "Content-Type: text/plain",
30 "",
31 "", // File contents
32 `${boundary}--`,
33 "",
34 ].join("\r\n");
35 }
36 );
37 const textPlain = formSubmissionTemplate2(
38 "text/plain",
39 filename => `a=${filename}\r\n`
40 );
41
42 // -----------------------------------------------------------------------------
43
44 (async () => {
45 // This creates an empty filesystem file with an arbitrary name and returns it
46 // as a File object with name "a\uD800b".
47 const file = SpecialPowers.unwrap(
48 await SpecialPowers.createFiles(
49 [{ data: "", options: { name: "a\uD800b", type: "text/plain" } }],
50 files => files[0]
51 )
52 );
53
54 urlencoded({
55 file,
56 formEncoding: "UTF-8",
57 expected: "a%EF%BF%BDb",
58 description: "lone surrogate in filename, UTF-8",
59 });
60
61 urlencoded({
62 file,
63 formEncoding: "windows-1252",
64 expected: "a%26%2365533%3Bb",
65 description: "lone surrogate in filename, windows-1252",
66 });
67
68 multipart({
69 file,
70 formEncoding: "UTF-8",
71 expected: "a\xEF\xBF\xBDb",
72 description: "lone surrogate in filename, UTF-8",
73 });
74
75 multipart({
76 file,
77 formEncoding: "windows-1252",
78 expected: "a�b",
79 description: "lone surrogate in filename, windows-1252",
80 });
81
82 textPlain({
83 file,
84 formEncoding: "UTF-8",
85 expected: "a\xEF\xBF\xBDb",
86 description: "lone surrogate in filename, UTF-8",
87 });
88
89 textPlain({
90 file,
91 formEncoding: "windows-1252",
92 expected: "a�b",
93 description: "lone surrogate in filename, windows-1252",
94 });
95 })();