Skip to content

Commit

Permalink
test: migrated custom-parser.3.test.js from tap to node:test (fastify…
Browse files Browse the repository at this point in the history
  • Loading branch information
Tony133 authored Dec 22, 2024
1 parent 221ec45 commit a835325
Showing 1 changed file with 56 additions and 45 deletions.
101 changes: 56 additions & 45 deletions test/custom-parser.3.test.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
'use strict'

const t = require('tap')
const test = t.test
const { test } = require('node:test')
const sget = require('simple-get').concat
const Fastify = require('../fastify')
const Fastify = require('..')
const jsonParser = require('fast-json-body')
const { getServerUrl } = require('./helper')

process.removeAllListeners('warning')

test('should be able to use default parser for extra content type', t => {
test('should be able to use default parser for extra content type', (t, done) => {
t.plan(4)
const fastify = Fastify()
t.teardown(() => fastify.close())
t.after(() => fastify.close())

fastify.post('/', (request, reply) => {
reply.send(request.body)
Expand All @@ -21,7 +20,7 @@ test('should be able to use default parser for extra content type', t => {
fastify.addContentTypeParser('text/json', { parseAs: 'string' }, fastify.getDefaultJsonParser('ignore', 'ignore'))

fastify.listen({ port: 0 }, err => {
t.error(err)
t.assert.ifError(err)

sget({
method: 'POST',
Expand All @@ -31,18 +30,17 @@ test('should be able to use default parser for extra content type', t => {
'Content-Type': 'text/json'
}
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.strictSame(JSON.parse(body.toString()), { hello: 'world' })
fastify.close()
t.assert.ifError(err)
t.assert.strictEqual(response.statusCode, 200)
t.assert.deepStrictEqual(JSON.parse(body.toString()), { hello: 'world' })
done()
})
})
})

test('contentTypeParser should add a custom parser with RegExp value', t => {
t.plan(3)

test('contentTypeParser should add a custom parser with RegExp value', async (t) => {
const fastify = Fastify()
t.after(() => fastify.close())

fastify.post('/', (req, reply) => {
reply.send(req.body)
Expand All @@ -58,13 +56,12 @@ test('contentTypeParser should add a custom parser with RegExp value', t => {
})
})

fastify.listen({ port: 0 }, err => {
t.error(err)

t.teardown(() => fastify.close())
fastify.listen({ port: 0 }, async err => {
t.assert.ifError(err)

t.test('in POST', t => {
await t.test('in POST', (t, done) => {
t.plan(3)
t.after(() => fastify.close())

sget({
method: 'POST',
Expand All @@ -74,14 +71,16 @@ test('contentTypeParser should add a custom parser with RegExp value', t => {
'Content-Type': 'application/vnd.test+json'
}
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.same(body.toString(), JSON.stringify({ hello: 'world' }))
t.assert.ifError(err)
t.assert.strictEqual(response.statusCode, 200)
t.assert.deepStrictEqual(body.toString(), JSON.stringify({ hello: 'world' }))
done()
})
})

t.test('in OPTIONS', t => {
await t.test('in OPTIONS', (t, done) => {
t.plan(3)
t.after(() => fastify.close())

sget({
method: 'OPTIONS',
Expand All @@ -91,9 +90,10 @@ test('contentTypeParser should add a custom parser with RegExp value', t => {
'Content-Type': 'weird/content-type+json'
}
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.same(body.toString(), JSON.stringify({ hello: 'world' }))
t.assert.ifError(err)
t.assert.strictEqual(response.statusCode, 200)
t.assert.deepStrictEqual(body.toString(), JSON.stringify({ hello: 'world' }))
done()
})
})
})
Expand All @@ -102,7 +102,7 @@ test('contentTypeParser should add a custom parser with RegExp value', t => {
test('contentTypeParser should add multiple custom parsers with RegExp values', async t => {
t.plan(6)
const fastify = Fastify()
t.teardown(fastify.close.bind(fastify))
t.after(() => fastify.close())

fastify.post('/', (req, reply) => {
reply.send(req.body)
Expand Down Expand Up @@ -137,8 +137,8 @@ test('contentTypeParser should add multiple custom parsers with RegExp values',
'Content-Type': 'application/vnd.hello+json'
}
})
t.equal(response.statusCode, 200)
t.same(response.payload.toString(), '{"hello":"world"}')
t.assert.strictEqual(response.statusCode, 200)
t.assert.deepStrictEqual(response.payload.toString(), '{"hello":"world"}')
}

{
Expand All @@ -150,8 +150,8 @@ test('contentTypeParser should add multiple custom parsers with RegExp values',
'Content-Type': 'application/test+xml'
}
})
t.equal(response.statusCode, 200)
t.same(response.payload.toString(), 'xml')
t.assert.strictEqual(response.statusCode, 200)
t.assert.deepStrictEqual(response.payload.toString(), 'xml')
}

await fastify.inject({
Expand All @@ -162,17 +162,17 @@ test('contentTypeParser should add multiple custom parsers with RegExp values',
'Content-Type': 'application/+myExtension'
}
}).then((response) => {
t.equal(response.statusCode, 200)
t.same(response.payload.toString(), 'abcdefgmyExtension')
t.assert.strictEqual(response.statusCode, 200)
t.assert.deepStrictEqual(response.payload.toString(), 'abcdefgmyExtension')
}).catch((err) => {
t.error(err)
t.assert.ifError(err)
})
})

test('catch all content type parser should not interfere with content type parser', t => {
test('catch all content type parser should not interfere with content type parser', (t, done) => {
t.plan(10)
const fastify = Fastify()
t.teardown(fastify.close.bind(fastify))
t.after(() => fastify.close())

fastify.post('/', (req, reply) => {
reply.send(req.body)
Expand Down Expand Up @@ -201,7 +201,15 @@ test('catch all content type parser should not interfere with content type parse
})

fastify.listen({ port: 0 }, err => {
t.error(err)
t.assert.ifError(err)

let pending = 3

function completed () {
if (--pending === 0) {
done()
}
}

sget({
method: 'POST',
Expand All @@ -211,9 +219,10 @@ test('catch all content type parser should not interfere with content type parse
'Content-Type': 'application/json'
}
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.same(body.toString(), JSON.stringify({ myKey: 'myValue' }))
t.assert.ifError(err)
t.assert.strictEqual(response.statusCode, 200)
t.assert.deepStrictEqual(body.toString(), JSON.stringify({ myKey: 'myValue' }))
completed()
})

sget({
Expand All @@ -224,9 +233,10 @@ test('catch all content type parser should not interfere with content type parse
'Content-Type': 'very-weird-content-type'
}
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.same(body.toString(), 'body')
t.assert.ifError(err)
t.assert.strictEqual(response.statusCode, 200)
t.assert.deepStrictEqual(body.toString(), 'body')
completed()
})

sget({
Expand All @@ -237,9 +247,10 @@ test('catch all content type parser should not interfere with content type parse
'Content-Type': 'text/html'
}
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.same(body.toString(), 'my texthtml')
t.assert.ifError(err)
t.assert.strictEqual(response.statusCode, 200)
t.assert.deepStrictEqual(body.toString(), 'my texthtml')
completed()
})
})
})

0 comments on commit a835325

Please sign in to comment.