atecarlos / protractor-http-mock

A library designed to work alongside Protractor for setting up mocks for your HTTP ajax requests.
MIT License
173 stars 70 forks source link

mock identification for other interceptors #113

Closed Timtam closed 7 years ago

Timtam commented 7 years ago

since protractor-http-mock executes the request() method of the interceptors on it's own, it might happen that some fragile interceptors might work incorrectly when called more often than once per request (1 time by e.g. $http.get and one more time by protractor-http-mock). Since all protractor-http-mock request() calls now contain the mock = 1 property, these interceptors can filter the protractor-http-mock request calls and therefore work correctly.

atecarlos commented 7 years ago

Hi @Timtam .

Thanks for the PR. I'm not quite sold on this feature though. It's generally not a good idea to have to modify your actual application code to handle a testing scenario such as this. Would the interceptor not get called twice when the app is running normally? One for the $http.get call and one for the call you are actually mocking. Maybe I'm missing something here or there is another bug with interceptors getting called twice.

Would you mind providing more information? Thanks!

atecarlos commented 7 years ago

An alternative could also be to extend the plugin functionality to allow for some decoration on received config, as opposed to having it built into the main code.

Timtam commented 7 years ago

Hi @atecarlos,

of course. The thing I noticed was, that, as you said, the $http.get method calls the request function of any interceptor once, and, as soon as it hits protractor-http-mock, protractor-http-mocks takes all interceptors and calls their request() method a second time in order to mock correctly. That's fine, at least for its functionality. The problem I had was that the interceptor we're using at work counts all requests going through and also all responses coming back. Since now requests are coming in twice, but only one response call is made for not currently mocked urls, this amount isn't stable anymore. That's where I began thinking.

I thought it would be useful to be able to detect if a request coming in or a response is currently mocked, not just for my case, but maybe some other interceptor might want to know that to handle some special case (this shouldn't be the case, but yeah, some really ugly things exist out there), and so I added the mock: 1 attribute into all request and response objects delivered by protractor-http-mock so interceptors can, if they want, determine which request/response is currently mocked or not. That's actually all I did. I thought it might be useful to take into the main project, since it's actually just this little attribute I added.

atecarlos commented 7 years ago

Hi @Timtam . I don't have a working Angular app at the moment, but I've been doing some testing here locally with the example app for this project. So to be clear, there is a repeated call only when there isn't a match.

So protractor-http-mock would first run the request provided to $http.get through all the interceptors before it matches as you described. If it doesn't find a match from that, then it will pass the call through to the native Angular $http.get. At that point, yes, it would call it a second time.

That doesn't quite match what you are referring to though, so I want to be clear on what's going on exactly so we can think of the best solution.