diff --git a/src/ng/http.js b/src/ng/http.js index 52cb9ffd5b23..582b168919d0 100644 --- a/src/ng/http.js +++ b/src/ng/http.js @@ -886,18 +886,31 @@ function $HttpProvider() { * @param {Object=} config Optional configuration object * @returns {HttpPromise} Future object */ - createShortMethodsWithData('post', 'put'); - - /** - * @ngdoc property - * @name $http#defaults - * - * @description - * Runtime equivalent of the `$httpProvider.defaults` property. Allows configuration of - * default headers, withCredentials as well as request and response transformations. - * - * See "Setting HTTP Headers" and "Transforming Requests and Responses" sections above. - */ + + /** + * @ngdoc method + * @name $http#patch + * + * @description + * Shortcut method to perform `PATCH` request. + * + * @param {string} url Relative or absolute URL specifying the destination of the request + * @param {*} data Request content + * @param {Object=} config Optional configuration object + * @returns {HttpPromise} Future object + */ + createShortMethodsWithData('post', 'put', 'patch'); + + /** + * @ngdoc property + * @name $http#defaults + * + * @description + * Runtime equivalent of the `$httpProvider.defaults` property. Allows configuration of + * default headers, withCredentials as well as request and response transformations. + * + * See "Setting HTTP Headers" and "Transforming Requests and Responses" sections above. + */ $http.defaults = defaults; diff --git a/src/ngMock/angular-mocks.js b/src/ngMock/angular-mocks.js index 3ba85ac64dc0..7472640fa031 100644 --- a/src/ngMock/angular-mocks.js +++ b/src/ngMock/angular-mocks.js @@ -1186,7 +1186,7 @@ function createHttpBackendMock($rootScope, $delegate, $browser) { * data string and returns true if the data is as expected. * @param {(Object|function(Object))=} headers HTTP headers or function that receives http header * object and returns true if the headers match the current definition. - * @returns {requestHandler} Returns an object with `respond` method that controls how a matched + * @returns {requestHandler} Returns an object with a `respond` method that controlss how a matched * request is handled. * * - respond – @@ -1222,7 +1222,7 @@ function createHttpBackendMock($rootScope, $delegate, $browser) { * * @param {string|RegExp} url HTTP url. * @param {(Object|function(Object))=} headers HTTP headers. - * @returns {requestHandler} Returns an object with `respond` method that control how a matched + * @returns {requestHandler} Returns an object with a `respond` method that controls how a matched * request is handled. */ @@ -1234,7 +1234,7 @@ function createHttpBackendMock($rootScope, $delegate, $browser) { * * @param {string|RegExp} url HTTP url. * @param {(Object|function(Object))=} headers HTTP headers. - * @returns {requestHandler} Returns an object with `respond` method that control how a matched + * @returns {requestHandler} Returns an object with a `respond` method that controls how a matched * request is handled. */ @@ -1246,7 +1246,7 @@ function createHttpBackendMock($rootScope, $delegate, $browser) { * * @param {string|RegExp} url HTTP url. * @param {(Object|function(Object))=} headers HTTP headers. - * @returns {requestHandler} Returns an object with `respond` method that control how a matched + * @returns {requestHandler} Returns an object with a `respond` method that controls how a matched * request is handled. */ @@ -1260,7 +1260,7 @@ function createHttpBackendMock($rootScope, $delegate, $browser) { * @param {(string|RegExp|function(string))=} data HTTP request body or function that receives * data string and returns true if the data is as expected. * @param {(Object|function(Object))=} headers HTTP headers. - * @returns {requestHandler} Returns an object with `respond` method that control how a matched + * @returns {requestHandler} Returns an object with a `respond` method that controls how a matched * request is handled. */ @@ -1274,7 +1274,21 @@ function createHttpBackendMock($rootScope, $delegate, $browser) { * @param {(string|RegExp|function(string))=} data HTTP request body or function that receives * data string and returns true if the data is as expected. * @param {(Object|function(Object))=} headers HTTP headers. - * @returns {requestHandler} Returns an object with `respond` method that control how a matched + * @returns {requestHandler} Returns an object with a `respond` method that controls how a matched + * request is handled. + */ + + /** + * @ngdoc method + * @name $httpBackend#whenPATCH + * @description + * Creates a new backend definition for PATCH requests. For more info see `when()`. + * + * @param {string|RegExp} url HTTP url. + * @param {(string|RegExp|function(string))=} data HTTP request body or function that receives + * data string and returns true if the data is as expected. + * @param {(Object|function(Object))=} headers HTTP headers. + * @returns {requestHandler} Returns an object with a `respond` method that controls how a matched * request is handled. */ @@ -1285,7 +1299,7 @@ function createHttpBackendMock($rootScope, $delegate, $browser) { * Creates a new backend definition for JSONP requests. For more info see `when()`. * * @param {string|RegExp} url HTTP url. - * @returns {requestHandler} Returns an object with `respond` method that control how a matched + * @returns {requestHandler} Returns an object with a `respond` method that controls how a matched * request is handled. */ createShortMethods('when'); @@ -1304,7 +1318,7 @@ function createHttpBackendMock($rootScope, $delegate, $browser) { * is in JSON format. * @param {(Object|function(Object))=} headers HTTP headers or function that receives http header * object and returns true if the headers match the current expectation. - * @returns {requestHandler} Returns an object with `respond` method that control how a matched + * @returns {requestHandler} Returns an object with a `respond` method that controls how a matched * request is handled. * * - respond – @@ -1333,7 +1347,7 @@ function createHttpBackendMock($rootScope, $delegate, $browser) { * * @param {string|RegExp} url HTTP url. * @param {Object=} headers HTTP headers. - * @returns {requestHandler} Returns an object with `respond` method that control how a matched + * @returns {requestHandler} Returns an object with a `respond` method that controls how a matched * request is handled. See #expect for more info. */ @@ -1345,7 +1359,7 @@ function createHttpBackendMock($rootScope, $delegate, $browser) { * * @param {string|RegExp} url HTTP url. * @param {Object=} headers HTTP headers. - * @returns {requestHandler} Returns an object with `respond` method that control how a matched + * @returns {requestHandler} Returns an object with a `respond` method that controls how a matched * request is handled. */ @@ -1357,7 +1371,7 @@ function createHttpBackendMock($rootScope, $delegate, $browser) { * * @param {string|RegExp} url HTTP url. * @param {Object=} headers HTTP headers. - * @returns {requestHandler} Returns an object with `respond` method that control how a matched + * @returns {requestHandler} Returns an object with a `respond` method that controls how a matched * request is handled. */ @@ -1372,7 +1386,7 @@ function createHttpBackendMock($rootScope, $delegate, $browser) { * receives data string and returns true if the data is as expected, or Object if request body * is in JSON format. * @param {Object=} headers HTTP headers. - * @returns {requestHandler} Returns an object with `respond` method that control how a matched + * @returns {requestHandler} Returns an object with a `respond` method that controls how a matched * request is handled. */ @@ -1387,7 +1401,7 @@ function createHttpBackendMock($rootScope, $delegate, $browser) { * receives data string and returns true if the data is as expected, or Object if request body * is in JSON format. * @param {Object=} headers HTTP headers. - * @returns {requestHandler} Returns an object with `respond` method that control how a matched + * @returns {requestHandler} Returns an object with a `respond` method that controls how a matched * request is handled. */ @@ -1402,7 +1416,7 @@ function createHttpBackendMock($rootScope, $delegate, $browser) { * receives data string and returns true if the data is as expected, or Object if request body * is in JSON format. * @param {Object=} headers HTTP headers. - * @returns {requestHandler} Returns an object with `respond` method that control how a matched + * @returns {requestHandler} Returns an object with a `respond` method that controls how a matched * request is handled. */ @@ -1413,7 +1427,7 @@ function createHttpBackendMock($rootScope, $delegate, $browser) { * Creates a new request expectation for JSONP requests. For more info see `expect()`. * * @param {string|RegExp} url HTTP url. - * @returns {requestHandler} Returns an object with `respond` method that control how a matched + * @returns {requestHandler} Returns an object with a `respond` method that controls how a matched * request is handled. */ createShortMethods('expect'); @@ -1506,7 +1520,7 @@ function createHttpBackendMock($rootScope, $delegate, $browser) { function createShortMethods(prefix) { - angular.forEach(['GET', 'DELETE', 'JSONP'], function(method) { + angular.forEach(['GET', 'DELETE', 'JSONP', 'HEAD'], function(method) { $httpBackend[prefix + method] = function(url, headers) { return $httpBackend[prefix](method, url, undefined, headers); }; diff --git a/test/ng/httpSpec.js b/test/ng/httpSpec.js index d0b9ec069a36..c7f76c247372 100644 --- a/test/ng/httpSpec.js +++ b/test/ng/httpSpec.js @@ -985,6 +985,18 @@ describe('$http', function() { }); + it('should have patch()', function() { + $httpBackend.expect('PATCH', '/url', 'some-data').respond(''); + $http.patch('/url', 'some-data'); + }); + + + it('patch() should allow config param', function() { + $httpBackend.expect('PATCH', '/url', 'some-data', checkHeader('Custom', 'Header')).respond(''); + $http.patch('/url', 'some-data', {headers: {'Custom': 'Header'}}); + }); + + it('should have jsonp()', function() { $httpBackend.expect('JSONP', '/url').respond(''); $http.jsonp('/url'); diff --git a/test/ngMock/angular-mocksSpec.js b/test/ngMock/angular-mocksSpec.js index e7bdc6a8e828..85f4ed5f21a4 100644 --- a/test/ngMock/angular-mocksSpec.js +++ b/test/ngMock/angular-mocksSpec.js @@ -947,6 +947,26 @@ describe('ngMock', function() { })); + it('should provide "expect" methods for each HTTP verb', function() { + expect(typeof hb.expectGET).toBe("function"); + expect(typeof hb.expectPOST).toBe("function"); + expect(typeof hb.expectPUT).toBe("function"); + expect(typeof hb.expectPATCH).toBe("function"); + expect(typeof hb.expectDELETE).toBe("function"); + expect(typeof hb.expectHEAD).toBe("function"); + }); + + + it('should provide "when" methods for each HTTP verb', function() { + expect(typeof hb.whenGET).toBe("function"); + expect(typeof hb.whenPOST).toBe("function"); + expect(typeof hb.whenPUT).toBe("function"); + expect(typeof hb.whenPATCH).toBe("function"); + expect(typeof hb.whenDELETE).toBe("function"); + expect(typeof hb.whenHEAD).toBe("function"); + }); + + it('should respond with first matched definition', function() { hb.when('GET', '/url1').respond(200, 'content', {}); hb.when('GET', '/url1').respond(201, 'another', {});