Skip to content

Commit a445526

Browse files
committed
fix(ngMock/$httpBackend): correctly ignore query params in {expect,when}Route
Fixes angular#14173
1 parent 586b6e8 commit a445526

File tree

2 files changed

+34
-12
lines changed

2 files changed

+34
-12
lines changed

src/ngMock/angular-mocks.js

+12-10
Original file line numberDiff line numberDiff line change
@@ -1481,8 +1481,9 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
14811481
* ```
14821482
* – The respond method takes a set of static data to be returned or a function that can
14831483
* return an array containing response status (number), response data (Array|Object|string),
1484-
* response headers (Object), and the text for the status (string). The respond method returns
1485-
* the `requestHandler` object for possible overrides.
1484+
* response headers (Object), HTTP status text (string), and XMLHttpRequest status (string:
1485+
* `complete`, `error`, `timeout` or `abort`). The respond method returns the `requestHandler`
1486+
* object for possible overrides.
14861487
*/
14871488
$httpBackend.when = function(method, url, data, headers, keys) {
14881489

@@ -1680,14 +1681,14 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
16801681
+ (optional ? '' : slash)
16811682
+ '(?:'
16821683
+ (optional ? slash : '')
1683-
+ (star && '(.+?)' || '([^/]+)')
1684+
+ (star ? '(.+?)' : '([^/?]+)')
16841685
+ (optional || '')
16851686
+ ')'
16861687
+ (optional || '');
16871688
})
16881689
.replace(/([/$*])/g, '\\$1');
16891690

1690-
ret.regexp = new RegExp('^' + url, 'i');
1691+
ret.regexp = new RegExp('^' + url + '(?:$|\\?)', 'i');
16911692
return ret;
16921693
}
16931694

@@ -1711,14 +1712,15 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
17111712
* order to change how a matched request is handled.
17121713
*
17131714
* - respond –
1714-
* ```
1715-
* { function([status,] data[, headers, statusText])
1716-
* | function(function(method, url, data, headers, params)}
1717-
* ```
1715+
* ```js
1716+
* {function([status,] data[, headers, statusText])
1717+
* | function(function(method, url, data, headers, params)}
1718+
* ```
17181719
* – The respond method takes a set of static data to be returned or a function that can
17191720
* return an array containing response status (number), response data (Array|Object|string),
1720-
* response headers (Object), and the text for the status (string). The respond method returns
1721-
* the `requestHandler` object for possible overrides.
1721+
* response headers (Object), HTTP status text (string), and XMLHttpRequest status (string:
1722+
* `complete`, `error`, `timeout` or `abort`). The respond method returns the `requestHandler`
1723+
* object for possible overrides.
17221724
*/
17231725
$httpBackend.expect = function(method, url, data, headers, keys) {
17241726

test/ngMock/angular-mocksSpec.js

+22-2
Original file line numberDiff line numberDiff line change
@@ -1943,10 +1943,30 @@ describe('ngMock', function() {
19431943
);
19441944
they('should ignore query param when matching in ' + routeShortcut + ' $prop method', methods,
19451945
function() {
1946-
hb[routeShortcut](this, '/route/:id').respond('path');
1946+
var paramsSpy = jasmine.createSpy('paramsSpy');
1947+
hb[routeShortcut](this, '/route/:id').respond(function(method, url, data, headers, params) {
1948+
paramsSpy(params);
1949+
// status, response, headers, statusText, xhrStatus
1950+
return [200, 'path', { 'x-header': 'foo' }, 'OK', 'complete'];
1951+
});
19471952
hb(this, '/route/123?q=str&foo=bar', undefined, callback);
19481953
hb.flush();
1949-
expect(callback).toHaveBeenCalledOnceWith(200, 'path', '', '', 'complete');
1954+
expect(callback).toHaveBeenCalledOnceWith(200, 'path', 'x-header: foo', 'OK', 'complete');
1955+
expect(paramsSpy).toHaveBeenCalledOnceWith({id: '123', q: 'str', foo: 'bar'});
1956+
}
1957+
);
1958+
they('should ignore query param when matching eager parameters in ' + routeShortcut + ' $prop method', methods,
1959+
function() {
1960+
var paramsSpy = jasmine.createSpy('paramsSpy');
1961+
hb[routeShortcut](this, '/route/:id*').respond(function(method, url, data, headers, params) {
1962+
paramsSpy(params);
1963+
// status, response, headers, statusText, xhrStatus
1964+
return [200, 'path', { 'x-header': 'foo' }, 'OK', 'complete'];
1965+
});
1966+
hb(this, '/route/123/456?q=str&foo=bar', undefined, callback);
1967+
hb.flush();
1968+
expect(callback).toHaveBeenCalledOnceWith(200, 'path', 'x-header: foo', 'OK', 'complete');
1969+
expect(paramsSpy).toHaveBeenCalledOnceWith({id: '123/456', q: 'str', foo: 'bar'});
19501970
}
19511971
);
19521972
});

0 commit comments

Comments
 (0)