Added test for duration, failing appropriately

This commit is contained in:
Ben Adida 2011-12-30 10:22:30 -08:00
Родитель 758114240c
Коммит 7512033304
3 изменённых файлов: 78 добавлений и 7 удалений

Просмотреть файл

@ -17,10 +17,12 @@ API
var cookieSessions = require("cookie-sessions");
app.use(cookieSessions({
cookieName: 'session',
secret: 'blargadeeblargblarg',
// true session duration: it will expire after
// duration (in ms) from
cookieName: 'session_state', // defaults to session_state
secret: 'blargadeeblargblarg', // MUST be set
// true session duration:
// will expire after duration (ms)
// from last session.reset() or
// initial cookieing.
duration: 24 * 60 * 60 * 1000, // defaults to 1 day
cookie: {
path: '/api',

Просмотреть файл

@ -197,12 +197,14 @@ function deriveKey(master, type) {
var cookieSession = function(opts) {
if (!opts)
throw "no options provided, some are required";
throw "no options provided, some are required"; // XXX rename opts?
if (!opts.secret)
throw "cannot set up sessions without a secret";
opts.cookieName = opts.cookieName || "session";
// defaults
opts.cookieName = opts.cookieName || "session_state";
opts.duration = opts.duration || 24*60*60*1000;
// set up cookie defaults
opts.cookie = opts.cookie || {};

Просмотреть файл

@ -207,4 +207,71 @@ suite.addBatch({
}
});
suite.addBatch({
"short duration" : {
topic: function() {
var self = this;
// simple app
var app = express.createServer();
app.use(cookieSessions({
cookieName: 'session',
secret: 'yo',
duration: 500 // 0.5 seconds
}));
app.get("/foo", function(req, res) {
req.session.foo = 'foobar';
res.send("foo");
});
return app;
},
"querying within the duration time": {
topic: function(app) {
var self = this;
app.get("/bar", function(req, res) {
self.callback(null, req);
res.send("bar");
});
var browser = tobi.createBrowser(app);
browser.get("/foo", function(res, $) {
setTimeout(function () {
browser.get("/bar", function(res, $) {
});
}, 200);
});
},
"session still has state": function(err, req) {
assert.equal(req.session.foo, 'foobar');
}
},
"querying outside the duration time": {
topic: function(app) {
var self = this;
app.get("/bar2", function(req, res) {
self.callback(null, req);
res.send("bar2");
});
var browser = tobi.createBrowser(app);
browser.get("/foo", function(res, $) {
setTimeout(function () {
browser.get("/bar2", function(res, $) {
});
}, 1000);
});
},
"session no longer has state": function(err, req) {
assert.isUndefined(req.session.foo);
}
}
}
});
suite.export(module);