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

Side by Side 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 unified diff | Download patch
OLDNEW
1 <!doctype html> 1 <!doctype html>
2 <html> 2 <script src="/resources/testharness.js"></script>
3 <body> 3 <script src="/resources/testharnessreport.js"></script>
4 <p>Test that upload progress events are not dispatched for simple cross-origin r equests
5 (i.e. if the listener is set after calling send(), and there are no other reason s to make a preflight request).</p>
6 <pre id="console"></pre>
7 <script> 4 <script>
8 if (window.testRunner) { 5 // A simple cross-origin request means a cross-origin request with only
9 testRunner.dumpAsText(); 6 // CORS-safelisted method and request-headers.
10 testRunner.waitUntilDone();
11 }
12
13 function log(message)
14 {
15 document.getElementById('console').appendChild(document.createTextNode(messa ge + '\n'));
16 }
17
18 var sawProgress;
19 var sawUploadProgress;
20
21 function progress(evt)
22 {
23 if (!sawProgress)
24 log("onprogress");
25 sawProgress = true;
26 }
27
28 function allowedUploadProgress()
29 {
30 if (sawUploadProgress)
31 return;
32
33 sawUploadProgress = true;
34 log("upload.onprogress");
35 }
36
37 function notAllowedUploadProgress()
38 {
39 if (sawUploadProgress)
40 return;
41
42 sawUploadProgress = true;
43 log("FAIL: upload.onprogress");
44 }
45 7
46 // Build a long string. 8 // Build a long string.
47 var stringToSend = "a"; 9 var stringToSend = "a";
48 for (var i = 0; i < 23; ++i) 10 for (var i = 0; i < 23; ++i)
49 stringToSend += stringToSend; 11 stringToSend += stringToSend;
50 12
51 function test1() 13 async_test(test => {
52 { 14 const xhr = new XMLHttpRequest();
53 sawProgress = false; 15 let onprogressCalled;
54 sawUploadProgress = false; 16 xhr.onprogress = test.step_func(() => {
17 onprogressCalled = true;
18 });
19 xhr.onload = test.step_func(() => {
20 assert_true(onprogressCalled, 'onprogress is called');
55 21
56 log("Test 1: The URL is allowed for cross-origin requests"); 22 assert_equals(xhr.responseText.length, 2 ** 23, 'responseText.length');
57 23
58 var xhr = new XMLHttpRequest(); 24 test.done();
59 xhr.onprogress = progress; 25 });
60 xhr.open("POST", "http://localhost:8000/xmlhttprequest/resources/cross-site- progress-events.cgi?allow", true); 26 xhr.onerror = test.step_func(() => {
61 xhr.setRequestHeader("Content-Type", "text/plain"); 27 assert_unreached('Unexpected onerror call');
62 xhr.send(stringToSend); 28 });
63 xhr.upload.onloadstart = function() { log("FAIL: upload.onloadstart"); };
64 xhr.upload.onprogress = notAllowedUploadProgress;
65 xhr.upload.onload = function() { log("FAIL: upload.onload"); };
66 xhr.upload.onerror = function() { log("FAIL: upload.onerror"); };
67 xhr.onerror = function() { log("onerror") };
68 xhr.onload = function() {
69 log("onload");
70 log("Response length: " + xhr.responseText.length);
71 test2();
72 };
73 }
74 29
75 function test2() 30 xhr.open("POST", "http://localhost:8000/xmlhttprequest/resources/cross-site-pr ogress-events.cgi?allow", true);
76 { 31 xhr.setRequestHeader("Content-Type", "text/plain");
77 sawProgress = false; 32 xhr.send(stringToSend);
78 sawUploadProgress = false;
79 33
80 log("\nTest 2: The URL is not allowed for cross-origin requests"); 34 // Set handlers after send()
35 const upload = xhr.upload;
36 upload.onloadstart = test.step_func(() => {
37 assert_unreached('Unexpected upload.onloadstart call');
38 });
39 upload.onprogress = test.step_func(() => {
40 assert_unreached('Unexpected upload.onprogress call');
41 });
42 upload.onload = test.step_func(() => {
43 assert_unreached('Unexpected upload.onload call');
44 });
45 upload.onerror = test.step_func(() => {
46 assert_unreached('Unexpected upload.onerror call');
47 });
48 }, 'Upload progress events should not be dispatched for a simple cross-origin re quest when no handler is set before send() even if the response includes the Acc ess-Control-Allow-Origin header');
81 49
82 var xhr = new XMLHttpRequest(); 50 async_test(test => {
83 xhr.onprogress = progress; 51 const xhr = new XMLHttpRequest();
84 xhr.open("POST", "http://localhost:8000/xmlhttprequest/resources/cross-site- progress-events.cgi", true);
85 xhr.setRequestHeader("Content-Type", "text/plain");
86 xhr.send(stringToSend);
87 xhr.upload.onloadstart = function() { log("FAIL: upload.onloadstart"); };
88 xhr.upload.onprogress = notAllowedUploadProgress;
89 xhr.upload.onload = function() { log("FAIL: upload.onload"); };
90 xhr.upload.onerror = function() { log("FAIL: upload.onerror"); };
91 xhr.onload = function() { log("onload"); };
92 xhr.onerror = function() {
93 log("onerror (expected)");
94 log("Response length: " + xhr.responseText.length);
95 test3();
96 };
97 }
98 52
99 function test3() 53 let onprogressCalled;
100 { 54 xhr.onprogress = test.step_func(() => {
101 sawProgress = false; 55 onprogressCalled = true;
102 sawUploadProgress = false; 56 });
57 xhr.onload = test.step_func(() => {
58 assert_true(onprogressCalled, 'onprogress is called');
103 59
104 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."); 60 assert_equals(xhr.responseText.length, 2 ** 23, 'responseText.length');
105 61
106 var xhr = new XMLHttpRequest(); 62 test.done();
107 xhr.onprogress = progress; 63 });
108 xhr.open("POST", "http://localhost:8000/xmlhttprequest/resources/cross-site- progress-events.cgi", true); 64 xhr.onerror = test.step_func(() => {
109 xhr.setRequestHeader("Content-Type", "text/plain"); 65 assert_unreached('Unexpected onerror call');
110 xhr.upload.onloadstart = function() { log("upload.onloadstart"); }; 66 });
111 xhr.send(stringToSend);
112 xhr.upload.onprogress = allowedUploadProgress;
113 xhr.upload.onload = function() { log("FAIL: upload.onload"); };
114 xhr.upload.onerror = function() { log("upload.onerror (expected)"); };
115 xhr.onload = function() { log("onload"); }
116 xhr.onerror = function() {
117 log("onerror (expected)");
118 log("Response length: " + xhr.responseText.length);
119 if (window.testRunner)
120 testRunner.notifyDone();
121 };
122 }
123 67
124 test1(); 68 xhr.open("POST", "http://localhost:8000/xmlhttprequest/resources/cross-site-pr ogress-events.cgi?allow", true);
69 xhr.setRequestHeader("Content-Type", "text/plain");
70 xhr.send(stringToSend);
71
72 // Set handlers after send()
73 const upload = xhr.upload;
74 upload.onloadstart = test.step_func(() => {
75 assert_unreached('Unexpected upload.onloadstart call');
76 });
77 upload.onprogress = test.step_func(() => {
78 assert_unreached('Unexpected upload.onprogress call');
79 });
80 upload.onload = test.step_func(() => {
81 assert_unreached('Unexpected upload.onload call');
82 });
83 upload.onerror = test.step_func(() => {
84 assert_unreached('Unexpected upload.onerror call');
85 });
86 }, 'Upload progress events should be dispatched for a simple cross-origin reques t');
87
88 async_test(test => {
89 const xhr = new XMLHttpRequest();
90 let onprogressCalled;
91 xhr.onprogress = test.step_func(() => {
92 onprogressCalled = true;
93 });
94 xhr.open("POST", "http://localhost:8000/xmlhttprequest/resources/cross-site-pr ogress-events.cgi", true);
95 xhr.setRequestHeader("Content-Type", "text/plain");
96
97 // Set one handler before send()
98 const upload = xhr.upload;
99 let uploadOnloadstartCalled;
100 upload.onloadstart = test.step_func(() => {
101 uploadOnloadstartCalled = true;
102 });
103 xhr.onload = test.step_func(() => {
104 assert_unreached('Unexpected onload call');
105 });
106 xhr.onerror = test.step_func(() => {
107 // Since cross-site-progress-events.cgi doesn't accept OPTIONS requests,
108 // the XHR should fail.
109
110 assert_true(onprogressCalled, 'onprogress is called');
111
112 assert_true(uploadOnloadstartCalled, 'upload.onloadstart is called');
113 assert_true(uploadOnprogressCalled, 'upload.onprogress is called');
114 assert_true(uploadOnerrorCalled, 'upload.onerror is called');
115
116 assert_equals(xhr.responseText.length, 0, 'responseText.length');
117
118 test.done();
119 });
120
121 xhr.send(stringToSend);
122
123 let uploadOnprogressCalled;
124 upload.onprogress = test.step_func(() => {
125 uploadOnprogressCalled = true;
126 });
127 upload.onload = test.step_func(() => {
128 assert_unreached('Unexpected upload.onload call');
129 });
130 let onerrorCalled;
131 upload.onerror = test.step_func(() => {
132 uploadOnerrorCalled = true;
133 });
134 }, 'Upload progress events should be dispatched for a simple cross-origin reques t when at least one handler is set before send()');
135
136 async_test(test => {
137 const xhr = new XMLHttpRequest();
138 let onprogressCalled;
139 xhr.onprogress = test.step_func(() => {
140 onprogressCalled = true;
141 });
142 xhr.onload = test.step_func(() => {
143 assert_true(onprogressCalled, 'onprogress is called');
144
145 assert_true(uploadOnprogressCalled, 'upload.onprogress is called');
146 assert_true(uploadOnloadCalled, 'upload.onload is called');
147
148 test.done();
149 });
150
151 // Set handlers after send()
152 const upload = xhr.upload;
153 let uploadOnprogressCalled;
154 upload.onprogress = test.step_func(() => {
155 uploadOnprogressCalled = true;
156 });
157 let onloadCalled;
158 upload.onload = test.step_func(() => {
159 uploadOnloadCalled = true;
160 });
161
162 // Use the script which responds to OPTIONS requests.
163 xhr.open("POST", "http://localhost:8000/xmlhttprequest/resources/access-contro l-basic-allow-no-credentials.cgi", true);
164 xhr.setRequestHeader("Content-Type", "text/plain");
165 xhr.send(stringToSend);
166 }, 'Upload progress events should be dispatched for a simple cross-origin reques t');
125 </script> 167 </script>
126 </body>
127 </html>
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698