From b799952e96bd8868e6045b91544d3badb10ac8b1 Mon Sep 17 00:00:00 2001 From: Sebastian Felis Date: Mon, 7 Aug 2017 15:37:27 +0200 Subject: [PATCH 1/3] Instantiate interceptor only once --- lib/httpMock.js | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/lib/httpMock.js b/lib/httpMock.js index b24b89c..9619a34 100644 --- a/lib/httpMock.js +++ b/lib/httpMock.js @@ -15,15 +15,13 @@ function mockTemplate() { $provide.decorator('$http', ['$delegate', '$q', '$injector', function($http, $q, $injector) { - var interceptors = $httpProvider.interceptors; + var interceptors = []; - function getInterceptor(interceptorExpression) { - if (angular.isString(interceptorExpression)) { - return $injector.get(interceptorExpression); - } else { - return $injector.invoke(interceptorExpression); - } - } + angular.forEach($httpProvider.interceptors, function(interceptorExpression) { + var interceptor = angular.isString(interceptorExpression) ? + $injector.get(interceptorExpression) : $injector.invoke(interceptorExpression); + interceptors.push(interceptor); + }); function transformData(data, headers, status, fns) { if (typeof fns === 'function') { @@ -50,7 +48,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){ @@ -84,7 +82,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){ From 9991b0a64aa5086eacb88defb568edb05c41abe3 Mon Sep 17 00:00:00 2001 From: Sebastian Felis Date: Mon, 7 Aug 2017 16:34:17 +0200 Subject: [PATCH 2/3] Replace ternary operator --- lib/httpMock.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/httpMock.js b/lib/httpMock.js index 9619a34..db5b85d 100644 --- a/lib/httpMock.js +++ b/lib/httpMock.js @@ -18,8 +18,12 @@ function mockTemplate() { var interceptors = []; angular.forEach($httpProvider.interceptors, function(interceptorExpression) { - var interceptor = angular.isString(interceptorExpression) ? - $injector.get(interceptorExpression) : $injector.invoke(interceptorExpression); + var interceptor; + if (angular.isString(interceptorExpression)) { + interceptor = $injector.get(interceptorExpression); + } else { + interceptor = $injector.invoke(interceptorExpression); + } interceptors.push(interceptor); }); From 2788de718082ba19961f71c58b39b70aea3806d6 Mon Sep 17 00:00:00 2001 From: Sebastian Felis Date: Fri, 18 Aug 2017 09:28:48 +0200 Subject: [PATCH 3/3] Add test for stateful interceptor --- tests/interceptors.test.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tests/interceptors.test.js b/tests/interceptors.test.js index 05b07ff..ee8cb17 100644 --- a/tests/interceptors.test.js +++ b/tests/interceptors.test.js @@ -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){ @@ -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',