Skip to content

Commit

Permalink
fix: check existence (#3324)
Browse files Browse the repository at this point in the history
  • Loading branch information
climba03003 committed Sep 17, 2021
1 parent f515fc1 commit 567609b
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/decorate.js
Expand Up @@ -75,10 +75,12 @@ function checkExistence (instance, name) {
}

function checkRequestExistence (name) {
if (name && this[kRequest].props.includes(name)) return true
return checkExistence(this[kRequest].prototype, name)
}

function checkReplyExistence (name) {
if (name && this[kReply].props.includes(name)) return true
return checkExistence(this[kReply].prototype, name)
}

Expand Down
27 changes: 27 additions & 0 deletions test/decorator.test.js
Expand Up @@ -17,6 +17,15 @@ test('server methods should exist', t => {
t.ok(fastify.hasDecorator)
})

test('should check if the given decoration already exist when null', t => {
t.plan(1)
const fastify = Fastify()
fastify.decorate('null', null)
fastify.ready(() => {
t.ok(fastify.hasDecorator('null'))
})
})

test('server methods should be encapsulated via .register', t => {
t.plan(2)
const fastify = Fastify()
Expand Down Expand Up @@ -425,6 +434,15 @@ test('hasRequestDecorator', t => {
t.ok(fastify.hasRequestDecorator(requestDecoratorName))
})

t.test('should check if the given request decoration already exist when null', t => {
t.plan(2)
const fastify = Fastify()

t.notOk(fastify.hasRequestDecorator(requestDecoratorName))
fastify.decorateRequest(requestDecoratorName, null)
t.ok(fastify.hasRequestDecorator(requestDecoratorName))
})

t.test('should be plugin encapsulable', t => {
t.plan(4)
const fastify = Fastify()
Expand Down Expand Up @@ -481,6 +499,15 @@ test('hasReplyDecorator', t => {
t.ok(fastify.hasReplyDecorator(replyDecoratorName))
})

t.test('should check if the given reply decoration already exist when null', t => {
t.plan(2)
const fastify = Fastify()

t.notOk(fastify.hasReplyDecorator(replyDecoratorName))
fastify.decorateReply(replyDecoratorName, null)
t.ok(fastify.hasReplyDecorator(replyDecoratorName))
})

t.test('should be plugin encapsulable', t => {
t.plan(4)
const fastify = Fastify()
Expand Down
60 changes: 60 additions & 0 deletions test/same-shape.test.js
Expand Up @@ -33,6 +33,36 @@ test('same shape on Request', async (t) => {
await app.inject('/')
})

test('same shape on Request when object', async (t) => {
t.plan(1)

const app = fastify()

let request

app.decorateRequest('object', null)

app.addHook('preHandler', (req, reply, done) => {
if (request) {
req.object = {}
}
done()
})

app.get('/', (req, reply) => {
if (request) {
t.equal(%HaveSameMap(request, req), true)
}

request = req

return 'hello world'
})

await app.inject('/')
await app.inject('/')
})

test('same shape on Reply', async (t) => {
t.plan(1)

Expand Down Expand Up @@ -62,3 +92,33 @@ test('same shape on Reply', async (t) => {
await app.inject('/')
await app.inject('/')
})

test('same shape on Reply when object', async (t) => {
t.plan(1)

const app = fastify()

let _reply

app.decorateReply('object', null)

app.addHook('preHandler', (req, reply, done) => {
if (_reply) {
reply.object = {}
}
done()
})

app.get('/', (req, reply) => {
if (_reply) {
t.equal(%HaveSameMap(_reply, reply), true)
}

_reply = reply

return 'hello world'
})

await app.inject('/')
await app.inject('/')
})

0 comments on commit 567609b

Please sign in to comment.