Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use regex in AngularJS $httpBackend ExpectGET

I'm passing a url as a parameter in a $resource GET request. Angular is url encoding this parameter and matching the request in the $httpBackend.expectGET method is finicky.

I see that you can use regular expressions to match the expected request but can't get it to work. I'd like to match the resource location but with any "uri" query string parameter value.

Ctrl

var resource = $resource('../api/lookup');
resource.get({ uri: "http://www.something.com" }, function (data) {
  // do something
});

Test

// mock out the $httpBackend response

$httpBackend.expectGET(/\.\.\/api\/lookup\?uri=.*/)).respond(200, { Response: "a response" });

// call my test method here

// ensure the $httpBackend work is done
$rootScope.$apply();
$httpBackend.flush();

// do assertions

Karma Output

Error: Unexpected request: GET ../api/lookup?uri=http%3A%2F%2Fwww.something.com

Expected GET /../api/lookup?uri=.*/

Can anyone see why my regex isn't being matched?

Edit

Even after improving the regex as suggested I couldn't get this working using the '$var' injection syntax. I rewrote the tests, injecting the dependencies with the inject function and ensuring the order of my test arrangement was correct. Now it's working like a charm!

like image 229
daddywoodland Avatar asked Oct 21 '13 14:10

daddywoodland


People also ask

How do I use HTTP mock in angular?

When an AngularJS application needs some data from a server, it calls the $http service, which sends the request to a real server using $httpBackend service. With dependency injection, it is easy to inject $httpBackend mock (which has the same API as $httpBackend) and use it to verify

What is $httpbackend in ngmock?

- service in module ngMock Overview Fake HTTP backend implementation suitable for unit testing applications that use the $http service. Note: For fake HTTP backend implementation suitable for end-to-end testing or backend-less development please see e2e $httpBackend mock.

What is the difference between respond and expectget methods?

The respond method returns the requestHandlerobject for possible overrides. expectGET(url, [headers], [keys]); Creates a new request expectation for GET requests.

What is the difference between request expectations and backend expectations?

Request Expectations vs Backend Definitions Request expectations provide a way to make assertions about requests made by the application and to define responses for those requests. The test will fail if the expected requests are not made or they are made in the wrong order.


1 Answers

You need to escape the question mark in your regex.

Without escaping it, you're just marking the token prior to it as optional.

> test = "../api/lookup?uri=http%3A%2F%2Fwww.something.com";

> test.match(/\.\.\/api\/lookup?uri=.*/);
null

> test.match(/\.\.\/api\/lookup\?uri=.*/);
["../api/lookup?uri=http%3A%2F%2Fwww.something.com"]
like image 197
John Ledbetter Avatar answered Oct 26 '22 21:10

John Ledbetter