0

I want to use $htppBackend to mock some service. My problem is some of service url have parameter, ex: http://domain/service1?param1=a&param2=b

I need an regex which can reconize http://domain/service1<whatever> is correct for http://domain/service1?param1=a&param2=b. One thing, the first part http://domain/service1 is not a constant, it can be http://domain/service2/sth/anything.

Please help. Thanks.

Edit:

I put my code here to make it easy to understand.

I have 4 api urls:

angular
    .module('moduleName')
    .constant('getApi', {
        attrList: 'http://domain/setup/attrList',
        eventList: 'http://domain/setup/eventList',
        vcList: 'http://domain/api/list1',
        getRaDetails: 'http://domain/abc/getDetails?raId={0}&bookId={1}'
    });

With attrList, eventList and vcList, they are ok with $httpBackend.whenGET(getApi.attrList).respond(responseObject); $httpBackend.whenGET(getApi.eventList).respond(responseObject); $httpBackend.whenGET(getApi.vcList).respond(responseObject);.

But the last one getRaDetails, it doesn't work with $httpBackend.whenGET(getApi.getRaDetails).respond(responseObject); because raId and bookId have different value each times.

Now I need a regex to make this rule - $httpBackend.whenGET(getApi.getRaDetails).respond(responseObject); works with all raId and bookId value.

Hope it can explain my question more clearly. Thanks

Stiger
  • 1,189
  • 2
  • 14
  • 29

3 Answers3

0

I need an regex which can reconize http://domain/service1 is correct for http://domain/service1?param1=a&param2=b

I don't think a regex is required here. Just simply check

var stringPattern = "http://domain/service1";
var input = "http://domain/service1?param1=a&param2=b";

if ( input.indexOf( stringPattern ) == 0 )
{
   alert( "correct" );
}
else
{   
   alert( "Incorrect" );
}
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
  • 1
    that will match on the input of "http://domain/service1/path2" as well, which doesn't seem like what the author wanted. – Paul May 09 '16 at 13:52
  • @Paul I am checking if the `index` is 0. How will it match something else? – gurvinder372 May 09 '16 at 13:53
  • 1
    `http://domain.com/` will have an index of 0 on `http://domain.com/path1` as well as `http://domain.com/path2`, etc. so my point is that if he wants the pattern to match the authority and path of the input, your suggestion won't guarantee that. – Paul May 09 '16 at 13:56
  • @Paul But that is precisely what `stringPattern` will have. If `stringPattern = "http://domain/service1";` how will it match `"http://domain/service2";` ? – gurvinder372 May 09 '16 at 13:58
  • @AdamKonieska By the look of it OP is looking to match a URL (hence a regex is requested). So if URL has the pattern it should match. Not sure if OP only want string after `?` – gurvinder372 May 09 '16 at 14:01
  • @Paul it will but I am checking `index == 0`, so it will still show incorrect – gurvinder372 May 09 '16 at 14:01
  • 1
    No, it won't. I just tested it to make sure I'm not crazy. `"http://domain/service1/path2".indexOf("http://domain/service1")` will, in fact, equal 0. because the input starts with the pattern. – Paul May 09 '16 at 14:04
  • @Paul That is fine. OP has specifically mentioned `http://domain/service1`. Please check. – gurvinder372 May 09 '16 at 14:05
  • 1
    I think we're reading the question differently. I think he means that the authority and path of the input URL should match the authority and path of the provided pattern, regardless of querystring values. Not that the input should simply start with the URL pattern. – Paul May 09 '16 at 14:09
  • @Paul `I think we're reading the question differently.` Yes apparently :). Maybe we will just agree to disagree on it. You can post your answer having the solution which you just described. – gurvinder372 May 09 '16 at 14:10
  • @gurvinder372, @Paul: thanks for looking at my question. It's what I mean `I think he means that the authority and path of the input URL should match the authority and path of the provided pattern, regardless of querystring values. Not that the input should simply start with the URL pattern`. Sorry because of my bad English. I update question with my real code for more details. Thanks. – Stiger May 09 '16 at 15:54
  • @gurvinder372: I need a regex to make $httpBackend understand that, it's easy to check but I don't know how to put `$httpBackend.whenGET(regex).respond(responseObject);` in `alert( "correct" );` – Stiger May 09 '16 at 22:03
0

So, if you are parsing Urls, it's best to use the URL object in JavaScript:

var input = 'http://domain/abc/getDetails?raId={0}&bookId={1}';
var test = 'http://domain/abc/getDetails';

var testUrl = new URL(test);
var inputUrl = new URL(input);

then test the various bits:

if(testUrl.pathname === inputUrl.pathname){
   //  they both have /abc/getDetails as the path
}
Paul
  • 35,689
  • 11
  • 93
  • 122
  • thanks for your answer, but I need a regex for `$httpBackend` understand that. – Stiger May 09 '16 at 22:01
  • No you don't, from the documentation: "HTTP url or function that receives a url and returns true if the url matches the current definition." https://docs.angularjs.org/api/ngMock/service/$httpBackend – Paul May 10 '16 at 00:13
0

This's my working code:

_(getApis).each(function (api) {
                var val = api.value.split('?')[0].replace(/\//g, '\\/'),
                    reg = new RegExp(val);

                $httpBackend.whenGET(reg).respond(dummy[api.key]);
            });

Thanks all.

Stiger
  • 1,189
  • 2
  • 14
  • 29