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

feat($http): Pass http config to response interceptors #1851

Closed
wants to merge 2 commits into from
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
7 changes: 4 additions & 3 deletions src/ng/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,12 +314,13 @@ function $HttpProvider() {
* The interceptors are service factories that are registered with the $httpProvider by
* adding them to the `$httpProvider.responseInterceptors` array. The factory is called and
* injected with dependencies (if specified) and returns the interceptor — a function that
* takes a {@link ng.$q promise} and returns the original or a new promise.
* takes a {@link ng.$q promise} and a config object, which is the same object that was
* passed to $http and returns the original or a new promise.
*
* <pre>
* // register the interceptor as a service
* $provide.factory('myHttpInterceptor', function($q, dependency1, dependency2) {
* return function(promise) {
* return function(promise, config) {
* return promise.then(function(response) {
* // do something on success
* }, function(response) {
Expand Down Expand Up @@ -541,7 +542,7 @@ function $HttpProvider() {

// apply interceptors
forEach(responseInterceptors, function(interceptor) {
promise = interceptor(promise);
promise = interceptor(promise, config);
});

promise.success = function(fn) {
Expand Down
28 changes: 28 additions & 0 deletions test/ng/httpSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,34 @@ describe('$http', function() {
expect(response).toBe('HELLO!');
});
});


it('should pass the http config through to the interceptors', function(){
var interceptedConfig;
var httpConfig = {
method: 'GET',
url: '/test'
}
module(function($httpProvider){
$httpProvider.responseInterceptors.push(function(){
return function(httpPromise, config){
return httpPromise.then(function(response){
interceptedConfig = config;
return httpPromise;
});
};
});
});
inject(function($http, $httpBackend){
$httpBackend.expect('GET', '/test').respond('hello!');
debugger;
$http(httpConfig);
expect(interceptedConfig).toBeUndefined();

$httpBackend.flush();
expect(interceptedConfig).toBe(httpConfig);
});
});
});
});

Expand Down