Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add flag to avoid trailing slash in prefix #123

Merged
merged 6 commits into from
Apr 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ Default: `'/'`

A URL path prefix used to create a virtual mount path for the static directory.

### `prefixAvoidTrailingSlash`

Default: `false`

If set to false prefix will get trailing "/" at the end. If set to true, prefix will not append "/" to prefix.

#### `schemaHide`

Default: `true`
Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ declare function fastifyStatic(): fastify.Plugin<
{
root: string;
prefix?: string;
prefixAvoidTrailingSlash?: boolean;
serve?: boolean;
decorateReply?: boolean;
schemaHide?: boolean;
Expand Down
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,12 @@ function fastifyStatic (fastify, opts, next) {
}

if (opts.prefix === undefined) opts.prefix = '/'
const prefix = opts.prefix[opts.prefix.length - 1] === '/' ? opts.prefix : (opts.prefix + '/')

let prefix = opts.prefix

if (!opts.prefixAvoidTrailingSlash) {
prefix = opts.prefix[opts.prefix.length - 1] === '/' ? opts.prefix : (opts.prefix + '/')
}
// Set the schema hide property if defined in opts or true by default
const schema = { schema: { hide: typeof opts.schemaHide !== 'undefined' ? opts.schemaHide : true } }

Expand Down
148 changes: 148 additions & 0 deletions test/static.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,154 @@ function genericErrorResponseChecks (t, response) {
t.ok(response.headers.date)
}

t.test('register /static prefixAvoidTrailingSlash', t => {
t.plan(11)

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

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

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

fastify.server.unref()

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

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

t.test('/static/', t => {
t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT)
simple.concat({
method: 'GET',
url: 'http://localhost:' + fastify.server.address().port + '/static/'
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 200)
t.strictEqual(body.toString(), indexContent)
genericResponseChecks(t, response)
})
})

t.test('/static', t => {
t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT)
simple.concat({
method: 'GET',
url: 'http://localhost:' + fastify.server.address().port + '/static'
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 200)
t.strictEqual(body.toString(), indexContent)
genericResponseChecks(t, response)
})
})

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

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

t.test('/static/this/path/for/test', t => {
t.plan(2 + GENERIC_ERROR_RESPONSE_CHECK_COUNT)
simple.concat({
method: 'GET',
url: 'http://localhost:' + fastify.server.address().port + '/static/this/path/for/test',
followRedirect: false
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 404)
genericErrorResponseChecks(t, response)
})
})

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

t.test('/static/../index.js', t => {
t.plan(2 + GENERIC_ERROR_RESPONSE_CHECK_COUNT)
simple.concat({
method: 'GET',
url: 'http://localhost:' + fastify.server.address().port + '/static/../index.js',
followRedirect: false
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 403)
genericErrorResponseChecks(t, response)
})
})

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

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

Expand Down
1 change: 1 addition & 0 deletions test/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const options = {
lastModified: true,
maxAge: '',
prefix: '',
prefixAvoidTrailingSlash: false,
root: '',
schemaHide: true,
serve: true,
Expand Down