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

test: add number coersion related tests #4297

Merged
merged 1 commit into from Sep 27, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/server.js
Expand Up @@ -332,7 +332,7 @@ function normalizeListenArgs (args) {

function normalizePort (firstArg) {
const port = Number(firstArg)
return port >= 0 && !Number.isNaN(port) ? port : 0
return port >= 0 && !Number.isNaN(port) && Number.isInteger(port) ? port : 0
}

function logServerAddress (server) {
Expand Down
59 changes: 59 additions & 0 deletions test/reply-code.test.js
@@ -0,0 +1,59 @@
'use strict'
jsumners marked this conversation as resolved.
Show resolved Hide resolved

const t = require('tap')
const test = t.test
const Fastify = require('..')

test('code should handle null/undefined/float', t => {
t.plan(8)

const fastify = Fastify()

fastify.get('/null', function (request, reply) {
reply.status(null).send()
})

fastify.get('/undefined', function (request, reply) {
reply.status(undefined).send()
})

fastify.get('/404.5', function (request, reply) {
reply.status(404.5).send()
})

fastify.inject({
method: 'GET',
url: '/null'
}, (error, res) => {
t.error(error)
t.equal(res.statusCode, 500)
t.same(res.json(), {
statusCode: 500,
code: 'FST_ERR_BAD_STATUS_CODE',
error: 'Internal Server Error',
message: 'Called reply with an invalid status code: null'
})
})

fastify.inject({
method: 'GET',
url: '/undefined'
}, (error, res) => {
t.error(error)
t.equal(res.statusCode, 500)
t.same(res.json(), {
statusCode: 500,
code: 'FST_ERR_BAD_STATUS_CODE',
error: 'Internal Server Error',
message: 'Called reply with an invalid status code: undefined'
})
})

fastify.inject({
method: 'GET',
url: '/404.5'
}, (error, res) => {
t.error(error)
t.equal(res.statusCode, 404)
})
})
54 changes: 54 additions & 0 deletions test/server.test.js
@@ -0,0 +1,54 @@
'use strict'
anonrig marked this conversation as resolved.
Show resolved Hide resolved

const t = require('tap')
const test = t.test
const Fastify = require('..')

test('listen should accept null port', t => {
anonrig marked this conversation as resolved.
Show resolved Hide resolved
t.plan(1)

const fastify = Fastify()
t.teardown(fastify.close.bind(fastify))
fastify.listen({ port: null }, (err) => {
t.error(err)
})
})

test('listen should accept undefined port', t => {
anonrig marked this conversation as resolved.
Show resolved Hide resolved
t.plan(1)

const fastify = Fastify()
t.teardown(fastify.close.bind(fastify))
fastify.listen({ port: undefined }, (err) => {
t.error(err)
})
})

test('listen should accept stringified number port', t => {
t.plan(1)

const fastify = Fastify()
t.teardown(fastify.close.bind(fastify))
fastify.listen({ port: '1234' }, (err) => {
t.error(err)
})
})

test('listen should reject string port', async (t) => {
t.plan(2)

const fastify = Fastify()
t.teardown(fastify.close.bind(fastify))

try {
await fastify.listen({ port: 'hello-world' })
} catch (error) {
t.same(error.message, 'options.port should be >= 0 and < 65536. Received hello-world.')
}

try {
await fastify.listen({ port: '1234hello' })
} catch (error) {
t.same(error.message, 'options.port should be >= 0 and < 65536. Received 1234hello.')
}
anonrig marked this conversation as resolved.
Show resolved Hide resolved
})