2

In my previous angularjs project I used interceptors to intercept the http calls, and to be able to serve mock data instead of the real data from the server. I found it very useful throughout the development process.

My question is, how could I do this without angularjs (In my current project I use another framework, which does not have interceptors)? Is there any other http library out there, that supports this? How could I achive this using jquery's or superagent's http capabilities?

gsanta
  • 754
  • 6
  • 21
  • https://github.com/jakerella/jquery-mockjax – Avinash Babu Feb 28 '15 at 16:20
  • 1
    also read http://verboselogging.com/2010/02/20/hijack-ajax-requests-like-a-terrorist – Angry 84 Feb 28 '15 at 16:20
  • thanks, I found mockjax too, but I'm worried a little bit that it's not actively developed (there are several month old issues there). And it's working only for jquery ajax calls. Is not there a more general solution (for example which works with superagent too)? – gsanta Feb 28 '15 at 16:24
  • 1
    Have a look at http://stackoverflow.com/q/13765031/1048572 – Bergi Feb 28 '15 at 16:37
  • this solution to overwrite the XMLHttpRequest's prototype seems to me a little low-level (I would be more happy with some library). However it is not that complicated, so I will use that if I don't find any better solution. – gsanta Feb 28 '15 at 16:44
  • Found a working example for you, so i made a fiddle – Angry 84 Feb 28 '15 at 16:47
  • 1
    please take a look on pretender https://github.com/trek/pretender – code-jaff Feb 28 '15 at 16:52
  • I could be mistaken, but pretenders restriction of "Pretender will only handle requests to paths (/some/kind/of/path) and not fully qualified URLs " may be an issue here, depends on the usage scope. Otherwise a very handy library – Angry 84 Feb 28 '15 at 17:01
  • Yes, but in my case (and generally for this mocking purpose) it's not an issue I think! So nice library. – gsanta Feb 28 '15 at 17:06

2 Answers2

2

So i found the following script: https://github.com/creotiv/AJAX-calls-intercepter/blob/master/index.html

Here is a live fiddle: http://jsfiddle.net/0eyadb88/1/

I'm not going to go over everything in the script as simply it looks like it does handle the XMLHttpRequest as i commented on. To what extent this works, well that would be just some testing of course and should be able to be expanded.

i've added a non jquery ajax call (testing with chrome here) and it handles that as well.

The main section to pay attention to is

(function (open) {
    XMLHttpRequest.prototype.open = function (method, url, async, user, pass) {
        alert('Intercept');
        open.call(this, method, url + ".ua", async, user, pass);
    };
})(XMLHttpRequest.prototype.open);

Personally i would use this approach unless a decent libary is around, but of course if such a libary exists. please do let us know ;)

Otherwise cleaning up the script or using that one in general should be fairly easy.

Angry 84
  • 2,935
  • 1
  • 25
  • 24
  • Thank you! I'm gonna put it into my app later, and than I accept the answer :) – gsanta Feb 28 '15 at 17:03
  • 1
    Your welcome, finding this script has already given be a few ideas and use for a project i have on the go.. so thanks for your question as well ;) – Angry 84 Feb 28 '15 at 17:05
0

You should check out dfournier/plasticine. I developed this library in order to intercept a request and fake the response or intercept server response and modify it. I use it at work and the backend team is not ready but we already defined the API.

David
  • 56
  • 4