OLD | NEW |
(Empty) | |
| 1 <!doctype html> |
| 2 <html> |
| 3 <head> |
| 4 <meta charset="utf-8"> |
| 5 <title>Response clone</title> |
| 6 <meta name="help" href="https://fetch.spec.whatwg.org/#response"> |
| 7 <meta name="author" title="Canon Research France" href="https://www.crf.cano
n.fr"> |
| 8 <script src="/resources/testharness.js"></script> |
| 9 <script src="/resources/testharnessreport.js"></script> |
| 10 </head> |
| 11 <body> |
| 12 <script src="../resources/utils.js"></script> |
| 13 <script> |
| 14 var defaultValues = { "type" : "default", |
| 15 "url" : "", |
| 16 "ok" : true, |
| 17 "status" : 200, |
| 18 "statusText" : "OK" |
| 19 }; |
| 20 |
| 21 var response = new Response(); |
| 22 var clonedResponse = response.clone(); |
| 23 test(function() { |
| 24 for (var attributeName in defaultValues) { |
| 25 var expectedValue = defaultValues[attributeName]; |
| 26 assert_equals(clonedResponse[attributeName], expectedValue, |
| 27 "Expect default response." + attributeName + " is " + expectedValu
e); |
| 28 } |
| 29 }, "Check Response's clone with default values, without body"); |
| 30 |
| 31 var body = "This is response body"; |
| 32 var headersInit = { "name" : "value" }; |
| 33 var responseInit = { "status" : 200, |
| 34 "statusText" : "GOOD", |
| 35 "headers" : headersInit |
| 36 }; |
| 37 var response = new Response(body, responseInit); |
| 38 var clonedResponse = response.clone(); |
| 39 test(function() { |
| 40 assert_equals(clonedResponse.status, responseInit["status"], |
| 41 "Expect response.status is " + responseInit["status"]); |
| 42 assert_equals(clonedResponse.statusText, responseInit["statusText"], |
| 43 "Expect response.statusText is " + responseInit["statusText"]); |
| 44 assert_equals(clonedResponse.headers.get("name"), "value", |
| 45 "Expect response.headers has name:value header"); |
| 46 }, "Check Response's clone has the expected attribute values"); |
| 47 |
| 48 promise_test(function(test) { |
| 49 return validateStreamFromString(response.body.getReader(), body); |
| 50 }, "Check orginal response's body after cloning"); |
| 51 |
| 52 promise_test(function(test) { |
| 53 return validateStreamFromString(clonedResponse.body.getReader(), body); |
| 54 }, "Check cloned response's body"); |
| 55 |
| 56 promise_test(function(test) { |
| 57 var disturbedResponse = new Response("data"); |
| 58 return disturbedResponse.text().then(function() { |
| 59 assert_true(disturbedResponse.bodyUsed, "response is disturbed"); |
| 60 assert_throws(new TypeError() , function() { disturbedResponse.clone
(); }, |
| 61 "Expect TypeError exception"); |
| 62 }); |
| 63 }, "Cannot clone a disturbed response"); |
| 64 |
| 65 promise_test(function(t) { |
| 66 var clone; |
| 67 var result; |
| 68 var response; |
| 69 return fetch('../resources/trickle.py?count=2&delay=100').then(function(
res) { |
| 70 clone = res.clone(); |
| 71 response = res; |
| 72 return clone.arrayBuffer(); |
| 73 }).then(function(r) { |
| 74 assert_equals(r.byteLength, 26); |
| 75 result = r; |
| 76 return response.arrayBuffer(); |
| 77 }).then(function(r) { |
| 78 assert_array_equals(r, result, "cloned responses should provide the
same data"); |
| 79 }); |
| 80 }, 'Cloned responses should provide the same data'); |
| 81 |
| 82 promise_test(function(t) { |
| 83 var clone; |
| 84 return fetch('../resources/trickle.py?count=2&delay=100').then(function(
res) { |
| 85 clone = res.clone(); |
| 86 res.body.cancel(); |
| 87 assert_true(res.bodyUsed); |
| 88 assert_false(clone.bodyUsed); |
| 89 return clone.arrayBuffer(); |
| 90 }).then(function(r) { |
| 91 assert_equals(r.byteLength, 26); |
| 92 assert_true(clone.bodyUsed); |
| 93 }); |
| 94 }, 'Cancelling stream should not affect cloned one'); |
| 95 |
| 96 function testReadableStreamClone(initialBuffer, bufferType) |
| 97 { |
| 98 promise_test(function(test) { |
| 99 var response = new Response(new ReadableStream({start : function(control
ler) { |
| 100 controller.enqueue(initialBuffer); |
| 101 controller.close(); |
| 102 }})); |
| 103 |
| 104 var clone = response.clone(); |
| 105 var stream1 = response.body; |
| 106 var stream2 = clone.body; |
| 107 |
| 108 var buffer; |
| 109 return stream1.getReader().read().then(function(data) { |
| 110 assert_false(data.done); |
| 111 assert_true(data.value === initialBuffer, "Buffer of being-cloned re
sponse stream is the same as the original buffer"); |
| 112 return stream2.getReader().read(); |
| 113 }).then(function(data) { |
| 114 assert_false(data.done); |
| 115 assert_array_equals(data.value, initialBuffer, "Cloned buffer chunks
have the same content"); |
| 116 assert_equals(Object.getPrototypeOf(data.value), Object.getPrototype
Of(initialBuffer), "Cloned buffers have the same type"); |
| 117 assert_true(data.value !== initialBuffer, "Buffer of cloned response
stream is a clone of the original buffer"); |
| 118 }); |
| 119 }, "Check response clone use structureClone for teed ReadableStreams (" + bu
fferType + "chunk)"); |
| 120 } |
| 121 |
| 122 var arrayBuffer = new ArrayBuffer(16); |
| 123 testReadableStreamClone(new Int8Array(arrayBuffer, 1), "Int8Array"); |
| 124 testReadableStreamClone(new Int16Array(arrayBuffer, 2, 2), "Int16Array"); |
| 125 testReadableStreamClone(new Int32Array(arrayBuffer), "Int32Array"); |
| 126 testReadableStreamClone(arrayBuffer, "ArrayBuffer"); |
| 127 testReadableStreamClone(new Uint8Array(arrayBuffer), "Uint8Array"); |
| 128 testReadableStreamClone(new Uint8ClampedArray(arrayBuffer), "Uint8ClampedArray")
; |
| 129 testReadableStreamClone(new Uint16Array(arrayBuffer, 2), "Uint16Array"); |
| 130 testReadableStreamClone(new Uint32Array(arrayBuffer), "Uint32Array"); |
| 131 testReadableStreamClone(new Float32Array(arrayBuffer), "Float32Array"); |
| 132 testReadableStreamClone(new Float64Array(arrayBuffer), "Float64Array"); |
| 133 testReadableStreamClone(new DataView(arrayBuffer, 2, 8), "DataView"); |
| 134 </script> |
| 135 </body> |
| 136 </html> |
OLD | NEW |