summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/fetch/api/body/mime-type.any.js
blob: 67c9af7da2dd539c219bc5848af45e6baaae65a9 (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
[
 () => new Request("about:blank", { headers: { "Content-Type": "text/plain" } }),
 () => new Response("", { headers: { "Content-Type": "text/plain" } })
].forEach(bodyContainerCreator => {
  const bodyContainer = bodyContainerCreator();
  promise_test(async t => {
    assert_equals(bodyContainer.headers.get("Content-Type"), "text/plain");
    const newMIMEType = "test/test";
    bodyContainer.headers.set("Content-Type", newMIMEType);
    const blob = await bodyContainer.blob();
    assert_equals(blob.type, newMIMEType);
  }, `${bodyContainer.constructor.name}: overriding explicit Content-Type`);
});

[
  () => new Request("about:blank", { body: new URLSearchParams(), method: "POST" }),
  () => new Response(new URLSearchParams()),
].forEach(bodyContainerCreator => {
  const bodyContainer = bodyContainerCreator();
  promise_test(async t => {
    assert_equals(bodyContainer.headers.get("Content-Type"), "application/x-www-form-urlencoded;charset=UTF-8");
    bodyContainer.headers.delete("Content-Type");
    const blob = await bodyContainer.blob();
    assert_equals(blob.type, "");
  }, `${bodyContainer.constructor.name}: removing implicit Content-Type`);
});

[
  () => new Request("about:blank", { body: new ArrayBuffer(), method: "POST" }),
  () => new Response(new ArrayBuffer()),
].forEach(bodyContainerCreator => {
  const bodyContainer = bodyContainerCreator();
  promise_test(async t => {
    assert_equals(bodyContainer.headers.get("Content-Type"), null);
    const newMIMEType = "test/test";
    bodyContainer.headers.set("Content-Type", newMIMEType);
    const blob = await bodyContainer.blob();
    assert_equals(blob.type, newMIMEType);
  }, `${bodyContainer.constructor.name}: setting missing Content-Type`);
});

[
  () => new Request("about:blank", { method: "POST" }),
  () => new Response(),
].forEach(bodyContainerCreator => {
  const bodyContainer = bodyContainerCreator();
  promise_test(async t => {
    const blob = await bodyContainer.blob();
    assert_equals(blob.type, "");
  }, `${bodyContainer.constructor.name}: MIME type for Blob from empty body`);
});

[
  () => new Request("about:blank", { method: "POST", headers: [["Content-Type", "Mytext/Plain"]] }),
  () => new Response("", { headers: [["Content-Type", "Mytext/Plain"]] })
].forEach(bodyContainerCreator => {
  const bodyContainer = bodyContainerCreator();
  promise_test(async t => {
    const blob = await bodyContainer.blob();
    assert_equals(blob.type, 'mytext/plain');
  }, `${bodyContainer.constructor.name}: MIME type for Blob from empty body with Content-Type`);
});

[
  () => new Request("about:blank", { body: new Blob([""]), method: "POST" }),
  () => new Response(new Blob([""]))
].forEach(bodyContainerCreator => {
  const bodyContainer = bodyContainerCreator();
  promise_test(async t => {
    const blob = await bodyContainer.blob();
    assert_equals(blob.type, "");
    assert_equals(bodyContainer.headers.get("Content-Type"), null);
  }, `${bodyContainer.constructor.name}: MIME type for Blob`);
});

[
  () => new Request("about:blank", { body: new Blob([""], { type: "Text/Plain" }), method: "POST" }),
  () => new Response(new Blob([""], { type: "Text/Plain" }))
].forEach(bodyContainerCreator => {
  const bodyContainer = bodyContainerCreator();
  promise_test(async t => {
    const blob = await bodyContainer.blob();
    assert_equals(blob.type, "text/plain");
    assert_equals(bodyContainer.headers.get("Content-Type"), "text/plain");
  }, `${bodyContainer.constructor.name}: MIME type for Blob with non-empty type`);
});

[
  () => new Request("about:blank", { method: "POST", body: new Blob([""], { type: "Text/Plain" }), headers: [["Content-Type", "Text/Html"]] }),
  () => new Response(new Blob([""], { type: "Text/Plain" }, { headers: [["Content-Type", "Text/Html"]] }))
].forEach(bodyContainerCreator => {
  const bodyContainer = bodyContainerCreator();
  const cloned = bodyContainer.clone();
  promise_test(async t => {
    const blobs = [await bodyContainer.blob(), await cloned.blob()];
    assert_equals(blobs[0].type, "text/html");
    assert_equals(blobs[1].type, "text/html");
    assert_equals(bodyContainer.headers.get("Content-Type"), "Text/Html");
    assert_equals(cloned.headers.get("Content-Type"), "Text/Html");
  }, `${bodyContainer.constructor.name}: Extract a MIME type with clone`);
});

[
  () => new Request("about:blank", { body: new Blob([], { type: "text/plain" }), method: "POST", headers: [["Content-Type", "text/html"]] }),
  () => new Response(new Blob([], { type: "text/plain" }), { headers: [["Content-Type", "text/html"]] }),
].forEach(bodyContainerCreator => {
  const bodyContainer = bodyContainerCreator();
  promise_test(async t => {
    assert_equals(bodyContainer.headers.get("Content-Type"), "text/html");
    const blob = await bodyContainer.blob();
    assert_equals(blob.type, "text/html");
  }, `${bodyContainer.constructor.name}: Content-Type in headers wins Blob"s type`);
});

[
  () => new Request("about:blank", { body: new Blob([], { type: "text/plain" }), method: "POST" }),
  () => new Response(new Blob([], { type: "text/plain" })),
].forEach(bodyContainerCreator => {
  const bodyContainer = bodyContainerCreator();
  promise_test(async t => {
    assert_equals(bodyContainer.headers.get("Content-Type"), "text/plain");
    const newMIMEType = "text/html";
    bodyContainer.headers.set("Content-Type", newMIMEType);
    const blob = await bodyContainer.blob();
    assert_equals(blob.type, newMIMEType);
  }, `${bodyContainer.constructor.name}: setting missing Content-Type in headers and it wins Blob"s type`);
});