OLD | NEW |
(Empty) | |
| 1 if (this.document === undefined) { |
| 2 importScripts("/resources/testharness.js"); |
| 3 importScripts("../resources/utils.js"); |
| 4 } |
| 5 |
| 6 function testUpload(desc, url, method, createBody, expectedBody) { |
| 7 var requestInit = {"method": method} |
| 8 promise_test(function(test){ |
| 9 let body = createBody(); |
| 10 if (body) |
| 11 requestInit["body"] = body; |
| 12 return fetch(url, requestInit).then(function(resp) { |
| 13 return resp.text().then((text)=> { |
| 14 assert_equals(text, expectedBody); |
| 15 }); |
| 16 }); |
| 17 }, desc); |
| 18 } |
| 19 |
| 20 function testUploadFailure(desc, url, method, createBody) { |
| 21 const requestInit = {"method": method}; |
| 22 promise_test(test => { |
| 23 let body = createBody(); |
| 24 if (body) |
| 25 requestInit["body"] = body; |
| 26 return promise_rejects(new TypeError(), fetch(url, requestInit)); |
| 27 }, desc); |
| 28 } |
| 29 |
| 30 var url = RESOURCES_DIR + "echo-content.py" |
| 31 |
| 32 testUpload("Fetch with PUT with body", url, |
| 33 "PUT", |
| 34 () => "Request's body", |
| 35 "Request's body"); |
| 36 testUpload("Fetch with POST with text body", url, |
| 37 "POST", |
| 38 () => "Request's body", |
| 39 "Request's body"); |
| 40 testUpload("Fetch with POST with URLSearchParams body", url, |
| 41 "POST", |
| 42 () => new URLSearchParams("name=value"), |
| 43 "name=value"); |
| 44 testUpload("Fetch with POST with Blob body", url, |
| 45 "POST", |
| 46 () => new Blob(["Test"]), |
| 47 "Test"); |
| 48 testUpload("Fetch with POST with ArrayBuffer body", url, |
| 49 "POST", |
| 50 () => new ArrayBuffer(4), |
| 51 "\0\0\0\0"); |
| 52 testUpload("Fetch with POST with Uint8Array body", url, |
| 53 "POST", |
| 54 () => new Uint8Array(4), |
| 55 "\0\0\0\0"); |
| 56 testUpload("Fetch with POST with Int8Array body", url, |
| 57 "POST", |
| 58 () => new Int8Array(4), |
| 59 "\0\0\0\0"); |
| 60 testUpload("Fetch with POST with Float32Array body", url, |
| 61 "POST", |
| 62 () => new Float32Array(1), |
| 63 "\0\0\0\0"); |
| 64 testUpload("Fetch with POST with Float64Array body", url, |
| 65 "POST", |
| 66 () => new Float64Array(1), |
| 67 "\0\0\0\0\0\0\0\0"); |
| 68 testUpload("Fetch with POST with DataView body", url, |
| 69 "POST", |
| 70 () => new DataView(new ArrayBuffer(8), 0, 4), |
| 71 "\0\0\0\0"); |
| 72 testUpload("Fetch with POST with Blob body with mime type", url, |
| 73 "POST", |
| 74 () => new Blob(["Test"], { type: "text/maybe" }), |
| 75 "Test"); |
| 76 testUpload("Fetch with POST with ReadableStream", url, |
| 77 "POST", |
| 78 () => { |
| 79 new ReadableStream({start: controller => { |
| 80 const encoder = new TextEncoder(); |
| 81 controller.enqueue(encoder.encode("Test")); |
| 82 controller.close(); |
| 83 }}) |
| 84 }, |
| 85 "Test"); |
| 86 testUploadFailure("Fetch with POST with ReadableStream containing String", url, |
| 87 "POST", |
| 88 () => { |
| 89 new ReadableStream({start: controller => { |
| 90 controller.enqueue("Test"); |
| 91 controller.close(); |
| 92 }}) |
| 93 }); |
| 94 testUploadFailure("Fetch with POST with ReadableStream containing null", url, |
| 95 "POST", |
| 96 () => { |
| 97 new ReadableStream({start: controller => { |
| 98 controller.enqueue(null); |
| 99 controller.close(); |
| 100 }}) |
| 101 }); |
| 102 testUploadFailure("Fetch with POST with ReadableStream containing number", url, |
| 103 "POST", |
| 104 () => { |
| 105 new ReadableStream({start: controller => { |
| 106 controller.enqueue(99); |
| 107 controller.close(); |
| 108 }}) |
| 109 }); |
| 110 testUploadFailure("Fetch with POST with ReadableStream containing ArrayBuffer",
url, |
| 111 "POST", |
| 112 () => { |
| 113 new ReadableStream({start: controller => { |
| 114 controller.enqueue(new ArrayBuffer()); |
| 115 controller.close(); |
| 116 }}) |
| 117 }); |
| 118 testUploadFailure("Fetch with POST with ReadableStream containing Blob", url, |
| 119 "POST", |
| 120 () => { |
| 121 new ReadableStream({start: controller => { |
| 122 controller.enqueue(new Blob()); |
| 123 controller.close(); |
| 124 }}) |
| 125 }); |
| 126 |
| 127 done(); |
OLD | NEW |