From 94e5589da7b5d3aad13a74fe94349924e35f0244 Mon Sep 17 00:00:00 2001 From: Scott Gress Date: Wed, 10 Feb 2016 22:30:06 -0600 Subject: [PATCH] Add tests for session middleware --- test/integration/middleware.session.test.js | 189 ++++++++++++++++++++ 1 file changed, 189 insertions(+) create mode 100644 test/integration/middleware.session.test.js diff --git a/test/integration/middleware.session.test.js b/test/integration/middleware.session.test.js new file mode 100644 index 000000000..280b850f2 --- /dev/null +++ b/test/integration/middleware.session.test.js @@ -0,0 +1,189 @@ +var _ = require('lodash'); +var request = require('request'); +var Sails = require('../../lib').Sails; +var assert = require('assert'); +var cookie = require('cookie'); + +describe('middleware :: ', function() { + + describe('session :: ', function() { + + describe('http requests :: ', function() { + + describe('with a valid session secret', function() { + + var sid; + + // Lift a Sails instance in production mode + var app = Sails(); + before(function (done){ + app.lift({ + globals: false, + port: 1535, + environment: 'development', + log: {level: 'silent'}, + session: { + secret: 'abc123' + }, + routes: { + '/test': function(req, res) { + var count = req.session.count || 1; + req.session.count = count + 1; + res.send("Count is " + count); + } + } + }, done); + }); + + after(function(done) { + return app.lower(function(){setTimeout(done, 100);}); + }); + + it('a server responses should supply a cookie with a session ID', function(done) { + + request( + { + method: 'GET', + uri: 'http://localhost:1535/test', + }, + function(err, response, body) { + assert.equal(body, 'Count is 1'); + assert(response.headers['set-cookie']); + var cookies = require('cookie').parse(response.headers['set-cookie'][0]); + assert(cookies['sails.sid']); + sid = cookies['sails.sid']; + return done(); + } + ); + }); + + it('a subsequent request using that session ID in a "Cookie" header should use the same session', function(done) { + + request( + { + method: 'GET', + uri: 'http://localhost:1535/test', + headers: { + Cookie: "sails.sid=" + sid + } + }, + function(err, response, body) { + assert.equal(body, 'Count is 2'); + return done(); + } + ); + + }); + + }); + + describe('with an invalid session secret', function() { + + var app = Sails(); + + it('should throw an error when lifting Sails', function(done) { + + app.lift({ + globals: false, + port: 1535, + environment: 'development', + log: {level: 'silent'}, + session: { + secret: 12345 + }, + routes: { + '/test': function(req, res) { + res.json({ + cookies: req.cookies, + signedCookies: req.signedCookies + }); + } + } + }, function(err) { + if (!err) {return done(new Error('Should have thrown an error!'));} + return done(); + }); + + }); + + after(function(done) { + return app.lower(function(){setTimeout(done, 100);}); + }); + + }); + + }); + + describe('virtual requests :: ', function() { + + describe('with a valid session secret', function() { + + var sid; + + // Lift a Sails instance in production mode + var app = Sails(); + before(function (done){ + app.load({ + globals: false, + environment: 'development', + log: {level: 'silent'}, + session: { + secret: 'abc123' + }, + routes: { + '/test': function(req, res) { + var count = req.session.count || 1; + req.session.count = count + 1; + res.send("Count is " + count); + } + } + }, done); + }); + + after(function(done) { + return app.lower(function(){setTimeout(done, 100);}); + }); + + it('a server responses should supply a cookie with a session ID', function(done) { + + app.request( + { + method: 'GET', + url: '/test', + }, + function(err, response, body) { + assert.equal(body, 'Count is 1'); + assert(response.headers['set-cookie']); + var cookies = require('cookie').parse(response.headers['set-cookie'][0]); + assert(cookies['sails.sid']); + sid = cookies['sails.sid']; + return done(); + } + ); + }); + + it('a subsequent request using that session ID in a "Cookie" header should use the same session', function(done) { + + app.request( + { + method: 'GET', + url: '/test', + headers: { + Cookie: "sails.sid=" + sid + } + }, + function(err, response, body) { + assert.equal(body, 'Count is 2'); + return done(); + } + ); + + }); + + }); + + }); + + }); + +});