forked from eiriklv/session.socket.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
159 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.DS_Store | ||
example/node_modules/ | ||
node_modules/ | ||
*.log |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
language: javascript | ||
before_install: | ||
- npm install | ||
- npm update | ||
script: make spec |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
spec: | ||
@./node_modules/mocha/bin/mocha \ | ||
--reporter spec \ | ||
$(p) \ | ||
spec/*.spec.js | ||
|
||
.PHONY: spec |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
var count = 1e7; | ||
|
||
var SessionSocket = require('../session.socket.io'); | ||
var EventEmitter = require('events').EventEmitter; | ||
|
||
// stubs | ||
var io = { sockets: new EventEmitter() }; | ||
var sessionStore = { | ||
load: function(cookie, fn) { | ||
fn(undefined, {test: 'hi'}) | ||
} | ||
} | ||
var cookieParser = function(handshake, idk, fn) { | ||
fn(); | ||
} | ||
var stubsocket = { | ||
handshake: { | ||
secureCookies: '123' | ||
} | ||
} | ||
// | ||
var s = new SessionSocket(io, sessionStore, cookieParser, 'key'); | ||
var type, startTime, d = 0; | ||
|
||
function run() { | ||
startTime = process.hrtime(); | ||
for (var i = 0; i < count; i++) { | ||
io.sockets.emit('connection', stubsocket); | ||
} | ||
} | ||
|
||
function done() { | ||
var elapsed = process.hrtime(startTime); | ||
var time = elapsed[0] + elapsed[1] / 1e9; | ||
var opcount = count / time; | ||
console.log('op/s: ', Math.round(opcount), ' | time: ', time); | ||
} | ||
|
||
if (process.argv[2] === 'session') { | ||
type = s; | ||
} else { | ||
type = io.sockets; | ||
} | ||
|
||
type.on('connection', function() { | ||
d++; | ||
if (d === count) done(); | ||
}) | ||
|
||
run(); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,9 +29,5 @@ | |
}, | ||
"homepage": "https://github.com/functioncallback/session.socket.io", | ||
"_id": "[email protected]", | ||
"dist": { | ||
"shasum": "fb3464a9beec66109bd47929c0a615c2797eb29e" | ||
}, | ||
"_from": "[email protected]", | ||
"_resolved": "https://registry.npmjs.org/session.socket.io/-/session.socket.io-0.1.6.tgz" | ||
"_from": "[email protected]" | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
var path = require('path') | ||
, expect = require('expect.js') | ||
, SessionSockets = require(path.resolve('session.socket.io')) | ||
, jonnySocket = stub({ foo: 'bar' }); | ||
|
||
describe('SessionSockets', function () { | ||
this.timeout(1); | ||
|
||
beforeEach(function () { | ||
this.sessionSockets = new SessionSockets(io(), sessionStore(), cookieParser); | ||
}); | ||
|
||
it('gets the corresponding session for a given socket client', function (done) { | ||
this.sessionSockets.getSession(jonnySocket, function (err, session) { | ||
expect(err).to.be(null); | ||
expect(session.foo).to.equal('bar'); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('provides session in connection callbacks on the global namespace', function (done) { | ||
this.sessionSockets.on('connection', function (err, socket, session) { | ||
expect(err).to.be(null); | ||
expect(session.foo).to.equal('bar'); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('provides session in connection callbacks on a specific namespace', function (done) { | ||
this.sessionSockets.of('/foobar').on('connection', function (err, socket, session) { | ||
expect(err).to.be(null); | ||
expect(session.foo).to.equal('bar'); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('gives a SessionStoreError upon invalid handshakes', function (done) { | ||
this.sessionSockets.getSession({ handshake: 'invalid' }, function (err, session) { | ||
expect(err).to.be.a(SessionStoreError); | ||
expect(session).to.be(null); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('gives a CookieParserError upon invalid lookups', function (done) { | ||
this.sessionSockets.getSession({ invalid: 'socket' }, function (err, session) { | ||
expect(err).to.be.a(CookieParserError); | ||
expect(session).to.be(null); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
function stub(attributes) { | ||
attributes.handshake = { signedCookies: { 'connect.sid': 42 }}; | ||
return attributes; | ||
} | ||
|
||
function ns() { | ||
return { | ||
on: function(event, callback) { | ||
if (event === 'connection') callback(jonnySocket); | ||
} | ||
}; | ||
} | ||
|
||
function io() { | ||
return { | ||
of: function() { return ns() }, | ||
sockets: ns() | ||
}; | ||
} | ||
|
||
function sessionStore() { | ||
return { | ||
load: function(cookie, callback) { | ||
if (cookie) callback(null, jonnySocket); | ||
else callback(new SessionStoreError(), null); | ||
} | ||
}; | ||
} | ||
|
||
function cookieParser(handshake, options, callback) { | ||
if (handshake) callback(); | ||
else callback(new CookieParserError()); | ||
} | ||
|
||
function SessionStoreError() {} | ||
function CookieParserError() {} |