Skip to content

Commit

Permalink
Update for latest Fastify (0.34.0)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsumners committed Nov 16, 2017
1 parent b99706c commit 94bab61
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 22 deletions.
15 changes: 15 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
6 changes: 4 additions & 2 deletions plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ 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)
return this
})

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
Expand All @@ -24,4 +26,4 @@ function plugin (fastify, options, next) {
next()
}

module.exports = fp(plugin, '>=0.15.0')
module.exports = fp(plugin, '>=0.34.0')
46 changes: 29 additions & 17 deletions test/integration.test.js → test/cookie.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 94bab61

Please sign in to comment.