Skip to content

Truly independent web browser

Tests/LibWeb: Import WPT request body tests for redirects and clones

Luke Wilde67593ed29b30
diff --git a/Tests/LibWeb/TestConfig.ini b/Tests/LibWeb/TestConfig.ini
index 0e837c5c6bd..d6d49e46764 100644
--- a/Tests/LibWeb/TestConfig.ini
+++ b/Tests/LibWeb/TestConfig.ini
@@ -776,6 +776,9 @@ Crash/HTML/fallback-favicon-invalid-base.html
; Tests HTTP Preflight Cache
Text/input/wpt-import/fetch/api/cors/cors-preflight-cache.any.html
+; Tests HTTP redirects.
+Text/input/wpt-import/fetch/api/redirect/redirect-method.any.html
+
; Trusted Types requires tuple origin.
; Fails in other browsers too when loaded from file://.
Text/input/wpt-import/trusted-types/TrustedTypePolicyFactory-getAttributeType-event-handler-content-attributes.tentative.html
diff --git a/Tests/LibWeb/Text/expected/wpt-import/fetch/api/redirect/redirect-method.any.txt b/Tests/LibWeb/Text/expected/wpt-import/fetch/api/redirect/redirect-method.any.txt
new file mode 100644
index 00000000000..c895afea45c
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/fetch/api/redirect/redirect-method.any.txt
@@ -0,0 +1,21 @@
+Harness status: OK
+
+Found 15 tests
+
+14 Pass
+1 Fail
+Pass Response.redirected should be false on not-redirected responses
+Pass Redirect 301 with GET
+Pass Redirect 301 with POST
+Pass Redirect 301 with HEAD
+Pass Redirect 302 with GET
+Pass Redirect 302 with POST
+Pass Redirect 302 with HEAD
+Pass Redirect 303 with GET
+Pass Redirect 303 with POST
+Pass Redirect 303 with HEAD
+Fail Redirect 303 with TESTING
+Pass Redirect 307 with GET
+Pass Redirect 307 with POST (string body)
+Pass Redirect 307 with POST (blob body)
+Pass Redirect 307 with HEAD
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/fetch/api/request/request-disturbed.any.txt b/Tests/LibWeb/Text/expected/wpt-import/fetch/api/request/request-disturbed.any.txt
new file mode 100644
index 00000000000..4d70c617516
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/fetch/api/request/request-disturbed.any.txt
@@ -0,0 +1,15 @@
+Harness status: OK
+
+Found 9 tests
+
+6 Pass
+3 Fail
+Pass Request's body: initial state
+Pass Request without body cannot be disturbed
+Pass Check cloning a disturbed request
+Pass Check creating a new request from a disturbed request
+Pass Check creating a new request with a new body from a disturbed request
+Fail Input request used for creating new request became disturbed
+Fail Input request used for creating new request became disturbed even if body is not used
+Pass Check consuming a disturbed request
+Fail Request construction failure should not set "bodyUsed"
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/wpt-import/fetch/api/redirect/redirect-method.any.html b/Tests/LibWeb/Text/input/wpt-import/fetch/api/redirect/redirect-method.any.html
new file mode 100644
index 00000000000..b2b08752b31
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/fetch/api/redirect/redirect-method.any.html
@@ -0,0 +1,15 @@
+<!doctype html>
+<meta charset=utf-8>
+
+<script>
+self.GLOBAL = {
+ isWindow: function() { return true; },
+ isWorker: function() { return false; },
+ isShadowRealm: function() { return false; },
+};
+</script>
+<script src="../../../resources/testharness.js"></script>
+<script src="../../../resources/testharnessreport.js"></script>
+<script src="../resources/utils.js"></script>
+<div id=log></div>
+<script src="../../../fetch/api/redirect/redirect-method.any.js"></script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/fetch/api/redirect/redirect-method.any.js b/Tests/LibWeb/Text/input/wpt-import/fetch/api/redirect/redirect-method.any.js
new file mode 100644
index 00000000000..9fe086a9db7
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/fetch/api/redirect/redirect-method.any.js
@@ -0,0 +1,112 @@
+// META: global=window,worker
+// META: script=../resources/utils.js
+
+// Creates a promise_test that fetches a URL that returns a redirect response.
+//
+// |opts| has additional options:
+// |opts.body|: the request body as a string or blob (default is empty body)
+// |opts.expectedBodyAsString|: the expected response body as a string. The
+// server is expected to echo the request body. The default is the empty string
+// if the request after redirection isn't POST; otherwise it's |opts.body|.
+// |opts.expectedRequestContentType|: the expected Content-Type of redirected
+// request.
+function redirectMethod(desc, redirectUrl, redirectLocation, redirectStatus, method, expectedMethod, opts) {
+ let url = redirectUrl;
+ let urlParameters = "?redirect_status=" + redirectStatus;
+ urlParameters += "&location=" + encodeURIComponent(redirectLocation);
+
+ let requestHeaders = {
+ "Content-Encoding": "Identity",
+ "Content-Language": "en-US",
+ "Content-Location": "foo",
+ };
+ let requestInit = {"method": method, "redirect": "follow", "headers" : requestHeaders};
+ opts = opts || {};
+ if (opts.body) {
+ requestInit.body = opts.body;
+ }
+
+ promise_test(function(test) {
+ return fetch(url + urlParameters, requestInit).then(function(resp) {
+ let expectedRequestContentType = "NO";
+ if (opts.expectedRequestContentType) {
+ expectedRequestContentType = opts.expectedRequestContentType;
+ }
+
+ assert_equals(resp.status, 200, "Response's status is 200");
+ assert_equals(resp.type, "basic", "Response's type basic");
+ assert_equals(
+ resp.headers.get("x-request-method"),
+ expectedMethod,
+ "Request method after redirection is " + expectedMethod);
+ let hasRequestBodyHeader = true;
+ if (opts.expectedStripRequestBodyHeader) {
+ hasRequestBodyHeader = !opts.expectedStripRequestBodyHeader;
+ }
+ assert_equals(
+ resp.headers.get("x-request-content-type"),
+ expectedRequestContentType,
+ "Request Content-Type after redirection is " + expectedRequestContentType);
+ [
+ "Content-Encoding",
+ "Content-Language",
+ "Content-Location"
+ ].forEach(header => {
+ let xHeader = "x-request-" + header.toLowerCase();
+ let expectedValue = hasRequestBodyHeader ? requestHeaders[header] : "NO";
+ assert_equals(
+ resp.headers.get(xHeader),
+ expectedValue,
+ "Request " + header + " after redirection is " + expectedValue);
+ });
+ assert_true(resp.redirected);
+ return resp.text().then(function(text) {
+ let expectedBody = "";
+ if (expectedMethod == "POST") {
+ expectedBody = opts.expectedBodyAsString || requestInit.body;
+ }
+ let expectedContentLength = expectedBody ? expectedBody.length.toString() : "NO";
+ assert_equals(text, expectedBody, "request body");
+ assert_equals(
+ resp.headers.get("x-request-content-length"),
+ expectedContentLength,
+ "Request Content-Length after redirection is " + expectedContentLength);
+ });
+ });
+ }, desc);
+}
+
+promise_test(function(test) {
+ assert_false(new Response().redirected);
+ return fetch(RESOURCES_DIR + "method.py").then(function(resp) {
+ assert_equals(resp.status, 200, "Response's status is 200");
+ assert_false(resp.redirected);
+ });
+}, "Response.redirected should be false on not-redirected responses");
+
+var redirUrl = RESOURCES_DIR + "redirect.py";
+var locationUrl = "method.py";
+
+const stringBody = "this is my body";
+const blobBody = new Blob(["it's me the blob!", " ", "and more blob!"]);
+const blobBodyAsString = "it's me the blob! and more blob!";
+
+redirectMethod("Redirect 301 with GET", redirUrl, locationUrl, 301, "GET", "GET");
+redirectMethod("Redirect 301 with POST", redirUrl, locationUrl, 301, "POST", "GET", { body: stringBody, expectedStripRequestBodyHeader: true });
+redirectMethod("Redirect 301 with HEAD", redirUrl, locationUrl, 301, "HEAD", "HEAD");
+
+redirectMethod("Redirect 302 with GET", redirUrl, locationUrl, 302, "GET", "GET");
+redirectMethod("Redirect 302 with POST", redirUrl, locationUrl, 302, "POST", "GET", { body: stringBody, expectedStripRequestBodyHeader: true });
+redirectMethod("Redirect 302 with HEAD", redirUrl, locationUrl, 302, "HEAD", "HEAD");
+
+redirectMethod("Redirect 303 with GET", redirUrl, locationUrl, 303, "GET", "GET");
+redirectMethod("Redirect 303 with POST", redirUrl, locationUrl, 303, "POST", "GET", { body: stringBody, expectedStripRequestBodyHeader: true });
+redirectMethod("Redirect 303 with HEAD", redirUrl, locationUrl, 303, "HEAD", "HEAD");
+redirectMethod("Redirect 303 with TESTING", redirUrl, locationUrl, 303, "TESTING", "GET", { expectedStripRequestBodyHeader: true });
+
+redirectMethod("Redirect 307 with GET", redirUrl, locationUrl, 307, "GET", "GET");
+redirectMethod("Redirect 307 with POST (string body)", redirUrl, locationUrl, 307, "POST", "POST", { body: stringBody , expectedRequestContentType: "text/plain;charset=UTF-8"});
+redirectMethod("Redirect 307 with POST (blob body)", redirUrl, locationUrl, 307, "POST", "POST", { body: blobBody, expectedBodyAsString: blobBodyAsString });
+redirectMethod("Redirect 307 with HEAD", redirUrl, locationUrl, 307, "HEAD", "HEAD");
+
+done();
diff --git a/Tests/LibWeb/Text/input/wpt-import/fetch/api/request/request-disturbed.any.html b/Tests/LibWeb/Text/input/wpt-import/fetch/api/request/request-disturbed.any.html
new file mode 100644
index 00000000000..1c2938d0841
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/fetch/api/request/request-disturbed.any.html
@@ -0,0 +1,15 @@
+<!doctype html>
+<meta charset=utf-8>
+<title>Request disturbed</title>
+<script>
+self.GLOBAL = {
+ isWindow: function() { return true; },
+ isWorker: function() { return false; },
+ isShadowRealm: function() { return false; },
+};
+</script>
+<script src="../../../resources/testharness.js"></script>
+<script src="../../../resources/testharnessreport.js"></script>
+<script src="../resources/utils.js"></script>
+<div id=log></div>
+<script src="../../../fetch/api/request/request-disturbed.any.js"></script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/fetch/api/request/request-disturbed.any.js b/Tests/LibWeb/Text/input/wpt-import/fetch/api/request/request-disturbed.any.js
new file mode 100644
index 00000000000..8a11de78ff6
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/fetch/api/request/request-disturbed.any.js
@@ -0,0 +1,109 @@
+// META: global=window,worker
+// META: title=Request disturbed
+// META: script=../resources/utils.js
+
+var initValuesDict = {"method" : "POST",
+ "body" : "Request's body"
+};
+
+var noBodyConsumed = new Request("");
+var bodyConsumed = new Request("", initValuesDict);
+
+test(() => {
+ assert_equals(noBodyConsumed.body, null, "body's default value is null");
+ assert_false(noBodyConsumed.bodyUsed , "bodyUsed is false when request is not disturbed");
+ assert_not_equals(bodyConsumed.body, null, "non-null body");
+ assert_true(bodyConsumed.body instanceof ReadableStream, "non-null body type");
+ assert_false(noBodyConsumed.bodyUsed, "bodyUsed is false when request is not disturbed");
+}, "Request's body: initial state");
+
+noBodyConsumed.blob();
+bodyConsumed.blob();
+
+test(function() {
+ assert_false(noBodyConsumed.bodyUsed , "bodyUsed is false when request is not disturbed");
+ try {
+ noBodyConsumed.clone();
+ } catch (e) {
+ assert_unreached("Can use request not disturbed for creating or cloning request");
+ }
+}, "Request without body cannot be disturbed");
+
+test(function() {
+ assert_true(bodyConsumed.bodyUsed , "bodyUsed is true when request is disturbed");
+ assert_throws_js(TypeError, function() { bodyConsumed.clone(); });
+}, "Check cloning a disturbed request");
+
+test(function() {
+ assert_true(bodyConsumed.bodyUsed , "bodyUsed is true when request is disturbed");
+ assert_throws_js(TypeError, function() { new Request(bodyConsumed); });
+}, "Check creating a new request from a disturbed request");
+
+promise_test(function() {
+ assert_true(bodyConsumed.bodyUsed , "bodyUsed is true when request is disturbed");
+ const originalBody = bodyConsumed.body;
+ const bodyReplaced = new Request(bodyConsumed, { body: "Replaced body" });
+ assert_not_equals(bodyReplaced.body, originalBody, "new request's body is new");
+ assert_false(bodyReplaced.bodyUsed, "bodyUsed is false when request is not disturbed");
+ return bodyReplaced.text().then(text => {
+ assert_equals(text, "Replaced body");
+ });
+}, "Check creating a new request with a new body from a disturbed request");
+
+promise_test(function() {
+ var bodyRequest = new Request("", initValuesDict);
+ const originalBody = bodyRequest.body;
+ assert_false(bodyRequest.bodyUsed , "bodyUsed is false when request is not disturbed");
+ var requestFromRequest = new Request(bodyRequest);
+ assert_true(bodyRequest.bodyUsed , "bodyUsed is true when request is disturbed");
+ assert_equals(bodyRequest.body, originalBody, "body should not change");
+ assert_not_equals(originalBody, undefined, "body should not be undefined");
+ assert_not_equals(originalBody, null, "body should not be null");
+ assert_not_equals(requestFromRequest.body, originalBody, "new request's body is new");
+ return requestFromRequest.text().then(text => {
+ assert_equals(text, "Request's body");
+ });
+}, "Input request used for creating new request became disturbed");
+
+promise_test(() => {
+ const bodyRequest = new Request("", initValuesDict);
+ const originalBody = bodyRequest.body;
+ assert_false(bodyRequest.bodyUsed , "bodyUsed is false when request is not disturbed");
+ const requestFromRequest = new Request(bodyRequest, { body : "init body" });
+ assert_true(bodyRequest.bodyUsed , "bodyUsed is true when request is disturbed");
+ assert_equals(bodyRequest.body, originalBody, "body should not change");
+ assert_not_equals(originalBody, undefined, "body should not be undefined");
+ assert_not_equals(originalBody, null, "body should not be null");
+ assert_not_equals(requestFromRequest.body, originalBody, "new request's body is new");
+
+ return requestFromRequest.text().then(text => {
+ assert_equals(text, "init body");
+ });
+}, "Input request used for creating new request became disturbed even if body is not used");
+
+promise_test(function(test) {
+ assert_true(bodyConsumed.bodyUsed , "bodyUsed is true when request is disturbed");
+ return promise_rejects_js(test, 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_js(TypeError,
+ function() { new Request(req, {method: 'GET'}); },
+ 'A get request may not have body.');
+
+ assert_false(req.bodyUsed, 'After the GET case');
+
+ assert_throws_js(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"');
diff --git a/Tests/LibWeb/Text/input/wpt-import/fetch/api/resources/method.py b/Tests/LibWeb/Text/input/wpt-import/fetch/api/resources/method.py
new file mode 100644
index 00000000000..c1a111b4cdf
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/fetch/api/resources/method.py
@@ -0,0 +1,18 @@
+from wptserve.utils import isomorphic_encode
+
+def main(request, response):
+ headers = []
+ if b"cors" in request.GET:
+ headers.append((b"Access-Control-Allow-Origin", b"*"))
+ headers.append((b"Access-Control-Allow-Credentials", b"true"))
+ headers.append((b"Access-Control-Allow-Methods", b"GET, POST, PUT, FOO"))
+ headers.append((b"Access-Control-Allow-Headers", b"x-test, x-foo"))
+ headers.append((b"Access-Control-Expose-Headers", b"x-request-method"))
+
+ headers.append((b"x-request-method", isomorphic_encode(request.method)))
+ headers.append((b"x-request-content-type", request.headers.get(b"Content-Type", b"NO")))
+ headers.append((b"x-request-content-length", request.headers.get(b"Content-Length", b"NO")))
+ headers.append((b"x-request-content-encoding", request.headers.get(b"Content-Encoding", b"NO")))
+ headers.append((b"x-request-content-language", request.headers.get(b"Content-Language", b"NO")))
+ headers.append((b"x-request-content-location", request.headers.get(b"Content-Location", b"NO")))
+ return headers, request.body
diff --git a/Tests/LibWeb/Text/input/wpt-import/fetch/api/resources/redirect.py b/Tests/LibWeb/Text/input/wpt-import/fetch/api/resources/redirect.py
new file mode 100644
index 00000000000..d52ab5f3eee
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/fetch/api/resources/redirect.py
@@ -0,0 +1,73 @@
+import time
+
+from urllib.parse import urlencode, urlparse
+
+from wptserve.utils import isomorphic_decode, isomorphic_encode
+
+def main(request, response):
+ stashed_data = {b'count': 0, b'preflight': b"0"}
+ status = 302
+ headers = [(b"Content-Type", b"text/plain"),
+ (b"Cache-Control", b"no-cache"),
+ (b"Pragma", b"no-cache")]
+ if b"Origin" in request.headers:
+ headers.append((b"Access-Control-Allow-Origin", request.headers.get(b"Origin", b"")))
+ headers.append((b"Access-Control-Allow-Credentials", b"true"))
+ else:
+ headers.append((b"Access-Control-Allow-Origin", b"*"))
+
+ token = None
+ if b"token" in request.GET:
+ token = request.GET.first(b"token")
+ data = request.server.stash.take(token)
+ if data:
+ stashed_data = data
+
+ if request.method == u"OPTIONS":
+ if b"allow_headers" in request.GET:
+ headers.append((b"Access-Control-Allow-Headers", request.GET[b'allow_headers']))
+ stashed_data[b'preflight'] = b"1"
+ #Preflight is not redirected: return 200
+ if not b"redirect_preflight" in request.GET:
+ if token:
+ request.server.stash.put(request.GET.first(b"token"), stashed_data)
+ return 200, headers, u""
+
+ if b"redirect_status" in request.GET:
+ status = int(request.GET[b'redirect_status'])
+ elif b"redirect_status" in request.POST:
+ status = int(request.POST[b'redirect_status'])
+
+ stashed_data[b'count'] += 1
+
+ if b"location" in request.GET:
+ url = isomorphic_decode(request.GET[b'location'])
+ if b"simple" not in request.GET:
+ scheme = urlparse(url).scheme
+ if scheme == u"" or scheme == u"http" or scheme == u"https":
+ url += u"&" if u'?' in url else u"?"
+ #keep url parameters in location
+ url_parameters = {}
+ for item in request.GET.items():
+ url_parameters[isomorphic_decode(item[0])] = isomorphic_decode(item[1][0])
+ url += urlencode(url_parameters)
+ #make sure location changes during redirection loop
+ url += u"&count=" + str(stashed_data[b'count'])
+ headers.append((b"Location", isomorphic_encode(url)))
+
+ if b"redirect_referrerpolicy" in request.GET:
+ headers.append((b"Referrer-Policy", request.GET[b'redirect_referrerpolicy']))
+
+ if b"delay" in request.GET:
+ time.sleep(float(request.GET.first(b"delay", 0)) / 1E3)
+
+ if token:
+ request.server.stash.put(request.GET.first(b"token"), stashed_data)
+ if b"max_count" in request.GET:
+ max_count = int(request.GET[b'max_count'])
+ #stop redirecting and return count
+ if stashed_data[b'count'] > max_count:
+ # -1 because the last is not a redirection
+ return str(stashed_data[b'count'] - 1)
+
+ return status, headers, u""