Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increase code coverage to 100%. Closes #56 #72

Merged
merged 2 commits into from
Jul 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .taprc

This file was deleted.

5 changes: 2 additions & 3 deletions lib/vary.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ function vary (field) {
: String(value)

// set new header
if ((value = append(header, field))) {
this.header('Vary', value)
}
Eomm marked this conversation as resolved.
Show resolved Hide resolved
value = append(header, field)
this.header('Vary', value)
}

module.exports = vary
Expand Down
15 changes: 7 additions & 8 deletions test/httpErrors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,6 @@ const Fastify = require('fastify')
const Sensible = require('../index')
const HttpError = require('../lib/httpErrors').HttpError

// fix unsupported status codes
const unsupported = [425]
for (const code in statusCodes) {
if (unsupported.includes(Number(code))) {
delete statusCodes[code]
}
}

test('Should generate the correct http error', t => {
const fastify = Fastify()
fastify.register(Sensible)
Expand All @@ -30,6 +22,10 @@ test('Should generate the correct http error', t => {
// `statusCodes` uses the capital T
if (err.message === 'I\'m a teapot') {
t.equal(err.statusCode, 418)
// `statusCodes` uses unsupported Unordered Collection
// TODO should be deleted after release of https://github.com/jshttp/http-errors/pull/73
} else if (err.message === 'Unordered Collection') {
t.equal(err.statusCode, 425)
} else {
t.equal(err.message, statusCodes[code])
}
Expand Down Expand Up @@ -92,6 +88,9 @@ test('Should generate the correct http error (with custom message)', t => {
function normalize (code, msg) {
if (code === '414') return 'uriTooLong'
if (code === '418') return 'imateapot'
// rename of supported tooEarly to the unsupported unorderedCollection
// TODO should be deleted after release of https://github.com/jshttp/http-errors/pull/73
if (code === '425') return 'unorderedCollection'
if (code === '505') return 'httpVersionNotSupported'
msg = msg.split(' ').join('').replace(/'/g, '')
msg = msg[0].toLowerCase() + msg.slice(1)
Expand Down
59 changes: 51 additions & 8 deletions test/httpErrorsReply.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,8 @@ const Sensible = require('../index')

// from Node.js v10 and above the 418 message has been changed
const node10 = Number(process.versions.node.split('.')[0]) >= 10

// fix unsupported status codes
const unsupported = [425]
for (const code in statusCodes) {
if (unsupported.includes(Number(code))) {
delete statusCodes[code]
}
}
// from Node.js v14 and above the 425 message has been changed
const node14 = Number(process.versions.node.split('.')[0]) >= 14

test('Should generate the correct http error', t => {
Object.keys(statusCodes).forEach(code => {
Expand Down Expand Up @@ -42,7 +36,53 @@ test('Should generate the correct http error', t => {
message: 'I\'m a teapot',
statusCode: 418
})
// TODO should be deleted after release of https://github.com/jshttp/http-errors/pull/73
} else if (code === '425') {
t.same(JSON.parse(res.payload), {
error: node14 ? 'Too Early' : 'Unordered Collection',
message: 'Unordered Collection',
statusCode: 425
})
} else {
t.same(JSON.parse(res.payload), {
error: statusCodes[code],
message: statusCodes[code],
statusCode: Number(code)
})
}
})
})
})
t.end()
})

test('Should generate the correct http error using getter', t => {
Object.keys(statusCodes).forEach(code => {
if (Number(code) < 400) return
t.test(code, t => {
t.plan(3)
const fastify = Fastify()
fastify.register(Sensible)

fastify.get('/', (req, reply) => {
reply.getHttpError(code)
})

fastify.inject({
method: 'GET',
url: '/'
}, (err, res) => {
t.error(err)
// TODO should be deleted after release of https://github.com/jshttp/http-errors/pull/73
if ((node10 && code === '418') || (node14 && code === '425')) {
Eomm marked this conversation as resolved.
Show resolved Hide resolved
t.equal(res.statusCode, 500)
t.same(JSON.parse(res.payload), {
error: 'Internal Server Error',
message: 'Something went wrong',
statusCode: 500
})
} else {
t.equal(res.statusCode, Number(code))
t.same(JSON.parse(res.payload), {
error: statusCodes[code],
message: statusCodes[code],
Expand Down Expand Up @@ -88,6 +128,9 @@ test('Should generate the correct http error (with custom message)', t => {
function normalize (code, msg) {
if (code === '414') return 'uriTooLong'
if (code === '418') return 'imateapot'
// rename of supported tooEarly to the unsupported unorderedCollection
// TODO should be deleted after release of https://github.com/jshttp/http-errors/pull/73
if (code === '425') return 'unorderedCollection'
if (code === '505') return 'httpVersionNotSupported'
msg = msg.split(' ').join('').replace(/'/g, '')
msg = msg[0].toLowerCase() + msg.slice(1)
Expand Down
59 changes: 43 additions & 16 deletions test/vary.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,53 @@ const Fastify = require('fastify')
const Sensible = require('../index')

test('reply.vary API', t => {
t.plan(4)
t.plan(2)

const fastify = Fastify()
fastify.register(Sensible)
t.test('accept string', t => {
t.plan(4)

fastify.get('/', (req, reply) => {
reply.vary('Accept')
reply.vary('Origin')
reply.vary('User-Agent')
reply.send('ok')
const fastify = Fastify()
fastify.register(Sensible)

fastify.get('/', (req, reply) => {
reply.vary('Accept')
reply.vary('Origin')
reply.vary('User-Agent')
reply.send('ok')
})

fastify.inject({
method: 'GET',
url: '/'
}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 200)
t.equal(res.headers.vary, 'Accept, Origin, User-Agent')
t.equal(res.payload, 'ok')
})
})

fastify.inject({
method: 'GET',
url: '/'
}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 200)
t.equal(res.headers.vary, 'Accept, Origin, User-Agent')
t.equal(res.payload, 'ok')
t.test('accept array of strings', t => {
t.plan(4)

const fastify = Fastify()
fastify.register(Sensible)

fastify.get('/', (req, reply) => {
reply.header('Vary', ['Accept', 'Origin'])
reply.vary('User-Agent')
reply.send('ok')
})

fastify.inject({
method: 'GET',
url: '/'
}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 200)
t.equal(res.headers.vary, 'Accept, Origin, User-Agent')
t.equal(res.payload, 'ok')
})
})
})

Expand Down