Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Match all GET url using angular-mocks for backendless

I am writing a frontend without backend ajax for now. I am using angular-mocks to simulate API call like this:

$httpBackend.when('GET', '/somelink').respond(function(method, url, data) {
  //do something
});

However, if the ajax passes params: {id:12345}, it will append to the url to be '/somelink?id=12345'. That doesn't catch by the when('GET', '/somelink')

Is there a way to use RegEx or some trick to work around this? Just so that regardless of what is inside params, the respond() still gets called?

Thanks.

UPDATE 1:

I cannot use .whenGET because my backendless system has POST and PUT as well. So I need to keep it generic. This two params .when('GET', '/somelink') are actually variables in my code.

Since '/somelink' is a variable in another JSON, having RegEx /\/somelink/ in JSON doesn't seem to work. At least that's what I see for now.

like image 399
HP. Avatar asked Feb 24 '14 09:02

HP.


1 Answers

Yes you can use a regex like this:

$httpBackend.whenGET(/\/somelink/).respond(function(method, url, data) {
    //do something
});

EDIT

ok, you can do:

var method = 'GET';
var url = '/somelink';
$httpBackend.when(method, new RegExp('\\' + url)).respond(function(method, url, data) {
    //do something
});
like image 165
kamilkp Avatar answered Oct 18 '22 17:10

kamilkp