Skip to content

Instantly share code, notes, and snippets.

@stefanotorresi
Last active February 25, 2016 23:19
Show Gist options
  • Save stefanotorresi/2eee19691139d39fd16a to your computer and use it in GitHub Desktop.
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
// 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
})
}
function Interceptor()
{
var headers;
this.request = function(request) {
if (! headers) {
headers = request.headers;
}
request = angular.copy(request);
request.headers = angular.copy(headers);
return request;
}
}
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);
});
});
{
"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"
}
}
@mu-arch
Copy link

mu-arch commented Feb 25, 2016

Here's a function I wrote for setting headers:

`return {

request: function (package_headers) {
  package_headers.headers['access-token'] = access_token;
  package_headers.headers['token-type'] =  token_type;
  package_headers.headers['client'] =  client;
  package_headers.headers['expiry'] =  expiry;
  package_headers.headers['uid'] = uid;
  return package_headers;
}

};`

@stefanotorresi
Copy link
Author

@salusinarduis i think you should read https://docs.angularjs.org/api/ng/service/$http#interceptors first of all

@stefanotorresi
Copy link
Author

@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"

@mu-arch
Copy link

mu-arch commented Feb 25, 2016

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