Skip to content

Commit

Permalink
test: increase code coverage to 100%. Closes fastify#56
Browse files Browse the repository at this point in the history
  • Loading branch information
0xvbetsun committed Jul 18, 2021
1 parent 266b3be commit 90be0eb
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 36 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ jobs:
- name: Run Tests
run: |
npm test
- name: Coveralls Parallel
uses: coverallsapp/github-action@1.1.3
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
parallel: true
flag-name: run-${{ matrix.node-version }}-${{ matrix.os }}

automerge:
needs: test
Expand All @@ -42,3 +49,13 @@ jobs:
- uses: fastify/github-action-merge-dependabot@v2.2.0
with:
github-token: ${{ secrets.GITHUB_TOKEN }}

coverage:
needs: test
runs-on: ubuntu-latest
steps:
- name: Coveralls Finished
uses: coverallsapp/github-action@1.1.3
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
parallel-finished: true
2 changes: 1 addition & 1 deletion .taprc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
check-coverage: false
100: true
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
[![NPM version](https://img.shields.io/npm/v/fastify-sensible.svg?style=flat)](https://www.npmjs.com/package/fastify-sensible)
[![Known Vulnerabilities](https://snyk.io/test/github/fastify/fastify-sensible/badge.svg)](https://snyk.io/test/github/fastify/fastify-sensible)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/)
[![Coverage Status](https://coveralls.io/repos/github/fastify/fastify-sensible/badge.svg?branch=main)](https://coveralls.io/github/fastify/fastify-sensible?branch=main)

Defaults for Fastify that everyone can agree on™.<br>
This plugin adds some useful utilities to your Fastify instance, see the API section to learn more.
Expand Down
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)
}
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
// 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
// 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
})
// 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)
// should be deleted after release of https://github.com/jshttp/http-errors/pull/73
if ((node10 && code === '418') || (node14 && code === '425')) {
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
// 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

0 comments on commit 90be0eb

Please sign in to comment.