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
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.
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;}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With