Last active
February 25, 2016 23:19
-
-
Save stefanotorresi/2eee19691139d39fd16a to your computer and use it in GitHub Desktop.
An example of an Angular $http interceptor to store and reuse headers between requests
This file contains hidden or 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
// Karma configuration | |
// Generated on Thu Feb 25 2016 23:29:01 GMT+0100 (CET) | |
module.exports = function(config) { | |
config.set({ | |
// base path that will be used to resolve all patterns (eg. files, exclude) | |
basePath: '', | |
// frameworks to use | |
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter | |
frameworks: ['jasmine'], | |
// list of files / patterns to load in the browser | |
files: [ | |
'node_modules/angular/angular.js', | |
'*.js' | |
], | |
// list of files to exclude | |
exclude: [ | |
], | |
// preprocess matching files before serving them to the browser | |
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor | |
preprocessors: { | |
}, | |
// test results reporter to use | |
// possible values: 'dots', 'progress' | |
// available reporters: https://npmjs.org/browse/keyword/karma-reporter | |
reporters: ['progress'], | |
// web server port | |
port: 9876, | |
// enable / disable colors in the output (reporters and logs) | |
colors: true, | |
// level of logging | |
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG | |
logLevel: config.LOG_INFO, | |
// enable / disable watching file and executing tests whenever any file changes | |
autoWatch: true, | |
// start these browsers | |
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher | |
browsers: ['PhantomJS'], | |
// Continuous Integration mode | |
// if true, Karma captures browsers, runs the tests and exits | |
singleRun: false, | |
// Concurrency level | |
// how many browser should be started simultaneous | |
concurrency: Infinity | |
}) | |
} |
This file contains hidden or 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
function Interceptor() | |
{ | |
var headers; | |
this.request = function(request) { | |
if (! headers) { | |
headers = request.headers; | |
} | |
request = angular.copy(request); | |
request.headers = angular.copy(headers); | |
return request; | |
} | |
} |
This file contains hidden or 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
describe("Interceptor.request", function() { | |
var SUT; | |
beforeEach(function(){ | |
SUT = new Interceptor; | |
}); | |
it("should return the same header on subsequent requests", function(){ | |
var firstRequest = { | |
headers: { | |
foo: 'bar' | |
} | |
}; | |
var secondRequest = {}; | |
SUT.request(firstRequest); | |
var interceptedSecondRequest = SUT.request(secondRequest); | |
expect(interceptedSecondRequest.headers).toEqual(firstRequest.headers); | |
}); | |
it("should not modify the requests but copy them", function(){ | |
var request = {}; | |
var interceptedRequest = SUT.request(request); | |
expect(interceptedRequest).not.toBe(request); | |
}); | |
}); |
This file contains hidden or 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
{ | |
"dependencies": { | |
"angular": "^1.5.0", | |
"jasmine-core": "^2.4.1", | |
"karma": "^0.13.21", | |
"karma-jasmine": "^0.3.7", | |
"karma-phantomjs-launcher": "^1.0.0", | |
"phantomjs-prebuilt": "^2.1.4" | |
}, | |
"scripts": { | |
"test": "karma start" | |
} | |
} |
os.config(function($httpProvider){
$httpProvider
.interceptors.push(function($q, ipCookie) {
return {
'request': function(config) {
// same as above
},
'response': function(response) {
// same as above
}
}
});
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@salusinarduis i've added some boilerplate to let you run the tests and play with it
just clone the gist and run "npm install && npm test"