diff options
author | Matt A. Tobin <mattatobin@localhost.localdomain> | 2018-02-02 04:16:08 -0500 |
---|---|---|
committer | Matt A. Tobin <mattatobin@localhost.localdomain> | 2018-02-02 04:16:08 -0500 |
commit | 5f8de423f190bbb79a62f804151bc24824fa32d8 (patch) | |
tree | 10027f336435511475e392454359edea8e25895d /testing/web-platform/tests/fetch/api/response | |
parent | 49ee0794b5d912db1f95dce6eb52d781dc210db5 (diff) | |
download | UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.gz UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.lz UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.xz UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.zip |
Add m-esr52 at 52.6.0
Diffstat (limited to 'testing/web-platform/tests/fetch/api/response')
16 files changed, 1026 insertions, 0 deletions
diff --git a/testing/web-platform/tests/fetch/api/response/response-cancel-stream.html b/testing/web-platform/tests/fetch/api/response/response-cancel-stream.html new file mode 100644 index 000000000..2a02e1f60 --- /dev/null +++ b/testing/web-platform/tests/fetch/api/response/response-cancel-stream.html @@ -0,0 +1,66 @@ +<!doctype html> +<html> + <head> + <meta charset="utf-8"> + <title>Response consume blob and http bodies</title> + <meta name="help" href="https://fetch.spec.whatwg.org/#response"> + <meta name="help" href="https://fetch.spec.whatwg.org/#body-mixin"> + <meta name="author" title="Canon Research France" href="https://www.crf.canon.fr"> + <script src="/resources/testharness.js"></script> + <script src="/resources/testharnessreport.js"></script> + </head> + <body> + <script src="../resources/utils.js"></script> + <script> + +promise_test(function(test) { + return new Response(new Blob([], { "type" : "text/plain" })).body.cancel(); +}, "Cancelling a starting blob Response stream"); + +promise_test(function(test) { + var response = new Response(new Blob(["This is data"], { "type" : "text/plain" })); + var reader = response.body.getReader(); + reader.read(); + return reader.cancel(); +}, "Cancelling a loading blob Response stream"); + +promise_test(function(test) { + var response = new Response(new Blob(["T"], { "type" : "text/plain" })); + var reader = response.body.getReader(); + + var closedPromise = reader.closed.then(function() { + return reader.cancel(); + }); + reader.read(); + return closedPromise; +}, "Cancelling a closed blob Response stream"); + +promise_test(function(test) { + return fetch(RESOURCES_DIR + "trickle.py?ms=30&count=100").then(function(response) { + return response.body.cancel(); + }); +}, "Cancelling a starting Response stream"); + +promise_test(function() { + return fetch(RESOURCES_DIR + "trickle.py?ms=30&count=100").then(function(response) { + var reader = response.body.getReader(); + return reader.read().then(function() { + return reader.cancel(); + }); + }); +}, "Cancelling a loading Response stream"); + +promise_test(function() { + return fetch(RESOURCES_DIR + "top.txt").then(function(response) { + var reader = response.body.getReader(); + var closedPromise = reader.closed.then(function() { + return reader.cancel(); + }); + reader.read(); + return closedPromise; + }); +}, "Cancelling a closed Response stream"); + + </script> + </body> +</html> diff --git a/testing/web-platform/tests/fetch/api/response/response-clone.html b/testing/web-platform/tests/fetch/api/response/response-clone.html new file mode 100644 index 000000000..1efb4da5e --- /dev/null +++ b/testing/web-platform/tests/fetch/api/response/response-clone.html @@ -0,0 +1,98 @@ +<!doctype html> +<html> + <head> + <meta charset="utf-8"> + <title>Response clone</title> + <meta name="help" href="https://fetch.spec.whatwg.org/#response"> + <meta name="author" title="Canon Research France" href="https://www.crf.canon.fr"> + <script src="/resources/testharness.js"></script> + <script src="/resources/testharnessreport.js"></script> + </head> + <body> + <script src="../resources/utils.js"></script> + <script> + var defaultValues = { "type" : "default", + "url" : "", + "ok" : true, + "status" : 200, + "statusText" : "OK" + }; + + var response = new Response(); + var clonedResponse = response.clone(); + test(function() { + for (var attributeName in defaultValues) { + var expectedValue = defaultValues[attributeName]; + assert_equals(clonedResponse[attributeName], expectedValue, + "Expect default response." + attributeName + " is " + expectedValue); + } + }, "Check Response's clone with default values, without body"); + + var body = "This is response body"; + var headersInit = { "name" : "value" }; + var responseInit = { "status" : 200, + "statusText" : "GOOD", + "headers" : headersInit + }; + var response = new Response(body, responseInit); + var clonedResponse = response.clone(); + test(function() { + assert_equals(clonedResponse.status, responseInit["status"], + "Expect response.status is " + responseInit["status"]); + assert_equals(clonedResponse.statusText, responseInit["statusText"], + "Expect response.statusText is " + responseInit["statusText"]); + assert_equals(clonedResponse.headers.get("name"), "value", + "Expect response.headers has name:value header"); + }, "Check Response's clone has the expected attribute values"); + + promise_test(function(test) { + return validateStreamFromString(response.body.getReader(), body); + }, "Check orginal response's body after cloning"); + + promise_test(function(test) { + return validateStreamFromString(clonedResponse.body.getReader(), body); + }, "Check cloned response's body"); + + promise_test(function(test) { + var disturbedResponse = new Response("data"); + return disturbedResponse.text().then(function() { + assert_true(disturbedResponse.bodyUsed, "response is disturbed"); + assert_throws(new TypeError() , function() { disturbedResponse.clone(); }, + "Expect TypeError exception"); + }); + }, "Cannot clone a disturbed response"); + + promise_test(function(t) { + var clone; + var result; + var response; + return fetch('../resources/trickle.py?count=2&delay=100').then(function(res) { + clone = res.clone(); + response = res; + return clone.arrayBuffer(); + }).then(function(r) { + assert_equals(r.byteLength, 26); + result = r; + return response.arrayBuffer(); + }).then(function(r) { + assert_array_equals(r, result, "cloned responses should provide the same data"); + }); + }, 'Cloned responses should provide the same data'); + + promise_test(function(t) { + var clone; + return fetch('../resources/trickle.py?count=2&delay=100').then(function(res) { + clone = res.clone(); + res.body.cancel(); + assert_true(res.bodyUsed); + assert_false(clone.bodyUsed); + return clone.arrayBuffer(); + }).then(function(r) { + assert_equals(r.byteLength, 26); + assert_true(clone.bodyUsed); + }); + }, 'Cancelling stream should not affect cloned one'); + + </script> + </body> +</html> diff --git a/testing/web-platform/tests/fetch/api/response/response-consume-empty.html b/testing/web-platform/tests/fetch/api/response/response-consume-empty.html new file mode 100644 index 000000000..788384699 --- /dev/null +++ b/testing/web-platform/tests/fetch/api/response/response-consume-empty.html @@ -0,0 +1,103 @@ +<!doctype html> +<html> + <head> + <meta charset="utf-8"> + <title>Response consume empty bodies</title> + <meta name="help" href="https://fetch.spec.whatwg.org/#response"> + <meta name="help" href="https://fetch.spec.whatwg.org/#body-mixin"> + <meta name="author" title="Canon Research France" href="https://www.crf.canon.fr"> + <script src="/resources/testharness.js"></script> + <script src="/resources/testharnessreport.js"></script> + </head> + <body> + <script> + function checkBodyText(response) { + return response.text().then(function(bodyAsText) { + assert_equals(bodyAsText, "", "Resolved value should be empty"); + assert_false(response.bodyUsed); + }); + } + + function checkBodyBlob(response) { + return response.blob().then(function(bodyAsBlob) { + var promise = new Promise(function(resolve, reject) { + var reader = new FileReader(); + reader.onload = function(evt) { + resolve(reader.result) + }; + reader.onerror = function() { + reject("Blob's reader failed"); + }; + reader.readAsText(bodyAsBlob); + }); + return promise.then(function(body) { + assert_equals(body, "", "Resolved value should be empty"); + assert_false(response.bodyUsed); + }); + }); + } + + function checkBodyArrayBuffer(response) { + return response.arrayBuffer().then(function(bodyAsArrayBuffer) { + assert_equals(bodyAsArrayBuffer.byteLength, 0, "Resolved value should be empty"); + assert_false(response.bodyUsed); + }); + } + + function checkBodyJSON(response) { + return response.json().then( + function(bodyAsJSON) { + assert_unreached("JSON parsing should fail"); + }, + function() { + assert_false(response.bodyUsed); + }); + } + + function checkBodyFormData(response) { + return response.formData().then(function(bodyAsFormData) { + assert_true(bodyAsFormData instanceof FormData, "Should receive a FormData"); + assert_false(response.bodyUsed); + }); + } + + function checkResponseWithNoBody(bodyType, checkFunction) { + promise_test(function(test) { + var response = new Response(); + assert_false(response.bodyUsed); + return checkFunction(response); + }, "Consume response's body as " + bodyType); + } + + var formData = new FormData(); + checkResponseWithNoBody("text", checkBodyText); + checkResponseWithNoBody("blob", checkBodyBlob); + checkResponseWithNoBody("arrayBuffer", checkBodyArrayBuffer); + checkResponseWithNoBody("json", checkBodyJSON); + checkResponseWithNoBody("formData", checkBodyFormData); + + function checkResponseWithEmptyBody(bodyType, body, asText) { + promise_test(function(test) { + var response = new Response(body); + assert_false(response.bodyUsed, "bodyUsed is false at init"); + if (asText) { + return response.text().then(function(bodyAsString) { + assert_equals(bodyAsString.length, 0, "Resolved value should be empty"); + assert_true(response.bodyUsed, "bodyUsed is true after being consumed"); + }); + } + return response.arrayBuffer().then(function(bodyAsArrayBuffer) { + assert_equals(bodyAsArrayBuffer.byteLength, 0, "Resolved value should be empty"); + assert_true(response.bodyUsed, "bodyUsed is true after being consumed"); + }); + }, "Consume empty " + bodyType + " response body as " + (asText ? "text" : "arrayBuffer")); + } + + // FIXME: Add BufferSource, FormData and URLSearchParams. + checkResponseWithEmptyBody("blob", new Blob([], { "type" : "text/plain" }), false); + checkResponseWithEmptyBody("text", "", false); + checkResponseWithEmptyBody("blob", new Blob([], { "type" : "text/plain" }), true); + checkResponseWithEmptyBody("text", "", true); + </script> + </body> +</html> diff --git a/testing/web-platform/tests/fetch/api/response/response-consume-stream.html b/testing/web-platform/tests/fetch/api/response/response-consume-stream.html new file mode 100644 index 000000000..f96192fa9 --- /dev/null +++ b/testing/web-platform/tests/fetch/api/response/response-consume-stream.html @@ -0,0 +1,66 @@ +<!doctype html> +<html> + <head> + <meta charset="utf-8"> + <title>Response consume</title> + <meta name="help" href="https://fetch.spec.whatwg.org/#response"> + <meta name="help" href="https://fetch.spec.whatwg.org/#body-mixin"> + <meta name="author" title="Canon Research France" href="https://www.crf.canon.fr"> + <script src="/resources/testharness.js"></script> + <script src="/resources/testharnessreport.js"></script> + <script src="../resources/utils.js"></script> + </head> + <body> + <script> + +promise_test(function(test) { + var body = ""; + var response = new Response(""); + return validateStreamFromString(response.body.getReader(), ""); +}, "Read empty text response's body as readableStream"); + +promise_test(function(test) { + var response = new Response(new Blob([], { "type" : "text/plain" })); + return validateStreamFromString(response.body.getReader(), ""); +}, "Read empty blob response's body as readableStream"); + +var formData = new FormData(); +formData.append("name", "value"); +var textData = JSON.stringify("This is response's body"); +var blob = new Blob([textData], { "type" : "text/plain" }); + +promise_test(function(test) { + var response = new Response(blob); + return validateStreamFromString(response.body.getReader(), textData); +}, "Read blob response's body as readableStream"); + +promise_test(function(test) { + var response = new Response(textData); + return validateStreamFromString(response.body.getReader(), textData); +}, "Read text response's body as readableStream"); + +promise_test(function(test) { + var arrayBuffer = new ArrayBuffer(textData.length); + var int8Array = new Int8Array(arrayBuffer); + for (var cptr = 0; cptr < textData.length; cptr++) + int8Array[cptr] = textData.charCodeAt(cptr); + + return validateStreamFromString(new Response(arrayBuffer).body.getReader(), textData); +}, "Read array buffer response's body as readableStream"); + +promise_test(function(test) { + var response = new Response(formData); + return validateStreamFromString(response.body.getReader(), "name=value"); +}, "Read form data response's body as readableStream"); + +test(function() { + assert_equals(Response.error().body, null); +}, "Getting an error Response stream"); + +promise_test(function(test) { + assert_equals(Response.redirect(301).body, null); +}, "Getting a redirect Response stream"); + + </script> + </body> +</html> diff --git a/testing/web-platform/tests/fetch/api/response/response-consume.html b/testing/web-platform/tests/fetch/api/response/response-consume.html new file mode 100644 index 000000000..56e234248 --- /dev/null +++ b/testing/web-platform/tests/fetch/api/response/response-consume.html @@ -0,0 +1,131 @@ +<!doctype html> +<html> + <head> + <meta charset="utf-8"> + <title>Response consume</title> + <meta name="help" href="https://fetch.spec.whatwg.org/#response"> + <meta name="help" href="https://fetch.spec.whatwg.org/#body-mixin"> + <meta name="author" title="Canon Research France" href="https://www.crf.canon.fr"> + <script src="/resources/testharness.js"></script> + <script src="/resources/testharnessreport.js"></script> + <script src="../resources/utils.js"></script> + </head> + <body> + <script> + function checkBodyText(response, expectedBody) { + return response.text().then( function(bodyAsText) { + assert_equals(bodyAsText, expectedBody, "Retrieve and verify response's body"); + assert_true(response.bodyUsed, "body as text: bodyUsed turned true"); + }); + } + + function checkBodyBlob(response, expectedBody, checkContentType) { + return response.blob().then(function(bodyAsBlob) { + if (checkContentType) + assert_equals(bodyAsBlob.type, "text/plain", "Blob body type should be computed from the response Content-Type"); + + var promise = new Promise( function (resolve, reject) { + var reader = new FileReader(); + reader.onload = function(evt) { + resolve(reader.result) + }; + reader.onerror = function () { + reject("Blob's reader failed"); + }; + reader.readAsText(bodyAsBlob); + }); + return promise.then(function(body) { + assert_equals(body, expectedBody, "Retrieve and verify response's body"); + assert_true(response.bodyUsed, "body as blob: bodyUsed turned true"); + }); + }); + } + + function checkBodyArrayBuffer(response, expectedBody) { + return response.arrayBuffer().then( function(bodyAsArrayBuffer) { + validateBufferFromString(bodyAsArrayBuffer, expectedBody, "Retrieve and verify response's body"); + assert_true(response.bodyUsed, "body as arrayBuffer: bodyUsed turned true"); + }); + } + + function checkBodyJSON(response, expectedBody) { + return response.json().then(function(bodyAsJSON) { + var strBody = JSON.stringify(bodyAsJSON) + assert_equals(strBody, expectedBody, "Retrieve and verify response's body"); + assert_true(response.bodyUsed, "body as json: bodyUsed turned true"); + }); + } + + function checkBodyFormData(response, expectedBody) { + return response.formData().then(function(bodyAsFormData) { + assert_true(bodyAsFormData instanceof FormData, "Should receive a FormData"); + assert_true(response.bodyUsed, "body as formData: bodyUsed turned true"); + }); + } + + function checkResponseBody(body, bodyType, checkFunction) { + promise_test(function(test) { + var response = new Response(body, { "headers": [["Content-Type", "text/PLAIN"]] }); + assert_false(response.bodyUsed, "bodyUsed is false at init"); + return checkFunction(response, body); + }, "Consume response's body as " + bodyType); + } + + var formData = new FormData(); + formData.append("name", "value"); + var textData = JSON.stringify("This is response's body"); + var blob = new Blob([textData], { "type" : "text/plain" }); + + checkResponseBody(textData, "text", checkBodyText); + checkResponseBody(textData, "blob", function(response, body) { checkBodyBlob(response, body, true); }); + checkResponseBody(textData, "arrayBuffer", checkBodyArrayBuffer); + checkResponseBody(textData, "json", checkBodyJSON); + checkResponseBody(formData, "formData", checkBodyFormData); + + function checkBlobResponseBody(blobBody, blobData, bodyType, checkFunction) { + promise_test(function(test) { + var response = new Response(blobBody); + assert_false(response.bodyUsed, "bodyUsed is false at init"); + return checkFunction(response, blobData); + }, "Consume blob response's body as " + bodyType); + } + + checkBlobResponseBody(blob, textData, "blob", checkBodyBlob); + checkBlobResponseBody(blob, textData, "text", checkBodyText); + checkBlobResponseBody(blob, textData, "json", checkBodyJSON); + checkBlobResponseBody(blob, textData, "arrayBuffer", checkBodyArrayBuffer); + + function checkReadableStreamResponseBody(streamData, bodyType, checkFunction) { + promise_test(function(test) { + var stream = new ReadableStream({ + start: function(controller) { + controller.enqueue((stringToArray(streamData))); + controller.close(); + } + }); + var response = new Response(stream); + assert_false(response.bodyUsed, "bodyUsed is false at init"); + return checkFunction(response, streamData); + }, "Consume stream response's body as " + bodyType); + } + + checkReadableStreamResponseBody(textData, "blob", checkBodyBlob); + checkReadableStreamResponseBody(textData, "text", checkBodyText); + checkReadableStreamResponseBody(textData, "json", checkBodyJSON); + checkReadableStreamResponseBody(textData, "arrayBuffer", checkBodyArrayBuffer); + + function checkFetchedResponseBody(bodyType, checkFunction) { + return promise_test(function(test) { + return fetch("../resources/top.txt").then(function(response) { + assert_false(response.bodyUsed, "bodyUsed is false at init"); + return checkFunction(response, "top"); + }); + }, "Consume fetched response's body as " + bodyType); + } + checkFetchedResponseBody("blob", checkBodyBlob); + checkFetchedResponseBody("text", checkBodyText); + checkFetchedResponseBody("arrayBuffer", checkBodyArrayBuffer); + + </script> + </body> +</html> diff --git a/testing/web-platform/tests/fetch/api/response/response-error.html b/testing/web-platform/tests/fetch/api/response/response-error.html new file mode 100644 index 000000000..1e68f6d01 --- /dev/null +++ b/testing/web-platform/tests/fetch/api/response/response-error.html @@ -0,0 +1,39 @@ +<!doctype html> +<html> + <head> + <meta charset="utf-8"> + <title>Response error</title> + <meta name="help" href="https://fetch.spec.whatwg.org/#response"> + <meta name="author" title="Canon Research France" href="https://www.crf.canon.fr"> + <script src="/resources/testharness.js"></script> + <script src="/resources/testharnessreport.js"></script> + </head> + <body> + <script> + var invalidStatus = [0, 100, 199, 600, 1000]; + invalidStatus.forEach(function(status) { + test(function() { + assert_throws(new RangeError() , function() { new Response("", { "status" : status }); }, + "Expect RangeError exception when status is " + status); + },"Throws RangeError when responseInit's status is " + status); + }); + + var invalidStatusText = ["\n", "Ā"]; + invalidStatusText.forEach(function(statusText) { + test(function() { + assert_throws(new TypeError() , function() { new Response("", { "statusText" : statusText }); }, + "Expect TypeError exception " + statusText); + },"Throws TypeError when responseInit's statusText is " + statusText); + }); + + var nullBodyStatus = [204, 205, 304]; + nullBodyStatus.forEach(function(status) { + test(function() { + assert_throws(new TypeError() , + function() { new Response("body", {"status" : status }); }, + "Expect TypeError exception "); + },"Throws TypeError when building a response with body and a body status of " + status); + }); + </script> + </body> +</html> diff --git a/testing/web-platform/tests/fetch/api/response/response-idl.html b/testing/web-platform/tests/fetch/api/response/response-idl.html new file mode 100644 index 000000000..e849856ce --- /dev/null +++ b/testing/web-platform/tests/fetch/api/response/response-idl.html @@ -0,0 +1,69 @@ +<!doctype html> +<html> + <head> + <meta charset="utf-8"> + <title>Response idl interface</title> + <meta name="help" href="https://fetch.spec.whatwg.org/#response"> + <meta name="author" title="Canon Research France" href="https://www.crf.canon.fr"> + <script src="/resources/testharness.js"></script> + <script src="/resources/testharnessreport.js"></script> + <script src="/resources/WebIDLParser.js"></script> + <script src="/resources/idlharness.js"></script> + </head> + <body> + <script id="body-idl" type="text/plain"> + typedef any JSON; + typedef (Blob or BufferSource or FormData or URLSearchParams or USVString) BodyInit; + + [NoInterfaceObject, + Exposed=(Window,Worker)] + interface Body { + readonly attribute boolean bodyUsed; + [NewObject] Promise<ArrayBuffer> arrayBuffer(); + [NewObject] Promise<Blob> blob(); + [NewObject] Promise<FormData> formData(); + [NewObject] Promise<JSON> json(); + [NewObject] Promise<USVString> text(); + }; + </script> + <script id="response-idl" type="text/plain"> + [Constructor(optional BodyInit body, optional ResponseInit init), + Exposed=(Window,Worker)] + interface Response { + [NewObject] static Response error(); + [NewObject] static Response redirect(USVString url, optional unsigned short status = 302); + + readonly attribute ResponseType type; + + readonly attribute USVString url; + readonly attribute unsigned short status; + readonly attribute boolean ok; + readonly attribute ByteString statusText; + [SameObject] readonly attribute Headers headers; + readonly attribute ReadableStream? body; + + [NewObject] Response clone(); + }; + Response implements Body; + + dictionary ResponseInit { + unsigned short status = 200; + ByteString statusText = "OK"; + HeadersInit headers; + }; + + enum ResponseType { "basic", "cors", "default", "error", "opaque", "opaqueredirect" }; + </script> + <script> + var idlsArray = new IdlArray(); + var idl = document.getElementById("body-idl").innerHTML + idl += document.getElementById("response-idl").innerHTML + + idlsArray.add_idls(idl); + idlsArray.add_untested_idls("interface Headers {};"); + idlsArray.add_untested_idls("interface ReadableStream {};"); + idlsArray.add_objects({ Response: ['new Response()'] }); + idlsArray.test(); + </script> + </body> +</html> diff --git a/testing/web-platform/tests/fetch/api/response/response-init-001.html b/testing/web-platform/tests/fetch/api/response/response-init-001.html new file mode 100644 index 000000000..4a8a7bd80 --- /dev/null +++ b/testing/web-platform/tests/fetch/api/response/response-init-001.html @@ -0,0 +1,63 @@ +<!doctype html> +<html> + <head> + <meta charset="utf-8"> + <title>Response init: simple cases</title> + <meta name="help" href="https://fetch.spec.whatwg.org/#response"> + <meta name="help" href="https://fetch.spec.whatwg.org/#concept-response"> + <meta name="author" title="Canon Research France" href="https://www.crf.canon.fr"> + <script src="/resources/testharness.js"></script> + <script src="/resources/testharnessreport.js"></script> + </head> + <body> + <script> + var defaultValues = { "type" : "default", + "url" : "", + "ok" : true, + "status" : 200, + "statusText" : "OK", + "body" : null + }; + + var statusCodes = { "givenValues" : [200, 300, 400, 500, 599], + "expectedValues" : [200, 300, 400, 500, 599] + }; + var statusTexts = { "givenValues" : ["OK", "with space", String.fromCharCode(0x80)], + "expectedValues" : ["OK", "with space", String.fromCharCode(0x80)] + }; + var initValuesDict = { "status" : statusCodes, + "statusText" : statusTexts + }; + + function isOkStatus(status) { + return 200 <= status && 299 >= status; + } + + var response = new Response(); + for (var attributeName in defaultValues) { + test(function() { + var expectedValue = defaultValues[attributeName]; + assert_equals(response[attributeName], expectedValue, + "Expect default response." + attributeName + " is " + expectedValue); + }, "Check default value for " + attributeName + " attribute"); + } + + for (var attributeName in initValuesDict) + test(function() { + var valuesToTest = initValuesDict[attributeName]; + for (var valueIdx in valuesToTest["givenValues"]) { + var givenValue = valuesToTest["givenValues"][valueIdx]; + var expectedValue = valuesToTest["expectedValues"][valueIdx]; + var responseInit = {}; + responseInit[attributeName] = givenValue; + var response = new Response("", responseInit); + assert_equals(response[attributeName], expectedValue, + "Expect response." + attributeName + " is " + expectedValue + + " when initialized with " + givenValue); + assert_equals(response.ok, isOkStatus(response.status), + "Expect response.ok is " + isOkStatus(response.status)); + } + }, "Check " + attributeName + " init values and associated getter"); + </script> + </body> +</html> diff --git a/testing/web-platform/tests/fetch/api/response/response-init-002.html b/testing/web-platform/tests/fetch/api/response/response-init-002.html new file mode 100644 index 000000000..0bb2e8d0b --- /dev/null +++ b/testing/web-platform/tests/fetch/api/response/response-init-002.html @@ -0,0 +1,70 @@ +<!doctype html> +<html> + <head> + <meta charset="utf-8"> + <title>Response init: body and headers</title> + <meta name="help" href="https://fetch.spec.whatwg.org/#response"> + <meta name="help" href="https://fetch.spec.whatwg.org/#concept-response"> + <meta name="author" title="Canon Research France" href="https://www.crf.canon.fr"> + <script src="/resources/testharness.js"></script> + <script src="/resources/testharnessreport.js"></script> + </head> + <body> + <script src="../resources/utils.js"></script> + <script> + test(function() { + var headerDict = {"name1": "value1", + "name2": "value2", + "name3": "value3" + }; + var headers = new Headers(headerDict); + var response = new Response("", { "headers" : headers }) + for (var name in headerDict) { + assert_equals(response.headers.get(name), headerDict[name], + "response's headers has " + name + " : " + headerDict[name]); + } + }, "Initialize Response with headers values"); + + function checkResponseInit(body, bodyType, expectedTextBody) { + promise_test(function(test) { + var response = new Response(body); + var resHeaders = response.headers; + var mime = resHeaders.get("Content-Type"); + assert_true(mime && mime.search(bodyType) > -1, "Content-Type header should be \"" + bodyType + "\" "); + return response.text().then(function(bodyAsText) { + //not equals: cannot guess formData exact value + assert_true(bodyAsText.search(expectedTextBody) > -1, "Retrieve and verify response body"); + }); + }, "Initialize Response's body with " + bodyType); + } + + var blob = new Blob(["This is a blob"], {type: "application/octet-binary"}); + var formaData = new FormData(); + formaData.append("name", "value"); + var urlSearchParams = "URLSearchParams are not supported"; + //avoid test timeout if not implemented + if (window.URLSearchParams) + urlSearchParams = new URLSearchParams("name=value"); + var usvString = "This is a USVString" + + checkResponseInit(blob, "application/octet-binary", "This is a blob"); + checkResponseInit(formaData, "multipart/form-data", "name=\"name\"\r\n\r\nvalue"); + checkResponseInit(urlSearchParams, "application/x-www-form-urlencoded;charset=UTF-8", "name=value"); + checkResponseInit(usvString, "text/plain;charset=UTF-8", "This is a USVString"); + + promise_test(function(test) { + var body = "This is response body"; + var response = new Response(body); + return validateStreamFromString(response.body.getReader(), body); + }, "Read Response's body as readableStream"); + + promise_test(function(test) { + var response = new Response("This is my fork", {"headers" : [["Content-Type", ""]]}); + return response.blob().then(function(blob) { + assert_equals(blob.type, "", "Blob type should be the empty string"); + }); + }, "Testing empty Response Content-Type header"); + + </script> + </body> +</html> diff --git a/testing/web-platform/tests/fetch/api/response/response-static-error.html b/testing/web-platform/tests/fetch/api/response/response-static-error.html new file mode 100644 index 000000000..6e927a8bf --- /dev/null +++ b/testing/web-platform/tests/fetch/api/response/response-static-error.html @@ -0,0 +1,25 @@ +<!doctype html> +<html> + <head> + <meta charset="utf-8"> + <title>Response: error static method</title> + <meta name="help" href="https://fetch.spec.whatwg.org/#response"> + <meta name="help" href="https://fetch.spec.whatwg.org/#concept-network-error"> + <meta name="author" title="Canon Research France" href="https://www.crf.canon.fr"> + <script src="/resources/testharness.js"></script> + <script src="/resources/testharnessreport.js"></script> + </head> + <body> + <script> + test(function() { + var responseError = Response.error(); + assert_equals(responseError.type, "error", "Network error response's type is error"); + assert_equals(responseError.status, 0, "Network error response's status is 0"); + assert_equals(responseError.statusText, "", "Network error response's statusText is empty"); + assert_equals(responseError.body, null, "Network error response's body is null"); + + assert_true(responseError.headers.entries().next().done, "Headers should be empty"); + }, "Check response returned by static method error()"); + </script> + </body> +</html> diff --git a/testing/web-platform/tests/fetch/api/response/response-static-redirect.html b/testing/web-platform/tests/fetch/api/response/response-static-redirect.html new file mode 100644 index 000000000..e09c66611 --- /dev/null +++ b/testing/web-platform/tests/fetch/api/response/response-static-redirect.html @@ -0,0 +1,45 @@ +<!doctype html> +<html> + <head> + <meta charset="utf-8"> + <title>Response: redirect static method</title> + <meta name="help" href="https://fetch.spec.whatwg.org/#response"> + <meta name="help" href="https://fetch.spec.whatwg.org/#redirect-status"> + <meta name="author" title="Canon Research France" href="https://www.crf.canon.fr"> + <script src="/resources/testharness.js"></script> + <script src="/resources/testharnessreport.js"></script> + </head> + <body> + <script> + var url = "http://test.url:1234/"; + test(function() { + redirectResponse = Response.redirect(url); + assert_equals(redirectResponse.status, 302, "Default redirect status is 302"); + assert_equals(redirectResponse.headers.get("Location"), url, + "redirected response has Location header with the correct url"); + }, "Check default redirect response"); + + var redirectStatus = [301, 302, 303, 307, 308]; + redirectStatus.forEach(function(status) { + test(function() { + redirectResponse = Response.redirect(url, status); + assert_equals(redirectResponse.status, status, "Redirect status is " + status); + }, "Check response returned by static method redirect(), status = " + status); + }); + + test(function() { + var invalidUrl = "http://:This is not an url"; + assert_throws(new TypeError(), function() { Response.redirect(invalidUrl); }, + "Expect TypeError exception"); + }, "Check error returned when giving invalid url to redirect()"); + + var invalidRedirectStatus = [200, 309, 400, 500]; + invalidRedirectStatus.forEach(function(invalidStatus) { + test(function() { + assert_throws(new RangeError() , function() { Response.redirect(url, invalidStatus); }, + "Expect RangeError exception"); + }, "Check error returned when giving invalid status to redirect(), status = " + invalidStatus); + }); + </script> + </body> +</html> diff --git a/testing/web-platform/tests/fetch/api/response/response-stream-disturbed-1.html b/testing/web-platform/tests/fetch/api/response/response-stream-disturbed-1.html new file mode 100644 index 000000000..e9db8f77b --- /dev/null +++ b/testing/web-platform/tests/fetch/api/response/response-stream-disturbed-1.html @@ -0,0 +1,57 @@ +<!doctype html> +<html> + <head> + <meta charset="utf-8"> + <title>Consuming Response body after getting a ReadableStream</title> + <meta name="help" href="https://fetch.spec.whatwg.org/#response"> + <meta name="help" href="https://fetch.spec.whatwg.org/#body-mixin"> + <meta name="author" title="Canon Research France" href="https://www.crf.canon.fr"> + <script src="/resources/testharness.js"></script> + <script src="/resources/testharnessreport.js"></script> + </head> + <body> + <script> + +function createResponseWithReadableStream(callback) { + return fetch("../resources/data.json").then(function(response) { + var reader = response.body.getReader(); + reader.releaseLock(); + return callback(response); + }); +} + +promise_test(function() { + return createResponseWithReadableStream(function(response) { + return response.blob().then(function(blob) { + assert_true(blob instanceof Blob); + }); + }); +}, "Getting blob after getting the Response body - not disturbed, not locked"); + +promise_test(function() { + return createResponseWithReadableStream(function(response) { + return response.text().then(function(text) { + assert_true(text.length > 0); + }); + }); +}, "Getting text after getting the Response body - not disturbed, not locked"); + +promise_test(function() { + return createResponseWithReadableStream(function(response) { + return response.json().then(function(json) { + assert_true(typeof json === "object"); + }); + }); +}, "Getting json after getting the Response body - not disturbed, not locked"); + +promise_test(function() { + return createResponseWithReadableStream(function(response) { + return response.arrayBuffer().then(function(arrayBuffer) { + assert_true(arrayBuffer.byteLength > 0); + }); + }); +}, "Getting arrayBuffer after getting the Response body - not disturbed, not locked"); + + </script> + </body> +</html> diff --git a/testing/web-platform/tests/fetch/api/response/response-stream-disturbed-2.html b/testing/web-platform/tests/fetch/api/response/response-stream-disturbed-2.html new file mode 100644 index 000000000..a1e2f3f24 --- /dev/null +++ b/testing/web-platform/tests/fetch/api/response/response-stream-disturbed-2.html @@ -0,0 +1,48 @@ +<!doctype html> +<html> + <head> + <meta charset="utf-8"> + <title>Consuming Response body after getting a ReadableStream</title> + <meta name="help" href="https://fetch.spec.whatwg.org/#response"> + <meta name="help" href="https://fetch.spec.whatwg.org/#body-mixin"> + <meta name="author" title="Canon Research France" href="https://www.crf.canon.fr"> + <script src="/resources/testharness.js"></script> + <script src="/resources/testharnessreport.js"></script> + </head> + <body> + <script> + +function createResponseWithLockedReadableStream(callback) { + return fetch("../resources/data.json").then(function(response) { + var reader = response.body.getReader(); + return callback(response); + }); +} + +promise_test(function(test) { + return createResponseWithLockedReadableStream(function(response) { + return promise_rejects(test, new TypeError(), response.blob()); + }); +}, "Getting blob after getting a locked Response body"); + +promise_test(function(test) { + return createResponseWithLockedReadableStream(function(response) { + return promise_rejects(test, new TypeError(), response.text()); + }); +}, "Getting text after getting a locked Response body"); + +promise_test(function(test) { + return createResponseWithLockedReadableStream(function(response) { + return promise_rejects(test, new TypeError(), response.json()); + }); +}, "Getting json after getting a locked Response body"); + +promise_test(function(test) { + return createResponseWithLockedReadableStream(function(response) { + return promise_rejects(test, new TypeError(), response.arrayBuffer()); + }); +}, "Getting arrayBuffer after getting a locked Response body"); + + </script> + </body> +</html> diff --git a/testing/web-platform/tests/fetch/api/response/response-stream-disturbed-3.html b/testing/web-platform/tests/fetch/api/response/response-stream-disturbed-3.html new file mode 100644 index 000000000..8d9212514 --- /dev/null +++ b/testing/web-platform/tests/fetch/api/response/response-stream-disturbed-3.html @@ -0,0 +1,49 @@ +<!doctype html> +<html> + <head> + <meta charset="utf-8"> + <title>Consuming Response body after getting a ReadableStream</title> + <meta name="help" href="https://fetch.spec.whatwg.org/#response"> + <meta name="help" href="https://fetch.spec.whatwg.org/#body-mixin"> + <meta name="author" title="Canon Research France" href="https://www.crf.canon.fr"> + <script src="/resources/testharness.js"></script> + <script src="/resources/testharnessreport.js"></script> + </head> + <body> + <script> + +function createResponseWithDisturbedReadableStream(callback) { + return fetch("../resources/data.json").then(function(response) { + var reader = response.body.getReader(); + reader.read(); + return callback(response); + }); +} + +promise_test(function(test) { + return createResponseWithDisturbedReadableStream(function(response) { + return promise_rejects(test, new TypeError(), response.blob()); + }); +}, "Getting blob after reading the Response body"); + +promise_test(function(test) { + return createResponseWithDisturbedReadableStream(function(response) { + return promise_rejects(test, new TypeError(), response.text()); + }); +}, "Getting text after reading the Response body"); + +promise_test(function(test) { + return createResponseWithDisturbedReadableStream(function(response) { + return promise_rejects(test, new TypeError(), response.json()); + }); +}, "Getting json after reading the Response body"); + +promise_test(function(test) { + return createResponseWithDisturbedReadableStream(function(response) { + return promise_rejects(test, new TypeError(), response.arrayBuffer()); + }); +}, "Getting arrayBuffer after reading the Response body"); + + </script> + </body> +</html> diff --git a/testing/web-platform/tests/fetch/api/response/response-stream-disturbed-4.html b/testing/web-platform/tests/fetch/api/response/response-stream-disturbed-4.html new file mode 100644 index 000000000..e74699211 --- /dev/null +++ b/testing/web-platform/tests/fetch/api/response/response-stream-disturbed-4.html @@ -0,0 +1,48 @@ +<!doctype html> +<html> + <head> + <meta charset="utf-8"> + <title>Consuming Response body after getting a ReadableStream</title> + <meta name="help" href="https://fetch.spec.whatwg.org/#response"> + <meta name="help" href="https://fetch.spec.whatwg.org/#body-mixin"> + <meta name="author" title="Canon Research France" href="https://www.crf.canon.fr"> + <script src="/resources/testharness.js"></script> + <script src="/resources/testharnessreport.js"></script> + </head> + <body> + <script> + +function createResponseWithCancelledReadableStream(callback) { + return fetch("../resources/data.json").then(function(response) { + response.body.cancel(); + return callback(response); + }); +} + +promise_test(function(test) { + return createResponseWithCancelledReadableStream(function(response) { + return promise_rejects(test, new TypeError(), response.blob()); + }); +}, "Getting blob after cancelling the Response body"); + +promise_test(function(test) { + return createResponseWithCancelledReadableStream(function(response) { + return promise_rejects(test, new TypeError(), response.text()); + }); +}, "Getting text after cancelling the Response body"); + +promise_test(function(test) { + return createResponseWithCancelledReadableStream(function(response) { + return promise_rejects(test, new TypeError(), response.json()); + }); +}, "Getting json after cancelling the Response body"); + +promise_test(function(test) { + return createResponseWithCancelledReadableStream(function(response) { + return promise_rejects(test, new TypeError(), response.arrayBuffer()); + }); +}, "Getting arrayBuffer after cancelling the Response body"); + + </script> + </body> +</html> diff --git a/testing/web-platform/tests/fetch/api/response/response-stream-disturbed-5.html b/testing/web-platform/tests/fetch/api/response/response-stream-disturbed-5.html new file mode 100644 index 000000000..546b7b888 --- /dev/null +++ b/testing/web-platform/tests/fetch/api/response/response-stream-disturbed-5.html @@ -0,0 +1,49 @@ +<!doctype html> +<html> + <head> + <meta charset="utf-8"> + <title>Consuming Response body after getting a ReadableStream</title> + <meta name="help" href="https://fetch.spec.whatwg.org/#response"> + <meta name="help" href="https://fetch.spec.whatwg.org/#body-mixin"> + <meta name="author" title="Canon Research France" href="https://www.crf.canon.fr"> + <script src="/resources/testharness.js"></script> + <script src="/resources/testharnessreport.js"></script> + </head> + <body> + <script> + +promise_test(function() { + return fetch("../resources/data.json").then(function(response) { + response.blob(); + assert_not_equals(response.body, null); + assert_throws(new TypeError(), function() { response.body.getReader(); }); + }); +}, "Getting a body reader after consuming as blob"); + +promise_test(function() { + return fetch("../resources/data.json").then(function(response) { + response.text(); + assert_not_equals(response.body, null); + assert_throws(new TypeError(), function() { response.body.getReader(); }); + }); +}, "Getting a body reader after consuming as text"); + +promise_test(function() { + return fetch("../resources/data.json").then(function(response) { + response.json(); + assert_not_equals(response.body, null); + assert_throws(new TypeError(), function() { response.body.getReader(); }); + }); +}, "Getting a body reader after consuming as json"); + +promise_test(function() { + return fetch("../resources/data.json").then(function(response) { + response.arrayBuffer(); + assert_not_equals(response.body, null); + assert_throws(new TypeError(), function() { response.body.getReader(); }); + }); +}, "Getting a body reader after consuming as arrayBuffer"); + + </script> + </body> +</html> |