Permalink
Browse files

tested session more thoroughly and fixed bugs

  • Loading branch information...
1 parent f2a01e5 commit 9a3a8800e71894b1a720c404bbc3a9fe720a36ce @coolaj86 committed Feb 10, 2012
Showing with 29 additions and 22 deletions.
  1. +10 −17 lib/lib/connect-cors-session.js
  2. +1 −1 lib/package.json
  3. +18 −4 test.js
@@ -5,6 +5,8 @@
var defaultSessionKey = 'userSession'
, defaultSessionAppKey = 'appSession'
, defaultSessionHeader = 'X-User-Session'
+ , sessionHeader = defaultSessionHeader
+ , lcSessionHeader = sessionHeader.toLowerCase()
, defaultSessionAppHeader = 'X-App-Session'
;
@@ -18,9 +20,9 @@
var http = require('http')
, resProto = http.ServerResponse.prototype
//, sendJsonProto = resProto.json
- , secret = options.secret || (Math.random() * new Date().valueOf()).toString('36').split('').sort(random).join('')
+ , secret = options.secret || (Math.random() * Date.now()).toString('36').split('').sort(random).join('')
, sessionKey = options.sessionKey || defaultSessionKey
- , lSessionKey = sessionKey.toLowerCase()
+ , lcSessionKey = sessionKey.toLowerCase()
, purgeInterval = options.purgeInterval || 10 * 60 * 1000
, maxAge = options.maxAge || 60 * 60 * 1000
, db = {}
@@ -36,13 +38,13 @@
// TODO rolling fingerprint that is different for each request
function createSessionId() {
return (secret +
- new Date().valueOf().toString('36') +
+ Date.now().toString('36') +
(Math.random() * 19860616).toString('36')
).split('').sort(random).join('').replace(/[\W]/g, '').substr(0, 32);
}
function purge() {
- var now = new Date().valueOf()
+ var now = Date.now()
, val
;
@@ -55,14 +57,12 @@
}
function session(req, res, next) {
- //req.activateSession = function(cb) {
var sessionId
- , timestamp = new Date().valueOf()
- , virgin
+ , timestamp = Date.now()
;
// TODO add Cookie support
- if (sessionId = req.headers[lSessionKey]) {
+ if (sessionId = req.headers[lcSessionHeader]) {
req.sessionId = sessionId;
} else if (sessionId = req.body && req.body[sessionKey]) {
req.sessionId = sessionId;
@@ -74,30 +74,23 @@
if (!(req.session = db[sessionId])) {
req.session = db[sessionId] = {};
- }
-
- if (virgin) {
req.session.virgin = true;
req.session.createdAt = timestamp;
- //req.session.sessionId;
} else {
delete req.session.virgin;
}
// TODO else if (req.expireSession) { delete a replaced session }
res.sessionId = req.sessionId;
req.session.touchedAt = timestamp;
- res.setHeader(defaultSessionHeader, sessionId);
-
- // cb();
- //};
+ res.setHeader(sessionHeader, sessionId);
next();
}
setInterval(purge, purgeInterval);
- session.headers = [defaultSessionHeader];
+ session.headers = [lcSessionHeader];
return session;
}
View
@@ -2,7 +2,7 @@
"author": "AJ ONeal <coolaj86@gmail.com> (http://coolaj86.info)",
"name": "steve",
"description": "JSON's best friend (a CORS/XHR2 application platform)",
- "version": "0.5.6",
+ "version": "0.5.7",
"repository": {
"type": "git",
"url": "git://github.com/coolaj86/steve.git"
View
22 test.js
@@ -9,17 +9,17 @@
, hostname = 'localhost'
, port = '6767'
, pathname = '/path/to/resource'
- , search = 'search=query&foo=bar'
+ , search = 'search=query&foo=bar&userSession=alphabetSoup'
, url = pathname + '?' + search
, fullurl = protocol + '//' + hostname + ':' + port + pathname + '?' + search
, query = {
search: 'query'
, foo: 'bar'
+ , userSession: 'alphabetSoup'
}
;
server = connect.createServer(function (req, res, next) {
- console.log(req.url);
res.json({
"url": req.url
//, "adddress": req.socket.address()
@@ -28,18 +28,32 @@
, "query": req.query
//, "search": req.search
, "path": req.path
+ , "headers": req.headers
});
});
server.listen(port, function () {
- request.get(fullurl).when(function (err, ahr, data) {
+ var headers
+ ;
+
+ headers = {
+ "X-User-Session": "headerSession"
+ , "User-Agent": "FooBarUrl"
+ };
+
+ request.get(fullurl, null, { headers: headers }).when(function (err, ahr, resp) {
+ var data
+ ;
+
server.close();
if (err) {
console.error(err);
return;
}
- data = data.result;
+ data = resp.result;
+ assert.strictEqual("headersession", resp.userSession, "sessions aren't a lowercase match");
+ assert.strictEqual("foobarurl", data.headers['user-agent'], 'user-agent is a lowercase match');
assert.strictEqual(url, data.url, "urls don't match");
//assert.strictEqual(protocol, data.protocol, "protocol don't match");
//assert.strictEqual(hostname, data.hostname, "hostnames don't match");

0 comments on commit 9a3a880

Please sign in to comment.