Skip to content

Commit 42007ef

Browse files
committed
tests: add helpers to validate set-cookie attributes
1 parent fe324f2 commit 42007ef

1 file changed

Lines changed: 44 additions & 64 deletions

File tree

test/session.js

Lines changed: 44 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -710,8 +710,7 @@ describe('session()', function(){
710710
request(this.server)
711711
.get('/')
712712
.set('X-Forwarded-Proto', 'https')
713-
.expect(shouldSetCookie('connect.sid'))
714-
.expect(shouldSetSecureCookie('connect.sid'))
713+
.expect(shouldSetCookieWithAttribute('connect.sid', 'Secure'))
715714
.expect(200, done)
716715
})
717716
})
@@ -725,8 +724,7 @@ describe('session()', function(){
725724
request(this.server)
726725
.get('/')
727726
.set('X-Forwarded-Proto', 'https')
728-
.expect(shouldSetCookie('connect.sid'))
729-
.expect(shouldNotSetSecureCookie('connect.sid'))
727+
.expect(shouldSetCookieWithoutAttribute('connect.sid', 'Secure'))
730728
.expect(200, done)
731729
})
732730
})
@@ -743,17 +741,15 @@ describe('session()', function(){
743741
request(this.app)
744742
.get('/')
745743
.set('X-Secure', 'true')
746-
.expect(shouldSetCookie('connect.sid'))
747-
.expect(shouldSetSecureCookie('connect.sid'))
744+
.expect(shouldSetCookieWithAttribute('connect.sid', 'Secure'))
748745
.expect(200, 'true', done)
749746
})
750747

751748
it('should not set secure if req.secure = false', function (done) {
752749
request(this.app)
753750
.get('/')
754751
.set('X-Secure', 'false')
755-
.expect(shouldSetCookie('connect.sid'))
756-
.expect(shouldNotSetSecureCookie('connect.sid'))
752+
.expect(shouldSetCookieWithoutAttribute('connect.sid', 'Secure'))
757753
.expect(200, 'false', done)
758754
})
759755
})
@@ -1681,24 +1677,16 @@ describe('session()', function(){
16811677
request(server)
16821678
.get('/')
16831679
.set('X-Forwarded-Proto', 'https')
1684-
.expect(200, function(err, res){
1685-
if (err) return done(err);
1686-
var val = cookie(res);
1687-
assert.equal(val.indexOf('HttpOnly'), -1, 'should not be HttpOnly cookie')
1688-
assert.notEqual(val.indexOf('Secure'), -1, 'should be Secure cookie')
1689-
done();
1690-
});
1680+
.expect(shouldSetCookieWithoutAttribute('connect.sid', 'HttpOnly'))
1681+
.expect(shouldSetCookieWithAttribute('connect.sid', 'Secure'))
1682+
.expect(200, done)
16911683
})
16921684

16931685
it('should default to a browser-session length cookie', function(done){
16941686
request(createServer({ cookie: { path: '/admin' } }))
16951687
.get('/admin')
1696-
.expect(200, function(err, res){
1697-
if (err) return done(err);
1698-
var val = cookie(res);
1699-
assert.equal(val.indexOf('Expires'), -1, 'should be not have cookie Expires')
1700-
done();
1701-
});
1688+
.expect(shouldSetCookieWithoutAttribute('connect.sid', 'Expires'))
1689+
.expect(200, done)
17021690
})
17031691

