From 973d97d17c2992f577e7151e3344163bb529c0ed Mon Sep 17 00:00:00 2001 From: Georgios Kalpakas Date: Sun, 28 Sep 2014 23:16:23 +0300 Subject: [PATCH] fix($http): add missing shortcut methods and missing docs There was some inconsistency in version 1.2.25 regarding the definition and documentation of shortcut methods in `ng.$http` and `ngMock[E2E].$httpBackend`. Some methods didn't exist (although documented as existing), whereas some methods did exist, but wheren't documented. This commit fixes the above errors and adds tests to verify the existence of all shortcut methods. More specificcally, the following issues were addressed: `ng.$http`: Add the missing `patch()` shortcut method and related docs. `ng.$http` spec: Add test to verify the existence and functionality of the `patch()` shortcut method. `ngMock[E2E].$httpBackend`: Add docs for the (existing) `whenPATCH()` shortcut method, add the missing `expectHEAD()`/`whenHEAD()` shortcut methods, fix grammatical errors. `ngMock[E2E].$httpBackend` spec: Add test to verify the existence of shortcut methods for all HTTP verbs. Closes #9180 --- src/ng/http.js | 37 ++++++++++++++++--------- src/ngMock/angular-mocks.js | 46 +++++++++++++++++++++----------- test/ng/httpSpec.js | 12 +++++++++ test/ngMock/angular-mocksSpec.js | 20 ++++++++++++++ 4 files changed, 87 insertions(+), 28 deletions(-) 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', {});