summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/fetch/api/response/response-init-contenttype.any.js
blob: 3a7744c28782c87f0a92cfaab17512ca0859b25c (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
test(() => {
  const response = new Response();
  assert_equals(response.headers.get("Content-Type"), null);
}, "Default Content-Type for Response with empty body");

test(() => {
  const blob = new Blob([]);
  const response = new Response(blob);
  assert_equals(response.headers.get("Content-Type"), null);
}, "Default Content-Type for Response with Blob body (no type set)");

test(() => {
  const blob = new Blob([], { type: "" });
  const response = new Response(blob);
  assert_equals(response.headers.get("Content-Type"), null);
}, "Default Content-Type for Response with Blob body (empty type)");

test(() => {
  const blob = new Blob([], { type: "a/b; c=d" });
  const response = new Response(blob);
  assert_equals(response.headers.get("Content-Type"), "a/b; c=d");
}, "Default Content-Type for Response with Blob body (set type)");

test(() => {
  const buffer = new Uint8Array();
  const response = new Response(buffer);
  assert_equals(response.headers.get("Content-Type"), null);
}, "Default Content-Type for Response with buffer source body");

promise_test(async () => {
  const formData = new FormData();
  formData.append("a", "b");
  const response = new Response(formData);
  const boundary = (await response.text()).split("\r\n")[0].slice(2);
  assert_equals(
    response.headers.get("Content-Type"),
    `multipart/form-data; boundary=${boundary}`,
  );
}, "Default Content-Type for Response with FormData body");

test(() => {
  const usp = new URLSearchParams();
  const response = new Response(usp);
  assert_equals(
    response.headers.get("Content-Type"),
    "application/x-www-form-urlencoded;charset=UTF-8",
  );
}, "Default Content-Type for Response with URLSearchParams body");

test(() => {
  const response = new Response("");
  assert_equals(
    response.headers.get("Content-Type"),
    "text/plain;charset=UTF-8",
  );
}, "Default Content-Type for Response with string body");

test(() => {
  const stream = new ReadableStream();
  const response = new Response(stream);
  assert_equals(response.headers.get("Content-Type"), null);
}, "Default Content-Type for Response with ReadableStream body");

// -----------------------------------------------------------------------------

const OVERRIDE_MIME = "test/only; mime=type";

function responseWithOverrideMime(body) {
  return new Response(
    body,
    { headers: { "Content-Type": OVERRIDE_MIME } },
  );
}

test(() => {
  const response = responseWithOverrideMime(undefined);
  assert_equals(response.headers.get("Content-Type"), OVERRIDE_MIME);
}, "Can override Content-Type for Response with empty body");

test(() => {
  const blob = new Blob([]);
  const response = responseWithOverrideMime(blob);
  assert_equals(response.headers.get("Content-Type"), OVERRIDE_MIME);
}, "Can override Content-Type for Response with Blob body (no type set)");

test(() => {
  const blob = new Blob([], { type: "" });
  const response = responseWithOverrideMime(blob);
  assert_equals(response.headers.get("Content-Type"), OVERRIDE_MIME);
}, "Can override Content-Type for Response with Blob body (empty type)");

test(() => {
  const blob = new Blob([], { type: "a/b; c=d" });
  const response = responseWithOverrideMime(blob);
  assert_equals(response.headers.get("Content-Type"), OVERRIDE_MIME);
}, "Can override Content-Type for Response with Blob body (set type)");

test(() => {
  const buffer = new Uint8Array();
  const response = responseWithOverrideMime(buffer);
  assert_equals(response.headers.get("Content-Type"), OVERRIDE_MIME);
}, "Can override Content-Type for Response with buffer source body");

test(() => {
  const formData = new FormData();
  const response = responseWithOverrideMime(formData);
  assert_equals(response.headers.get("Content-Type"), OVERRIDE_MIME);
}, "Can override Content-Type for Response with FormData body");

test(() => {
  const usp = new URLSearchParams();
  const response = responseWithOverrideMime(usp);
  assert_equals(response.headers.get("Content-Type"), OVERRIDE_MIME);
}, "Can override Content-Type for Response with URLSearchParams body");

test(() => {
  const response = responseWithOverrideMime("");
  assert_equals(response.headers.get("Content-Type"), OVERRIDE_MIME);
}, "Can override Content-Type for Response with string body");

test(() => {
  const stream = new ReadableStream();
  const response = responseWithOverrideMime(stream);
  assert_equals(response.headers.get("Content-Type"), OVERRIDE_MIME);
}, "Can override Content-Type for Response with ReadableStream body");