feat($http): remove deprecated callback methods: success()/error()

Closes #15157

BREAKING CHANGE:

`$http`'s deprecated custom callback methods - `success()` and `error()` - have been removed.
You can use the standard `then()`/`catch()` promise methods instead, but note that the method
signatures and return values are different.

`success(fn)` can be replaced with `then(fn)`, and `error(fn)` can be replaced with either
`then(null, fn)` or `catch(fn)`.

Before:

```js
$http(...).
  success(function onSuccess(data, status, headers, config) {
    // Handle success
    ...
  }).
  error(function onError(data, status, headers, config) {
    // Handle error
    ...
  });
```

After:

```js
$http(...).
  then(function onSuccess(response) {
    // Handle success
    var data = response.data;
    var status = response.status;
    var statusText = response.statusText;
    var headers = response.headers;
    var config = response.config;
    ...
  }, function onError(response) {
    // Handle error
    var data = response.data;
    var status = response.status;
    var statusText = response.statusText;
    var headers = response.headers;
    var config = response.config;
    ...
  });

// or

$http(...).
  then(function onSuccess(response) {
    // Handle success
    var data = response.data;
    var status = response.status;
    var statusText = response.statusText;
    var headers = response.headers;
    var config = response.config;
    ...
  }).
  catch(function onError(response) {
    // Handle error
    var data = response.data;
    var status = response.status;
    var statusText = response.statusText;
    var headers = response.headers;
    var config = response.config;
    ...
  });
```

