Skip to content

Commit 2bc2903

Browse files
committed
Adjusted test for node test runner
1 parent 1d0a2cf commit 2bc2903

File tree

1 file changed

+88
-140
lines changed

1 file changed

+88
-140
lines changed

test/static.test.js

Lines changed: 88 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -3493,8 +3493,8 @@ test('respect the .type when using with sendFile with contentType disabled', asy
34933493
t.assert.deepStrictEqual(await response.text(), fooContent)
34943494
})
34953495

3496-
t.test('register /static/ with custom log level', t => {
3497-
t.plan(11)
3496+
test('register /static/ with custom log level', async t => {
3497+
t.plan(9)
34983498

34993499
const pluginOptions = {
35003500
root: path.join(__dirname, '/static'),
@@ -3505,163 +3505,111 @@ t.test('register /static/ with custom log level', t => {
35053505
logger: {
35063506
stream: {
35073507
write: (logLine) => {
3508-
const log = JSON.parse(logLine)
3509-
3510-
if (log.reqId) {
3511-
t.fail('Not expecting any log since plugin\'s log level is set at WARN')
3508+
if (logLine.includes('"msg":"incoming request"')) {
3509+
console.warn(logLine)
3510+
throw new Error('Should never reach this point since log level is set at WARN!! Unexpected log line: ' + logLine)
35123511
}
35133512
},
35143513
},
35153514
},
35163515
})
35173516
fastify.register(fastifyStatic, pluginOptions)
35183517

3519-
t.teardown(fastify.close.bind(fastify))
3518+
t.after(() => fastify.close())
35203519

3521-
fastify.listen({ port: 0 }, (err) => {
3522-
t.error(err)
3520+
await fastify.listen({ port: 0 })
3521+
fastify.server.unref()
35233522

3524-
fastify.server.unref()
3523+
await t.test('/static/index.html', async (t) => {
3524+
t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT)
35253525

3526-
t.test('/static/index.html', (t) => {
3527-
t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT)
3528-
simple.concat({
3529-
method: 'GET',
3530-
url: 'http://localhost:' + fastify.server.address().port + '/static/index.html'
3531-
}, (err, response, body) => {
3532-
t.error(err)
3533-
t.equal(response.statusCode, 200)
3534-
t.equal(body.toString(), indexContent)
3535-
genericResponseChecks(t, response)
3536-
})
3537-
})
3526+
const response = await fetch('http://localhost:' + fastify.server.address().port + '/static/index.html')
3527+
t.assert.ok(response.ok)
3528+
t.assert.deepStrictEqual(response.status, 200)
3529+
t.assert.deepStrictEqual(await response.text(), indexContent)
3530+
genericResponseChecks(t, response)
3531+
})
35383532

3539-
t.test('/static/index.html', t => {
3540-
t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT)
3541-
simple.concat({
3542-
method: 'HEAD',
3543-
url: 'http://localhost:' + fastify.server.address().port + '/static/index.html'
3544-
}, (err, response, body) => {
3545-
t.error(err)
3546-
t.equal(response.statusCode, 200)
3547-
t.equal(body.toString(), '')
3548-
genericResponseChecks(t, response)
3549-
})
3550-
})
3533+
await t.test('/static/index.html', async t => {
3534+
t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT)
3535+
const response = await fetch('http://localhost:' + fastify.server.address().port + '/static/index.html', { method: 'HEAD' })
3536+
t.assert.ok(response.ok)
3537+
t.assert.deepStrictEqual(response.status, 200)
3538+
t.assert.deepStrictEqual(await response.text(), '')
3539+
genericResponseChecks(t, response)
3540+
})
35513541

3552-
t.test('/static/index.css', (t) => {
3553-
t.plan(2 + GENERIC_RESPONSE_CHECK_COUNT)
3554-
simple.concat({
3555-
method: 'GET',
3556-
url: 'http://localhost:' + fastify.server.address().port + '/static/index.css'
3557-
}, (err, response, body) => {
3558-
t.error(err)
3559-
t.equal(response.statusCode, 200)
3560-
genericResponseChecks(t, response)
3561-
})
3562-
})
3542+
await t.test('/static/index.css', async (t) => {
3543+
t.plan(2 + GENERIC_RESPONSE_CHECK_COUNT)
3544+
const response = await fetch('http://localhost:' + fastify.server.address().port + '/static/index.css')
3545+
t.assert.ok(response.ok)
3546+
t.assert.deepStrictEqual(response.status, 200)
3547+
genericResponseChecks(t, response)
3548+
})
35633549

3564-
t.test('/static/', (t) => {
3565-
t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT)
3566-
simple.concat({
3567-
method: 'GET',
3568-
url: 'http://localhost:' + fastify.server.address().port + '/static/'
3569-
}, (err, response, body) => {
3570-
t.error(err)
3571-
t.equal(response.statusCode, 200)
3572-
t.equal(body.toString(), indexContent)
3573-
genericResponseChecks(t, response)
3574-
})
3575-
})
3550+
await t.test('/static/', async (t) => {
3551+
t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT)
3552+
const response = await fetch('http://localhost:' + fastify.server.address().port + '/static/')
35763553

3577-
t.test('/static/deep/path/for/test/purpose/foo.html', (t) => {
3578-
t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT)
3579-
simple.concat({
3580-
method: 'GET',
3581-
url: 'http://localhost:' + fastify.server.address().port + '/static/deep/path/for/test/purpose/foo.html'
3582-
}, (err, response, body) => {
3583-
t.error(err)
3584-
t.equal(response.statusCode, 200)
3585-
t.equal(body.toString(), deepContent)
3586-
genericResponseChecks(t, response)
3587-
})
3588-
})
3554+
t.assert.ok(response.ok)
3555+
t.assert.deepStrictEqual(response.status, 200)
3556+
t.assert.deepStrictEqual(await response.text(), indexContent)
3557+
genericResponseChecks(t, response)
3558+
})
35893559

3590-
t.test('/static/deep/path/for/test/', (t) => {
3591-
t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT)
3592-
simple.concat({
3593-
method: 'GET',
3594-
url: 'http://localhost:' + fastify.server.address().port + '/static/deep/path/for/test/'
3595-
}, (err, response, body) => {
3596-
t.error(err)
3597-
t.equal(response.statusCode, 200)
3598-
t.equal(body.toString(), innerIndex)
3599-
genericResponseChecks(t, response)
3600-
})
3601-
})
3560+
await t.test('/static/deep/path/for/test/purpose/foo.html', async (t) => {
3561+
t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT)
3562+
const response = await fetch('http://localhost:' + fastify.server.address().port + '/static/deep/path/for/test/purpose/foo.html')
36023563

3603-
t.test('/static/this/path/for/test', (t) => {
3604-
t.plan(2 + GENERIC_ERROR_RESPONSE_CHECK_COUNT)
3605-
simple.concat({
3606-
method: 'GET',
3607-
url: 'http://localhost:' + fastify.server.address().port + '/static/this/path/for/test',
3608-
followRedirect: false
3609-
}, (err, response, body) => {
3610-
t.error(err)
3611-
t.equal(response.statusCode, 404)
3612-
genericErrorResponseChecks(t, response)
3613-
})
3614-
})
3564+
t.assert.ok(response.ok)
3565+
t.assert.deepStrictEqual(response.status, 200)
3566+
t.assert.deepStrictEqual(await response.text(), deepContent)
3567+
genericResponseChecks(t, response)
3568+
})
3569+
await t.test('/static/deep/path/for/test/', async (t) => {
3570+
t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT)
3571+
const response = await fetch('http://localhost:' + fastify.server.address().port + '/static/deep/path/for/test/')
36153572

3616-
t.test('/static/this/path/doesnt/exist.html', (t) => {
3617-
t.plan(2 + GENERIC_ERROR_RESPONSE_CHECK_COUNT)
3618-
simple.concat({
3619-
method: 'GET',
3620-
url: 'http://localhost:' + fastify.server.address().port + '/static/this/path/doesnt/exist.html',
3621-
followRedirect: false
3622-
}, (err, response, body) => {
3623-
t.error(err)
3624-
t.equal(response.statusCode, 404)
3625-
genericErrorResponseChecks(t, response)
3626-
})
3627-
})
3573+
t.assert.ok(response.ok)
3574+
t.assert.deepStrictEqual(response.status, 200)
3575+
t.assert.deepStrictEqual(await response.text(), innerIndex)
3576+
genericResponseChecks(t, response)
3577+
})
36283578

