request-upload.any.js (4190B)
1 // META: global=window,worker 2 // META: script=../resources/utils.js 3 // META: script=/common/utils.js 4 // META: script=/common/get-host-info.sub.js 5 6 function testUpload(desc, url, method, createBody, expectedBody) { 7 const requestInit = {method}; 8 promise_test(function(test){ 9 const body = createBody(); 10 if (body) { 11 requestInit["body"] = body; 12 requestInit.duplex = "half"; 13 } 14 return fetch(url, requestInit).then(function(resp) { 15 return resp.text().then((text)=> { 16 assert_equals(text, expectedBody); 17 }); 18 }); 19 }, desc); 20 } 21 22 function testUploadFailure(desc, url, method, createBody) { 23 const requestInit = {method}; 24 promise_test(t => { 25 const body = createBody(); 26 if (body) { 27 requestInit["body"] = body; 28 } 29 return promise_rejects_js(t, TypeError, fetch(url, requestInit)); 30 }, desc); 31 } 32 33 const url = RESOURCES_DIR + "echo-content.py" 34 35 testUpload("Fetch with PUT with body", url, 36 "PUT", 37 () => "Request's body", 38 "Request's body"); 39 testUpload("Fetch with POST with text body", url, 40 "POST", 41 () => "Request's body", 42 "Request's body"); 43 testUpload("Fetch with POST with URLSearchParams body", url, 44 "POST", 45 () => new URLSearchParams("name=value"), 46 "name=value"); 47 testUpload("Fetch with POST with Blob body", url, 48 "POST", 49 () => new Blob(["Test"]), 50 "Test"); 51 testUpload("Fetch with POST with ArrayBuffer body", url, 52 "POST", 53 () => new ArrayBuffer(4), 54 "\0\0\0\0"); 55 testUpload("Fetch with POST with Uint8Array body", url, 56 "POST", 57 () => new Uint8Array(4), 58 "\0\0\0\0"); 59 testUpload("Fetch with POST with Int8Array body", url, 60 "POST", 61 () => new Int8Array(4), 62 "\0\0\0\0"); 63 testUpload("Fetch with POST with Float16Array body", url, 64 "POST", 65 () => new Float16Array(2), 66 "\0\0\0\0"); 67 testUpload("Fetch with POST with Float32Array body", url, 68 "POST", 69 () => new Float32Array(1), 70 "\0\0\0\0"); 71 testUpload("Fetch with POST with Float64Array body", url, 72 "POST", 73 () => new Float64Array(1), 74 "\0\0\0\0\0\0\0\0"); 75 testUpload("Fetch with POST with DataView body", url, 76 "POST", 77 () => new DataView(new ArrayBuffer(8), 0, 4), 78 "\0\0\0\0"); 79 testUpload("Fetch with POST with Blob body with mime type", url, 80 "POST", 81 () => new Blob(["Test"], { type: "text/maybe" }), 82 "Test"); 83 84 testUploadFailure("Fetch with POST with ReadableStream containing String", url, 85 "POST", 86 () => { 87 return new ReadableStream({start: controller => { 88 controller.enqueue("Test"); 89 controller.close(); 90 }}) 91 }); 92 testUploadFailure("Fetch with POST with ReadableStream containing null", url, 93 "POST", 94 () => { 95 return new ReadableStream({start: controller => { 96 controller.enqueue(null); 97 controller.close(); 98 }}) 99 }); 100 testUploadFailure("Fetch with POST with ReadableStream containing number", url, 101 "POST", 102 () => { 103 return new ReadableStream({start: controller => { 104 controller.enqueue(99); 105 controller.close(); 106 }}) 107 }); 108 testUploadFailure("Fetch with POST with ReadableStream containing ArrayBuffer", url, 109 "POST", 110 () => { 111 return new ReadableStream({start: controller => { 112 controller.enqueue(new ArrayBuffer()); 113 controller.close(); 114 }}) 115 }); 116 testUploadFailure("Fetch with POST with ReadableStream containing Blob", url, 117 "POST", 118 () => { 119 return new ReadableStream({start: controller => { 120 controller.enqueue(new Blob()); 121 controller.close(); 122 }}) 123 }); 124 125 promise_test(async (test) => { 126 const resp = await fetch( 127 "/fetch/connection-pool/resources/network-partition-key.py?" 128 + `status=421&uuid=${token()}&partition_id=${get_host_info().ORIGIN}` 129 + `&dispatch=check_partition&addcounter=true`, 130 {method: "POST", body: "foobar"}); 131 assert_equals(resp.status, 421); 132 const text = await resp.text(); 133 assert_equals(text, "ok. Request was sent 2 times. 2 connections were created."); 134 }, "Fetch with POST with text body on 421 response should be retried once on new connection."); 135 136 promise_test(async (test) => { 137 const body = new ReadableStream({start: c => c.close()}); 138 await promise_rejects_js(test, TypeError, fetch('/', {method: 'POST', body})); 139 }, "Streaming upload shouldn't work on Http/1.1.");