**Note:**
There is a subtle difference between the variations showed above. When using
`$http(...).success(onSuccess).error(onError)` or `$http(...).then(onSuccess, onError)`, the
`onError()` callback will only handle errors/rejections produced by the `$http()` call. If the
`onSuccess()` callback produces an error/rejection, it won't be handled by `onError()` and might go
unnoticed. In contrast, when using `$http(...).then(onSuccess).catch(onError)`, `onError()` will
handle errors/rejections produced by both `$http()` _and_ `onSuccess()`.
This commit is contained in:
Georgios Kalpakas
2016-09-19 18:26:25 +03:00
parent fb66341871
commit b54a39e202
4 changed files with 95 additions and 331 deletions
-45
View File
@@ -1,45 +0,0 @@
@ngdoc error
@name $http:legacy
@fullName The `success` and `error` methods on the promise returned from `$http` have been disabled.
@description
This error occurs when the legacy promise extensions (`success` and `error`)
{@link $httpProvider#useLegacyPromiseExtensions legacy `$http` promise extensions} have been disabled.
To resolve this error, either turn on the legacy extensions by adding
`$httpProvider.useLegacyPromiseExtensions(true);` to your application's configuration; or refactor you
use of `$http` to use `.then()` rather than `.success()` and `.error()`.
For example if you code looked like this:
```js
// Simple GET request example :
$http.get('/someUrl').
success(function(data, status, headers, config) {
// This callback will be called asynchronously
// when the response is available
}).
error(function(data, status, headers, config) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
```
then you would change it to look like:
```js
// Simple GET request example :
$http.get('/someUrl').
then(function(response) {
// (The response object contains the data, status, headers and config properties)
// This callback will be called asynchronously
// when the response is available.
}, function(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
```
For more information, see the
{@link $httpProvider#useLegacyPromiseExtensions `$httpProvider.useLegacyPromiseExtensions`}
documentation.
-60
View File
@@ -9,11 +9,6 @@ var JSON_ENDS = {
};
var JSON_PROTECTION_PREFIX = /^\)\]\}',?\n/;
var $httpMinErr = minErr('$http');
var $httpMinErrLegacyFn = function(method) {
return function() {
throw $httpMinErr('legacy', 'The method `{0}` on the promise returned from `$http` has been disabled.', method);
};
};
function serializeValue(v) {
if (isObject(v)) {
@@ -346,30 +341,6 @@ function $HttpProvider() {
return useApplyAsync;
};
var useLegacyPromise = true;
/**
* @ngdoc method
* @name $httpProvider#useLegacyPromiseExtensions
* @description
*
* Configure `$http` service to return promises without the shorthand methods `success` and `error`.
* This should be used to make sure that applications work without these methods.
*
* Defaults to true. If no value is specified, returns the current configured value.
*
* @param {boolean=} value If true, `$http` will return a promise with the deprecated legacy `success` and `error` methods.
*
* @returns {boolean|Object} If a value is specified, returns the $httpProvider for chaining.
* otherwise, returns the current configured value.
**/
this.useLegacyPromiseExtensions = function(value) {
if (isDefined(value)) {
useLegacyPromise = !!value;
return this;
}
return useLegacyPromise;
};
/**
* @ngdoc property
* @name $httpProvider#interceptors
@@ -503,14 +474,6 @@ function $HttpProvider() {
* $httpBackend.flush();
* ```
*
* ## Deprecation Notice
* <div class="alert alert-danger">
* The `$http` legacy promise methods `success` and `error` have been deprecated.
* Use the standard `then` method instead.
* If {@link $httpProvider#useLegacyPromiseExtensions `$httpProvider.useLegacyPromiseExtensions`} is set to
* `false` then these methods will throw {@link $http:legacy `$http/legacy`} error.
* </div>
*
* ## Setting HTTP Headers
*
* The $http service will automatically add certain HTTP headers to all requests. These defaults
@@ -1000,29 +963,6 @@ function $HttpProvider() {
promise = chainInterceptors(promise, responseInterceptors);
promise = promise.finally(completeOutstandingRequest);
if (useLegacyPromise) {
promise.success = function(fn) {
assertArgFn(fn, 'fn');
promise.then(function(response) {
fn(response.data, response.status, response.headers, config);
});
return promise;
};
promise.error = function(fn) {
assertArgFn(fn, 'fn');
promise.then(null, function(response) {
fn(response.data, response.status, response.headers, config);
});
return promise;
};
} else {
promise.success = $httpMinErrLegacyFn('success');
promise.error = $httpMinErrLegacyFn('error');
}
return promise;
+2 -2
View File
@@ -612,8 +612,8 @@ function $SceDelegateProvider() {
* .controller('AppController', ['$http', '$templateCache', '$sce',
* function AppController($http, $templateCache, $sce) {
* var self = this;
* $http.get('test_data.json', {cache: $templateCache}).success(function(userComments) {
* self.userComments = userComments;
* $http.get('test_data.json', {cache: $templateCache}).then(function(response) {
* self.userComments = response.data;
* });
* self.explicitlyTrustedHtml = $sce.trustAsHtml(
* '<span onmouseover="this.textContent=&quot;Explicitly trusted HTML bypasses ' +
+93 -224
View File
@@ -462,110 +462,6 @@ describe('$http', function() {
$httpBackend.flush();
expect(callback).toHaveBeenCalledOnce();
});
describe('success', function() {
it('should allow http specific callbacks to be registered via "success"', function() {
$httpBackend.expect('GET', '/url').respond(207, 'my content', {'content-encoding': 'smurf'});
$http({url: '/url', method: 'GET'}).success(function(data, status, headers, config) {
expect(data).toBe('my content');
expect(status).toBe(207);
expect(headers()).toEqual(extend(Object.create(null), {'content-encoding': 'smurf'}));
expect(config.url).toBe('/url');
callback();
});
$httpBackend.flush();
expect(callback).toHaveBeenCalledOnce();
});
it('should return the original http promise', function() {
$httpBackend.expect('GET', '/url').respond(207, 'my content', {'content-encoding': 'smurf'});
var httpPromise = $http({url: '/url', method: 'GET'});
expect(httpPromise.success(callback)).toBe(httpPromise);
});
it('should error if the callback is not a function', function() {
expect(function() {
$http({url: '/url', method: 'GET'}).success();
}).toThrowMinErr('ng', 'areq');
expect(function() {
$http({url: '/url', method: 'GET'}).success(undefined);
}).toThrowMinErr('ng', 'areq');
expect(function() {
$http({url: '/url', method: 'GET'}).success(null);
}).toThrowMinErr('ng', 'areq');
expect(function() {
$http({url: '/url', method: 'GET'}).success({});
}).toThrowMinErr('ng', 'areq');
expect(function() {
$http({url: '/url', method: 'GET'}).success([]);
}).toThrowMinErr('ng', 'areq');
expect(function() {
$http({url: '/url', method: 'GET'}).success('error');
}).toThrowMinErr('ng', 'areq');
});
});
describe('error', function() {
it('should allow http specific callbacks to be registered via "error"', function() {
$httpBackend.expect('GET', '/url').respond(543, 'bad error', {'request-id': '123'});
$http({url: '/url', method: 'GET'}).error(function(data, status, headers, config) {
expect(data).toBe('bad error');
expect(status).toBe(543);
expect(headers()).toEqual(extend(Object.create(null), {'request-id': '123'}));
expect(config.url).toBe('/url');
callback();
});
$httpBackend.flush();
expect(callback).toHaveBeenCalledOnce();
});
it('should return the original http promise', function() {
$httpBackend.expect('GET', '/url').respond(543, 'bad error', {'request-id': '123'});
var httpPromise = $http({url: '/url', method: 'GET'});
expect(httpPromise.error(callback)).toBe(httpPromise);
});
it('should error if the callback is not a function', function() {
expect(function() {
$http({url: '/url', method: 'GET'}).error();
}).toThrowMinErr('ng', 'areq');
expect(function() {
$http({url: '/url', method: 'GET'}).error(undefined);
}).toThrowMinErr('ng', 'areq');
expect(function() {
$http({url: '/url', method: 'GET'}).error(null);
}).toThrowMinErr('ng', 'areq');
expect(function() {
$http({url: '/url', method: 'GET'}).error({});
}).toThrowMinErr('ng', 'areq');
expect(function() {
$http({url: '/url', method: 'GET'}).error([]);
}).toThrowMinErr('ng', 'areq');
expect(function() {
$http({url: '/url', method: 'GET'}).error('error');
}).toThrowMinErr('ng', 'areq');
});
});
});
@@ -1235,7 +1131,7 @@ describe('$http', function() {
transformRequest: function(data, headers) {
return headers('h1');
}
}).success(callback);
}).then(callback);
$httpBackend.flush();
expect(callback).toHaveBeenCalledOnce();
@@ -1248,7 +1144,7 @@ describe('$http', function() {
transformRequest: function(data, headers) {
return headers('H1');
}
}).success(callback);
}).then(callback);
$httpBackend.flush();
expect(callback).toHaveBeenCalledOnce();
@@ -1265,7 +1161,7 @@ describe('$http', function() {
};
$httpBackend.expect('GET', '/url', undefined, {Accept: 'bar'}).respond(200);
$http.get('/url', config).success(callback);
$http.get('/url', config).then(callback);
$httpBackend.flush();
expect(callback).toHaveBeenCalledOnce();
@@ -1279,7 +1175,7 @@ describe('$http', function() {
$http.post('/url', 'req', {
headers: {h1: 'v1'},
transformRequest: [first, second]
}).success(callback);
}).then(callback);
$httpBackend.flush();
expect(callback).toHaveBeenCalledOnce();
@@ -1293,127 +1189,127 @@ describe('$http', function() {
it('should deserialize json objects', function() {
$httpBackend.expect('GET', '/url').respond('{"foo":"bar","baz":23}');
$http({method: 'GET', url: '/url'}).success(callback);
$http({method: 'GET', url: '/url'}).then(callback);
$httpBackend.flush();
expect(callback).toHaveBeenCalledOnce();
expect(callback.calls.mostRecent().args[0]).toEqual({foo: 'bar', baz: 23});
expect(callback.calls.mostRecent().args[0].data).toEqual({foo: 'bar', baz: 23});
});
it('should deserialize json arrays', function() {
$httpBackend.expect('GET', '/url').respond('[1, "abc", {"foo":"bar"}]');
$http({method: 'GET', url: '/url'}).success(callback);
$http({method: 'GET', url: '/url'}).then(callback);
$httpBackend.flush();
expect(callback).toHaveBeenCalledOnce();
expect(callback.calls.mostRecent().args[0]).toEqual([1, 'abc', {foo: 'bar'}]);
expect(callback.calls.mostRecent().args[0].data).toEqual([1, 'abc', {foo: 'bar'}]);
});
it('should ignore leading/trailing whitespace', function() {
$httpBackend.expect('GET', '/url').respond(' \n {"foo":"bar","baz":23} \r\n \n ');
$http({method: 'GET', url: '/url'}).success(callback);
$http({method: 'GET', url: '/url'}).then(callback);
$httpBackend.flush();
expect(callback).toHaveBeenCalledOnce();
expect(callback.calls.mostRecent().args[0]).toEqual({foo: 'bar', baz: 23});
expect(callback.calls.mostRecent().args[0].data).toEqual({foo: 'bar', baz: 23});
});
it('should deserialize json numbers when response header contains application/json',
function() {
$httpBackend.expect('GET', '/url').respond('123', {'Content-Type': 'application/json'});
$http({method: 'GET', url: '/url'}).success(callback);
$http({method: 'GET', url: '/url'}).then(callback);
$httpBackend.flush();
expect(callback).toHaveBeenCalledOnce();
expect(callback.calls.mostRecent().args[0]).toEqual(123);
expect(callback.calls.mostRecent().args[0].data).toEqual(123);
});
it('should deserialize json strings when response header contains application/json',
function() {
$httpBackend.expect('GET', '/url').respond('"asdf"', {'Content-Type': 'application/json'});
$http({method: 'GET', url: '/url'}).success(callback);
$http({method: 'GET', url: '/url'}).then(callback);
$httpBackend.flush();
expect(callback).toHaveBeenCalledOnce();
expect(callback.calls.mostRecent().args[0]).toEqual('asdf');
expect(callback.calls.mostRecent().args[0].data).toEqual('asdf');
});
it('should deserialize json nulls when response header contains application/json',
function() {
$httpBackend.expect('GET', '/url').respond('null', {'Content-Type': 'application/json'});
$http({method: 'GET', url: '/url'}).success(callback);
$http({method: 'GET', url: '/url'}).then(callback);
$httpBackend.flush();
expect(callback).toHaveBeenCalledOnce();
expect(callback.calls.mostRecent().args[0]).toEqual(null);
expect(callback.calls.mostRecent().args[0].data).toEqual(null);
});
it('should deserialize json true when response header contains application/json',
function() {
$httpBackend.expect('GET', '/url').respond('true', {'Content-Type': 'application/json'});
$http({method: 'GET', url: '/url'}).success(callback);
$http({method: 'GET', url: '/url'}).then(callback);
$httpBackend.flush();
expect(callback).toHaveBeenCalledOnce();
expect(callback.calls.mostRecent().args[0]).toEqual(true);
expect(callback.calls.mostRecent().args[0].data).toEqual(true);
});
it('should deserialize json false when response header contains application/json',
function() {
$httpBackend.expect('GET', '/url').respond('false', {'Content-Type': 'application/json'});
$http({method: 'GET', url: '/url'}).success(callback);
$http({method: 'GET', url: '/url'}).then(callback);
$httpBackend.flush();
expect(callback).toHaveBeenCalledOnce();
expect(callback.calls.mostRecent().args[0]).toEqual(false);
expect(callback.calls.mostRecent().args[0].data).toEqual(false);
});
it('should deserialize json empty string when response header contains application/json',
function() {
$httpBackend.expect('GET', '/url').respond('""', {'Content-Type': 'application/json'});
$http({method: 'GET', url: '/url'}).success(callback);
$http({method: 'GET', url: '/url'}).then(callback);
$httpBackend.flush();
expect(callback).toHaveBeenCalledOnce();
expect(callback.calls.mostRecent().args[0]).toEqual('');
expect(callback.calls.mostRecent().args[0].data).toEqual('');
});
it('should deserialize json with security prefix', function() {
$httpBackend.expect('GET', '/url').respond(')]}\',\n[1, "abc", {"foo":"bar"}]');
$http({method: 'GET', url: '/url'}).success(callback);
$http({method: 'GET', url: '/url'}).then(callback);
$httpBackend.flush();
expect(callback).toHaveBeenCalledOnce();
expect(callback.calls.mostRecent().args[0]).toEqual([1, 'abc', {foo:'bar'}]);
expect(callback.calls.mostRecent().args[0].data).toEqual([1, 'abc', {foo:'bar'}]);
});
it('should deserialize json with security prefix ")]}\'"', function() {
$httpBackend.expect('GET', '/url').respond(')]}\'\n\n[1, "abc", {"foo":"bar"}]');
$http({method: 'GET', url: '/url'}).success(callback);
$http({method: 'GET', url: '/url'}).then(callback);
$httpBackend.flush();
expect(callback).toHaveBeenCalledOnce();
expect(callback.calls.mostRecent().args[0]).toEqual([1, 'abc', {foo:'bar'}]);
expect(callback.calls.mostRecent().args[0].data).toEqual([1, 'abc', {foo:'bar'}]);
});
it('should retain security prefix if response is not json', function() {
$httpBackend.expect('GET', '/url').respond(')]}\',\n This is not JSON !');
$http({method: 'GET', url: '/url'}).success(callback);
$http({method: 'GET', url: '/url'}).then(callback);
$httpBackend.flush();
expect(callback).toHaveBeenCalledOnce();
expect(callback.calls.mostRecent().args[0]).toEqual(')]}\',\n This is not JSON !');
expect(callback.calls.mostRecent().args[0].data).toEqual(')]}\',\n This is not JSON !');
});
@@ -1421,55 +1317,55 @@ describe('$http', function() {
//per http spec for Content-Type, HEAD request should return a Content-Type header
//set to what the content type would have been if a get was sent
$httpBackend.expect('HEAD', '/url').respond('', {'Content-Type': 'application/json'});
$http({method: 'HEAD', url: '/url'}).success(callback);
$http({method: 'HEAD', url: '/url'}).then(callback);
$httpBackend.flush();
expect(callback).toHaveBeenCalledOnce();
expect(callback.calls.mostRecent().args[0]).toEqual('');
expect(callback.calls.mostRecent().args[0].data).toEqual('');
});
it('should not attempt to deserialize json for an empty response whose header contains application/json', function() {
//per http spec for Content-Type, HEAD request should return a Content-Type header
//set to what the content type would have been if a get was sent
$httpBackend.expect('GET', '/url').respond('', {'Content-Type': 'application/json'});
$http({method: 'GET', url: '/url'}).success(callback);
$http({method: 'GET', url: '/url'}).then(callback);
$httpBackend.flush();
expect(callback).toHaveBeenCalledOnce();
expect(callback.calls.mostRecent().args[0]).toEqual('');
expect(callback.calls.mostRecent().args[0].data).toEqual('');
});
it('should not attempt to deserialize json for a blank response whose header contains application/json', function() {
//per http spec for Content-Type, HEAD request should return a Content-Type header
//set to what the content type would have been if a get was sent
$httpBackend.expect('GET', '/url').respond(' ', {'Content-Type': 'application/json'});
$http({method: 'GET', url: '/url'}).success(callback);
$http({method: 'GET', url: '/url'}).then(callback);
$httpBackend.flush();
expect(callback).toHaveBeenCalledOnce();
expect(callback.calls.mostRecent().args[0]).toEqual(' ');
expect(callback.calls.mostRecent().args[0].data).toEqual(' ');
});
it('should not deserialize tpl beginning with ng expression', function() {
$httpBackend.expect('GET', '/url').respond('{{some}}');
$http.get('/url').success(callback);
$http.get('/url').then(callback);
$httpBackend.flush();
expect(callback).toHaveBeenCalledOnce();
expect(callback.calls.mostRecent().args[0]).toEqual('{{some}}');
expect(callback.calls.mostRecent().args[0].data).toEqual('{{some}}');
});
it('should not deserialize json when the opening and closing brackets do not match',
function() {
$httpBackend.expect('GET', '/url1').respond('[Code](url): function() {}');
$httpBackend.expect('GET', '/url2').respond('{"is": "not"} ["json"]');
$http.get('/url1').success(callback);
$http.get('/url2').success(callback);
$http.get('/url1').then(callback);
$http.get('/url2').then(callback);
$httpBackend.flush();
expect(callback).toHaveBeenCalledTimes(2);
expect(callback.calls.argsFor(0)[0]).toEqual('[Code](url): function() {}');
expect(callback.calls.argsFor(1)[0]).toEqual('{"is": "not"} ["json"]');
expect(callback.calls.argsFor(0)[0].data).toEqual('[Code](url): function() {}');
expect(callback.calls.argsFor(1)[0].data).toEqual('{"is": "not"} ["json"]');
}
);
});
@@ -1481,11 +1377,11 @@ describe('$http', function() {
transformResponse: function(data, headers) {
return headers('h1');
}
}).success(callback);
}).then(callback);
$httpBackend.flush();
expect(callback).toHaveBeenCalledOnce();
expect(callback.calls.mostRecent().args[0]).toBe('header1');
expect(callback.calls.mostRecent().args[0].data).toBe('header1');
});
it('should have access to response status', function() {
@@ -1494,11 +1390,11 @@ describe('$http', function() {
transformResponse: function(data, headers, status) {
return status;
}
}).success(callback);
}).then(callback);
$httpBackend.flush();
expect(callback).toHaveBeenCalledOnce();
expect(callback.calls.mostRecent().args[0]).toBe(200);
expect(callback.calls.mostRecent().args[0].data).toBe(200);
});
@@ -1507,11 +1403,11 @@ describe('$http', function() {
function second(d) {return uppercase(d);}
$httpBackend.expect('POST', '/url').respond(200, 'resp', {h1: 'v1'});
$http.post('/url', '', {transformResponse: [first, second]}).success(callback);
$http.post('/url', '', {transformResponse: [first, second]}).then(callback);
$httpBackend.flush();
expect(callback).toHaveBeenCalledOnce();
expect(callback.calls.mostRecent().args[0]).toBe('RESP-FIRST:V1');
expect(callback.calls.mostRecent().args[0].data).toBe('RESP-FIRST:V1');
});
@@ -1555,11 +1451,11 @@ describe('$http', function() {
it('should cache GET request when cache is provided', inject(function($rootScope) {
doFirstCacheRequest();
$http({method: 'get', url: '/url', cache: cache}).success(callback);
$http({method: 'get', url: '/url', cache: cache}).then(callback);
$rootScope.$digest();
expect(callback).toHaveBeenCalledOnce();
expect(callback.calls.mostRecent().args[0]).toBe('content');
expect(callback.calls.mostRecent().args[0].data).toBe('content');
}));
it('should cache JSONP request when cache is provided', inject(function($rootScope) {
@@ -1567,21 +1463,21 @@ describe('$http', function() {
$http({method: 'JSONP', url: $sce.trustAsResourceUrl('/url'), cache: cache});
$httpBackend.flush();
$http({method: 'JSONP', url: $sce.trustAsResourceUrl('/url'), cache: cache}).success(callback);
$http({method: 'JSONP', url: $sce.trustAsResourceUrl('/url'), cache: cache}).then(callback);
$rootScope.$digest();
expect(callback).toHaveBeenCalledOnce();
expect(callback.calls.mostRecent().args[0]).toBe('content');
expect(callback.calls.mostRecent().args[0].data).toBe('content');
}));
it('should cache request when cache is provided and no method specified', function() {
doFirstCacheRequest();
$http({url: '/url', cache: cache}).success(callback);
$http({url: '/url', cache: cache}).then(callback);
$rootScope.$digest();
expect(callback).toHaveBeenCalledOnce();
expect(callback.calls.mostRecent().args[0]).toBe('content');
expect(callback.calls.mostRecent().args[0].data).toBe('content');
});
@@ -1614,11 +1510,11 @@ describe('$http', function() {
doFirstCacheRequest('POST');
$httpBackend.expect('POST', '/url').respond('content2');
$http({method: 'POST', url: '/url', cache: cache}).success(callback);
$http({method: 'POST', url: '/url', cache: cache}).then(callback);
$httpBackend.flush();
expect(callback).toHaveBeenCalledOnce();
expect(callback.calls.mostRecent().args[0]).toBe('content2');
expect(callback.calls.mostRecent().args[0].data).toBe('content2');
});
@@ -1626,11 +1522,11 @@ describe('$http', function() {
doFirstCacheRequest('PUT');
$httpBackend.expect('PUT', '/url').respond('content2');
$http({method: 'PUT', url: '/url', cache: cache}).success(callback);
$http({method: 'PUT', url: '/url', cache: cache}).then(callback);
$httpBackend.flush();
expect(callback).toHaveBeenCalledOnce();
expect(callback.calls.mostRecent().args[0]).toBe('content2');
expect(callback.calls.mostRecent().args[0].data).toBe('content2');
});
@@ -1638,7 +1534,7 @@ describe('$http', function() {
doFirstCacheRequest('DELETE');
$httpBackend.expect('DELETE', '/url').respond(206);
$http({method: 'DELETE', url: '/url', cache: cache}).success(callback);
$http({method: 'DELETE', url: '/url', cache: cache}).then(callback);
$httpBackend.flush();
expect(callback).toHaveBeenCalledOnce();
@@ -1649,22 +1545,25 @@ describe('$http', function() {
doFirstCacheRequest('GET', 404);
$httpBackend.expect('GET', '/url').respond('content2');
$http({method: 'GET', url: '/url', cache: cache}).success(callback);
$http({method: 'GET', url: '/url', cache: cache}).then(callback);
$httpBackend.flush();
expect(callback).toHaveBeenCalledOnce();
expect(callback.calls.mostRecent().args[0]).toBe('content2');
expect(callback.calls.mostRecent().args[0].data).toBe('content2');
});
it('should cache the headers as well', inject(function($rootScope) {
doFirstCacheRequest('GET', 200, {'content-encoding': 'gzip', 'server': 'Apache'});
callback.and.callFake(function(r, s, headers) {
expect(headers()).toEqual(extend(Object.create(null), {'content-encoding': 'gzip', 'server': 'Apache'}));
expect(headers('server')).toBe('Apache');
callback.and.callFake(function(response) {
expect(response.headers()).toEqual(extend(Object.create(null), {
'content-encoding': 'gzip',
'server': 'Apache'
}));
expect(response.headers('server')).toBe('Apache');
});
$http({method: 'GET', url: '/url', cache: cache}).success(callback);
$http({method: 'GET', url: '/url', cache: cache}).then(callback);
$rootScope.$digest();
expect(callback).toHaveBeenCalledOnce();
}));
@@ -1672,12 +1571,12 @@ describe('$http', function() {
it('should not share the cached headers object instance', inject(function($rootScope) {
doFirstCacheRequest('GET', 200, {'content-encoding': 'gzip', 'server': 'Apache'});
callback.and.callFake(function(r, s, headers) {
expect(headers()).toEqual(cache.get('/url')[2]);
expect(headers()).not.toBe(cache.get('/url')[2]);
callback.and.callFake(function(response) {
expect(response.headers()).toEqual(cache.get('/url')[2]);
expect(response.headers()).not.toBe(cache.get('/url')[2]);
});
$http({method: 'GET', url: '/url', cache: cache}).success(callback);
$http({method: 'GET', url: '/url', cache: cache}).then(callback);
$rootScope.$digest();
expect(callback).toHaveBeenCalledOnce();
}));
@@ -1703,11 +1602,11 @@ describe('$http', function() {
it('should cache status code as well', inject(function($rootScope) {
doFirstCacheRequest('GET', 201);
callback.and.callFake(function(r, status, h) {
expect(status).toBe(201);
callback.and.callFake(function(response) {
expect(response.status).toBe(201);
});
$http({method: 'get', url: '/url', cache: cache}).success(callback);
$http({method: 'get', url: '/url', cache: cache}).then(callback);
$rootScope.$digest();
expect(callback).toHaveBeenCalledOnce();
}));
@@ -1716,13 +1615,13 @@ describe('$http', function() {
it('should use cache even if second request was made before the first returned', function() {
$httpBackend.expect('GET', '/url').respond(201, 'fake-response');
callback.and.callFake(function(response, status, headers) {
expect(response).toBe('fake-response');
expect(status).toBe(201);
callback.and.callFake(function(response) {
expect(response.data).toBe('fake-response');
expect(response.status).toBe(201);
});
$http({method: 'GET', url: '/url', cache: cache}).success(callback);
$http({method: 'GET', url: '/url', cache: cache}).success(callback);
$http({method: 'GET', url: '/url', cache: cache}).then(callback);
$http({method: 'GET', url: '/url', cache: cache}).then(callback);
$httpBackend.flush();
expect(callback).toHaveBeenCalled();
@@ -1767,12 +1666,12 @@ describe('$http', function() {
it('should allow the cached value to be an empty string', function() {
cache.put('/abc', '');
callback.and.callFake(function(response, status, headers) {
expect(response).toBe('');
expect(status).toBe(200);
callback.and.callFake(function(response) {
expect(response.data).toBe('');
expect(response.status).toBe(200);
});
$http({method: 'GET', url: '/abc', cache: cache}).success(callback);
$http({method: 'GET', url: '/abc', cache: cache}).then(callback);
$rootScope.$digest();
expect(callback).toHaveBeenCalled();
});
@@ -1781,10 +1680,10 @@ describe('$http', function() {
it('should default to status code 200 and empty headers if cache contains a non-array element',
inject(function($rootScope) {
cache.put('/myurl', 'simple response');
$http.get('/myurl', {cache: cache}).success(function(data, status, headers) {
expect(data).toBe('simple response');
expect(status).toBe(200);
expect(headers()).toEqual(Object.create(null));
$http.get('/myurl', {cache: cache}).then(function(response) {
expect(response.data).toBe('simple response');
expect(response.status).toBe(200);
expect(response.headers()).toEqual(Object.create(null));
callback();
});
@@ -1808,11 +1707,11 @@ describe('$http', function() {
$httpBackend.flush();
// Second should be served from cache, without sending request to server.
$http({method: 'get', url: '/url'}).success(callback);
$http({method: 'get', url: '/url'}).then(callback);
$rootScope.$digest();
expect(callback).toHaveBeenCalledOnce();
expect(callback.calls.mostRecent().args[0]).toBe('content');
expect(callback.calls.mostRecent().args[0].data).toBe('content');
// Invalidate cache entry.
$http.defaults.cache.remove('/url');
@@ -1838,17 +1737,17 @@ describe('$http', function() {
$httpBackend.flush();
// Serve request from default cache when no local given.
$http({method: 'get', url: '/url'}).success(callback);
$http({method: 'get', url: '/url'}).then(callback);
$rootScope.$digest();
expect(callback).toHaveBeenCalledOnce();
expect(callback.calls.mostRecent().args[0]).toBe('content-default-cache');
expect(callback.calls.mostRecent().args[0].data).toBe('content-default-cache');
callback.calls.reset();
// Serve request from local cache when it is given (but default filled too).
$http({method: 'get', url: '/url', cache: localCache}).success(callback);
$http({method: 'get', url: '/url', cache: localCache}).then(callback);
$rootScope.$digest();
expect(callback).toHaveBeenCalledOnce();
expect(callback.calls.mostRecent().args[0]).toBe('content-local-cache');
expect(callback.calls.mostRecent().args[0].data).toBe('content-local-cache');
}));
it('should be skipped if {cache: false} is passed in request config', function() {
@@ -1945,7 +1844,7 @@ describe('$http', function() {
it('should remove the request before firing callbacks', function() {
$httpBackend.when('GET').respond(200);
$http({method: 'get', url: '/url'}).success(function() {
$http({method: 'get', url: '/url'}).then(function() {
expect($http.pendingRequests.length).toBe(0);
});
@@ -2338,35 +2237,6 @@ describe('$http with $applyAsync', function() {
});
});
describe('$http without useLegacyPromiseExtensions', function() {
var $httpBackend, $http;
beforeEach(module(function($httpProvider) {
$httpProvider.useLegacyPromiseExtensions(false);
}, provideLog));
beforeEach(inject(['$httpBackend', '$http', '$rootScope', function($hb, $h, $rs) {
$httpBackend = $hb;
$http = $h;
}]));
it('should throw when the success or error methods are called if useLegacyPromiseExtensions is false', function() {
$httpBackend.expect('GET', '/url').respond('');
var promise = $http({url: '/url'});
function callSucess() {
promise.success();
}
function callError() {
promise.error();
}
expect(callSucess).toThrowMinErr(
'$http', 'legacy', 'The method `success` on the promise returned from `$http` has been disabled.');
expect(callError).toThrowMinErr(
'$http', 'legacy', 'The method `error` on the promise returned from `$http` has been disabled.');
});
});
describe('$http param serializers', function() {
@@ -2425,5 +2295,4 @@ describe('$http param serializers', function() {
//a[]=b&a[]=c&d[0][e]=f&d[0][g]=h&d[]=i&d[2][j]=k
});
});
});