Skip to content

Commit

Permalink
add tests for file extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak committed May 23, 2023
1 parent a99d206 commit 4077f78
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 0 deletions.
84 changes: 84 additions & 0 deletions test/content-type.test.js
@@ -0,0 +1,84 @@
'use strict'

/* eslint n/no-deprecated-api: "off" */

const path = require('path')
const { test } = require('tap')
const simple = require('simple-get')
const Fastify = require('fastify')

const fastifyStatic = require('../')

test('register /content-type', t => {
t.plan(6)

const pluginOptions = {
root: path.join(__dirname, '/content-type'),
prefix: '/content-type'
}
const fastify = Fastify()
fastify.register(fastifyStatic, pluginOptions)

t.teardown(fastify.close.bind(fastify))

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

fastify.server.unref()

t.test('/content-type/index.html', (t) => {
t.plan(2)
simple.concat({
method: 'GET',
url: 'http://localhost:' + fastify.server.address().port + '/content-type/index.html'
}, (err, response) => {
t.error(err)
t.equal(response.headers['content-type'], 'text/html; charset=UTF-8')
})
})

t.test('/content-type/index.css', (t) => {
t.plan(2)
simple.concat({
method: 'GET',
url: 'http://localhost:' + fastify.server.address().port + '/content-type/index.css'
}, (err, response) => {
t.error(err)
t.equal(response.headers['content-type'], 'text/css; charset=UTF-8')
})
})

t.test('/content-type/sample.jpg', (t) => {
t.plan(2)
simple.concat({
method: 'GET',
url: 'http://localhost:' + fastify.server.address().port + '/content-type/sample.jpg'
}, (err, response) => {
t.error(err)
t.equal(response.headers['content-type'], 'image/jpeg')
})
})

t.test('/content-type/test.txt', (t) => {
t.plan(2)
simple.concat({
method: 'GET',
url: 'http://localhost:' + fastify.server.address().port + '/content-type/test.txt'
}, (err, response) => {
t.error(err)
t.equal(response.headers['content-type'], 'text/plain; charset=UTF-8')
})
})

t.test('/content-type/binary', (t) => {
t.plan(2)
simple.concat({
method: 'GET',
url: 'http://localhost:' + fastify.server.address().port + '/content-type/binary'
}, (err, response) => {
t.error(err)
t.equal(response.headers['content-type'], 'application/octet-stream')
})
})
})
})
Empty file added test/content-type/binary
Empty file.
Empty file added test/content-type/index.css
Empty file.
5 changes: 5 additions & 0 deletions test/content-type/index.html
@@ -0,0 +1,5 @@
<html>
<body>
the body
</body>
</html>
Binary file added test/content-type/sample.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file added test/content-type/test.txt
Empty file.

0 comments on commit 4077f78

Please sign in to comment.