From 43090f6f727365c25958b5d2a6e38008f1b2c921 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCrg=C3=BCn=20Day=C4=B1o=C4=9Flu?= Date: Sun, 16 Jul 2023 17:00:09 +0300 Subject: [PATCH 1/3] add tests for #234 --- test/static-encode/[...]/a .md | 1 + test/static.test.js | 35 ++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 test/static-encode/[...]/a .md diff --git a/test/static-encode/[...]/a .md b/test/static-encode/[...]/a .md new file mode 100644 index 0000000..96236f8 --- /dev/null +++ b/test/static-encode/[...]/a .md @@ -0,0 +1 @@ +example \ No newline at end of file diff --git a/test/static.test.js b/test/static.test.js index e8b2fd1..ee4f7f1 100644 --- a/test/static.test.js +++ b/test/static.test.js @@ -3756,6 +3756,7 @@ t.test( t.end() } ) + t.test( 'converts URL to path', async (t) => { @@ -3803,3 +3804,37 @@ t.test( t.same(response.body, foobarContent) } ) + +t.test( + 'serves files that have characters modified by encodeUri when wildcard is false', + async (t) => { + t.plan(4) + const pluginOptions = { + root: url.pathToFileURL(path.join(__dirname, '/static-encode')), + wildcard: false + } + + const fastify = Fastify() + + fastify.register(fastifyStatic, pluginOptions) + const response = await fastify.inject({ + method: 'GET', + url: '[...]/a .md', + headers: { + 'accept-encoding': '*, *' + } + }) + t.equal(response.statusCode, 200) + t.same(response.body, fs.readFileSync(path.join(__dirname, 'static-encode/[...]', 'a .md'), 'utf-8')) + + const response2 = await fastify.inject({ + method: 'GET', + url: '%5B...%5D/a%20.md', + headers: { + 'accept-encoding': '*, *' + } + }) + t.equal(response2.statusCode, 200) + t.same(response2.body, fs.readFileSync(path.join(__dirname, 'static-encode/[...]', 'a .md'), 'utf-8')) + } +) From 12b8a29f51fea9f5277d2eb07343052a905faf6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCrg=C3=BCn=20Day=C4=B1o=C4=9Flu?= Date: Sun, 16 Jul 2023 17:08:55 +0300 Subject: [PATCH 2/3] change description --- test/static.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/static.test.js b/test/static.test.js index ee4f7f1..1ee2c53 100644 --- a/test/static.test.js +++ b/test/static.test.js @@ -3806,7 +3806,7 @@ t.test( ) t.test( - 'serves files that have characters modified by encodeUri when wildcard is false', + 'serves files with paths that have characters modified by encodeUri when wildcard is false', async (t) => { t.plan(4) const pluginOptions = { From 34f2299d02fb525a7356d235b97c5dea9a60b2a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCrg=C3=BCn=20Day=C4=B1o=C4=9Flu?= Date: Sun, 16 Jul 2023 17:12:31 +0300 Subject: [PATCH 3/3] use variable to store file contents --- test/static.test.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/static.test.js b/test/static.test.js index 1ee2c53..8f86e6c 100644 --- a/test/static.test.js +++ b/test/static.test.js @@ -3808,6 +3808,8 @@ t.test( t.test( 'serves files with paths that have characters modified by encodeUri when wildcard is false', async (t) => { + const aContent = fs.readFileSync(path.join(__dirname, 'static-encode/[...]', 'a .md'), 'utf-8') + t.plan(4) const pluginOptions = { root: url.pathToFileURL(path.join(__dirname, '/static-encode')), @@ -3825,7 +3827,7 @@ t.test( } }) t.equal(response.statusCode, 200) - t.same(response.body, fs.readFileSync(path.join(__dirname, 'static-encode/[...]', 'a .md'), 'utf-8')) + t.same(response.body, aContent) const response2 = await fastify.inject({ method: 'GET', @@ -3835,6 +3837,6 @@ t.test( } }) t.equal(response2.statusCode, 200) - t.same(response2.body, fs.readFileSync(path.join(__dirname, 'static-encode/[...]', 'a .md'), 'utf-8')) + t.same(response2.body, aContent) } )