Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(42)

Unified Diff: third_party/WebKit/LayoutTests/http/tests/xmlhttprequest/simple-cross-origin-progress-events.html

Issue 2427553002: Fix XHR's logic to determine whether or not to dispatch upload progress events
Patch Set: a Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/LayoutTests/http/tests/xmlhttprequest/simple-cross-origin-progress-events.html
diff --git a/third_party/WebKit/LayoutTests/http/tests/xmlhttprequest/simple-cross-origin-progress-events.html b/third_party/WebKit/LayoutTests/http/tests/xmlhttprequest/simple-cross-origin-progress-events.html
index 4735be930570526bc560c4b9b8168c1613d7e224..a0cbadf0a7c5bf2868794acdbb7415740aa8a4e5 100644
--- a/third_party/WebKit/LayoutTests/http/tests/xmlhttprequest/simple-cross-origin-progress-events.html
+++ b/third_party/WebKit/LayoutTests/http/tests/xmlhttprequest/simple-cross-origin-progress-events.html
@@ -1,127 +1,167 @@
<!doctype html>
-<html>
-<body>
-<p>Test that upload progress events are not dispatched for simple cross-origin requests
-(i.e. if the listener is set after calling send(), and there are no other reasons to make a preflight request).</p>
-<pre id="console"></pre>
+<script src="/resources/testharness.js"></script>
+<script src="/resources/testharnessreport.js"></script>
<script>
-if (window.testRunner) {
- testRunner.dumpAsText();
- testRunner.waitUntilDone();
-}
-
-function log(message)
-{
- document.getElementById('console').appendChild(document.createTextNode(message + '\n'));
-}
-
-var sawProgress;
-var sawUploadProgress;
-
-function progress(evt)
-{
- if (!sawProgress)
- log("onprogress");
- sawProgress = true;
-}
-
-function allowedUploadProgress()
-{
- if (sawUploadProgress)
- return;
-
- sawUploadProgress = true;
- log("upload.onprogress");
-}
-
-function notAllowedUploadProgress()
-{
- if (sawUploadProgress)
- return;
-
- sawUploadProgress = true;
- log("FAIL: upload.onprogress");
-}
+// A simple cross-origin request means a cross-origin request with only
+// CORS-safelisted method and request-headers.
// Build a long string.
var stringToSend = "a";
for (var i = 0; i < 23; ++i)
- stringToSend += stringToSend;
-
-function test1()
-{
- sawProgress = false;
- sawUploadProgress = false;
-
- log("Test 1: The URL is allowed for cross-origin requests");
-
- var xhr = new XMLHttpRequest();
- xhr.onprogress = progress;
- xhr.open("POST", "http://localhost:8000/xmlhttprequest/resources/cross-site-progress-events.cgi?allow", true);
- xhr.setRequestHeader("Content-Type", "text/plain");
- xhr.send(stringToSend);
- xhr.upload.onloadstart = function() { log("FAIL: upload.onloadstart"); };
- xhr.upload.onprogress = notAllowedUploadProgress;
- xhr.upload.onload = function() { log("FAIL: upload.onload"); };
- xhr.upload.onerror = function() { log("FAIL: upload.onerror"); };
- xhr.onerror = function() { log("onerror") };
- xhr.onload = function() {
- log("onload");
- log("Response length: " + xhr.responseText.length);
- test2();
- };
-}
-
-function test2()
-{
- sawProgress = false;
- sawUploadProgress = false;
-
- log("\nTest 2: The URL is not allowed for cross-origin requests");
-
- var xhr = new XMLHttpRequest();
- xhr.onprogress = progress;
- xhr.open("POST", "http://localhost:8000/xmlhttprequest/resources/cross-site-progress-events.cgi", true);
- xhr.setRequestHeader("Content-Type", "text/plain");
- xhr.send(stringToSend);
- xhr.upload.onloadstart = function() { log("FAIL: upload.onloadstart"); };
- xhr.upload.onprogress = notAllowedUploadProgress;
- xhr.upload.onload = function() { log("FAIL: upload.onload"); };
- xhr.upload.onerror = function() { log("FAIL: upload.onerror"); };
- xhr.onload = function() { log("onload"); };
- xhr.onerror = function() {
- log("onerror (expected)");
- log("Response length: " + xhr.responseText.length);
- test3();
- };
-}
-
-function test3()
-{
- sawProgress = false;
- sawUploadProgress = false;
-
- log("\nTest 3: The URL is not allowed for cross-origin requests and at least one upload event is listened for before doing the send.");
-
- var xhr = new XMLHttpRequest();
- xhr.onprogress = progress;
- xhr.open("POST", "http://localhost:8000/xmlhttprequest/resources/cross-site-progress-events.cgi", true);
- xhr.setRequestHeader("Content-Type", "text/plain");
- xhr.upload.onloadstart = function() { log("upload.onloadstart"); };
- xhr.send(stringToSend);
- xhr.upload.onprogress = allowedUploadProgress;
- xhr.upload.onload = function() { log("FAIL: upload.onload"); };
- xhr.upload.onerror = function() { log("upload.onerror (expected)"); };
- xhr.onload = function() { log("onload"); }
- xhr.onerror = function() {
- log("onerror (expected)");
- log("Response length: " + xhr.responseText.length);
- if (window.testRunner)
- testRunner.notifyDone();
- };
-}
-
-test1();
+ stringToSend += stringToSend;
+
+async_test(test => {
+ const xhr = new XMLHttpRequest();
+ let onprogressCalled;
+ xhr.onprogress = test.step_func(() => {
+ onprogressCalled = true;
+ });
+ xhr.onload = test.step_func(() => {
+ assert_true(onprogressCalled, 'onprogress is called');
+
+ assert_equals(xhr.responseText.length, 2 ** 23, 'responseText.length');
+
+ test.done();
+ });
+ xhr.onerror = test.step_func(() => {
+ assert_unreached('Unexpected onerror call');
+ });
+
+ xhr.open("POST", "http://localhost:8000/xmlhttprequest/resources/cross-site-progress-events.cgi?allow", true);
+ xhr.setRequestHeader("Content-Type", "text/plain");
+ xhr.send(stringToSend);
+
+ // Set handlers after send()
+ const upload = xhr.upload;
+ upload.onloadstart = test.step_func(() => {
+ assert_unreached('Unexpected upload.onloadstart call');
+ });
+ upload.onprogress = test.step_func(() => {
+ assert_unreached('Unexpected upload.onprogress call');
+ });
+ upload.onload = test.step_func(() => {
+ assert_unreached('Unexpected upload.onload call');
+ });
+ upload.onerror = test.step_func(() => {
+ assert_unreached('Unexpected upload.onerror call');
+ });
+}, 'Upload progress events should not be dispatched for a simple cross-origin request when no handler is set before send() even if the response includes the Access-Control-Allow-Origin header');
+
+async_test(test => {
+ const xhr = new XMLHttpRequest();
+
+ let onprogressCalled;
+ xhr.onprogress = test.step_func(() => {
+ onprogressCalled = true;
+ });
+ xhr.onload = test.step_func(() => {
+ assert_true(onprogressCalled, 'onprogress is called');
+
+ assert_equals(xhr.responseText.length, 2 ** 23, 'responseText.length');
+
+ test.done();
+ });
+ xhr.onerror = test.step_func(() => {
+ assert_unreached('Unexpected onerror call');
+ });
+
+ xhr.open("POST", "http://localhost:8000/xmlhttprequest/resources/cross-site-progress-events.cgi?allow", true);
+ xhr.setRequestHeader("Content-Type", "text/plain");
+ xhr.send(stringToSend);
+
+ // Set handlers after send()
+ const upload = xhr.upload;
+ upload.onloadstart = test.step_func(() => {
+ assert_unreached('Unexpected upload.onloadstart call');
+ });
+ upload.onprogress = test.step_func(() => {
+ assert_unreached('Unexpected upload.onprogress call');
+ });
+ upload.onload = test.step_func(() => {
+ assert_unreached('Unexpected upload.onload call');
+ });
+ upload.onerror = test.step_func(() => {
+ assert_unreached('Unexpected upload.onerror call');
+ });
+}, 'Upload progress events should be dispatched for a simple cross-origin request');
+
+async_test(test => {
+ const xhr = new XMLHttpRequest();
+ let onprogressCalled;
+ xhr.onprogress = test.step_func(() => {
+ onprogressCalled = true;
+ });
+ xhr.open("POST", "http://localhost:8000/xmlhttprequest/resources/cross-site-progress-events.cgi", true);
+ xhr.setRequestHeader("Content-Type", "text/plain");
+
+ // Set one handler before send()
+ const upload = xhr.upload;
+ let uploadOnloadstartCalled;
+ upload.onloadstart = test.step_func(() => {
+ uploadOnloadstartCalled = true;
+ });
+ xhr.onload = test.step_func(() => {
+ assert_unreached('Unexpected onload call');
+ });
+ xhr.onerror = test.step_func(() => {
+ // Since cross-site-progress-events.cgi doesn't accept OPTIONS requests,
+ // the XHR should fail.
+
+ assert_true(onprogressCalled, 'onprogress is called');
+
+ assert_true(uploadOnloadstartCalled, 'upload.onloadstart is called');
+ assert_true(uploadOnprogressCalled, 'upload.onprogress is called');
+ assert_true(uploadOnerrorCalled, 'upload.onerror is called');
+
+ assert_equals(xhr.responseText.length, 0, 'responseText.length');
+
+ test.done();
+ });
+
+ xhr.send(stringToSend);
+
+ let uploadOnprogressCalled;
+ upload.onprogress = test.step_func(() => {
+ uploadOnprogressCalled = true;
+ });
+ upload.onload = test.step_func(() => {
+ assert_unreached('Unexpected upload.onload call');
+ });
+ let onerrorCalled;
+ upload.onerror = test.step_func(() => {
+ uploadOnerrorCalled = true;
+ });
+}, 'Upload progress events should be dispatched for a simple cross-origin request when at least one handler is set before send()');
+
+async_test(test => {
+ const xhr = new XMLHttpRequest();
+ let onprogressCalled;
+ xhr.onprogress = test.step_func(() => {
+ onprogressCalled = true;
+ });
+ xhr.onload = test.step_func(() => {
+ assert_true(onprogressCalled, 'onprogress is called');
+
+ assert_true(uploadOnprogressCalled, 'upload.onprogress is called');
+ assert_true(uploadOnloadCalled, 'upload.onload is called');
+
+ test.done();
+ });
+
+ // Set handlers after send()
+ const upload = xhr.upload;
+ let uploadOnprogressCalled;
+ upload.onprogress = test.step_func(() => {
+ uploadOnprogressCalled = true;
+ });
+ let onloadCalled;
+ upload.onload = test.step_func(() => {
+ uploadOnloadCalled = true;
+ });
+
+ // Use the script which responds to OPTIONS requests.
+ xhr.open("POST", "http://localhost:8000/xmlhttprequest/resources/access-control-basic-allow-no-credentials.cgi", true);
+ xhr.setRequestHeader("Content-Type", "text/plain");
+ xhr.send(stringToSend);
+}, 'Upload progress events should be dispatched for a simple cross-origin request');
</script>
-</body>
-</html>

Powered by Google App Engine
This is Rietveld 408576698