Skip to content

Commit

Permalink
Improving scope of various fetch API tests (web-platform-tests#3643)
Browse files Browse the repository at this point in the history
* Adding tests for upload of ArrayBufferView. Adding data-url fetch for various fetch modes. Fixing missing returned promise in cors-origins.js. Fixing FormData tests to still work when FormData is not defined. Adding new request disturbed tests.

* style
  • Loading branch information
youennf authored and jdm committed Sep 6, 2016
1 parent d1026e2 commit 6b34250
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 11 deletions.
14 changes: 9 additions & 5 deletions fetch/api/basic/request-headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function checkContentType(contentType, body)
}

var expectedContentType = "text/plain;charset=UTF-8";
if(body === null || body instanceof ArrayBuffer)
if(body === null || body instanceof ArrayBuffer || body.buffer instanceof ArrayBuffer)
expectedContentType = null;
else if (body instanceof Blob)
expectedContentType = body.type ? body.type : null;
Expand All @@ -27,16 +27,15 @@ function requestHeaders(desc, url, method, body, expectedOrigin, expectedContent
body = body();
if (body)
requestInit["body"] = body;

return fetch(url + urlParameters, requestInit).then(function(resp) {
assert_equals(resp.status, 200, "HTTP status is 200");
assert_equals(resp.type , "basic", "Response's type is basic");
checkContentType(resp.headers.get("x-request-content-type"), body);
if (expectedContentLength !== undefined)
assert_equals(resp.headers.get("x-request-content-length") , expectedContentLength, "Request should have header content-length: " + expectedContentLength);
assert_true(resp.headers.has("x-request-user-agent"), "Request has header user-agent");
assert_false(resp.headers.has("accept-charset"), "Request has header accept-charset");
assert_equals(resp.headers.get("x-request-origin") , expectedOrigin, "Request should have header origin: " + expectedOrigin);
if (expectedContentLength !== undefined)
assert_equals(resp.headers.get("x-request-content-length") , expectedContentLength, "Request should have header content-length: " + expectedContentLength);
checkContentType(resp.headers.get("x-request-content-type"), body);
});
}, desc);
}
Expand All @@ -52,6 +51,11 @@ requestHeaders("Fetch with POST with text body", url, "POST", "Request's body",
requestHeaders("Fetch with POST with FormData body", url, "POST", function() { return new FormData(); }, location.origin);
requestHeaders("Fetch with POST with Blob body", url, "POST", new Blob(["Test"]), location.origin, "4");
requestHeaders("Fetch with POST with ArrayBuffer body", url, "POST", new ArrayBuffer(4), location.origin, "4");
requestHeaders("Fetch with POST with Uint8Array body", url, "POST", new Uint8Array(4), location.origin, "4");
requestHeaders("Fetch with POST with Int8Array body", url, "POST", new Int8Array(4), location.origin, "4");
requestHeaders("Fetch with POST with Float32Array body", url, "POST", new Float32Array(1), location.origin, "4");
requestHeaders("Fetch with POST with Float64Array body", url, "POST", new Float64Array(1), location.origin, "8");
requestHeaders("Fetch with POST with DataView body", url, "POST", new DataView(new ArrayBuffer(8), 0, 4), location.origin, "4");
requestHeaders("Fetch with POST with Blob body with mime type", url, "POST", new Blob(["Test"], { type: "text/maybe" }), location.origin, "4");
requestHeaders("Fetch with Chicken", url, "Chicken", null, location.origin, null);
requestHeaders("Fetch with Chicken with body", url, "Chicken", "Request's body", location.origin, "14");
Expand Down
13 changes: 10 additions & 3 deletions fetch/api/basic/scheme-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ if (this.document === undefined) {
importScripts("../resources/utils.js");
}

function checkFetchResponse(url, data, mime) {
function checkFetchResponse(url, data, mime, fetchMode) {
var cut = (url.length >= 40) ? "[...]" : "";
desc = "Fetching " + url.substring(0, 40) + cut + " is OK"
desc = "Fetching " + url.substring(0, 40) + cut + " is OK";
var init = { };
if (fetchMode) {
init.mode = fetchMode;
desc += " (" + fetchMode + ")";
}
promise_test(function(test) {
return fetch(url).then(function(resp) {
return fetch(url, init).then(function(resp) {
assert_equals(resp.status, 200, "HTTP status is 200");
assert_equals(resp.statusText, "OK", "HTTP statusText is OK");
assert_equals(resp.type, "basic", "response type is basic");
Expand All @@ -20,6 +25,8 @@ function checkFetchResponse(url, data, mime) {
}

checkFetchResponse("data:,response%27s%20body", "response's body", "text/plain;charset=US-ASCII");
checkFetchResponse("data:,response%27s%20body", "response's body", "text/plain;charset=US-ASCII", "same-origin");
checkFetchResponse("data:,response%27s%20body", "response's body", "text/plain;charset=US-ASCII", "cors");
checkFetchResponse("data:text/plain;base64,cmVzcG9uc2UncyBib2R5", "response's body", "text/plain");
checkFetchResponse("data:image/png;base64,cmVzcG9uc2UncyBib2R5",
"response's body",
Expand Down
2 changes: 1 addition & 1 deletion fetch/api/cors/cors-origin.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function corsOrigin(desc, baseURL, method, origin, shouldPass) {
var requestInit = {"mode": "cors", "method": method};

promise_test(function(test) {
fetch(RESOURCES_DIR + "clean-stash.py?token=" + uuid_token).then(function(resp) {
return fetch(RESOURCES_DIR + "clean-stash.py?token=" + uuid_token).then(function(resp) {
assert_equals(resp.status, 200, "Clean stash response's status is 200");
if (shouldPass) {
return fetch(url + urlParameters, requestInit).then(function(resp) {
Expand Down
4 changes: 2 additions & 2 deletions fetch/api/request/request-consume.html
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@
}, "Consume " + bodyType + " request's body as JSON");
}

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" });

Expand Down Expand Up @@ -117,6 +115,8 @@
checkRequestBody(new DataView(getArrayBufferWithZeros(), 1, 8), string, "DataView");

promise_test(function(test) {
var formData = new FormData();
formData.append("name", "value")
var request = new Request("", {"method": "POST", "body": formData });
assert_false(request.bodyUsed, "bodyUsed is false at init");
return checkBodyFormData(request, formData);
Expand Down
22 changes: 22 additions & 0 deletions fetch/api/request/request-disturbed.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,28 @@
assert_true(bodyConsumed.bodyUsed , "bodyUsed is true when request is disturbed");
return promise_rejects(test, new TypeError(), bodyConsumed.blob());
}, "Check consuming a disturbed request");

test(function() {
var req = new Request(URL, {method: 'POST', body: 'hello'});
assert_false(req.bodyUsed,
'Request should not be flagged as used if it has not been ' +
'consumed.');
assert_throws(new TypeError(),
function() { new Request(req, {method: 'GET'}); },
'A get request may not have body.');

assert_false(req.bodyUsed, 'After the GET case');

assert_throws(new TypeError(),
function() { new Request(req, {method: 'CONNECT'}); },
'Request() with a forbidden method must throw.');

assert_false(req.bodyUsed, 'After the forbidden method case');

var req2 = new Request(req);
assert_true(req.bodyUsed,
'Request should be flagged as used if it has been consumed.');
}, 'Request construction failure should not set "bodyUsed"');
</script>
</body>
</html>

0 comments on commit 6b34250

Please sign in to comment.