Skip to content

Commit

Permalink
XHR: reduce try/catch usage
Browse files Browse the repository at this point in the history
This also improves checking for the correct exception by a bit.
  • Loading branch information
annevk committed May 9, 2018
1 parent da28291 commit 9b96b14
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 57 deletions.
8 changes: 2 additions & 6 deletions xhr/access-control-basic-denied.htm
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,8 @@
const xhr = new XMLHttpRequest;

xhr.open("GET", get_host_info().HTTP_REMOTE_ORIGIN + path, false);
try {
xhr.send();
assert_unreached("Error should occur here");
} catch(e) {
assert_equals(xhr.status, 0);
}
assert_throws("NetworkError", () => xhr.send());
assert_equals(xhr.status, 0);
}, "Cross-origin request denied");
</script>
</body>
Expand Down
9 changes: 2 additions & 7 deletions xhr/access-control-basic-get-fail-non-simple.htm
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,8 @@
xhr.setRequestHeader("x-test", "foobar");

// This fails because the server-side script is not prepared for an OPTIONS request
try {
xhr.send();
} catch(e) {
assert_equals(xhr.status, 0);
return;
}
assert_unreached("Preflighted request was not denied.");
assert_throws("NetworkError", () => xhr.send());
assert_equals(xhr.status, 0);
}, "Preflighted cross-origin request denied");
</script>
</body>
Expand Down
9 changes: 2 additions & 7 deletions xhr/access-control-basic-non-cors-safelisted-content-type.htm
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,8 @@
"/xhr/resources/access-control-basic-put-allow.py", false);
xhr.setRequestHeader("Content-Type", "application/xml");

try {
xhr.send("FAIL: PUT data received");
} catch(e) {
assert_equals(xhr.status, 0, "Cross-domain access was denied in 'send'.");
return;
}
assert_unreached("Cross-domain access was not denied in 'send'.");
assert_throws("NetworkError", () => xhr.send("FAIL: PUT data received"));
assert_equals(xhr.status, 0, "Cross-domain access was denied in 'send'.");
}, "Deny cross-origin request with non-CORS-safelisted content type");
</script>
</body>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,8 @@

xhr.setRequestHeader("Content-Type", "application/xml");

try {
xhr.send();
} catch(e) {
assert_equals(xhr.status, 0, "Cross-domain access was denied in 'send'.");
return;
}
assert_unreached("Cross-domain access was not denied in 'send'.");
assert_throws("NetworkError", () => xhr.send());
assert_equals(xhr.status, 0, "Cross-domain access was denied in 'send'.");
}, "CORS request with non-safelisted content type sends preflight and fails");
</script>
</body>
Expand Down
15 changes: 5 additions & 10 deletions xhr/access-control-preflight-sync-header-denied.htm
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,12 @@
xhr.open("GET", url + "&command=header", false);
xhr.setRequestHeader("x-test", "foo");

try {
xhr.send();
} catch(e) {
xhr = new XMLHttpRequest;
xhr.open("GET", url + "&command=complete", false);
xhr.send();
assert_equals(xhr.responseText, "Request successfully blocked.");
return;
}
assert_throws("NetworkError", () => xhr.send());

assert_unreached("Cross-domain access with custom header allowed without throwing exception");
xhr = new XMLHttpRequest;
xhr.open("GET", url + "&command=complete", false);
xhr.send();
assert_equals(xhr.responseText, "Request successfully blocked.");
}, "Sync request denied at preflight");
</script>
</body>
Expand Down
15 changes: 5 additions & 10 deletions xhr/access-control-preflight-sync-method-denied.htm
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,12 @@
xhr = new XMLHttpRequest;
xhr.open("DELETE", url + "&command=method", false);

try {
xhr.send();
} catch(e) {
xhr = new XMLHttpRequest;
xhr.open("GET", url + "&command=complete", false);
xhr.send();
assert_equals(xhr.responseText, "Request successfully blocked.");
return;
}
assert_throws("NetworkError", () => xhr.send());

assert_unreached("Cross-domain access with non-CORS-safelisted method allowed without throwing exception");
xhr = new XMLHttpRequest;
xhr.open("GET", url + "&command=complete", false);
xhr.send();
assert_equals(xhr.responseText, "Request successfully blocked.");
});
</script>
</body>
Expand Down
15 changes: 5 additions & 10 deletions xhr/access-control-preflight-sync-not-supported.htm
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,12 @@
xhr = new XMLHttpRequest;
xhr.open("PUT", url, false);

try {
xhr.send("");
} catch(e) {
xhr = new XMLHttpRequest;
xhr.open("GET", url + "&command=complete", false);
xhr.send();
assert_equals(xhr.responseText, "Request successfully blocked.");
return;
}
assert_throws("NetworkError", () => xhr.send(""));

assert_unreached("Cross-domain access allowed without throwing exception");
xhr = new XMLHttpRequest;
xhr.open("GET", url + "&command=complete", false);
xhr.send();
assert_equals(xhr.responseText, "Request successfully blocked.");
});
</script>
</body>
Expand Down

0 comments on commit 9b96b14

Please sign in to comment.