Skip to content

Commit

Permalink
tests: break out the overwrite tests for frameworks
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Jun 7, 2019
1 parent 8b1c942 commit 023926f
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 17 deletions.
72 changes: 64 additions & 8 deletions test/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,18 @@ describeExpress('Express', function () {
// set a signed cookie
.set( "signed", "bar", { signed: true } )

// set a cookie that will be overwritten
.set( "overwrite", "old-value", { signed: true } )
.set( "overwrite", "new-value", { overwrite: true, signed: true } )

res.writeHead(302, {Location: "/"})
res.end()
})

app.get("/", function(req, res) {
var unsigned = req.cookies.get( "unsigned" )
, signed = req.cookies.get( "signed", { signed: true } )
, overwrite = req.cookies.get( "overwrite", { signed: true } )

assert.equal( unsigned, "foo" )
assert.equal( req.cookies.get( "unsigned.sig", { signed:false } ), undefined)
assert.equal( signed, "bar" )
assert.equal( req.cookies.get( "signed.sig", { signed: false } ), keys.sign('signed=bar') )
assert.equal( overwrite, "new-value" )
assert.equal( req.cookies.get( "overwrite.sig", { signed:false } ), keys.sign('overwrite=new-value') )

res.send(
"unsigned expected: foo\n" +
Expand All @@ -70,7 +63,7 @@ describeExpress('Express', function () {
if (err) return done(err)

header = res.headers['set-cookie']
assert.equal(header.length, 6)
assert.equal(header.length, 4)
done()
})
})
Expand All @@ -82,6 +75,69 @@ describeExpress('Express', function () {
.expect(200, done)
})

describe('when "overwrite: false"', function () {
it('should set second cookie with same name', function (done) {
var app = express()

app.set('env', 'test')
app.use(cookies())
app.get('/', function (req, res) {
res.cookies.set('foo', 'bar')
res.cookies.set('foo', 'fizz', { overwrite: false })
res.end()
})

request(app)
.get('/')
.expect(shouldSetCookies([
{ name: 'foo', value: 'bar', path: '/', httponly: true },
{ name: 'foo', value: 'fizz', path: '/', httponly: true }
]))
.expect(200, done)
})
})

describe('when "overwrite: true"', function () {
it('should replace previously set value', function (done) {
var app = express()

app.set('env', 'test')
app.use(cookies())
app.get('/', function (req, res, next) {
res.cookies.set('foo', 'bar')
res.cookies.set('foo', 'fizz', { overwrite: true })
res.end()
})

request(app)
.get('/')
.expect(shouldSetCookies([
{ name: 'foo', value: 'fizz', path: '/', httponly: true }
]))
.expect(200, done)
})

it('should set signature correctly', function (done) {
var app = express()

app.set('env', 'test')
app.use(cookies(keys))
app.get('/', function (req, res, next) {
res.cookies.set('foo', 'bar')
res.cookies.set('foo', 'fizz', { overwrite: true })
res.end()
})

request(app)
.get('/')
.expect(shouldSetCookies([
{ name: 'foo', value: 'fizz', path: '/', httponly: true },
{ name: 'foo.sig', value: 'hVIYdxZSelh3gIK5wQxzrqoIndU', path: '/', httponly: true }
]))
.expect(200, done)
})
})

describe('when "secure: true"', function () {
it('should not set when not secure', function (done) {
var app = express()
Expand Down
107 changes: 98 additions & 9 deletions test/restify.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,38 +49,127 @@ describeRestify('Restify', function () {
.set('Cookie', header.join(';'))
.expect(200, done)
})

describe('when "overwrite: false"', function () {
it('should set second cookie with same name', function (done) {
var server = restify.createServer()

server.get('/', function (req, res) {
var cookies = new Cookies(req, res)

cookies.set('foo', 'bar')
cookies.set('foo', 'fizz', { overwrite: false })

res.send(200)
})

request(server)
.get('/')
.expect(shouldSetCookies([
{ name: 'foo', value: 'bar', path: '/', httponly: true },
{ name: 'foo', value: 'fizz', path: '/', httponly: true }
]))
.expect(200, done)
})
})

describe('when "overwrite: true"', function () {
it('should replace previously set value', function (done) {
var server = restify.createServer()

server.get('/', function (req, res) {
var cookies = new Cookies(req, res)

cookies.set('foo', 'bar')
cookies.set('foo', 'fizz', { overwrite: true })

res.send(200)
})

request(server)
.get('/')
.expect(shouldSetCookies([
{ name: 'foo', value: 'fizz', path: '/', httponly: true }
]))
.expect(200, done)
})

it('should set signature correctly', function (done) {
var server = restify.createServer()

server.get('/', function (req, res) {
var cookies = new Cookies(req, res, keys)

cookies.set('foo', 'bar')
cookies.set('foo', 'fizz', { overwrite: true })

res.send(200)
})

request(server)
.get('/')
.expect(shouldSetCookies([
{ name: 'foo', value: 'fizz', path: '/', httponly: true },
{ name: 'foo.sig', value: 'hVIYdxZSelh3gIK5wQxzrqoIndU', path: '/', httponly: true }
]))
.expect(200, done)
})
})
})

function setCookies(req, res) {
var cookies = new Cookies(req, res, keys)
cookies
.set('unsigned', 'foo', { signed:false, httpOnly: false })
.set('signed', 'bar', { signed: true })
.set('overwrite', 'old-value', { signed: true })
.set('overwrite', 'new-value', { overwrite: true, signed: true })
}

function assertCookies(req, res) {
var cookies = new Cookies(req, res, keys)
var unsigned = cookies.get('unsigned'),
signed = cookies.get('signed', { signed: true }),
overwrite = cookies.get('overwrite', { signed: true })
signed = cookies.get('signed', { signed: true })

assert.equal(unsigned, 'foo')
assert.equal(cookies.get('unsigned.sig', { signed:false }), undefined)
assert.equal(signed, 'bar')
assert.equal(cookies.get('signed.sig', { signed: false }), keys.sign('signed=bar'))
assert.equal(overwrite, 'new-value')
assert.equal(cookies.get('overwrite.sig', { signed:false }), keys.sign('overwrite=new-value'))
}

function assertSetCookieHeader(header) {
assert.equal(header.length, 5)
assert.equal(header.length, 3)
assert.equal(header[0], 'unsigned=foo; path=/')
assert.equal(header[1], 'signed=bar; path=/; httponly')
assert.ok(/^signed\.sig=.{27}; path=\/; httponly$/.test(header[2]))
assert.equal(header[3], 'overwrite=new-value; path=/; httponly')
assert.ok(/^overwrite\.sig=.{27}; path=\/; httponly$/.test(header[4]))
}

function getCookies (res) {
var setCookies = res.headers['set-cookie'] || []
return setCookies.map(parseSetCookie)
}

function parseSetCookie (header) {
var match
var pairs = []
var pattern = /\s*([^=;]+)(?:=([^;]*);?|;|$)/g

while ((match = pattern.exec(header))) {
pairs.push({ name: match[1], value: match[2] })
}

var cookie = pairs.shift()

for (var i = 0; i < pairs.length; i++) {
match = pairs[i]
cookie[match.name.toLowerCase()] = (match.value || true)
}

return cookie
}

function shouldSetCookies (expected) {
return function (res) {
assert.deepEqual(getCookies(res), expected)
}
}

function tryRequire (name) {
Expand Down

0 comments on commit 023926f

Please sign in to comment.