3
3
// META: script=/common/utils.js
4
4
// META: script=/common/get-host-info.sub.js
5
5
6
+ const duplex = "half" ;
7
+
6
8
function testUpload ( desc , url , method , createBody , expectedBody ) {
7
9
const requestInit = { method} ;
8
10
promise_test ( async ( ) => {
@@ -17,6 +19,17 @@ function testUpload(desc, url, method, createBody, expectedBody) {
17
19
} , desc ) ;
18
20
}
19
21
22
+ function createStream ( chunks ) {
23
+ return new ReadableStream ( {
24
+ start : ( controller ) => {
25
+ for ( const chunk of chunks ) {
26
+ controller . enqueue ( chunk ) ;
27
+ }
28
+ controller . close ( ) ;
29
+ }
30
+ } ) ;
31
+ }
32
+
20
33
const url = RESOURCES_DIR + "echo-content.h2.py"
21
34
22
35
testUpload ( "Fetch with POST with empty ReadableStream" , url ,
@@ -49,7 +62,7 @@ promise_test(async (test) => {
49
62
"/fetch/connection-pool/resources/network-partition-key.py?"
50
63
+ `status=421&uuid=${ token ( ) } &partition_id=${ self . origin } `
51
64
+ `&dispatch=check_partition&addcounter=true` ,
52
- { method : "POST" , body : body } ) ;
65
+ { method : "POST" , body : body , duplex } ) ;
53
66
assert_equals ( resp . status , 421 ) ;
54
67
const text = await resp . text ( ) ;
55
68
assert_equals ( text , "ok. Request was sent 1 times. 1 connections were created." ) ;
@@ -82,3 +95,44 @@ promise_test(async (test) => {
82
95
assert_equals ( await response . text ( ) , 'test' , `Response has correct body` ) ;
83
96
} , "Feature detect for POST with ReadableStream, using request object" ) ;
84
97
98
+ promise_test ( async ( t ) => {
99
+ const body = createStream ( [ "hello" ] ) ;
100
+ const method = "POST" ;
101
+ await promise_rejects_js ( t , TypeError , fetch ( url , { method, body, duplex } ) ) ;
102
+ } , "Streaming upload with body containing a String" ) ;
103
+
104
+ promise_test ( async ( t ) => {
105
+ const body = createStream ( [ null ] ) ;
106
+ const method = "POST" ;
107
+ await promise_rejects_js ( t , TypeError , fetch ( url , { method, body, duplex } ) ) ;
108
+ } , "Streaming upload with body containing null" ) ;
109
+
110
+ promise_test ( async ( t ) => {
111
+ const body = createStream ( [ 33 ] ) ;
112
+ const method = "POST" ;
113
+ await promise_rejects_js ( t , TypeError , fetch ( url , { method, body, duplex } ) ) ;
114
+ } , "Streaming upload with body containing a number" ) ;
115
+
116
+ promise_test ( async ( t ) => {
117
+ const url = "/fetch/api/resources/redirect.h2.py?location=/common/blank.html" ;
118
+ const body = createStream ( [ ] ) ;
119
+ const method = "POST" ;
120
+ await promise_rejects_js ( t , TypeError , fetch ( url , { method, body, duplex } ) ) ;
121
+ } , "Streaming upload should fail on redirect (302)" ) ;
122
+
123
+ promise_test ( async ( t ) => {
124
+ const url = "/fetch/api/resources/redirect.h2.py?" +
125
+ "redirect_status=303&location=/common/blank.html" ;
126
+ const body = createStream ( [ ] ) ;
127
+ const method = "POST" ;
128
+ const resp = await fetch ( url , { method, body, duplex } ) ;
129
+ assert_equals ( resp . status , 200 , 'status' ) ;
130
+ } , "Streaming upload should work with 303" ) ;
131
+
132
+ promise_test ( async ( t ) => {
133
+ const url = "/fetch/api/resources/authentication.py?realm=test" ;
134
+ const body = createStream ( [ ] ) ;
135
+ const method = "POST" ;
136
+ await promise_rejects_js ( t , TypeError , fetch ( url , { method, body, duplex } ) ) ;
137
+ } , "Streaming upload should fail on a 401 response" ) ;
138
+
0 commit comments