From aa2ed8c534b84e843b3ee7b610f36d04671eb95c Mon Sep 17 00:00:00 2001 From: James Sumners Date: Wed, 27 Feb 2019 17:59:12 -0500 Subject: [PATCH 1/3] Use onRequest instead of preHandler --- plugin.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugin.js b/plugin.js index f09c55f..e8a71a8 100644 --- a/plugin.js +++ b/plugin.js @@ -26,7 +26,7 @@ function fastifyCookieSetCookie (name, value, options) { return this } -function fastifyCookiePreHandler (fastifyReq, fastifyRes, done) { +function fastifyCookieOnReqHandler (fastifyReq, fastifyRes, done) { const cookieHeader = fastifyReq.req.headers.cookie fastifyReq.cookies = (cookieHeader) ? cookie.parse(cookieHeader) : {} done() @@ -35,7 +35,7 @@ function fastifyCookiePreHandler (fastifyReq, fastifyRes, done) { function plugin (fastify, options, next) { fastify.decorateRequest('cookies', {}) fastify.decorateReply('setCookie', fastifyCookieSetCookie) - fastify.addHook('preHandler', fastifyCookiePreHandler) + fastify.addHook('onRequest', fastifyCookieOnReqHandler) next() } From 0b211c9722f63600d24bfd79b776616a014b7eb0 Mon Sep 17 00:00:00 2001 From: James Sumners Date: Wed, 27 Feb 2019 18:05:12 -0500 Subject: [PATCH 2/3] [ci-skip] Update readme --- Readme.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Readme.md b/Readme.md index efa7054..53360d2 100644 --- a/Readme.md +++ b/Readme.md @@ -5,8 +5,8 @@ A plugin for [Fastify](http://fastify.io/) that adds support for reading and setting cookies. -This plugin's cookie parsing works via Fastify's `preHandler` hook. Therefore, -you should register it prior to any other `preHandler` hooks that will depend +This plugin's cookie parsing works via Fastify's `onRequest` hook. Therefore, +you should register it prior to any other `onRequest` hooks that will depend upon this plugin's actions. ## Example @@ -33,7 +33,7 @@ fastify.get('/', (req, reply) => { ### Parsing -Cookies are parsed in the `preHandler` Fastify hook and attached to the request +Cookies are parsed in the `onRequest` Fastify hook and attached to the request as an object named `cookies`. Thus, if a request contains the header `Cookie: foo=foo` then, within your handler, `req.cookies.foo` would equal `'foo'`. From 365424edcf53762fc91c399d5904289868a2d3c2 Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Thu, 28 Feb 2019 17:06:38 +0100 Subject: [PATCH 3/3] Added test --- test/cookie.test.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/test/cookie.test.js b/test/cookie.test.js index b6091f8..2eaeace 100644 --- a/test/cookie.test.js +++ b/test/cookie.test.js @@ -123,10 +123,20 @@ test('cookies get set correctly with millisecond dates', (t) => { }) test('parses incoming cookies', (t) => { - t.plan(6) + t.plan(6 + 3 * 3) const fastify = Fastify() fastify.register(plugin) + // check that it parses the cookies in the onRequest hook + for (let hook of ['preParsing', 'preValidation', 'preHandler']) { + fastify.addHook(hook, (req, reply, done) => { + t.ok(req.cookies) + t.ok(req.cookies.bar) + t.is(req.cookies.bar, 'bar') + done() + }) + } + fastify.get('/test2', (req, reply) => { t.ok(req.cookies) t.ok(req.cookies.bar)