17041692
it('should Set-Cookie only once for browser-session cookies', function(done){
@@ -1725,15 +1713,11 @@ describe('session()', function(){
17251713

17261714
request(server)
17271715
.get('/admin')
1728-
.expect(200, function(err, res){
1729-
if (err) return done(err);
1730-
var val = cookie(res);
1731-
assert.equal(val.indexOf('HttpOnly'), -1, 'should not be HttpOnly cookie')
1732-
assert.equal(val.indexOf('Secure'), -1, 'should not be Secure cookie')
1733-
assert.notEqual(val.indexOf('Path=/admin'), -1, 'should have cookie path /admin')
1734-
assert.notEqual(val.indexOf('Expires'), -1, 'should have cookie Expires')
1735-
done();
1736-
});
1716+
.expect(shouldSetCookieWithAttribute('connect.sid', 'Expires'))
1717+
.expect(shouldSetCookieWithoutAttribute('connect.sid', 'HttpOnly'))
1718+
.expect(shouldSetCookieWithAttributeAndValue('connect.sid', 'Path', '/admin'))
1719+
.expect(shouldSetCookieWithoutAttribute('connect.sid', 'Secure'))
1720+
.expect(200, done)
17371721
})
17381722

17391723
it('should preserve cookies set before writeHead is called', function(done){
@@ -1917,11 +1901,8 @@ describe('session()', function(){
19171901

19181902
request(server)
19191903
.get('/')
1920-
.end(function(err, res){
1921-
if (err) return done(err)
1922-
assert.equal(expires(res), 'Thu, 01 Jan 1970 00:00:00 GMT')
1923-
done();
1924-
});
1904+
.expect(shouldSetCookieWithAttributeAndValue('connect.sid', 'Expires', 'Thu, 01 Jan 1970 00:00:00 GMT'))
1905+
.expect(200, done)
19251906
})
19261907
})
19271908

@@ -1934,12 +1915,8 @@ describe('session()', function(){
19341915

19351916
request(server)
19361917
.get('/')
1937-
.expect(200, function(err, res){
1938-
if (err) return done(err);
1939-
var val = cookie(res);
1940-
assert.equal(val.indexOf('Expires'), -1, 'should be not have cookie Expires')
1941-
done();
1942-
});
1918+
.expect(shouldSetCookieWithoutAttribute('connect.sid', 'Expires'))
1919+
.expect(200, done)
19431920
})
19441921

19451922
it('should not reset cookie', function (done) {
@@ -1950,18 +1927,14 @@ describe('session()', function(){
19501927

19511928
request(server)
19521929
.get('/')
1930+
.expect(shouldSetCookieWithoutAttribute('connect.sid', 'Expires'))
19531931
.expect(200, function (err, res) {
19541932
if (err) return done(err);
1955-
var val = cookie(res);
1956-
assert.equal(val.indexOf('Expires'), -1, 'should be not have cookie Expires')
19571933
request(server)
19581934
.get('/')
1959-
.set('Cookie', val)
1960-
.expect(200, function (err, res) {
1961-
if (err) return done(err);
1962-
assert.ok(!cookie(res));
1963-
done();
1964-
});
1935+
.set('Cookie', cookie(res))
1936+
.expect(shouldNotHaveHeader('Set-Cookie'))
1937+
.expect(200, done)
19651938
});
19661939
})
19671940

@@ -1974,18 +1947,14 @@ describe('session()', function(){
19741947

19751948
request(server)
19761949
.get('/')
1950+
.expect(shouldSetCookieWithoutAttribute('connect.sid', 'Expires'))
19771951
.expect(200, function (err, res) {
19781952
if (err) return done(err);
1979-
var val = cookie(res);
1980-
assert.equal(val.indexOf('Expires'), -1, 'should be not have cookie Expires')
19811953
request(server)
19821954
.get('/')
1983-
.set('Cookie', val)
1984-
.expect(200, function (err, res) {
1985-
if (err) return done(err);
1986-
assert.ok(!cookie(res));
1987-
done();
1988-
});
1955+
.set('Cookie', cookie(res))
1956+
.expect(shouldNotHaveHeader('Set-Cookie'))
1957+
.expect(200, done)
19891958
});
19901959
})
19911960
})
@@ -2218,42 +2187,53 @@ function shouldNotSetSessionInStore(store) {
22182187
}
22192188
}
22202189

2221-
function shouldNotSetSecureCookie(name) {
2190+
function shouldSetCookie (name) {
22222191
return function (res) {
22232192
var header = cookie(res)
22242193
var data = header && parseSetCookie(header)
22252194
assert.ok(header, 'should have a cookie header')
22262195
assert.equal(data.name, name, 'should set cookie ' + name)
2227-
assert.ok(!data.secure, 'should not set secure cookie')
22282196
}
22292197
}
22302198

2231-
function shouldSetCookie (name) {
2199+
function shouldSetCookieToValue (name, val) {
22322200
return function (res) {
22332201
var header = cookie(res)
22342202
var data = header && parseSetCookie(header)
22352203
assert.ok(header, 'should have a cookie header')
22362204
assert.equal(data.name, name, 'should set cookie ' + name)
2205+
assert.equal(data.value, val, 'should set cookie ' + name + ' to ' + val)
22372206
}
22382207
}
22392208

2240-
function shouldSetCookieToValue (name, val) {
2209+
function shouldSetCookieWithAttribute (name, attrib) {
22412210
return function (res) {
22422211
var header = cookie(res)
22432212
var data = header && parseSetCookie(header)
22442213
assert.ok(header, 'should have a cookie header')
22452214
assert.equal(data.name, name, 'should set cookie ' + name)
2246-
assert.equal(data.value, val, 'should set cookie ' + name + ' to ' + val)
2215+
assert.ok((attrib.toLowerCase() in data), 'should set cookie with attribute ' + attrib)
2216+
}
2217+
}
2218+
2219+
function shouldSetCookieWithAttributeAndValue (name, attrib, value) {
2220+
return function (res) {
2221+
var header = cookie(res)
2222+
var data = header && parseSetCookie(header)
2223+
assert.ok(header, 'should have a cookie header')
2224+
assert.equal(data.name, name, 'should set cookie ' + name)
2225+
assert.ok((attrib.toLowerCase() in data), 'should set cookie with attribute ' + attrib)
2226+
assert.equal(data[attrib.toLowerCase()], value, 'should set cookie with attribute ' + attrib + ' set to ' + value)
22472227
}
22482228
}
22492229

2250-
function shouldSetSecureCookie (name) {
2230+
function shouldSetCookieWithoutAttribute (name, attrib) {
22512231
return function (res) {
22522232
var header = cookie(res)
22532233
var data = header && parseSetCookie(header)
22542234
assert.ok(header, 'should have a cookie header')
22552235
assert.equal(data.name, name, 'should set cookie ' + name)
2256-
assert.ok(data.secure, 'should set secure cookie')
2236+
assert.ok(!(attrib.toLowerCase() in data), 'should set cookie without attribute ' + attrib)
22572237
}
22582238
}
22592239

0 commit comments

Comments
 (0)