supertest-session
Advanced tools
Comparing version 1.0.1 to 1.1.0
129
index.js
@@ -1,2 +0,3 @@ | ||
var cookie = require('cookie'), | ||
var assign = require('object-assign'), | ||
cookie = require('cookie'), | ||
methods = require('methods'), | ||
@@ -26,78 +27,96 @@ request = require('supertest'), | ||
function assignEnvs (envs) { | ||
Object.keys(envs).forEach(function(e) { | ||
process.env[e] = envs[e]; | ||
}); | ||
function Session (app, options) { | ||
if (!app) { | ||
throw new Error('Session requires an `app`'); | ||
} | ||
this.app = app; | ||
this.options = options || {}; | ||
if (this.options.helpers instanceof Object) { | ||
assign(this, this.options.helpers); | ||
} | ||
} | ||
var deprecatedAssignEnvs = util.deprecate(assignEnvs, | ||
'supertest-session env configuration is deprecated and will be removed in next version.'); | ||
Session.prototype._before = function (req) { | ||
if (this.cookies) { | ||
req.cookies = this.cookies.map(serializeCookie).join('; '); | ||
} | ||
module.exports = function (config) { | ||
if (this.options.before) { | ||
this.options.before.call(this, req); | ||
} | ||
}; | ||
if (!config) config = {}; | ||
// Extract cookies once request is complete | ||
Session.prototype._after = function (req, res) { | ||
if (this.options.after) { | ||
this.options.after.call(this, req, res); | ||
} | ||
function Session () { | ||
this.app = config.app; | ||
if (res.headers.hasOwnProperty('set-cookie')) { | ||
this.cookies = res.headers['set-cookie'].map(cookie.parse); | ||
} | ||
}; | ||
if (config.envs && (config.envs instanceof Object)) { | ||
deprecatedAssignEnvs(config.envs); | ||
} | ||
Session.prototype.destroy = function () { | ||
if (this.options.destroy) { | ||
this.options.destroy.call(this); | ||
} | ||
this.cookies = null; | ||
}; | ||
Session.prototype._before = function (req) { | ||
if (this.cookies) { | ||
req.cookies = this.cookies.map(serializeCookie).join('; '); | ||
} | ||
if (config.before) config.before.call(this, req); | ||
}; | ||
Session.prototype.request = function (meth, route) { | ||
var req = request(this.app)[meth](route); | ||
var sess = this; | ||
var _end = req.end.bind(req); | ||
// Extract cookies once request is complete | ||
Session.prototype._after = function (req, res) { | ||
if (config.after) config.after.call(this, req, res); | ||
if (res.headers.hasOwnProperty('set-cookie')) { | ||
this.cookies = res.headers['set-cookie'].map(cookie.parse); | ||
} | ||
this._before(req); | ||
req.end = function (callback) { | ||
return _end(function (err, res) { | ||
if (err === null) sess._after(req, res); | ||
return callback(err, res); | ||
}); | ||
}; | ||
Session.prototype.destroy = function () { | ||
if (config.destroy) config.destroy.call(this); | ||
this.cookies = null; | ||
return req; | ||
}; | ||
methods.forEach(function (m) { | ||
Session.prototype[m] = function () { | ||
var args = [].slice.call(arguments); | ||
return this.request.apply(this, [m].concat(args)); | ||
}; | ||
}); | ||
Session.prototype.request = function (meth, route) { | ||
var req = request(this.app)[meth](route); | ||
var sess = this; | ||
var _end = req.end.bind(req); | ||
Session.prototype.del = util.deprecate(Session.prototype.delete, | ||
'supertest-session: Session.del is deprecated; please use Session.delete'); | ||
this._before(req); | ||
function legacySession (config) { | ||
req.end = function (callback) { | ||
return _end(function (err, res) { | ||
if (err === null) sess._after(req, res); | ||
return callback(err, res); | ||
}); | ||
}; | ||
if (!config) config = {}; | ||
return req; | ||
}; | ||
// Bind session to `config` | ||
function LegacySession () { | ||
Session.call(this, config.app, config); | ||
} | ||
methods.forEach(function (m) { | ||
Session.prototype[m] = function () { | ||
var args = [].slice.call(arguments); | ||
return this.request.apply(this, [m].concat(args)); | ||
}; | ||
}); | ||
util.inherits(LegacySession, Session); | ||
assign(LegacySession.prototype, {}, config.helpers); | ||
Session.prototype.del = util.deprecate(Session.prototype.delete, | ||
'Session.del is deprecated; please use Session.delete'); | ||
return LegacySession; | ||
} | ||
if (config.helpers instanceof Object) { | ||
Object.keys(config.helpers).forEach(function (key) { | ||
Session.prototype[key] = config.helpers[key]; | ||
}); | ||
var deprecatedLegacySession = util.deprecate(legacySession, | ||
'supertest-session: module configuration will be removed in next version.'); | ||
module.exports = function (app, options) { | ||
if (!(app.listen instanceof Function)) { | ||
return deprecatedLegacySession(app); | ||
} | ||
return Session; | ||
return new Session(app, options); | ||
}; | ||
{ | ||
"name": "supertest-session", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Cookie-based session persistence for Supertest", | ||
@@ -35,2 +35,3 @@ "main": "index.js", | ||
"methods": "^1.1.1", | ||
"object-assign": "^4.0.1", | ||
"supertest": "^1.0.1" | ||
@@ -37,0 +38,0 @@ }, |
@@ -24,27 +24,15 @@ # Supertest sessions | ||
var Session = require('supertest-session')({ | ||
app: require('../../path/to/app') | ||
}); | ||
var session = require('supertest-session'); | ||
var myApp = require('../../path/to/app'); | ||
You can set environmental variables by including an `envs` object: | ||
var testSession = null; | ||
var Session = require('supertest-session')({ | ||
app: require('../../path/to/app'), | ||
envs: { NODE_ENV: 'development' } | ||
beforeEach(function () { | ||
testSession = session(myApp); | ||
}); | ||
Set up a session: | ||
before(function () { | ||
this.sess = new Session(); | ||
}); | ||
after(function () { | ||
this.sess.destroy(); | ||
}); | ||
And set some expectations: | ||
it('should fail accessing a restricted page', function (done) { | ||
this.sess.get('/restricted') | ||
testSession.get('/restricted') | ||
.expect(401) | ||
@@ -55,3 +43,3 @@ .end(done) | ||
it('should sign in', function (done) { | ||
this.sess.post('/signin') | ||
testSession.post('/signin') | ||
.send({ username: 'foo', password: 'password' }) | ||
@@ -63,3 +51,3 @@ .expect(200) | ||
it('should get a restricted page', function (done) { | ||
this.sess.get('/restricted') | ||
testSession.get('/restricted') | ||
.expect(200) | ||
@@ -74,3 +62,3 @@ .end(done) | ||
it('should set session details correctly', function (done) { | ||
var sessionCookie = _.find(sess.cookies, function (cookie) { | ||
var sessionCookie = _.find(testSession.cookies, function (cookie) { | ||
return _.has(cookie, 'connect.sid'); | ||
@@ -91,4 +79,3 @@ }); | ||
var Session = require('supertest-session')({ | ||
app: require('../../path/to/app'), | ||
var testSession = session(myApp, { | ||
before: function (req) { | ||
@@ -95,0 +82,0 @@ req.set('authorization', 'Basic aGVsbG86d29ybGQK'); |
@@ -32,8 +32,3 @@ var connect = require('connect'), | ||
if (req.url === '/env') { | ||
res.end(JSON.stringify(process.env)); | ||
} | ||
else { | ||
res.end([req.method, req.session.type, req.session.count].join(',')); | ||
} | ||
res.end([req.method, req.session.type, req.session.count].join(',')); | ||
} | ||
@@ -40,0 +35,0 @@ |
@@ -5,13 +5,8 @@ var assert = require('assert'), | ||
describe('supertest session', function () { | ||
describe('supertest-session', function () { | ||
var sess = null; | ||
var Session = session({ | ||
app: app, | ||
envs: { NODE_ENV: 'development'} | ||
}); | ||
beforeEach(function (done) { | ||
sess = new Session(); | ||
sess = session(app); | ||
sess.request('get', '/') | ||
@@ -30,12 +25,2 @@ .expect(200) | ||
it('should set enviromental variables', function(done) { | ||
sess.request('get', '/env') | ||
.expect(200) | ||
.end(function(err, res) { | ||
assert.equal(err, undefined); | ||
assert.equal(JSON.parse(res.text).NODE_ENV, 'development'); | ||
done(); | ||
}); | ||
}); | ||
it('should destroy session', function (done) { | ||
@@ -73,11 +58,9 @@ sess.destroy(); | ||
var Session = session({ | ||
app: app, | ||
before: function (req) { | ||
req.set('authorization', 'bearer TEST_SESSION_TOKEN'); | ||
} | ||
}); | ||
beforeEach(function (done) { | ||
sess = session(app, { | ||
before: function (req) { | ||
req.set('authorization', 'bearer TEST_SESSION_TOKEN'); | ||
} | ||
}); | ||
beforeEach(function (done) { | ||
sess = new Session(); | ||
sess.request('get', '/token') | ||
@@ -84,0 +67,0 @@ .expect(200) |
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 2 instances in 1 package
12594
16
252
0
4
85
+ Addedobject-assign@^4.0.1
+ Addedobject-assign@4.1.1(transitive)