Skip to content
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
merged 3 commits into from
Sep 20, 2017
Merged
Changes from 1 commit
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
18 changes: 8 additions & 10 deletions lib/httpMock.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

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.

var interceptor = angular.isString(interceptorExpression) ?
$injector.get(interceptorExpression) : $injector.invoke(interceptorExpression);
interceptors.push(interceptor);
});
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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') {
Expand All @@ -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){
Expand Down Expand Up @@ -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){
Expand Down