|
| 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 Float32Array body", url, |
| 64 | + "POST", |
| 65 | + () => new Float32Array(1), |
| 66 | + "\0\0\0\0"); |
| 67 | +testUpload("Fetch with POST with Float64Array body", url, |
| 68 | + "POST", |
| 69 | + () => new Float64Array(1), |
| 70 | + "\0\0\0\0\0\0\0\0"); |
| 71 | +testUpload("Fetch with POST with DataView body", url, |
| 72 | + "POST", |
| 73 | + () => new DataView(new ArrayBuffer(8), 0, 4), |
| 74 | + "\0\0\0\0"); |
| 75 | +testUpload("Fetch with POST with Blob body with mime type", url, |
| 76 | + "POST", |
| 77 | + () => new Blob(["Test"], { type: "text/maybe" }), |
| 78 | + "Test"); |
| 79 | + |
| 80 | +testUploadFailure("Fetch with POST with ReadableStream containing String", url, |
| 81 | + "POST", |
| 82 | + () => { |
| 83 | + return new ReadableStream({start: controller => { |
| 84 | + controller.enqueue("Test"); |
| 85 | + controller.close(); |
| 86 | + }}) |
| 87 | + }); |
| 88 | +testUploadFailure("Fetch with POST with ReadableStream containing null", url, |
| 89 | + "POST", |
| 90 | + () => { |
| 91 | + return new ReadableStream({start: controller => { |
| 92 | + controller.enqueue(null); |
| 93 | + controller.close(); |
| 94 | + }}) |
| 95 | + }); |
| 96 | +testUploadFailure("Fetch with POST with ReadableStream containing number", url, |
| 97 | + "POST", |
| 98 | + () => { |
| 99 | + return new ReadableStream({start: controller => { |
| 100 | + controller.enqueue(99); |
| 101 | + controller.close(); |
| 102 | + }}) |
| 103 | + }); |
| 104 | +testUploadFailure("Fetch with POST with ReadableStream containing ArrayBuffer", url, |
| 105 | + "POST", |
| 106 | + () => { |
| 107 | + return new ReadableStream({start: controller => { |
| 108 | + controller.enqueue(new ArrayBuffer()); |
| 109 | + controller.close(); |
| 110 | + }}) |
| 111 | + }); |
| 112 | +testUploadFailure("Fetch with POST with ReadableStream containing Blob", url, |
| 113 | + "POST", |
| 114 | + () => { |
| 115 | + return new ReadableStream({start: controller => { |
| 116 | + controller.enqueue(new Blob()); |
| 117 | + controller.close(); |
| 118 | + }}) |
| 119 | + }); |
| 120 | + |
| 121 | +promise_test(async (test) => { |
| 122 | + const resp = await fetch( |
| 123 | + "/fetch/connection-pool/resources/network-partition-key.py?" |
| 124 | + + `status=421&uuid=${token()}&partition_id=${get_host_info().ORIGIN}` |
| 125 | + + `&dispatch=check_partition&addcounter=true`, |
| 126 | + {method: "POST", body: "foobar"}); |
| 127 | + assert_equals(resp.status, 421); |
| 128 | + const text = await resp.text(); |
| 129 | + assert_equals(text, "ok. Request was sent 2 times. 2 connections were created."); |
| 130 | +}, "Fetch with POST with text body on 421 response should be retried once on new connection."); |
| 131 | + |
| 132 | +promise_test(async (test) => { |
| 133 | + const body = new ReadableStream({start: c => c.close()}); |
| 134 | + await promise_rejects_js(test, TypeError, fetch('/', {method: 'POST', body})); |
| 135 | +}, "Streaming upload shouldn't work on Http/1.1."); |
0 commit comments