Skip to content

Commit

Permalink
fix: disable root path check when serve is false
Browse files Browse the repository at this point in the history
  • Loading branch information
nimesh0505 committed Aug 30, 2024
1 parent 39e89ea commit 7efb216
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
8 changes: 6 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ send.mime.default_type = 'application/octet-stream'

async function fastifyStatic (fastify, opts) {
opts.root = normalizeRoot(opts.root)
checkRootPathForErrors(fastify, opts.root)
checkRootPathForErrors(fastify, opts.root, opts.serve);

Check failure on line 22 in index.js

View workflow job for this annotation

GitHub Actions / test / Lint Code

Extra semicolon

const setHeaders = opts.setHeaders
if (setHeaders !== undefined && typeof setHeaders !== 'function') {
Expand Down Expand Up @@ -408,7 +408,11 @@ function normalizeRoot (root) {
return root
}

function checkRootPathForErrors (fastify, rootPath) {
function checkRootPathForErrors(fastify, rootPath, serve = true) {

Check failure on line 411 in index.js

View workflow job for this annotation

GitHub Actions / test / Lint Code

Missing space before function parentheses
if (serve === false) {
return;

Check failure on line 413 in index.js

View workflow job for this annotation

GitHub Actions / test / Lint Code

Extra semicolon
}

if (rootPath === undefined) {
throw new Error('"root" option is required')
}
Expand Down
48 changes: 48 additions & 0 deletions test/static.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4023,3 +4023,51 @@ t.test('content-length in head route should not return zero when using wildcard'
})
})
})

t.test('serve: false disables root path check', (t) => {
t.plan(3)

const pluginOptions = {
root: path.join(__dirname, '/static'),
prefix: '/static',
serve: false
};

Check failure on line 4034 in test/static.test.js

View workflow job for this annotation

GitHub Actions / test / Lint Code

Extra semicolon
const fastify = Fastify();

Check failure on line 4035 in test/static.test.js

View workflow job for this annotation

GitHub Actions / test / Lint Code

Extra semicolon
fastify.register(fastifyStatic, pluginOptions);

Check failure on line 4036 in test/static.test.js

View workflow job for this annotation

GitHub Actions / test / Lint Code

Extra semicolon

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

Check failure on line 4038 in test/static.test.js

View workflow job for this annotation

GitHub Actions / test / Lint Code

Extra semicolon

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

Check failure on line 4041 in test/static.test.js

View workflow job for this annotation

GitHub Actions / test / Lint Code

Extra semicolon

fastify.server.unref();

Check failure on line 4043 in test/static.test.js

View workflow job for this annotation

GitHub Actions / test / Lint Code

Extra semicolon

t.test('/static/index.html', (t) => {
t.plan(2 + GENERIC_ERROR_RESPONSE_CHECK_COUNT);

Check failure on line 4046 in test/static.test.js

View workflow job for this annotation

GitHub Actions / test / Lint Code

Extra semicolon
simple.concat({
method: 'GET',
url: 'http://localhost:' + fastify.server.address().port + '/static/index.html'
}, (err, response, body) => {
t.error(err);
t.equal(response.statusCode, 404);
genericErrorResponseChecks(t, response);
t.end()
});
});

t.test('/static/deep/path/for/test/purpose/foo.html', (t) => {
t.plan(2 + GENERIC_ERROR_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.equal(response.statusCode, 404);
genericErrorResponseChecks(t, response);
t.end()
});
});

t.end()
});
});

0 comments on commit 7efb216

Please sign in to comment.