Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

update angular-mocks.js url matching #12762

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/ngMock/angular-mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -1628,7 +1628,7 @@ function MockHttpExpectation(method, url, data, headers) {
if (!url) return true;
if (angular.isFunction(url.test)) return url.test(u);
if (angular.isFunction(url)) return url(u);
return url == u;
return (url == u || this.compareUrl(u));
};

this.matchHeaders = function(h) {
Expand All @@ -1646,6 +1646,15 @@ function MockHttpExpectation(method, url, data, headers) {
}
return data == d;
};

this.getUrlParams = function (u){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do these two methods (getUrlParams and compareUrl) need to be public ?

var params = u.slice(u.indexOf('?') + 1).split('&');
return params.sort();
};

this.compareUrl = function (u){
return (url.slice(0,url.indexOf('?')) == u.slice(0,u.indexOf('?')) && this.getUrlParams(url).join() == this.getUrlParams(u).join())
};

this.toString = function() {
return method + ' ' + url;
Expand Down
5 changes: 5 additions & 0 deletions test/ngMock/angular-mocksSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1515,6 +1515,11 @@ describe('ngMock', function() {
expect(exp.match('GET', 'a/x')).toBe(false);
});

it('should match url with same query params, but different order', function() {
var exp = new MockHttpExpectation('GET', 'www.example.com/x/y?a=b&c=d&e=f');

expect(exp.matchUrl('www.example.com/x/y?e=f&c=d&a=b')).toBe(true);
});

it('should accept url as function', function() {
var urlValidator = function(url) {
Expand Down