$httpBackend with request with query param

Abhiram mishra picture Abhiram mishra · Aug 14, 2015 · Viewed 14.6k times · Source
$httpBackend.whenGET('/restpath/api/v1/books')
.respond({// some data}); 

I get the following error

Error: Unexpected request: GET /restpath/api/v1/books
 Expected GET /restpath/api/v1/books?limit=10&start=1

For the expectGET I have the following , and this creates dynamic query string. mostly the 'start' parameter, and the whenGET part, I am trying to server a dynamic content depending on the 'start'

$httpBackend.expectGET('/restpath/api/v1/books?limit=10&start=1'); // the actual service goes here , which does the $http service. we don't care $httpBackend.flush();

Answer

Arno_Geismar picture Arno_Geismar · Aug 14, 2015

(for angular apps with versions lower than v1.5.0-build.4371 )

If you dont care about the parameters after your '?' you can do this :

$httpBackend.expectGET(/.*?restpath\/api\/v1\/books?.*/g).respond(200, '{}');

if you care about the first param do this :

$httpBackend.expectGET(/.*?restpath\/api\/v1\/books?limit=10.*/g).respond(200, '{}');

if you care about them all do this :

$httpBackend.expectGET("/restpath/api/v1/books?limit=10&start=1").respond(200, '{}');