-
Notifications
You must be signed in to change notification settings - Fork 70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Instantiate interceptor only once #121
Merged
atecarlos
merged 3 commits into
atecarlos:master
from
xemle:instantiate-interceptors-only-once
Sep 20, 2017
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it'd be cleaner to keep the if/else block rather than use a ternary operator. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No problem. Fixed. |
||
|
||
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){ | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if it happens, but if interceptors are added to
$httpProvider.interceptors
after this loop runs then they'll be ignored.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe. I can not answer this question.
However we had problems with angular-loading-bar and protractor-http-mock working together. The root cause was that a stateful interceptor by anonymous factory which caused troubles with protractor-http-mock. It is hard to blame and fix one of each side in this case.
If the interceptor uses a named provider, the $injector can cache and reuse the instance and this PR can be dropped. But this issue is hard to track down and it would be nice if it does not occur in the future.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Regarding added interceptors: This case should only happen if the $httpProvider is modified after the mock() call in a jasmine test scenario. The $http service can not modify interceptors. I think the normal case is to mock the $http service at the very and of a test scenario in a beforeEach() block. Therefore the mock is the last delegate of the $http service, isn't it?
And than this PR should be safe.