Skip to content

Commit d26e6ee

Browse files
committed
Currect the behavior if root is set and path is /favicon.ico
1 parent 8f15053 commit d26e6ee

File tree

2 files changed

+24
-24
lines changed

2 files changed

+24
-24
lines changed

src/serve-static.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,12 @@ export const serveStatic = <E extends Env = any>(
8686
const requestPath = options.rewriteRequestPath
8787
? options.rewriteRequestPath(filename, c)
8888
: filename
89-
let path: string
90-
91-
if (optionPath) {
92-
// Use path option directly if specified
93-
path = resolve(root, optionPath)
94-
} else {
95-
// Build with root + requestPath
96-
path = resolve(join(root, requestPath))
97-
}
89+
90+
let path = optionPath
91+
? options.root
92+
? resolve(join(root, optionPath))
93+
: optionPath
94+
: resolve(join(root, requestPath))
9895

9996
let stats = getStats(path)
10097

test/serve-static.test.ts

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -269,23 +269,26 @@ describe('Serve Static Middleware', () => {
269269
path.join(__dirname, 'assets'),
270270
__dirname + path.sep + '..' + path.sep + 'test' + path.sep + 'assets',
271271
]
272+
const optionPaths = ['favicon.ico', '/favicon.ico']
272273
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',
274+
optionPaths.forEach((optionPath) => {
275+
describe(`${root} + ${optionPath}`, () => {
276+
const app = new Hono()
277+
const server = createAdaptorServer(app)
278+
279+
app.use(
280+
'/favicon.ico',
281+
serveStatic({
282+
root,
283+
path: optionPath,
284+
})
285+
)
286+
287+
it('Should return 200 response if both root and path set', async () => {
288+
const res = await request(server).get('/favicon.ico')
289+
expect(res.status).toBe(200)
290+
expect(res.headers['content-type']).toBe('image/x-icon')
282291
})
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')
289292
})
290293
})
291294
})

0 commit comments

Comments
 (0)