Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to set a wildcard for $httpBackend responses?

Suppose I have the following test code in AngularJS:

var someURL;
var dummyJSON;
$httpBackend.whenGET(someURL).respond(dummyJSON);

Is there a way of making this the response for a set of URLs rather than just one? For example, I'd like it to respond with the same dummy JSON for ANY url that starts with /api/, but not ones that start with /app/. Like a wildcard URL ("/app/*") ?

Thanks for the help

like image 745
r.bilgil Avatar asked Dec 30 '13 21:12

r.bilgil


2 Answers

I've had luck with expressions like whenGET(/^\/api\//) – which means starts with /api/. In the regex, ^ will match the start of the string and \/ will match a literal / in the URL.

If that doesn't match your requests, try whenGET(/\/api\//) which should match absolute URLs as well as relative URLs.

like image 165
paulmelnikow Avatar answered Oct 21 '22 22:10

paulmelnikow


You can also use a function if you don't want to deal with regex. Something like:

$httpBackend.when("GET", function(url) {return url.indexOf("/api/") === 0;}
like image 35
morgancodes Avatar answered Oct 21 '22 22:10

morgancodes