summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/FileAPI/support/send-file-formdata-helper.js
blob: 53c8cca7e09b8e8c039828ebcdcb21293fb2699e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
"use strict";

const kTestChars = "ABC~‾¥≈¤・・•∙·☼★星🌟星★☼·∙•・・¤≈¥‾~XYZ";

// formDataPostFileUploadTest - verifies multipart upload structure and
// numeric character reference replacement for filenames, field names,
// and field values using FormData and fetch().
//
// Uses /fetch/api/resources/echo-content.py to echo the upload
// POST (unlike in send-file-form-helper.js, here we expect all
// multipart/form-data request bodies to be UTF-8, so we don't need to
// escape controls and non-ASCII bytes).
//
// Fields in the parameter object:
//
// - fileNameSource: purely explanatory and gives a clue about which
//   character encoding is the source for the non-7-bit-ASCII parts of
//   the fileBaseName, or Unicode if no smaller-than-Unicode source
//   contains all the characters. Used in the test name.
// - fileBaseName: the not-necessarily-just-7-bit-ASCII file basename
//   used for the constructed test file. Used in the test name.
const formDataPostFileUploadTest = ({
  fileNameSource,
  fileBaseName,
}) => {
  promise_test(async (testCase) => {
    const formData = new FormData();
    let file = new Blob([kTestChars], { type: "text/plain" });
    try {
      // Switch to File in browsers that allow this
      file = new File([file], fileBaseName, { type: file.type });
    } catch (ignoredException) {
    }

    // Used to verify that the browser agrees with the test about
    // field value replacement and encoding independently of file system
    // idiosyncracies.
    formData.append("filename", fileBaseName);

    // Same, but with name and value reversed to ensure field names
    // get the same treatment.
    formData.append(fileBaseName, "filename");

    formData.append("file", file, fileBaseName);

    const formDataText = await (await fetch(
      `/fetch/api/resources/echo-content.py`,
      {
        method: "POST",
        body: formData,
      },
    )).text();
    const formDataLines = formDataText.split("\r\n");
    if (formDataLines.length && !formDataLines[formDataLines.length - 1]) {
      --formDataLines.length;
    }
    assert_greater_than(
      formDataLines.length,
      2,
      `${fileBaseName}: multipart form data must have at least 3 lines: ${
        JSON.stringify(formDataText)
      }`,
    );
    const boundary = formDataLines[0];
    assert_equals(
      formDataLines[formDataLines.length - 1],
      boundary + "--",
      `${fileBaseName}: multipart form data must end with ${boundary}--: ${
        JSON.stringify(formDataText)
      }`,
    );

    const asValue = fileBaseName.replace(/\r\n?|\n/g, "\r\n");
    const asName = asValue.replace(/[\r\n"]/g, encodeURIComponent);
    const asFilename = fileBaseName.replace(/[\r\n"]/g, encodeURIComponent);
    const expectedText = [
      boundary,
      'Content-Disposition: form-data; name="filename"',
      "",
      asValue,
      boundary,
      `Content-Disposition: form-data; name="${asName}"`,
      "",
      "filename",
      boundary,
      `Content-Disposition: form-data; name="file"; ` +
      `filename="${asFilename}"`,
      "Content-Type: text/plain",
      "",
      kTestChars,
      boundary + "--",
    ].join("\r\n");

    assert_true(
      formDataText.startsWith(expectedText),
      `Unexpected multipart-shaped form data received:\n${formDataText}\nExpected:\n${expectedText}`,
    );
  }, `Upload ${fileBaseName} (${fileNameSource}) in fetch with FormData`);
};