Permalink
Browse files

Parse ServiceInterfaceClientSessionId from http header

  • Loading branch information...
1 parent 220bc83 commit cd655aefae7325651aff910367fea66031aab38d jden committed Jul 28, 2016
Showing with 33 additions and 14 deletions.
  1. +1 −1 src/server/service_interface.js
  2. +32 −13 test/service_interface.test.js
@@ -135,7 +135,7 @@ function allowedOp (op) {
}
function ServiceInterfaceClientSession (req, res) {
- this.id = req.id
+ this.id = req.headers['x-session-id'] || req.id
this._req = req
this._res = res
}
@@ -8,6 +8,8 @@ var literalStream = require('literal-stream')
var _ = require('lodash')
var uuid = require('uuid')
+var EMPTY_REQ = {headers: {}}
+
describe('ServiceInterface', function () {
var ServiceInterface = require('../src/server/service_interface')
var serviceInterface
@@ -18,7 +20,8 @@ describe('ServiceInterface', function () {
function getReq (o) {
return _.extend({
method: 'GET',
- url: '/radar/service'
+ url: '/radar/service',
+ headers: {}
}, o)
}
@@ -188,7 +191,7 @@ describe('ServiceInterface', function () {
done()
})
- var req = {}
+ var req = EMPTY_REQ
var res = {}
serviceInterface._processIncomingMessage(msg, req, res)
@@ -216,7 +219,7 @@ describe('ServiceInterface', function () {
done()
})
- var req = {}
+ var req = EMPTY_REQ
var res = {}
serviceInterface._processIncomingMessage(msg, req, res)
@@ -240,14 +243,30 @@ describe('ServiceInterface', function () {
done()
})
- var req = {}
+ var req = EMPTY_REQ
var res = {}
serviceInterface._processIncomingMessage(msg, req, res)
})
})
+ it('uses the x-session-id header for the clientSession.id if set', function (done) {
+ var msg = {
+ op: 'set',
+ to: 'status:/test/result',
+ value: 'pending'
+ }
+
+ serviceInterface.on('request', function (clientSession, message) {
+ expect(clientSession.id).to.equal('qwerty')
+ done()
+ })
- it('uses the req.id for the clientSession.id', function (done) {
+ var req = {id: 'asdfg', headers: {'x-session-id': 'qwerty'}}
+ var res = {}
+
+ serviceInterface._processIncomingMessage(msg, req, res)
+ })
+ it('uses the req.id for the clientSession.id if header not set', function (done) {
var msg = {
op: 'set',
to: 'status:/test/result',
@@ -259,7 +278,7 @@ describe('ServiceInterface', function () {
done()
})
- var req = {id: 'asdfg'}
+ var req = {id: 'asdfg', headers: {}}
var res = {}
serviceInterface._processIncomingMessage(msg, req, res)
@@ -276,7 +295,7 @@ describe('ServiceInterface', function () {
done()
})
- var req = {id: 'asdfg'}
+ var req = {id: 'asdfg', headers: {}}
var res = {}
serviceInterface._processIncomingMessage(msg, req, res)
@@ -294,7 +313,7 @@ describe('ServiceInterface', function () {
done()
})
- var req = {}
+ var req = EMPTY_REQ
var res = {}
serviceInterface._processIncomingMessage(msg, req, res)
@@ -307,7 +326,7 @@ describe('ServiceInterface', function () {
clientSession.send({message: 'contents'})
})
- var req = {}
+ var req = EMPTY_REQ
var res = {
write: sinon.spy(),
end: function () {
@@ -327,7 +346,7 @@ describe('ServiceInterface', function () {
clientSession.send({op: 'err', value: 'invalid'})
})
- var req = {}
+ var req = EMPTY_REQ
var res = {
statusCode: 200,
write: sinon.spy(),
@@ -349,7 +368,7 @@ describe('ServiceInterface', function () {
})
it('auth errors are statusCode 403 by default', function (done) {
- var req = {}
+ var req = EMPTY_REQ
var res = {
statusCode: 200,
write: sinon.spy(),
@@ -362,7 +381,7 @@ describe('ServiceInterface', function () {
serviceInterface._processIncomingMessage(msg, req, res)
})
it('if res.statusCode is already 401 or 403, existing value is used', function (done) {
- var req = {}
+ var req = EMPTY_REQ
var res = {
statusCode: 401,
write: sinon.spy(),
@@ -385,7 +404,7 @@ describe('ServiceInterface', function () {
clientSession.send({message: '2'})
})
- var req = {}
+ var req = EMPTY_REQ
var res = {
write: sinon.spy(),
end: function () {

0 comments on commit cd655ae

Please sign in to comment.