Skip to content

Commit

Permalink
Use async XHR and test that non-2XX preflight responses raise errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ferjm committed Mar 11, 2017
1 parent 9ba7fa9 commit 7bb0bd3
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 47 deletions.
54 changes: 54 additions & 0 deletions cors/preflight-failure.htm
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<!DOCTYPE html>
<meta charset=utf-8>
<title>CORS - Preflight responds with non-2XX status code</title>
<meta name=author title="Fernando Jiménez Moreno" href="mailto:ferjmoreno@gmail.com">

<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script src=support.js?pipe=sub></script>

<h1>Preflight responds with non-2XX status code</h1>

<div id=log></div>
<script>

// Request count for cache busting and easy identifying of request in traffic
// analyzer.
var req_c = 0;

var CROSSDOMAIN_URL = CROSSDOMAIN + 'resources/cors-makeheader.py?';

/*
* Redirection with preflights.
*/
function preflight_failure(code) {
var desc = 'Should throw error if preflight respond with ' + code;
async_test(desc).step(function() {
var client = new XMLHttpRequest();
var redirect =
encodeURIComponent(CROSSDOMAIN_URL + 'headers=x-test&' + req_c++);

client.open('GET',
CROSSDOMAIN_URL + 'headers=x-test&location=' + redirect
+ '&code=' + code + '&preflight=' + code
+ '&' + req_c++,
true /* async */);
client.setRequestHeader('x-test', 'test');
client.onerror = this.step_func(function() {
this.done();
});
client.onreadystatechange = this.step_func(function() {
assert_equals(client.status, 0);
});
client.onload = this.step_func(function() {
assert_unreached('Unexpected onload');
});
client.send(null);
});
}
[100, 101,
300, 301, 302, 303, 304, 305, 306, 307, 308,
400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 414, 415, 416, 417,
500, 501, 502, 503, 504, 505].forEach(preflight_failure);

</script>
67 changes: 20 additions & 47 deletions cors/redirect-preflight.htm
Original file line number Diff line number Diff line change
Expand Up @@ -19,56 +19,29 @@ <h1>Redirect with preflight</h1>

var CROSSDOMAIN_URL = CROSSDOMAIN + 'resources/cors-makeheader.py?';

/*
* Redirection with preflights.
*/
function redir_preflight(code) {
test(function() {
var client = new XMLHttpRequest();
var redirect =
encodeURIComponent(CROSSDOMAIN_URL + 'headers=x-test&' + req_c++);

client.open('GET',
CROSSDOMAIN_URL + 'headers=x-test&location=' + redirect
+ '&code=' + code + '&preflight=' + code
+ '&' + req_c++,
false);
client.setRequestHeader('x-test', 'test');
assert_throws(null, function() { client.send(null) });

}, 'Redirect ' + code + ' on preflight');
}
redir_preflight(301);
redir_preflight(302);
redir_preflight(303);
redir_preflight(307);
redir_preflight(308);

/*
* Redirection after successfull (200) preflight.
*/
function redir_after_preflight(code) {
test(function() {
var client = new XMLHttpRequest();
var redirect = encodeURIComponent(
CROSSDOMAIN + 'resources/cors-makeheader.py?headers=x-test&' + req_c++
);

client.open('GET', CROSSDOMAIN + 'resources/cors-makeheader.py?'
+ 'preflight=200&headers=x-test&location='
+ redirect + '&code=' + code + '&' + req_c++,
false);
client.setRequestHeader('x-test', 'test');
client.send(null);
assert_equals(client.status, 200, "Successfull redirect");

},
'Allow redirect ' + code + ' after succesful (200) preflight');
function redir_after_successfull_preflight(code) {
var desc = 'Should allow redirect ' + code + ' after succesful (200) preflight';
async_test(desc).step(function() {
var client = new XMLHttpRequest();
var redirect = encodeURIComponent(
CROSSDOMAIN + 'resources/cors-makeheader.py?headers=x-test&' + req_c++
);

client.open('GET', CROSSDOMAIN + 'resources/cors-makeheader.py?'
+ 'preflight=200&headers=x-test&location='
+ redirect + '&code=' + code + '&' + req_c++,
true /* async */);
client.setRequestHeader('x-test', 'test');
client.onreadystatechange = this.step_func(function() {
assert_equals(client.status, 200, 'Successfull redirect');
this.done();
});
client.send(null);
});
}
redir_after_preflight(301);
redir_after_preflight(302);
redir_after_preflight(303);
redir_after_preflight(307);
redir_after_preflight(308);
[301, 302, 303, 307, 308].forEach(redir_after_successfull_preflight);

</script>

0 comments on commit 7bb0bd3

Please sign in to comment.