summaryrefslogtreecommitdiff
path: root/tests/unit/body_test.ts
blob: 18cdb22be0b089d9de8bd95485fbe5cf51e2dacc (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { assert, assertEquals } from "./test_util.ts";

// just a hack to get a body object
// deno-lint-ignore no-explicit-any
function buildBody(body: any, headers?: Headers): Body {
  const stub = new Request("http://foo/", {
    body: body,
    headers,
    method: "POST",
  });
  return stub as Body;
}

const intArrays = [
  Int8Array,
  Int16Array,
  Int32Array,
  Uint8Array,
  Uint16Array,
  Uint32Array,
  Uint8ClampedArray,
  Float32Array,
  Float64Array,
];
Deno.test(async function arrayBufferFromByteArrays() {
  const buffer = new TextEncoder().encode("ahoyhoy8").buffer;

  for (const type of intArrays) {
    const body = buildBody(new type(buffer));
    const text = new TextDecoder("utf-8").decode(await body.arrayBuffer());
    assertEquals(text, "ahoyhoy8");
  }
});

//FormData
Deno.test(
  { permissions: { net: true } },
  async function bodyMultipartFormData() {
    const response = await fetch(
      "http://localhost:4545/multipart_form_data.txt",
    );
    assert(response.body instanceof ReadableStream);

    const text = await response.text();

    const body = buildBody(text, response.headers);

    const formData = await body.formData();
    assert(formData.has("field_1"));
    assertEquals(formData.get("field_1")!.toString(), "value_1 \r\n");
    assert(formData.has("field_2"));
  },
);

// FormData: non-ASCII names and filenames
Deno.test(
  { permissions: { net: true } },
  async function bodyMultipartFormDataNonAsciiNames() {
    const boundary = "----01230123";
    const payload = [
      `--${boundary}`,
      `Content-Disposition: form-data; name="文字"`,
      "",
      "文字",
      `--${boundary}`,
      `Content-Disposition: form-data; name="file"; filename="文字"`,
      "Content-Type: application/octet-stream",
      "",
      "",
      `--${boundary}--`,
    ].join("\r\n");

    const body = buildBody(
      new TextEncoder().encode(payload),
      new Headers({
        "Content-Type": `multipart/form-data; boundary=${boundary}`,
      }),
    );

    const formData = await body.formData();
    assert(formData.has("文字"));
    assertEquals(formData.get("文字"), "文字");
    assert(formData.has("file"));
    assert(formData.get("file") instanceof File);
    assertEquals((formData.get("file") as File).name, "文字");
  },
);

// FormData: non-ASCII names and filenames roundtrip
Deno.test(
  { permissions: { net: true } },
  async function bodyMultipartFormDataNonAsciiRoundtrip() {
    const inFormData = new FormData();
    inFormData.append("文字", "文字");
    inFormData.append("file", new File([], "文字"));

    const body = buildBody(inFormData);

    const formData = await body.formData();
    assert(formData.has("文字"));
    assertEquals(formData.get("文字"), "文字");
    assert(formData.has("file"));
    assert(formData.get("file") instanceof File);
    assertEquals((formData.get("file") as File).name, "文字");
  },
);

Deno.test(
  { permissions: { net: true } },
  async function bodyURLEncodedFormData() {
    const response = await fetch(
      "http://localhost:4545/subdir/form_urlencoded.txt",
    );
    assert(response.body instanceof ReadableStream);

    const text = await response.text();

    const body = buildBody(text, response.headers);

    const formData = await body.formData();
    assert(formData.has("field_1"));
    assertEquals(formData.get("field_1")!.toString(), "Hi");
    assert(formData.has("field_2"));
    assertEquals(formData.get("field_2")!.toString(), "<Deno>");
  },
);

Deno.test({ permissions: {} }, async function bodyURLSearchParams() {
  const body = buildBody(new URLSearchParams({ hello: "world" }));

  const text = await body.text();
  assertEquals(text, "hello=world");
});

Deno.test(async function bodyArrayBufferMultipleParts() {
  const parts: Uint8Array[] = [];
  let size = 0;
  for (let i = 0; i <= 150000; i++) {
    const part = new Uint8Array([1]);
    parts.push(part);
    size += part.length;
  }

  let offset = 0;
  const stream = new ReadableStream({
    pull(controller) {
      // parts.shift() takes forever: https://github.com/denoland/deno/issues/5259
      const chunk = parts[offset++];
      if (!chunk) return controller.close();
      controller.enqueue(chunk);
    },
  });

  const body = buildBody(stream);
  assertEquals((await body.arrayBuffer()).byteLength, size);
});

// https://github.com/denoland/deno/issues/20793
Deno.test(
  { permissions: { net: true } },
  async function bodyMultipartFormDataMultipleHeaders() {
    const boundary = "----formdata-polyfill-0.970665446687947";
    const payload = [
      "------formdata-polyfill-0.970665446687947",
      'Content-Disposition: form-data; name="x"; filename="blob"',
      "Content-Length: 1",
      "Content-Type: application/octet-stream",
      "last-modified: Wed, 04 Oct 2023 20:28:45 GMT",
      "",
      "y",
      "------formdata-polyfill-0.970665446687947--",
    ].join("\r\n");

    const body = buildBody(
      new TextEncoder().encode(payload),
      new Headers({
        "Content-Type": `multipart/form-data; boundary=${boundary}`,
      }),
    );

    const formData = await body.formData();
    const file = formData.get("x");
    assert(file instanceof File);
    const text = await file.text();
    assertEquals(text, "y");
    assertEquals(file.size, 1);
  },
);