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

fix(ngMock/$httpBackend): fail if a url is provided but is undefined #12777

Merged
Merged
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
69 changes: 46 additions & 23 deletions src/ngMock/angular-mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -1423,7 +1423,7 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
* Creates a new backend definition.
*
* @param {string} method HTTP method.
* @param {string|RegExp|function(string)} url HTTP url or function that receives a url
* @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
* and returns true if the url matches the current definition.
* @param {(string|RegExp|function(string))=} data HTTP request body or function that receives
* data string and returns true if the data is as expected.
Expand All @@ -1445,6 +1445,9 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
* the `requestHandler` object for possible overrides.
*/
$httpBackend.when = function(method, url, data, headers, keys) {

assertArgDefined(arguments, 1, 'url');

var definition = new MockHttpExpectation(method, url, data, headers, keys),
chain = {
respond: function(status, data, headers, statusText) {
Expand Down Expand Up @@ -1472,7 +1475,7 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
* @description
* Creates a new backend definition for GET requests. For more info see `when()`.
*
* @param {string|RegExp|function(string)} url HTTP url or function that receives a url
* @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
* and returns true if the url matches the current definition.
* @param {(Object|function(Object))=} headers HTTP headers.
* @param {(Array)=} keys Array of keys to assign to regex matches in request url described above.
Expand All @@ -1487,7 +1490,7 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
* @description
* Creates a new backend definition for HEAD requests. For more info see `when()`.
*
* @param {string|RegExp|function(string)} url HTTP url or function that receives a url
* @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
* and returns true if the url matches the current definition.
* @param {(Object|function(Object))=} headers HTTP headers.
* @param {(Array)=} keys Array of keys to assign to regex matches in request url described above.
Expand All @@ -1502,7 +1505,7 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
* @description
* Creates a new backend definition for DELETE requests. For more info see `when()`.
*
* @param {string|RegExp|function(string)} url HTTP url or function that receives a url
* @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
* and returns true if the url matches the current definition.
* @param {(Object|function(Object))=} headers HTTP headers.
* @param {(Array)=} keys Array of keys to assign to regex matches in request url described above.
Expand All @@ -1517,7 +1520,7 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
* @description
* Creates a new backend definition for POST requests. For more info see `when()`.
*
* @param {string|RegExp|function(string)} url HTTP url or function that receives a url
* @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
* and returns true if the url matches the current definition.
* @param {(string|RegExp|function(string))=} data HTTP request body or function that receives
* data string and returns true if the data is as expected.
Expand All @@ -1534,7 +1537,7 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
* @description
* Creates a new backend definition for PUT requests. For more info see `when()`.
*
* @param {string|RegExp|function(string)} url HTTP url or function that receives a url
* @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
* and returns true if the url matches the current definition.
* @param {(string|RegExp|function(string))=} data HTTP request body or function that receives
* data string and returns true if the data is as expected.
Expand All @@ -1551,7 +1554,7 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
* @description
* Creates a new backend definition for JSONP requests. For more info see `when()`.
*
* @param {string|RegExp|function(string)} url HTTP url or function that receives a url
* @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
* and returns true if the url matches the current definition.
* @param {(Array)=} keys Array of keys to assign to regex matches in request url described above.
* @returns {requestHandler} Returns an object with `respond` method that controls how a matched
Expand Down Expand Up @@ -1614,7 +1617,7 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
* Creates a new request expectation.
*
* @param {string} method HTTP method.
* @param {string|RegExp|function(string)} url HTTP url or function that receives a url
* @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
* and returns true if the url matches the current definition.
* @param {(string|RegExp|function(string)|Object)=} data HTTP request body or function that
* receives data string and returns true if the data is as expected, or Object if request body
Expand All @@ -1637,6 +1640,9 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
* the `requestHandler` object for possible overrides.
*/
$httpBackend.expect = function(method, url, data, headers, keys) {

assertArgDefined(arguments, 1, 'url');

var expectation = new MockHttpExpectation(method, url, data, headers, keys),
chain = {
respond: function(status, data, headers, statusText) {
Expand All @@ -1655,7 +1661,7 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
* @description
* Creates a new request expectation for GET requests. For more info see `expect()`.
*
* @param {string|RegExp|function(string)} url HTTP url or function that receives a url
* @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
* and returns true if the url matches the current definition.
* @param {Object=} headers HTTP headers.
* @param {(Array)=} keys Array of keys to assign to regex matches in request url described above.
Expand All @@ -1670,7 +1676,7 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
* @description
* Creates a new request expectation for HEAD requests. For more info see `expect()`.
*
* @param {string|RegExp|function(string)} url HTTP url or function that receives a url
* @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
* and returns true if the url matches the current definition.
* @param {Object=} headers HTTP headers.
* @param {(Array)=} keys Array of keys to assign to regex matches in request url described above.
Expand All @@ -1685,7 +1691,7 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
* @description
* Creates a new request expectation for DELETE requests. For more info see `expect()`.
*
* @param {string|RegExp|function(string)} url HTTP url or function that receives a url
* @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
* and returns true if the url matches the current definition.
* @param {Object=} headers HTTP headers.
* @param {(Array)=} keys Array of keys to assign to regex matches in request url described above.
Expand All @@ -1700,7 +1706,7 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
* @description
* Creates a new request expectation for POST requests. For more info see `expect()`.
*
* @param {string|RegExp|function(string)} url HTTP url or function that receives a url
* @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
* and returns true if the url matches the current definition.
* @param {(string|RegExp|function(string)|Object)=} data HTTP request body or function that
* receives data string and returns true if the data is as expected, or Object if request body
Expand All @@ -1718,7 +1724,7 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
* @description
* Creates a new request expectation for PUT requests. For more info see `expect()`.
*
* @param {string|RegExp|function(string)} url HTTP url or function that receives a url
* @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
* and returns true if the url matches the current definition.
* @param {(string|RegExp|function(string)|Object)=} data HTTP request body or function that
* receives data string and returns true if the data is as expected, or Object if request body
Expand All @@ -1736,7 +1742,7 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
* @description
* Creates a new request expectation for PATCH requests. For more info see `expect()`.
*
* @param {string|RegExp|function(string)} url HTTP url or function that receives a url
* @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
* and returns true if the url matches the current definition.
* @param {(string|RegExp|function(string)|Object)=} data HTTP request body or function that
* receives data string and returns true if the data is as expected, or Object if request body
Expand All @@ -1754,7 +1760,7 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
* @description
* Creates a new request expectation for JSONP requests. For more info see `expect()`.
*
* @param {string|RegExp|function(string)} url HTTP url or function that receives an url
* @param {string|RegExp|function(string)=} url HTTP url or function that receives an url
* and returns true if the url matches the current definition.
* @param {(Array)=} keys Array of keys to assign to regex matches in request url described above.
* @returns {requestHandler} Returns an object with `respond` method that controls how a matched
Expand Down Expand Up @@ -1870,18 +1876,35 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
function createShortMethods(prefix) {
angular.forEach(['GET', 'DELETE', 'JSONP', 'HEAD'], function(method) {
$httpBackend[prefix + method] = function(url, headers, keys) {
assertArgDefined(arguments, 0, 'url');

// Change url to `null` if `undefined` to stop it throwing an exception further down
if (angular.isUndefined(url)) url = null;

return $httpBackend[prefix](method, url, undefined, headers, keys);
};
});

angular.forEach(['PUT', 'POST', 'PATCH'], function(method) {
$httpBackend[prefix + method] = function(url, data, headers, keys) {
assertArgDefined(arguments, 0, 'url');

// Change url to `null` if `undefined` to stop it throwing an exception further down
if (angular.isUndefined(url)) url = null;

return $httpBackend[prefix](method, url, data, headers, keys);
};
});
}
}

function assertArgDefined(args, index, name) {
if (args.length > index && angular.isUndefined(args[index])) {
throw new Error("Undefined argument `" + name + "`; the argument is provided but not defined");
}
}


function MockHttpExpectation(method, url, data, headers, keys) {

function getUrlParams(u) {
Expand Down Expand Up @@ -2462,7 +2485,7 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {
* Creates a new backend definition.
*
* @param {string} method HTTP method.
* @param {string|RegExp|function(string)} url HTTP url or function that receives a url
* @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
* and returns true if the url matches the current definition.
* @param {(string|RegExp)=} data HTTP request body.
* @param {(Object|function(Object))=} headers HTTP headers or function that receives http header
Expand Down Expand Up @@ -2494,7 +2517,7 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {
* @description
* Creates a new backend definition for GET requests. For more info see `when()`.
*
* @param {string|RegExp|function(string)} url HTTP url or function that receives a url
* @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
* and returns true if the url matches the current definition.
* @param {(Object|function(Object))=} headers HTTP headers.
* @param {(Array)=} keys Array of keys to assign to regex matches in request url described on
Expand All @@ -2511,7 +2534,7 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {
* @description
* Creates a new backend definition for HEAD requests. For more info see `when()`.
*
* @param {string|RegExp|function(string)} url HTTP url or function that receives a url
* @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
* and returns true if the url matches the current definition.
* @param {(Object|function(Object))=} headers HTTP headers.
* @param {(Array)=} keys Array of keys to assign to regex matches in request url described on
Expand All @@ -2528,7 +2551,7 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {
* @description
* Creates a new backend definition for DELETE requests. For more info see `when()`.
*
* @param {string|RegExp|function(string)} url HTTP url or function that receives a url
* @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
* and returns true if the url matches the current definition.
* @param {(Object|function(Object))=} headers HTTP headers.
* @param {(Array)=} keys Array of keys to assign to regex matches in request url described on
Expand All @@ -2545,7 +2568,7 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {
* @description
* Creates a new backend definition for POST requests. For more info see `when()`.
*
* @param {string|RegExp|function(string)} url HTTP url or function that receives a url
* @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
* and returns true if the url matches the current definition.
* @param {(string|RegExp)=} data HTTP request body.
* @param {(Object|function(Object))=} headers HTTP headers.
Expand All @@ -2563,7 +2586,7 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {
* @description
* Creates a new backend definition for PUT requests. For more info see `when()`.
*
* @param {string|RegExp|function(string)} url HTTP url or function that receives a url
* @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
* and returns true if the url matches the current definition.
* @param {(string|RegExp)=} data HTTP request body.
* @param {(Object|function(Object))=} headers HTTP headers.
Expand All @@ -2581,7 +2604,7 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {
* @description
* Creates a new backend definition for PATCH requests. For more info see `when()`.
*
* @param {string|RegExp|function(string)} url HTTP url or function that receives a url
* @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
* and returns true if the url matches the current definition.
* @param {(string|RegExp)=} data HTTP request body.
* @param {(Object|function(Object))=} headers HTTP headers.
Expand All @@ -2599,7 +2622,7 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {
* @description
* Creates a new backend definition for JSONP requests. For more info see `when()`.
*
* @param {string|RegExp|function(string)} url HTTP url or function that receives a url
* @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
* and returns true if the url matches the current definition.
* @param {(Array)=} keys Array of keys to assign to regex matches in request url described on
* {@link ngMock.$httpBackend $httpBackend mock}.
Expand Down
91 changes: 91 additions & 0 deletions test/ngMock/angular-mocksSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1173,6 +1173,97 @@ describe('ngMock', function() {
});


it('should not error if the url is not provided', function() {
expect(function() {
hb.when('GET');

hb.whenGET();
hb.whenPOST();
hb.whenPUT();
hb.whenPATCH();
hb.whenDELETE();
hb.whenHEAD();

hb.expect('GET');

hb.expectGET();
hb.expectPOST();
hb.expectPUT();
hb.expectPATCH();
hb.expectDELETE();
hb.expectHEAD();
}).not.toThrow();
});


it('should error if the url is undefined', function() {
expect(function() {
hb.when('GET', undefined);
}).toThrowError('Undefined argument `url`; the argument is provided but not defined');

expect(function() {
hb.whenGET(undefined);
}).toThrowError('Undefined argument `url`; the argument is provided but not defined');

expect(function() {
hb.whenDELETE(undefined);
}).toThrowError('Undefined argument `url`; the argument is provided but not defined');

expect(function() {
hb.whenJSONP(undefined);
}).toThrowError('Undefined argument `url`; the argument is provided but not defined');

expect(function() {
hb.whenHEAD(undefined);
}).toThrowError('Undefined argument `url`; the argument is provided but not defined');

expect(function() {
hb.whenPATCH(undefined);
}).toThrowError('Undefined argument `url`; the argument is provided but not defined');

expect(function() {
hb.whenPOST(undefined);
}).toThrowError('Undefined argument `url`; the argument is provided but not defined');

expect(function() {
hb.whenPUT(undefined);
}).toThrowError('Undefined argument `url`; the argument is provided but not defined');


expect(function() {
hb.expect('GET', undefined);
}).toThrowError('Undefined argument `url`; the argument is provided but not defined');

expect(function() {
hb.expectGET(undefined);
}).toThrowError('Undefined argument `url`; the argument is provided but not defined');

expect(function() {
hb.expectDELETE(undefined);
}).toThrowError('Undefined argument `url`; the argument is provided but not defined');

expect(function() {
hb.expectJSONP(undefined);
}).toThrowError('Undefined argument `url`; the argument is provided but not defined');

expect(function() {
hb.expectHEAD(undefined);
}).toThrowError('Undefined argument `url`; the argument is provided but not defined');

expect(function() {
hb.expectPATCH(undefined);
}).toThrowError('Undefined argument `url`; the argument is provided but not defined');

expect(function() {
hb.expectPOST(undefined);
}).toThrowError('Undefined argument `url`; the argument is provided but not defined');

expect(function() {
hb.expectPUT(undefined);
}).toThrowError('Undefined argument `url`; the argument is provided but not defined');
});


it('should preserve the order of requests', function() {
hb.when('GET', '/url1').respond(200, 'first');
hb.when('GET', '/url2').respond(201, 'second');
Expand Down