Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add number coersion related tests
- Loading branch information
Showing
3 changed files
with
114 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
'use strict' | ||
|
||
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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
'use strict' | ||
|
||
const t = require('tap') | ||
const test = t.test | ||
const Fastify = require('..') | ||
|
||
test('listen should accept null port', t => { | ||
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 => { | ||
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.') | ||
} | ||
}) |