Skip to content

Commit

Permalink
Merge pull request fastify#44 from fastify/correctly-run-without-prefix
Browse files Browse the repository at this point in the history
Do not add / route. Updated dependencies
  • Loading branch information
allevo authored Mar 28, 2018
2 parents 044e014 + 18bd277 commit c0e93cd
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 5 deletions.
4 changes: 0 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,6 @@ function fastifyStatic (fastify, opts, next) {
pumpSendToReply(req, reply, '/' + req.params['*'])
})

fastify.get(prefix, function (req, reply) {
pumpSendToReply(req, reply, '/')
})

fastify.decorateReply('sendFile', function (filePath) {
pumpSendToReply(this.request, this, filePath)
})
Expand Down
135 changes: 134 additions & 1 deletion test/static.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function genericErrorResponseChecks (t, response) {
}

t.test('register /static', t => {
t.plan(9)
t.plan(10)

const pluginOptions = {
root: path.join(__dirname, '/static'),
Expand All @@ -37,6 +37,8 @@ t.test('register /static', t => {
const fastify = require('fastify')()
fastify.register(fastifyStatic, pluginOptions)

t.tearDown(fastify.close.bind(fastify))

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

Expand Down Expand Up @@ -142,6 +144,18 @@ t.test('register /static', t => {
genericErrorResponseChecks(t, response)
})
})

t.test('file not exposed outside of the plugin', t => {
t.plan(2)
request.get({
method: 'GET',
// foobar is in static
uri: 'http://localhost:' + fastify.server.address().port + '/foobar.html'
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 404)
})
})
})
})

Expand All @@ -155,6 +169,8 @@ t.test('register /static/', t => {
const fastify = require('fastify')()
fastify.register(fastifyStatic, pluginOptions)

t.tearDown(fastify.close.bind(fastify))

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

Expand Down Expand Up @@ -277,6 +293,8 @@ t.test('error responses can be customized with fastify.setErrorHandler()', t =>

fastify.register(fastifyStatic, pluginOptions)

t.tearDown(fastify.close.bind(fastify))

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

Expand Down Expand Up @@ -313,6 +331,8 @@ t.test('not found responses can be customized with fastify.setNotFoundHandler()'

fastify.register(fastifyStatic, pluginOptions)

t.tearDown(fastify.close.bind(fastify))

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

Expand Down Expand Up @@ -425,6 +445,8 @@ t.test('setHeaders option', t => {
const fastify = require('fastify')()
fastify.register(fastifyStatic, pluginOptions)

t.tearDown(fastify.close.bind(fastify))

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

Expand Down Expand Up @@ -507,3 +529,114 @@ t.test('errors', t => {
})
})
})

t.test('register no prefix', t => {
t.plan(8)

const pluginOptions = {
root: path.join(__dirname, '/static')
}
const fastify = require('fastify')()
fastify.register(fastifyStatic, pluginOptions)

fastify.get('/', (request, reply) => {
reply.send({ hello: 'world' })
})

t.tearDown(fastify.close.bind(fastify))

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

fastify.server.unref()

t.test('/index.html', t => {
t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT)
request.get({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port + '/index.html'
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 200)
t.strictEqual(body, indexContent)
genericResponseChecks(t, response)
})
})

t.test('/index.css', t => {
t.plan(2 + GENERIC_RESPONSE_CHECK_COUNT)
request.get({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port + '/index.css'
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 200)
genericResponseChecks(t, response)
})
})

t.test('/', t => {
t.plan(3)
request.get({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 200)
t.deepEqual(JSON.parse(body), { hello: 'world' })
})
})

t.test('/deep/path/for/test/purpose/foo.html', t => {
t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT)
request.get({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port + '/deep/path/for/test/purpose/foo.html'
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 200)
t.strictEqual(body, deepContent)
genericResponseChecks(t, response)
})
})

t.test('/deep/path/for/test/', t => {
t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT)
request.get({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port + '/deep/path/for/test/'
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 200)
t.strictEqual(body, innerIndex)
genericResponseChecks(t, response)
})
})

t.test('/this/path/doesnt/exist.html', t => {
t.plan(2 + GENERIC_ERROR_RESPONSE_CHECK_COUNT)
request.get({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port + '/this/path/doesnt/exist.html',
followRedirect: false
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 404)
genericErrorResponseChecks(t, response)
})
})

t.test('/../index.js', t => {
t.plan(2 + GENERIC_ERROR_RESPONSE_CHECK_COUNT)
request.get({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port + '/../index.js',
followRedirect: false
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 403)
genericErrorResponseChecks(t, response)
})
})
})
})
3 changes: 3 additions & 0 deletions test/static/foobar.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<html>
<body>foobar</body>
</html>

0 comments on commit c0e93cd

Please sign in to comment.