3629-
t.test('/static/../index.js', (t) => {
3630-
t.plan(2 + GENERIC_ERROR_RESPONSE_CHECK_COUNT)
3631-
simple.concat({
3632-
method: 'GET',
3633-
url: 'http://localhost:' + fastify.server.address().port + '/static/../index.js',
3634-
followRedirect: false
3635-
}, (err, response, body) => {
3636-
t.error(err)
3637-
t.equal(response.statusCode, 403)
3638-
genericErrorResponseChecks(t, response)
3639-
})
3640-
})
3579+
await t.test('/static/this/path/for/test', async (t) => {
3580+
t.plan(2 + GENERIC_ERROR_RESPONSE_CHECK_COUNT)
3581+
const response = await fetch('http://localhost:' + fastify.server.address().port + '/static/this/path/for/test')
36413582

3642-
t.test('304', t => {
3643-
t.plan(5 + GENERIC_RESPONSE_CHECK_COUNT)
3644-
simple.concat({
3645-
method: 'GET',
3646-
url: 'http://localhost:' + fastify.server.address().port + '/static/index.html'
3647-
}, (err, response, body) => {
3648-
t.error(err)
3649-
const etag = response.headers.etag
3650-
t.equal(response.statusCode, 200)
3651-
t.equal(body.toString(), indexContent)
3652-
genericResponseChecks(t, response)
3653-
3654-
simple.concat({
3655-
method: 'GET',
3656-
url: 'http://localhost:' + fastify.server.address().port + '/static/index.html',
3657-
headers: {
3658-
'if-none-match': etag
3659-
}
3660-
}, (err, response, body) => {
3661-
t.error(err)
3662-
t.equal(response.statusCode, 304)
3663-
})
3664-
})
3583+
t.assert.ok(!response.ok)
3584+
t.assert.deepStrictEqual(response.status, 404)
3585+
genericErrorResponseChecks(t, response)
3586+
})
3587+
3588+
await t.test('/static/this/path/doesnt/exist.html', async (t) => {
3589+
t.plan(2 + GENERIC_ERROR_RESPONSE_CHECK_COUNT)
3590+
const response = await fetch('http://localhost:' + fastify.server.address().port + '/static/this/path/doesnt/exist.html')
3591+
3592+
t.assert.ok(!response.ok)
3593+
t.assert.deepStrictEqual(response.status, 404)
3594+
genericErrorResponseChecks(t, response)
3595+
})
3596+
3597+
await t.test('304', async t => {
3598+
t.plan(5 + GENERIC_RESPONSE_CHECK_COUNT)
3599+
const response = await fetch('http://localhost:' + fastify.server.address().port + '/static/index.html')
3600+
3601+
t.assert.ok(response.ok)
3602+
t.assert.deepStrictEqual(response.status, 200)
3603+
t.assert.deepStrictEqual(await response.text(), indexContent)
3604+
genericResponseChecks(t, response)
3605+
3606+
const response2 = await fetch('http://localhost:' + fastify.server.address().port + '/static/index.html', {
3607+
headers: {
3608+
'if-none-match': response.headers.get('etag')
3609+
},
3610+
cache: 'no-cache'
36653611
})
3612+
t.assert.ok(!response2.ok)
3613+
t.assert.deepStrictEqual(response2.status, 304)
36663614
})
36673615
})

0 commit comments

Comments
 (0)