Skip to content

Commit

Permalink
Add io.sails.headers option to supply global headers
Browse files Browse the repository at this point in the history
  • Loading branch information
sgress454 committed Jan 14, 2016
1 parent 1157b2c commit 6376e04
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 4 deletions.
17 changes: 14 additions & 3 deletions sails.io.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@
self.url = self.url||io.sails.url;
self.transports = self.transports || io.sails.transports;
self.query = self.query || io.sails.query;
// TODO: support `io.sails.headers` as a way to set common headers for all outgoing socket requests
self.headers = self.query || io.sails.headers;

// Ensure URL has no trailing slash
self.url = self.url ? self.url.replace(/(\/)$/, '') : undefined;
Expand Down Expand Up @@ -870,15 +870,26 @@
if (cb && typeof cb !== 'function') {
throw new Error('Invalid callback function!\n' + usage);
}


// Default headers to an empty object
options.headers = options.headers || {};

// Merge global headers in
if (this.headers && 'object' == typeof this.headers) {
for (var header in this.headers) {
if (!options.hasOwnProperty(header)) {
options.headers[header] = this.headers[header];
}
}
}

// Build a simulated request object
// (and sanitize/marshal options along the way)
var requestCtx = {

method: options.method.toLowerCase() || 'get',

headers: options.headers || {},
headers: options.headers,

data: options.params || options.data || {},

Expand Down
58 changes: 58 additions & 0 deletions test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,20 @@ var EXPECTED_RESPONSES = {
'get /someError': {
body: { blah: 'blah' },
statusCode: 501
},
'get /headers': {
'req.headers.x-test-header-one': 'foo',
'req.headers.x-test-header-two': 'bar',
},
'get /headersOverride': {
'req.headers.x-test-header-one': 'baz',
'req.headers.x-test-header-two': 'bar',
},
'get /headersRemove': {
'req.headers.x-test-header-two': 'bar',
}


};
var setupRoutes = _setupRoutes(EXPECTED_RESPONSES);
var assertResponse = _assertResponse(EXPECTED_RESPONSES);
Expand Down Expand Up @@ -75,6 +88,51 @@ describe('io.socket', function () {
});


after(lifecycle.teardown);

});

describe('Using headers option', function() {
before(function(done) {
lifecycle.setup({
headers: {
'x-test-header-one': 'foo',
'x-test-header-two': 'bar',
}
}, done);
});
before(setupRoutes);

it('should connect automatically', function (cb) {
io.socket.on('connect', cb);
});

describe('once connected, socket', function () {

it('should be able to send a GET request and receive the expected response, including custom headers', function (cb) {
io.socket.get('/headers', function (body, jwr) {
assertResponse('get /headers', arguments);
return cb();
});
});

it('should be able to override the global headers on a per-request basis', function (cb) {
io.socket.request({method: 'get', url: '/headersOverride', headers: {'x-test-header-one': 'baz'}}, function (body, jwr) {
assertResponse('get /headersOverride', arguments);
return cb();
});
});

it('should be able to remove the global headers on a per-request basis', function (cb) {
io.socket.request({method: 'get', url: '/headersRemove', headers: {'x-test-header-one': undefined}}, function (body, jwr) {
assertResponse('get /headersRemove', arguments);
return cb();
});
});

});


after(lifecycle.teardown);

});
Expand Down
4 changes: 4 additions & 0 deletions test/helpers/lifecycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ module.exports = {
io.sails.query = opts.query;
}

if (typeof (opts.headers) != 'undefined') {
io.sails.headers = opts.headers;
}

// Globalize sails app as `server`
global.server = app;

Expand Down
2 changes: 1 addition & 1 deletion test/helpers/setupRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var _ = require('lodash');

function _dotToObject(obj, path) {
return path.split('.').reduce(function objectIndex(obj, i) {
return obj[i]
return obj[i];
}, obj);
}

Expand Down

0 comments on commit 6376e04

Please sign in to comment.