@fastify/cookie 9.0.2 → 9.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/plugin.js +15 -12
- package/test/cookie.test.js +26 -0
package/package.json
CHANGED
package/plugin.js
CHANGED
|
@@ -9,6 +9,8 @@ const kReplySetCookies = Symbol('fastify.reply.setCookies')
|
|
|
9
9
|
const kReplySetCookiesHookRan = Symbol('fastify.reply.setCookiesHookRan')
|
|
10
10
|
|
|
11
11
|
function fastifyCookieSetCookie (reply, name, value, options) {
|
|
12
|
+
parseCookies(reply.context.server, reply.request, reply)
|
|
13
|
+
|
|
12
14
|
const opts = Object.assign({}, options)
|
|
13
15
|
|
|
14
16
|
if (opts.expires && Number.isInteger(opts.expires)) {
|
|
@@ -46,24 +48,25 @@ function fastifyCookieClearCookie (reply, name, options) {
|
|
|
46
48
|
return fastifyCookieSetCookie(reply, name, '', opts)
|
|
47
49
|
}
|
|
48
50
|
|
|
51
|
+
function parseCookies (fastify, request, reply) {
|
|
52
|
+
if (reply[kReplySetCookies]) return
|
|
53
|
+
|
|
54
|
+
request.cookies = {} // New container per request. Issue #53
|
|
55
|
+
const cookieHeader = request.raw.headers.cookie
|
|
56
|
+
if (cookieHeader) {
|
|
57
|
+
request.cookies = fastify.parseCookie(cookieHeader)
|
|
58
|
+
}
|
|
59
|
+
reply[kReplySetCookies] = new Map()
|
|
60
|
+
}
|
|
61
|
+
|
|
49
62
|
function onReqHandlerWrapper (fastify, hook) {
|
|
50
63
|
return hook === 'preParsing'
|
|
51
64
|
? function fastifyCookieHandler (fastifyReq, fastifyRes, payload, done) {
|
|
52
|
-
fastifyReq
|
|
53
|
-
const cookieHeader = fastifyReq.raw.headers.cookie
|
|
54
|
-
if (cookieHeader) {
|
|
55
|
-
fastifyReq.cookies = fastify.parseCookie(cookieHeader)
|
|
56
|
-
}
|
|
57
|
-
fastifyRes[kReplySetCookies] = new Map()
|
|
65
|
+
parseCookies(fastify, fastifyReq, fastifyRes)
|
|
58
66
|
done()
|
|
59
67
|
}
|
|
60
68
|
: function fastifyCookieHandler (fastifyReq, fastifyRes, done) {
|
|
61
|
-
fastifyReq
|
|
62
|
-
const cookieHeader = fastifyReq.raw.headers.cookie
|
|
63
|
-
if (cookieHeader) {
|
|
64
|
-
fastifyReq.cookies = fastify.parseCookie(cookieHeader)
|
|
65
|
-
}
|
|
66
|
-
fastifyRes[kReplySetCookies] = new Map()
|
|
69
|
+
parseCookies(fastify, fastifyReq, fastifyRes)
|
|
67
70
|
done()
|
|
68
71
|
}
|
|
69
72
|
}
|
package/test/cookie.test.js
CHANGED
|
@@ -1226,3 +1226,29 @@ test('cookies get set correctly if set inside multiple onSends', (t) => {
|
|
|
1226
1226
|
t.equal(cookies[1].path, '/')
|
|
1227
1227
|
})
|
|
1228
1228
|
})
|
|
1229
|
+
|
|
1230
|
+
test('cookies get set correctly if set inside onRequest', (t) => {
|
|
1231
|
+
t.plan(7)
|
|
1232
|
+
const fastify = Fastify()
|
|
1233
|
+
fastify.addHook('onRequest', async (req, reply) => {
|
|
1234
|
+
reply.setCookie('foo', 'foo', { path: '/' })
|
|
1235
|
+
return reply.send({ hello: 'world' })
|
|
1236
|
+
})
|
|
1237
|
+
|
|
1238
|
+
fastify.register(plugin)
|
|
1239
|
+
|
|
1240
|
+
fastify.inject({
|
|
1241
|
+
method: 'GET',
|
|
1242
|
+
url: '/test1'
|
|
1243
|
+
}, (err, res) => {
|
|
1244
|
+
t.error(err)
|
|
1245
|
+
t.equal(res.statusCode, 200)
|
|
1246
|
+
t.same(JSON.parse(res.body), { hello: 'world' })
|
|
1247
|
+
|
|
1248
|
+
const cookies = res.cookies
|
|
1249
|
+
t.equal(cookies.length, 1)
|
|
1250
|
+
t.equal(cookies[0].name, 'foo')
|
|
1251
|
+
t.equal(cookies[0].value, 'foo')
|
|
1252
|
+
t.equal(cookies[0].path, '/')
|
|
1253
|
+
})
|
|
1254
|
+
})
|