2

In my project I build some of my HTTP requests like so:

var options = {
    params:{
        foo: 'bar'
        hello: world
    }
};
$http.get("my/service", options)

Which means that the final HTTP call looks something like my/service?foo=bar&hello=worldVar

How do I setup my $httpBackend to account for this?

The problems I see are:

  1. I'm not guaranteed order in my parameters with this style, which means setting up the first parameter in expectGet will be hard.
  2. Its hard to test calls when I really don't care about the parameters
David says Reinstate Monica
  • 19,209
  • 22
  • 79
  • 122

2 Answers2

0

The best solution I've found to this is to use the override of expect/when that accepts a function, and to mix it in with this answer. That gets me to something like

$httpBackend.expectGET(function(url){
    var parser = document.createElement('a');
    parser.href=url;
    return parser.search.indexOf('foo=bar') > -1 && 
           parser.search.indexOf('hello=world') > -1;
})

or if I don't care about the parameters I can just test for parser.pathname.

However, this still isnt perfect because its a pain to test that I dont have extra parameters, so Im still actively looking for an alternative solution.

Community
  • 1
  • 1
David says Reinstate Monica
  • 19,209
  • 22
  • 79
  • 122
0

if you don't care about the parameters, you can just use regex to match to the static part of the URL:

$httpBackend.whenGET(/my\/service/)