Forcing an HTTP request to fail in browser

21,911

Solution 1

In Chrome (just checked v63), you can actually block a specific URL (or even a whole domain) from the Network tab. You only need to right-click on the entry and select Block request URL (or Block request domain.)

Solution 2

One possible solution is to modify the XMLHttpRequest objects that will be used by the browser. Running this code in a javascript console will cause all future AJAX calls on the page to be redirected to a different URL (which will probably give a 404 error):

XMLHttpRequest.prototype._old_open =
  XMLHttpRequest.prototype._old_open || XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = function(method, url, async, user, pass) {
  return XMLHttpRequest.prototype._old_open.call(
    this, method, 'TEST-'+url, async, user, pass);
};

Solution 3

Don't overlook the simplest solution: disconnect your computer from the Internet, and then trigger the AJAX call.

Solution 4

Chrome's dev tools have an option to "beautify" (i.e. re-indent) minified JavaScript (press the "{}" button at the bottom left). This can be combined with the "XHR breakpoint" option to break when the request is made. XHR breakpoints don't support modifying the response though AFAIK, but you should be able to find a way to do it via code.

Share:
21,911
Peter Maidens
Author by

Peter Maidens

Updated on January 23, 2020

Comments

  • Peter Maidens
    Peter Maidens over 4 years

    Is it possible to make an http request that has been sent to a server by the browser fail without having to alter the javascript?

    I have a POST request that my website is sending to the server and we are trying to test how our code reacts when the request fails (e.g. an HTTP 500 response). Unfortunately, the environment that I need to test it in has uglified and compressed javascript, so inserting a breakpoint or altering the javascript isn't an option. Is there a way for us to utilize any browser to simulate a failed request?

    The request takes a long time to complete, so using the browser's console to run a javascript command is a possibility.

    I have tried using window.stop(), however, this does not work since I need to failure code to execute.

    I am aware of the option of setting up a proxy server, but would like to avoid this is possible.