|
1 | 1 | import { Hono } from 'hono' |
2 | 2 | import request from 'supertest' |
| 3 | +import { chmodSync, statSync } from 'node:fs' |
3 | 4 | import path from 'node:path' |
4 | 5 | import { serveStatic } from './../src/serve-static' |
5 | 6 | import { createAdaptorServer } from './../src/server' |
@@ -331,6 +332,34 @@ describe('Serve Static Middleware', () => { |
331 | 332 | expect(res.status).toBe(200) |
332 | 333 | }) |
333 | 334 | }) |
| 335 | + |
| 336 | + describe('Stream error handling', () => { |
| 337 | + const testFile = path.join(__dirname, 'assets', 'static', 'plain.txt') |
| 338 | + console.log(testFile) |
| 339 | + let originalMode: number |
| 340 | + |
| 341 | + beforeEach(() => { |
| 342 | + const stats = statSync(testFile) |
| 343 | + originalMode = stats.mode |
| 344 | + // Remove read permission to trigger stream error |
| 345 | + chmodSync(testFile, 0o000) |
| 346 | + }) |
| 347 | + |
| 348 | + afterEach(() => { |
| 349 | + chmodSync(testFile, originalMode) |
| 350 | + }) |
| 351 | + |
| 352 | + // Skip on Windows as chmod doesn't work for file permissions |
| 353 | + ;(process.platform === 'win32' ? it.skip : it)( |
| 354 | + 'Should handle read permission errors gracefully', |
| 355 | + async () => { |
| 356 | + const app = new Hono() |
| 357 | + app.use('/static/*', serveStatic({ root: './test/assets' })) |
| 358 | + const server = createAdaptorServer(app) |
| 359 | + await expect(request(server).get('/static/plain.txt')).rejects.toThrow() |
| 360 | + } |
| 361 | + ) |
| 362 | + }) |
334 | 363 | }) |
335 | 364 |
|
336 | 365 | describe('Serve Static Middleware with wrong path', () => { |
|
0 commit comments