Skip to content

Commit f6f5b90

Browse files
yusukebeusualoma
andcommitted
resolve if both root and path set
Co-authored-by: Taku Amano <[email protected]>
1 parent ff18658 commit f6f5b90

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/serve-static.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export const serveStatic = <E extends Env = any>(
9191

9292
if (optionPath) {
9393
// Use path option directly if specified
94-
path = resolve(optionPath)
94+
path = resolve(optionRoot, optionPath)
9595
} else {
9696
// Build with root + requestPath
9797
path = resolve(join(optionRoot, requestPath))

test/serve-static.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,34 @@ describe('Serve Static Middleware', () => {
263263
})
264264
})
265265

266+
describe('Root and path combination tests', () => {
267+
const rootPaths = [
268+
path.join(__dirname, 'assets'),
269+
path.join(__dirname, 'assets'),
270+
__dirname + path.sep + '..' + path.sep + 'test' + path.sep + 'assets',
271+
]
272+
rootPaths.forEach((root) => {
273+
describe(root, () => {
274+
const app = new Hono()
275+
const server = createAdaptorServer(app)
276+
277+
app.use(
278+
'/favicon.ico',
279+
serveStatic({
280+
root: './test/assets',
281+
path: 'favicon.ico',
282+
})
283+
)
284+
285+
it('Should return 200 response if both root and path set', async () => {
286+
const res = await request(server).get('/favicon.ico')
287+
expect(res.status).toBe(200)
288+
expect(res.headers['content-type']).toBe('image/x-icon')
289+
})
290+
})
291+
})
292+
})
293+
266294
describe('Security tests', () => {
267295
const app = new Hono()
268296
const server = createAdaptorServer(app)

0 commit comments

Comments
 (0)