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

fix($http): include interceptors as a part of $browser's outstandingRequest #13862

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
17 changes: 14 additions & 3 deletions src/ng/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,8 @@ function $HttpProvider() {
**/
var interceptorFactories = this.interceptors = [];

this.$get = ['$httpBackend', '$$cookieReader', '$cacheFactory', '$rootScope', '$q', '$injector',
function($httpBackend, $$cookieReader, $cacheFactory, $rootScope, $q, $injector) {
this.$get = ['$browser', '$httpBackend', '$$cookieReader', '$cacheFactory', '$rootScope', '$q', '$injector',
function($browser, $httpBackend, $$cookieReader, $cacheFactory, $rootScope, $q, $injector) {

var defaultCache = $cacheFactory('$http');

Expand Down Expand Up @@ -956,7 +956,7 @@ function $HttpProvider() {
};

var chain = [serverRequest, undefined];
var promise = $q.when(config);
var promise = initiateOutstandingRequest(config);

// apply interceptors
forEach(reversedInterceptors, function(interceptor) {
Expand All @@ -975,6 +975,8 @@ function $HttpProvider() {
promise = promise.then(thenFn, rejectFn);
}

promise.finally(completeOutstandingRequest);

if (useLegacyPromise) {
promise.success = function(fn) {
assertArgFn(fn, 'fn');
Expand All @@ -1000,6 +1002,15 @@ function $HttpProvider() {

return promise;

function initiateOutstandingRequest(config) {
$browser.$$incOutstandingRequestCount();
return $q.when(config);
}

function completeOutstandingRequest() {
$browser.$$completeOutstandingRequest(noop);
}

function transformResponse(response) {
// make a copy since the response must be cacheable
var resp = extend({}, response);
Expand Down
2 changes: 0 additions & 2 deletions src/ng/httpBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ function $HttpBackendProvider() {
function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDocument) {
// TODO(vojta): fix the signature
return function(method, url, post, callback, headers, timeout, withCredentials, responseType) {
$browser.$$incOutstandingRequestCount();
url = url || $browser.url();

if (lowercase(method) == 'jsonp') {
Expand Down Expand Up @@ -158,7 +157,6 @@ function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDoc
jsonpDone = xhr = null;

callback(status, response, headersString, statusText);
$browser.$$completeOutstandingRequest(noop);
}
};

Expand Down
8 changes: 8 additions & 0 deletions test/e2e/fixtures/http/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!DOCTYPE html>
<html ng-app="test">
<div ng-controller="TestCtrl">
<p>{{text}}</p>
</div>
<script src="angular.js"></script>
<script src="script.js"></script>
</html>
29 changes: 29 additions & 0 deletions test/e2e/fixtures/http/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
angular.module('test', [])
.controller('TestCtrl', function($scope, $http, $cacheFactory, $timeout) {
var cache = $cacheFactory('sites');
var siteUrl = "http://some.site";
cache.put(siteUrl, "Something");
$http.get(siteUrl, {cache: cache}).then(function(data) {
$scope.text = "Hello, world!";
});
})
.config(function($httpProvider) {
$httpProvider.interceptors.push(function($q, $window) {
return {
'request': function(config) {
return $q(function(resolve,reject) {
$window.setTimeout(function() {
resolve(config);
}, 50);
});
},
'response': function(response) {
return $q(function(resolve,reject) {
$window.setTimeout(function() {
resolve(response);
}, 50);
});
}
};
});
});
11 changes: 11 additions & 0 deletions test/e2e/tests/httpSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

describe('$http', function() {
beforeEach(function() {
loadFixture("http").andWaitForAngular();
});

it('should have the interpolated text', function() {
expect(element(by.binding('text')).getText())
.toBe('Hello, world!');
});
});
31 changes: 31 additions & 0 deletions test/ng/httpSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1866,6 +1866,37 @@ describe('$http', function() {
expect(paramSerializer({foo: 'foo', bar: ['bar', 'baz']})).toEqual('bar=bar&bar=baz&foo=foo');
});
});

describe('$browser\'s outstandingRequestCount', function() {
var incOutstandingRequestCountSpy, completeOutstandingRequestSpy;

beforeEach(inject(function($browser) {
incOutstandingRequestCountSpy
= spyOn($browser, '$$incOutstandingRequestCount').andCallThrough();
completeOutstandingRequestSpy
= spyOn($browser, '$$completeOutstandingRequest').andCallThrough();
}));

it('should update $browser outstandingRequestCount on success', function() {
$httpBackend.when('GET').respond(200);

expect(incOutstandingRequestCountSpy).not.toHaveBeenCalled();
$http.get('');
expect(incOutstandingRequestCountSpy).toHaveBeenCalledOnce();
$httpBackend.flush();
expect(completeOutstandingRequestSpy).toHaveBeenCalledOnce();
});

it('should update $browser outstandingRequestCount on error', function() {
$httpBackend.when('GET').respond(500);

expect(incOutstandingRequestCountSpy).not.toHaveBeenCalled();
$http.get('');
expect(incOutstandingRequestCountSpy).toHaveBeenCalledOnce();
$httpBackend.flush();
expect(completeOutstandingRequestSpy).toHaveBeenCalledOnce();
});
});
});


Expand Down