forked from balderdashy/sails
-
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
1 changed file
with
189 additions
and
0 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,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(); | ||
} | ||
); | ||
|
||
}); | ||
|
||
}); | ||
|
||
}); | ||
|
||
}); | ||
|
||
}); |