From 94bab61cc230f7e027b93801d0201e80c028d3e1 Mon Sep 17 00:00:00 2001 From: James Sumners Date: Thu, 16 Nov 2017 10:46:12 -0500 Subject: [PATCH] Update for latest Fastify (0.34.0) --- .travis.yml | 15 +++++++ package.json | 7 +-- plugin.js | 6 ++- test/{integration.test.js => cookie.test.js} | 46 ++++++++++++-------- 4 files changed, 52 insertions(+), 22 deletions(-) create mode 100644 .travis.yml rename test/{integration.test.js => cookie.test.js} (60%) diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..edb52e6 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,15 @@ +language: node_js + +node_js: + - "9" + - "8" + - "6" + +script: + - npm run lint-ci + - npm run test-ci + +notifications: + email: + on_success: never + on_failure: always diff --git a/package.json b/package.json index eb57ed7..5518ff7 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,9 @@ "main": "plugin.js", "scripts": { "test": "tap 'test/**/*.test.js'", - "lint": "standard | snazzy" + "test-ci": "tap --cov 'test/**/*.test.js'", + "lint": "standard | snazzy", + "lint-ci": "standard" }, "precommit": [ "lint", @@ -26,8 +28,7 @@ }, "homepage": "https://github.com/fastify/fastify-cookie#readme", "devDependencies": { - "abstract-logging": "^1.0.0", - "fastify": "^0.26.2", + "fastify": "^0.34.0", "pre-commit": "^1.2.2", "request": "^2.81.0", "snazzy": "^7.0.0", diff --git a/plugin.js b/plugin.js index 528a99f..e0ac064 100644 --- a/plugin.js +++ b/plugin.js @@ -4,6 +4,8 @@ const fp = require('fastify-plugin') const cookie = require('cookie') function plugin (fastify, options, next) { + fastify.decorateRequest('cookies', []) + fastify.decorateReply('setCookie', function (name, value, options) { const seriaized = cookie.serialize(name, value, options || {}) this.header('Set-Cookie', seriaized) @@ -11,7 +13,7 @@ function plugin (fastify, options, next) { }) fastify.addHook('preHandler', (fastifyReq, fastifyRes, done) => { - if (fastifyReq.cookies) done() + if (fastifyReq.cookies.length > 0) done() if (!fastifyReq.req.headers) fastifyReq.req.headers = {} const cookieHeader = fastifyReq.req.headers.cookie @@ -24,4 +26,4 @@ function plugin (fastify, options, next) { next() } -module.exports = fp(plugin, '>=0.15.0') +module.exports = fp(plugin, '>=0.34.0') diff --git a/test/integration.test.js b/test/cookie.test.js similarity index 60% rename from test/integration.test.js rename to test/cookie.test.js index 97cb8ec..f8eb99c 100644 --- a/test/integration.test.js +++ b/test/cookie.test.js @@ -3,26 +3,24 @@ const tap = require('tap') const test = tap.test -const fastify = require('fastify')({logger: require('abstract-logging')}) +const Fastify = require('fastify') const request = require('request') const plugin = require('../') -fastify.register(plugin, (err) => { - if (err) throw err -}) - -fastify.listen(0, (err) => { - if (err) tap.error(err) - fastify.server.unref() +test('cookies get set correctly', (t) => { + t.plan(7) + const fastify = Fastify() + fastify.register(plugin) - const reqOpts = { - method: 'GET', - baseUrl: 'http://localhost:' + fastify.server.address().port - } - const req = request.defaults(reqOpts) + fastify.listen(0, (err) => { + if (err) tap.error(err) + fastify.server.unref() - test('cookies get set correctly', (t) => { - t.plan(7) + const reqOpts = { + method: 'GET', + baseUrl: 'http://localhost:' + fastify.server.address().port + } + const req = request.defaults(reqOpts) fastify.get('/test1', (req, reply) => { reply @@ -43,9 +41,23 @@ fastify.listen(0, (err) => { t.is(cookies[0].path, '/') }) }) +}) + +test('parses incoming cookies', (t) => { + t.plan(6) + const fastify = Fastify() + fastify.register(plugin) + + fastify.listen(0, (err) => { + if (err) tap.error(err) + fastify.server.unref() + + const reqOpts = { + method: 'GET', + baseUrl: 'http://localhost:' + fastify.server.address().port + } + const req = request.defaults(reqOpts) - test('parses incoming cookies', (t) => { - t.plan(6) fastify.get('/test2', (req, reply) => { t.ok(req.cookies) t.ok(req.cookies.bar)