@@ -6,7 +6,7 @@ import { tmpdir } from 'node:os'
66import { join } from 'node:path'
77import { H3 , toNodeHandler } from 'h3'
88import { afterEach , describe , expect , it } from 'vitest'
9- import { serveStaticHandler , serveStaticNodeMiddleware } from './serve-static'
9+ import { mountStaticHandler , serveStaticHandler , serveStaticNodeMiddleware } from './serve-static'
1010
1111interface Fixture {
1212 dir : string
@@ -167,6 +167,36 @@ describe('serveStaticHandler', () => {
167167 } )
168168} )
169169
170+ describe ( 'mountStaticHandler' , ( ) => {
171+ it ( 'keeps static bases with overlapping prefixes isolated' , async ( ) => {
172+ const viteDir = makeTmp ( 'devframe-serve-vite-' )
173+ const vitestDir = makeTmp ( 'devframe-serve-vitest-' )
174+ writeFileSync ( join ( viteDir , 'index.html' ) , 'vite-index' , 'utf-8' )
175+ writeFileSync ( join ( viteDir , 'favicon.svg' ) , 'vite' , 'utf-8' )
176+ writeFileSync ( join ( vitestDir , 'favicon.svg' ) , 'vitest' , 'utf-8' )
177+
178+ const app = new H3 ( )
179+ mountStaticHandler ( app , '/__devtools-vite/' , viteDir )
180+ mountStaticHandler ( app , '/__devtools-vitest/' , vitestDir )
181+
182+ const viteBaseResponse = await app . request ( '/__devtools-vite' )
183+ const viteBaseSlashResponse = await app . request ( '/__devtools-vite/' )
184+ const viteResponse = await app . request ( '/__devtools-vite/favicon.svg' )
185+ const vitestResponse = await app . request ( '/__devtools-vitest/favicon.svg' )
186+ const adjacentResponse = await app . request ( '/__devtools-vite-extra/favicon.svg' )
187+
188+ expect ( viteBaseResponse . status ) . toBe ( 200 )
189+ expect ( await viteBaseResponse . text ( ) ) . toBe ( 'vite-index' )
190+ expect ( viteBaseSlashResponse . status ) . toBe ( 200 )
191+ expect ( await viteBaseSlashResponse . text ( ) ) . toBe ( 'vite-index' )
192+ expect ( viteResponse . status ) . toBe ( 200 )
193+ expect ( await viteResponse . text ( ) ) . toBe ( 'vite' )
194+ expect ( vitestResponse . status ) . toBe ( 200 )
195+ expect ( await vitestResponse . text ( ) ) . toBe ( 'vitest' )
196+ expect ( adjacentResponse . status ) . toBe ( 404 )
197+ } )
198+ } )
199+
170200describe ( 'serveStaticNodeMiddleware' , ( ) => {
171201 let fx : Fixture | undefined
172202
0 commit comments