Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"@sinonjs/fake-timers": "^15.0.0",
"@types/node": "^26.0.1",
"c8": "^11.0.0",
"cookie": "^1.0.1",
"cookie": "^2.0.0",
"eslint": "^9.17.0",
"fastify": "^5.0.0",
"neostandard": "^0.13.0",
Expand Down
39 changes: 30 additions & 9 deletions test/delete.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
const { test } = require('node:test')
const Fastify = require('fastify')
const sodium = require('sodium-native')
const cookie = require('cookie')
const { parseSetCookie } = require('cookie')
const key = Buffer.alloc(sodium.crypto_secretbox_KEYBYTES)
const expires = new Date(Date.now() + (86400 * 1000))
const expires = new Date(Date.now() + 86400 * 1000)
const expiresUTC = expires.toUTCString()

sodium.randombytes_buf(key)
Expand Down Expand Up @@ -56,9 +56,18 @@ test('Deletes the cookie', async (t) => {
t.assert.ok(postResponse)
t.assert.strictEqual(postResponse.statusCode, 200)
t.assert.ok(postResponse.headers['set-cookie'])
t.assert.strictEqual(cookie.parse(postResponse.headers['set-cookie']).Path, '/')
t.assert.strictEqual(cookie.parse(postResponse.headers['set-cookie']).Expires, expiresUTC)
t.assert.strictEqual(cookie.parse(postResponse.headers['set-cookie'])['Max-Age'], '86400')
t.assert.strictEqual(
parseSetCookie(postResponse.headers['set-cookie']).path,
'/'
)
t.assert.strictEqual(
parseSetCookie(postResponse.headers['set-cookie']).expires.toUTCString(),
expiresUTC
)
t.assert.strictEqual(
parseSetCookie(postResponse.headers['set-cookie']).maxAge,
86400
)

const getResponse = await fastify.inject({
method: 'GET',
Expand All @@ -68,7 +77,10 @@ test('Deletes the cookie', async (t) => {
}
})
t.assert.ok(getResponse)
t.assert.deepStrictEqual(JSON.parse(getResponse.payload), { some: 'someData', some2: { a: 1, c: 3 } })
t.assert.deepStrictEqual(JSON.parse(getResponse.payload), {
some: 'someData',
some2: { a: 1, c: 3 }
})

const deleteResponse = await fastify.inject({
method: 'POST',
Expand All @@ -80,7 +92,16 @@ test('Deletes the cookie', async (t) => {
t.assert.ok(deleteResponse)
t.assert.strictEqual(deleteResponse.statusCode, 200)
t.assert.ok(deleteResponse.headers['set-cookie'])
t.assert.strictEqual(cookie.parse(deleteResponse.headers['set-cookie']).Path, '/')
t.assert.strictEqual(cookie.parse(deleteResponse.headers['set-cookie']).Expires, 'Thu, 01 Jan 1970 00:00:00 GMT')
t.assert.strictEqual(cookie.parse(deleteResponse.headers['set-cookie'])['Max-Age'], '0')
t.assert.strictEqual(
parseSetCookie(deleteResponse.headers['set-cookie']).path,
'/'
)
t.assert.strictEqual(
parseSetCookie(deleteResponse.headers['set-cookie']).expires.toUTCString(),
'Thu, 01 Jan 1970 00:00:00 GMT'
)
t.assert.strictEqual(
parseSetCookie(deleteResponse.headers['set-cookie']).maxAge,
0
)
})
7 changes: 5 additions & 2 deletions test/path.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const { test } = require('node:test')
const Fastify = require('fastify')
const sodium = require('sodium-native')
const cookie = require('cookie')
const { parseSetCookie } = require('cookie')
const key = Buffer.alloc(sodium.crypto_secretbox_KEYBYTES)

sodium.randombytes_buf(key)
Expand Down Expand Up @@ -34,5 +34,8 @@ test('sets path on the cookie', async (t) => {
t.assert.ok(response)
t.assert.strictEqual(response.statusCode, 200)
t.assert.ok(response.headers['set-cookie'])
t.assert.strictEqual(cookie.parse(response.headers['set-cookie']).Path, '/')
t.assert.strictEqual(
parseSetCookie(response.headers['set-cookie']).path,
'/'
)
})
24 changes: 18 additions & 6 deletions test/touch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

const { test } = require('node:test')
const sodium = require('sodium-native')
const cookie = require('cookie')
const { parseSetCookie } = require('cookie')
const key = Buffer.alloc(sodium.crypto_secretbox_KEYBYTES)
sodium.randombytes_buf(key)

test('Sends cookies when touch is invoked and session data has not changed', async t => {
test('Sends cookies when touch is invoked and session data has not changed', async (t) => {
const maxAge = 3600
const fastify = require('fastify')({ logger: false })
t.after(() => fastify.close())
Expand Down Expand Up @@ -39,8 +39,14 @@ test('Sends cookies when touch is invoked and session data has not changed', asy

t.assert.strictEqual(loginResponse.statusCode, 200)
t.assert.ok(loginResponse.headers['set-cookie'])
t.assert.strictEqual(cookie.parse(loginResponse.headers['set-cookie']).Path, '/')
t.assert.strictEqual(cookie.parse(loginResponse.headers['set-cookie'])['Max-Age'], `${maxAge}`)
t.assert.strictEqual(
parseSetCookie(loginResponse.headers['set-cookie']).path,
'/'
)
t.assert.strictEqual(
parseSetCookie(loginResponse.headers['set-cookie']).maxAge,
maxAge
)

const pingResponse = await fastify.inject({
method: 'GET',
Expand All @@ -49,6 +55,12 @@ test('Sends cookies when touch is invoked and session data has not changed', asy

t.assert.strictEqual(pingResponse.statusCode, 200)
t.assert.ok(pingResponse.headers['set-cookie'])
t.assert.strictEqual(cookie.parse(loginResponse.headers['set-cookie']).Path, '/')
t.assert.strictEqual(cookie.parse(loginResponse.headers['set-cookie'])['Max-Age'], `${maxAge}`)
t.assert.strictEqual(
parseSetCookie(loginResponse.headers['set-cookie']).path,
'/'
)
t.assert.strictEqual(
parseSetCookie(loginResponse.headers['set-cookie']).maxAge,
maxAge
)
})
Loading