Skip to content

Commit

Permalink
Merge pull request #121 from xemle/instantiate-interceptors-only-once
Browse files Browse the repository at this point in the history
Instantiate interceptor only once
  • Loading branch information
atecarlos authored Sep 20, 2017
2 parents f1f0ec1 + 2788de7 commit d08ed66
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
16 changes: 9 additions & 7 deletions lib/httpMock.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@ function mockTemplate() {

$provide.decorator('$http', ['$delegate', '$q', '$injector', function($http, $q, $injector) {

var interceptors = $httpProvider.interceptors;
var interceptors = [];

function getInterceptor(interceptorExpression) {
angular.forEach($httpProvider.interceptors, function(interceptorExpression) {
var interceptor;
if (angular.isString(interceptorExpression)) {
return $injector.get(interceptorExpression);
interceptor = $injector.get(interceptorExpression);
} else {
return $injector.invoke(interceptorExpression);
interceptor = $injector.invoke(interceptorExpression);
}
}
interceptors.push(interceptor);
});

function transformData(data, headers, status, fns) {
if (typeof fns === 'function') {
Expand All @@ -50,7 +52,7 @@ function mockTemplate() {

function getTransformedAndInterceptedRequestConfig(requestConfig) {
for (var i = 0; i < interceptors.length; i++) {
var interceptor = getInterceptor(interceptors[i]);
var interceptor = interceptors[i];

if (interceptor.request) {
$q.when(interceptor.request(requestConfig)).then(function(interceptedRequestConfig){
Expand Down Expand Up @@ -84,7 +86,7 @@ function mockTemplate() {

// Response interceptors are invoked in reverse order as per docs
for (var i = interceptors.length - 1; i >= 0; i--) {
var interceptor = getInterceptor(interceptors[i]);
var interceptor = interceptors[i];

if (interceptor.response && statusIsSuccessful(response.status)) {
$q.when(interceptor.response(response)).then(function(interceptedResponse){
Expand Down
27 changes: 27 additions & 0 deletions tests/interceptors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ describe('interceptors', function(){
}
});

$httpProvider.interceptors.push(function(){
var count = 0;
return {
response: function(response){
count++;
response.headers['stateful-anonymous-response-count'] = count;

return response;
}
}
});

$httpProvider.interceptors.push(['$q', function($q){
return {
request: function(config){
Expand Down Expand Up @@ -115,6 +127,21 @@ describe('interceptors', function(){
});
});

it('allows for intercepts through stateful anonymous factory', function(done){
http({
method: 'GET',
url: 'test-url.com/anonymous-intercept'
}).then(function(){
return http({
method: 'GET',
url: 'test-url.com/anonymous-intercept'
});
}).then(function(response){
expect(response.headers['stateful-anonymous-response-count']).toBeGreaterThan(1);
done();
});
});

it('allows for intercepts that return a promise from a request', function(done){
http({
method: 'POST',
Expand Down

0 comments on commit d08ed66

Please sign in to comment.