summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/fetch/api/request/request-init-stream.any.js
blob: f0ae441a00258ddd37998387a347d801b4029cec (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
// META: global=window,worker

"use strict";

const duplex = "half";
const method = "POST";

test(() => {
  const body = new ReadableStream();
  const request = new Request("...", { method, body, duplex });
  assert_equals(request.body, body);
}, "Constructing a Request with a stream holds the original object.");

test((t) => {
  const body = new ReadableStream();
  body.getReader();
  assert_throws_js(TypeError,
                   () => new Request("...", { method, body, duplex }));
}, "Constructing a Request with a stream on which getReader() is called");

test((t) => {
  const body = new ReadableStream();
  body.getReader().read();
  assert_throws_js(TypeError,
                   () => new Request("...", { method, body, duplex }));
}, "Constructing a Request with a stream on which read() is called");

promise_test(async (t) => {
  const body = new ReadableStream({ pull: c => c.enqueue(new Uint8Array()) });
  const reader = body.getReader();
  await reader.read();
  reader.releaseLock();
  assert_throws_js(TypeError,
                   () => new Request("...", { method, body, duplex }));
}, "Constructing a Request with a stream on which read() and releaseLock() are called");

test((t) => {
  const request = new Request("...", { method: "POST", body: "..." });
  request.body.getReader();
  assert_throws_js(TypeError, () => new Request(request));
  // This doesn't throw.
  new Request(request, { body: "..." });
}, "Constructing a Request with a Request on which body.getReader() is called");

test((t) => {
  const request = new Request("...", { method: "POST", body: "..." });
  request.body.getReader().read();
  assert_throws_js(TypeError, () => new Request(request));
  // This doesn't throw.
  new Request(request, { body: "..." });
}, "Constructing a Request with a Request on which body.getReader().read() is called");

promise_test(async (t) => {
  const request = new Request("...", { method: "POST", body: "..." });
  const reader = request.body.getReader();
  await reader.read();
  reader.releaseLock();
  assert_throws_js(TypeError, () => new Request(request));
  // This doesn't throw.
  new Request(request, { body: "..." });
}, "Constructing a Request with a Request on which read() and releaseLock() are called");

test((t) => {
  new Request("...", { method, body: null });
}, "It is OK to omit .duplex when the body is null.");

test((t) => {
  new Request("...", { method, body: "..." });
}, "It is OK to omit .duplex when the body is a string.");

test((t) => {
  new Request("...", { method, body: new Uint8Array(3) });
}, "It is OK to omit .duplex when the body is a Uint8Array.");

test((t) => {
  new Request("...", { method, body: new Blob([]) });
}, "It is OK to omit .duplex when the body is a Blob.");

test((t) => {
  const body = new ReadableStream();
  assert_throws_js(TypeError,
                   () => new Request("...", { method, body }));
}, "It is error to omit .duplex when the body is a ReadableStream.");

test((t) => {
  new Request("...", { method, body: null, duplex: "half" });
}, "It is OK to set .duplex = 'half' when the body is null.");

test((t) => {
  new Request("...", { method, body: "...", duplex: "half" });
}, "It is OK to set .duplex = 'half' when the body is a string.");

test((t) => {
  new Request("...", { method, body: new Uint8Array(3), duplex: "half" });
}, "It is OK to set .duplex = 'half' when the body is a Uint8Array.");

test((t) => {
  new Request("...", { method, body: new Blob([]), duplex: "half" });
}, "It is OK to set .duplex = 'half' when the body is a Blob.");

test((t) => {
  const body = new ReadableStream();
  new Request("...", { method, body, duplex: "half" });
}, "It is OK to set .duplex = 'half' when the body is a ReadableStream.");

test((t) => {
  const body = null;
  const duplex = "full";
  assert_throws_js(TypeError,
                   () => new Request("...", { method, body, duplex }));
}, "It is error to set .duplex = 'full' when the body is null.");

test((t) => {
  const body = "...";
  const duplex = "full";
  assert_throws_js(TypeError,
                   () => new Request("...", { method, body, duplex }));
}, "It is error to set .duplex = 'full' when the body is a string.");

test((t) => {
  const body = new Uint8Array(3);
  const duplex = "full";
  assert_throws_js(TypeError,
                   () => new Request("...", { method, body, duplex }));
}, "It is error to set .duplex = 'full' when the body is a Uint8Array.");

test((t) => {
  const body = new Blob([]);
  const duplex = "full";
  assert_throws_js(TypeError,
                   () => new Request("...", { method, body, duplex }));
}, "It is error to set .duplex = 'full' when the body is a Blob.");

test((t) => {
  const body = new ReadableStream();
  const duplex = "full";
  assert_throws_js(TypeError,
                   () => new Request("...", { method, body, duplex }));
}, "It is error to set .duplex = 'full' when the body is a ReadableStream.");

test((t) => {
  const body = new ReadableStream();
  const duplex = "half";
  const req1 = new Request("...", { method, body, duplex });
  const req2 = new Request(req1);
}, "It is OK to omit duplex when init.body is not given and input.body is given.");