I\'m trying to match an url with a regex. But the follwing regex doesn\'t match.
$httpBackend.whenGET(\'/^rest\\/find-reservations\\?.*/)\').respond(function
There are two problems with your code:
Enclosing an expression in '...'
makes it a string (even if it looks like a regular expression.
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', {}];
});
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');
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'], {}];
});
Error: Unexpected request:
It can be for some reasons:
$httpBackend.expectGET(url)
.expectGET
should be the same as the order of the requests
.$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
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: