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

Commit ec4fe1b

Browse files
gkalpakpetebacondarwin
authored andcommitted
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 Closes #9321
1 parent 3303fe4 commit ec4fe1b

File tree

4 files changed

+87
-28
lines changed

4 files changed

+87
-28
lines changed

src/ng/http.js

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -887,18 +887,31 @@ function $HttpProvider() {
887887
* @param {Object=} config Optional configuration object
888888
* @returns {HttpPromise} Future object
889889
*/
890-
createShortMethodsWithData('post', 'put');
891-
892-
/**
893-
* @ngdoc property
894-
* @name $http#defaults
895-
*
896-
* @description
897-
* Runtime equivalent of the `$httpProvider.defaults` property. Allows configuration of
898-
* default headers, withCredentials as well as request and response transformations.
899-
*
900-
* See "Setting HTTP Headers" and "Transforming Requests and Responses" sections above.
901-
*/
890+
891+
/**
892+
* @ngdoc method
893+
* @name $http#patch
894+
*
895+
* @description
896+
* Shortcut method to perform `PATCH` request.
897+
*
898+
* @param {string} url Relative or absolute URL specifying the destination of the request
899+
* @param {*} data Request content
900+
* @param {Object=} config Optional configuration object
901+
* @returns {HttpPromise} Future object
902+
*/
903+
createShortMethodsWithData('post', 'put', 'patch');
904+
905+
/**
906+
* @ngdoc property
907+
* @name $http#defaults
908+
*
909+
* @description
910+
* Runtime equivalent of the `$httpProvider.defaults` property. Allows configuration of
911+
* default headers, withCredentials as well as request and response transformations.
912+
*
913+
* See "Setting HTTP Headers" and "Transforming Requests and Responses" sections above.
914+
*/
902915
$http.defaults = defaults;
903916

904917

src/ngMock/angular-mocks.js

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,7 +1186,7 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
11861186
* data string and returns true if the data is as expected.
11871187
* @param {(Object|function(Object))=} headers HTTP headers or function that receives http header
11881188
* object and returns true if the headers match the current definition.
1189-
* @returns {requestHandler} Returns an object with `respond` method that controls how a matched
1189+
* @returns {requestHandler} Returns an object with a `respond` method that controls how a matched
11901190
* request is handled.
11911191
*
11921192
* - respond –
@@ -1222,7 +1222,7 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
12221222
*
12231223
* @param {string|RegExp} url HTTP url.
12241224
* @param {(Object|function(Object))=} headers HTTP headers.
1225-
* @returns {requestHandler} Returns an object with `respond` method that control how a matched
1225+
* @returns {requestHandler} Returns an object with a `respond` method that controls how a matched
12261226
* request is handled.
12271227
*/
12281228

@@ -1234,7 +1234,7 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
12341234
*
12351235
* @param {string|RegExp} url HTTP url.
12361236
* @param {(Object|function(Object))=} headers HTTP headers.
1237-
* @returns {requestHandler} Returns an object with `respond` method that control how a matched
1237+
* @returns {requestHandler} Returns an object with a `respond` method that controls how a matched
12381238
* request is handled.
12391239
*/
12401240

@@ -1246,7 +1246,7 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
12461246
*
12471247
* @param {string|RegExp} url HTTP url.
12481248
* @param {(Object|function(Object))=} headers HTTP headers.
1249-
* @returns {requestHandler} Returns an object with `respond` method that control how a matched
1249+
* @returns {requestHandler} Returns an object with a `respond` method that controls how a matched
12501250
* request is handled.
12511251
*/
12521252

@@ -1260,7 +1260,7 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
12601260
* @param {(string|RegExp|function(string))=} data HTTP request body or function that receives
12611261
* data string and returns true if the data is as expected.
12621262
* @param {(Object|function(Object))=} headers HTTP headers.
1263-
* @returns {requestHandler} Returns an object with `respond` method that control how a matched
1263+
* @returns {requestHandler} Returns an object with a `respond` method that controls how a matched
12641264
* request is handled.
12651265
*/
12661266

@@ -1274,7 +1274,21 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
12741274
* @param {(string|RegExp|function(string))=} data HTTP request body or function that receives
12751275
* data string and returns true if the data is as expected.
12761276
* @param {(Object|function(Object))=} headers HTTP headers.
1277-
* @returns {requestHandler} Returns an object with `respond` method that control how a matched
1277+
* @returns {requestHandler} Returns an object with a `respond` method that controls how a matched
1278+
* request is handled.
1279+
*/
1280+
1281+
/**
1282+
* @ngdoc method
1283+
* @name $httpBackend#whenPATCH
1284+
* @description
1285+
* Creates a new backend definition for PATCH requests. For more info see `when()`.
1286+
*
1287+
* @param {string|RegExp} url HTTP url.
1288+
* @param {(string|RegExp|function(string))=} data HTTP request body or function that receives
1289+
* data string and returns true if the data is as expected.
1290+
* @param {(Object|function(Object))=} headers HTTP headers.
1291+
* @returns {requestHandler} Returns an object with a `respond` method that controls how a matched
12781292
* request is handled.
12791293
*/
12801294

@@ -1285,7 +1299,7 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
12851299
* Creates a new backend definition for JSONP requests. For more info see `when()`.
12861300
*
12871301
* @param {string|RegExp} url HTTP url.
1288-
* @returns {requestHandler} Returns an object with `respond` method that control how a matched
1302+
* @returns {requestHandler} Returns an object with a `respond` method that controls how a matched
12891303
* request is handled.
12901304
*/
12911305
createShortMethods('when');
@@ -1304,7 +1318,7 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
13041318
* is in JSON format.
13051319
* @param {(Object|function(Object))=} headers HTTP headers or function that receives http header
13061320
* object and returns true if the headers match the current expectation.
1307-
* @returns {requestHandler} Returns an object with `respond` method that control how a matched
1321+
* @returns {requestHandler} Returns an object with a `respond` method that controls how a matched
13081322
* request is handled.
13091323
*
13101324
* - respond –
@@ -1333,7 +1347,7 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
13331347
*
13341348
* @param {string|RegExp} url HTTP url.
13351349
* @param {Object=} headers HTTP headers.
1336-
* @returns {requestHandler} Returns an object with `respond` method that control how a matched
1350+
* @returns {requestHandler} Returns an object with a `respond` method that controls how a matched
13371351
* request is handled. See #expect for more info.
13381352
*/
13391353

@@ -1345,7 +1359,7 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
13451359
*
13461360
* @param {string|RegExp} url HTTP url.
13471361
* @param {Object=} headers HTTP headers.
1348-
* @returns {requestHandler} Returns an object with `respond` method that control how a matched
1362+
* @returns {requestHandler} Returns an object with a `respond` method that controls how a matched
13491363
* request is handled.
13501364
*/
13511365

@@ -1357,7 +1371,7 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
13571371
*
13581372
* @param {string|RegExp} url HTTP url.
13591373
* @param {Object=} headers HTTP headers.
1360-
* @returns {requestHandler} Returns an object with `respond` method that control how a matched
1374+
* @returns {requestHandler} Returns an object with a `respond` method that controls how a matched
13611375
* request is handled.
13621376
*/
13631377

@@ -1372,7 +1386,7 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
13721386
* receives data string and returns true if the data is as expected, or Object if request body
13731387
* is in JSON format.
13741388
* @param {Object=} headers HTTP headers.
1375-
* @returns {requestHandler} Returns an object with `respond` method that control how a matched
1389+
* @returns {requestHandler} Returns an object with a `respond` method that controls how a matched
13761390
* request is handled.
13771391
*/
13781392

@@ -1387,7 +1401,7 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
13871401
* receives data string and returns true if the data is as expected, or Object if request body
13881402
* is in JSON format.
13891403
* @param {Object=} headers HTTP headers.
1390-
* @returns {requestHandler} Returns an object with `respond` method that control how a matched
1404+
* @returns {requestHandler} Returns an object with a `respond` method that controls how a matched
13911405
* request is handled.
13921406
*/
13931407

@@ -1402,7 +1416,7 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
14021416
* receives data string and returns true if the data is as expected, or Object if request body
14031417
* is in JSON format.
14041418
* @param {Object=} headers HTTP headers.
1405-
* @returns {requestHandler} Returns an object with `respond` method that control how a matched
1419+
* @returns {requestHandler} Returns an object with a `respond` method that controls how a matched
14061420
* request is handled.
14071421
*/
14081422

@@ -1413,7 +1427,7 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
14131427
* Creates a new request expectation for JSONP requests. For more info see `expect()`.
14141428
*
14151429
* @param {string|RegExp} url HTTP url.
1416-
* @returns {requestHandler} Returns an object with `respond` method that control how a matched
1430+
* @returns {requestHandler} Returns an object with a `respond` method that controls how a matched
14171431
* request is handled.
14181432
*/
14191433
createShortMethods('expect');
@@ -1506,7 +1520,7 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
15061520

15071521

15081522
function createShortMethods(prefix) {
1509-
angular.forEach(['GET', 'DELETE', 'JSONP'], function(method) {
1523+
angular.forEach(['GET', 'DELETE', 'JSONP', 'HEAD'], function(method) {
15101524
$httpBackend[prefix + method] = function(url, headers) {
15111525
return $httpBackend[prefix](method, url, undefined, headers);
15121526
};

test/ng/httpSpec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -985,6 +985,18 @@ describe('$http', function() {
985985
});
986986

987987

988+
it('should have patch()', function() {
989+
$httpBackend.expect('PATCH', '/url', 'some-data').respond('');
990+
$http.patch('/url', 'some-data');
991+
});
992+
993+
994+
it('patch() should allow config param', function() {
995+
$httpBackend.expect('PATCH', '/url', 'some-data', checkHeader('Custom', 'Header')).respond('');
996+
$http.patch('/url', 'some-data', {headers: {'Custom': 'Header'}});
997+
});
998+
999+
9881000
it('should have jsonp()', function() {
9891001
$httpBackend.expect('JSONP', '/url').respond('');
9901002
$http.jsonp('/url');

test/ngMock/angular-mocksSpec.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -947,6 +947,26 @@ describe('ngMock', function() {
947947
}));
948948

949949

950+
it('should provide "expect" methods for each HTTP verb', function() {
951+
expect(typeof hb.expectGET).toBe("function");
952+
expect(typeof hb.expectPOST).toBe("function");
953+
expect(typeof hb.expectPUT).toBe("function");
954+
expect(typeof hb.expectPATCH).toBe("function");
955+
expect(typeof hb.expectDELETE).toBe("function");
956+
expect(typeof hb.expectHEAD).toBe("function");
957+
});
958+
959+
960+
it('should provide "when" methods for each HTTP verb', function() {
961+
expect(typeof hb.whenGET).toBe("function");
962+
expect(typeof hb.whenPOST).toBe("function");
963+
expect(typeof hb.whenPUT).toBe("function");
964+
expect(typeof hb.whenPATCH).toBe("function");
965+
expect(typeof hb.whenDELETE).toBe("function");
966+
expect(typeof hb.whenHEAD).toBe("function");
967+
});
968+
969+
950970
it('should respond with first matched definition', function() {
951971
hb.when('GET', '/url1').respond(200, 'content', {});
952972
hb.when('GET', '/url1').respond(201, 'another', {});

0 commit comments

Comments
 (0)