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

fix($http): add missing shortcut methods and missing docs #9321

Closed
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
37 changes: 25 additions & 12 deletions src/ng/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand Down
46 changes: 30 additions & 16 deletions src/ngMock/angular-mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

controlss !

* request is handled.
*
* - respond –
Expand Down Expand Up @@ -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.
*/

Expand All @@ -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.
*/

Expand All @@ -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.
*/

Expand All @@ -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.
*/

Expand All @@ -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.
*/

Expand All @@ -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');
Expand All @@ -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 –
Expand Down Expand Up @@ -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.
*/

Expand All @@ -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.
*/

Expand All @@ -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.
*/

Expand All @@ -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.
*/

Expand All @@ -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.
*/

Expand All @@ -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.
*/

Expand All @@ -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');
Expand Down Expand Up @@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should add in the HEAD method to 1.2.x

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@petebacondarwin : HEAD is already there. This is just adding the mock.

$httpBackend[prefix + method] = function(url, headers) {
return $httpBackend[prefix](method, url, undefined, headers);
};
Expand Down
12 changes: 12 additions & 0 deletions test/ng/httpSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
20 changes: 20 additions & 0 deletions test/ngMock/angular-mocksSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', {});
Expand Down