20

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.

Community
  • 1
  • 1
Peter Maidens
  • 363
  • 1
  • 2
  • 10

5 Answers5

42

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.)

davguij
  • 568
  • 5
  • 11
6

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);
};
Macil
  • 2,916
  • 1
  • 19
  • 17
3

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

Macil
  • 2,916
  • 1
  • 19
  • 17
  • 9
    You can also do that choosing **Offline** checkbox in Chrome's **Network** in **Tools > Developer Tools**. – Terbiy Oct 11 '16 at 08:14
1

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.

Matti Virkkunen
  • 58,926
  • 7
  • 111
  • 152
-7

Just type at the brower a changed URL, e.g. the well formed URL e.g. http://thedomain.com/welcome/ by another placing "XX": http://thedomain.com/welcomeXX/ , that will cause a 404 error (not found)