Vincent
Vincent

Reputation: 6188

mock $httpBackend regex not matched

I'm trying to match an url with a regex. But the follwing regex doesn't match.

$httpBackend.whenGET('/^rest\/find-reservations\?.*/)').respond(function () {
        return [200, ['succes'], {}];
    });

I keep getting the follwing error:

Error: Unexpected request: GET rest/find-reservations?end=1421424299193&reservationClass=Reservation&start=1358352299193
No more request expected

When I change the regex to an absolute string '/find-reservations' the whenGET is triggered. Why is this?

edit: I'm mocking a backend. http://plnkr.co/edit/VF4KbZO3FvngWQsiUcte?p=preview The following plnkr works fine for static urls and partials. But it does not in the above case.

Upvotes: 13

Views: 12905

Answers (4)

BitfulByte
BitfulByte

Reputation: 4466

I came across this question but in my case the issue was the fact the regex was created with the /g global flag and I'd like to share my answer in case someone else stumbles upon it for the same reason I did.

Although this doesn't answer the question, I hope it's usefull for someone nonetheless:

Short version:

Don't use /g global flags in combination with httpBackend url matching.

TL;DR:

Asuming you have a service called SomeService with a function called someFunction that executes a GET request, consider the following tests that demonstrate the impact of the global flag on the regex.

describe('Regex demo', function() {
    afterEach(function() {
        httpBackend.flush();
    });
    describe('Normal regex used once', function() {
        var url = new RegExp(baseUrl);
        it('first test', function() {
            httpBackend.expectGET(url).respond(200, {});
            someService.someFunction();
        });
    });
    describe('Normal regex used multiple times', function() {
        var url = new RegExp(baseUrl);
        it('first test', function() {
            httpBackend.expectGET(url).respond(200, {});
            someService.someFunction();
        });
        it('second test, no problem', function() {
            httpBackend.expectGET(url).respond(200, {});
            someService.someFunction();
        });
    });
    describe('Regex with GLOBAL flag', function() {
        var url = new RegExp(baseUrl, 'g');
        it('first test', function() {
            httpBackend.expectGET(url).respond(200, {});
            someService.someFunction();
        });
        it('second test with global, this will not pass!', function() {
            httpBackend.expectGET(url).respond(200, {});
            someService.someFunction();
        });
    });
    describe('Regex with GLOBAL flag in before each', function() {
        var url;
        beforeEach(function() {
            url = new RegExp(baseUrl, 'g');
        });
        it('first test', function() {
            httpBackend.expectGET(url).respond(200, {});
            someService.someFunction();
        });
        it('second test, passes although regex is global, it was renewed in     the before each', function() {
            httpBackend.expectGET(url).respond(200, {});
            someService.someFunction();
        });
    });
});

Here are the corresponding test results: enter image description here

Upvotes: 1

hannes neukermans
hannes neukermans

Reputation: 13277

 this.httpBackend.whenGET(new RegExp('.*')).respond(function(){ return [200, 'MISS', {}] });
 this.httpBackend.whenPOST('/report/').respond(function(){return [200, 'HIT', {}] ; });
 this.httpBackend.whenPOST(new RegExp('.*')).respond(function(){ return      [200, 'MISS', {}] });

var _received;

var result = this.reportService.save(new this.Report());
result.then(function(response){
    _received = response.data;
});          
this.httpBackend.flush();
expect(_received).toBe('HIT');          

Upvotes: 1

Ilan Frumer
Ilan Frumer

Reputation: 32367

If you want to match this url:

"rest/find-reservations?end=1421424299193&reservationClass=Reservation&start=1358352299193"

use this code:

$httpBackend.whenGET(/^rest\/find-reservations\?.*/).respond(function () {
  return [200, ['success'], {}];
});

If you see this error Error: Unexpected request:

It can be for some reasons:

  • You forget $httpBackend.expectGET(url).
  • The order of expectGET should be the same as the order of the requests.
  • You called $httpBackend.verifyNoOutstandingExpectation() before $httpBackend.flush().

It is not related to $httpBackend.whenGET at all.

From the $httpBackend docs:

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

Upvotes: 15

gkalpak
gkalpak

Reputation: 48211

There are two problems with your code:

  1. Enclosing an expression in '...' makes it a string (even if it looks like a regular expression.

  2. You must include the leading / in your pattern.


Your code should be modified like this:

$httpBackend.whenGET(/^\/rest\/find-reservations\?.*/).respond(function () {
    return [200, 'success', {}];
});

Upvotes: 8

Related Questions