@fastify/cookie
Advanced tools
Comparing version 9.0.2 to 9.0.3
{ | ||
"name": "@fastify/cookie", | ||
"version": "9.0.2", | ||
"version": "9.0.3", | ||
"description": "Plugin for fastify to add support for cookies", | ||
@@ -5,0 +5,0 @@ "main": "plugin.js", |
@@ -12,2 +12,4 @@ 'use strict' | ||
function fastifyCookieSetCookie (reply, name, value, options) { | ||
parseCookies(reply.context.server, reply.request, reply) | ||
const opts = Object.assign({}, options) | ||
@@ -50,20 +52,21 @@ | ||
function parseCookies (fastify, request, reply) { | ||
if (reply[kReplySetCookies]) return | ||
request.cookies = {} // New container per request. Issue #53 | ||
const cookieHeader = request.raw.headers.cookie | ||
if (cookieHeader) { | ||
request.cookies = fastify.parseCookie(cookieHeader) | ||
} | ||
reply[kReplySetCookies] = new Map() | ||
} | ||
function onReqHandlerWrapper (fastify, hook) { | ||
return hook === 'preParsing' | ||
? function fastifyCookieHandler (fastifyReq, fastifyRes, payload, done) { | ||
fastifyReq.cookies = {} // New container per request. Issue #53 | ||
const cookieHeader = fastifyReq.raw.headers.cookie | ||
if (cookieHeader) { | ||
fastifyReq.cookies = fastify.parseCookie(cookieHeader) | ||
} | ||
fastifyRes[kReplySetCookies] = new Map() | ||
parseCookies(fastify, fastifyReq, fastifyRes) | ||
done() | ||
} | ||
: function fastifyCookieHandler (fastifyReq, fastifyRes, done) { | ||
fastifyReq.cookies = {} // New container per request. Issue #53 | ||
const cookieHeader = fastifyReq.raw.headers.cookie | ||
if (cookieHeader) { | ||
fastifyReq.cookies = fastify.parseCookie(cookieHeader) | ||
} | ||
fastifyRes[kReplySetCookies] = new Map() | ||
parseCookies(fastify, fastifyReq, fastifyRes) | ||
done() | ||
@@ -70,0 +73,0 @@ } |
@@ -1229,1 +1229,27 @@ 'use strict' | ||
}) | ||
test('cookies get set correctly if set inside onRequest', (t) => { | ||
t.plan(7) | ||
const fastify = Fastify() | ||
fastify.addHook('onRequest', async (req, reply) => { | ||
reply.setCookie('foo', 'foo', { path: '/' }) | ||
return reply.send({ hello: 'world' }) | ||
}) | ||
fastify.register(plugin) | ||
fastify.inject({ | ||
method: 'GET', | ||
url: '/test1' | ||
}, (err, res) => { | ||
t.error(err) | ||
t.equal(res.statusCode, 200) | ||
t.same(JSON.parse(res.body), { hello: 'world' }) | ||
const cookies = res.cookies | ||
t.equal(cookies.length, 1) | ||
t.equal(cookies[0].name, 'foo') | ||
t.equal(cookies[0].value, 'foo') | ||
t.equal(cookies[0].path, '/') | ||
}) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
72159
1858