Skip to content

Commit

Permalink
Add tests for session middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
sgress454 committed Feb 11, 2016
1 parent 492a772 commit 94e5589
Showing 1 changed file with 189 additions and 0 deletions.
189 changes: 189 additions & 0 deletions test/integration/middleware.session.test.js
Original file line number Diff line number Diff line change
@@ -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();
}
);

});

});

});

});

});

0 comments on commit 94e5589

Please sign in to comment.