0

I've bumped into some very strange behaviour when trying to mock server-api while performing e2e tests.

Here's my code:

$httpBackend.whenPOST(...).respond(function () {
           return [200, {success:true, errors: []}];
});

This part works fine, but when I turn success = false like so:

$httpBackend.whenPOST(...).respond(function () {
           return [200, {success:false, errors: [ { ... } ]}];
});

the response code becomes 500, something I don't want to happen.

Interestingly, when I change the key 'success' to something else (like 'sukcess'), the response code remains 200 and everything works as expected.

Does $httpBackend look at the json response and alter the response code if it finds the key 'success' equals false in the response data? If so this is very strange behaviour. Has anyone encountered something like this before? And if so, is there anything I can do to avoid this behaviour other than changing the word 'success' in all of my json responses?

Gil Moshayof
  • 16,633
  • 4
  • 47
  • 58
  • Are you doing unit tests or end-to-end tests? What angular version are you using? I tested your code in e2e and it works fine. Also, as far as I know, the httpbackend does not check for any 'magic' properties in the response data. I also looked in the angular source and couldn't find anything like it. Angular decides that a request is success or failure only by looking at the status code – Tiborg Dec 17 '14 at 13:27
  • I'm doing end to end tests, and I'm using Angular 1.3 – Gil Moshayof Dec 17 '14 at 14:22
  • I switched to angular 1.3, and I'm still not able to reproduce the problem. Maybe try and set up a jsfiddle / plunker, or make a demo somewhere, so I can see exactly what's happening. – Tiborg Dec 17 '14 at 14:41
  • I wish I could... but the project I'm working on is classified, so I can't. However, you gave me a lead by making me realise that there's probably some custom code written somewhere in the system that's doing this. I shall investigate. – Gil Moshayof Dec 17 '14 at 16:33

0 Answers0