11import type { GenericApp } from '../../vitest-environment.ts'
22
33export async function createFetchForH3V1 ( ) {
4- const [ { createApp, toNodeListener } , { fetchNodeRequestHandler } ] = await Promise . all ( [
5- // @ts -expect-error resolved to the project's h3 by the vitest config
6- import ( '#nuxt-test-utils/h3' ) as Promise < typeof import ( 'h3' ) > ,
7- import ( 'node-mock-http' ) ,
8- ] )
4+ // @ts -expect-error resolved to the project's h3 by the vitest config
5+ const { createApp, toWebHandler } = await ( import ( '#nuxt-test-utils/h3' ) as Promise < typeof import ( 'h3' ) > )
96
107 const h3App = createApp ( )
11- const nodeHandler = toNodeListener ( h3App )
8+ const webHandler = toWebHandler ( h3App )
129
1310 const registry = new Set < string > ( )
1411 const _fetch = fetch
@@ -36,8 +33,7 @@ export async function createFetchForH3V1() {
3633 url = '/_' + url
3734 }
3835 if ( url . startsWith ( '/' ) ) {
39- const response = await fetchNodeRequestHandler ( nodeHandler , url , init )
40- return normalizeFetchResponse ( response )
36+ return webHandler ( new Request ( new URL ( url , 'http://localhost' ) , init ) )
4137 }
4238 return _fetch ( input , _init )
4339 } ) as typeof fetch
@@ -48,39 +44,3 @@ export async function createFetchForH3V1() {
4844 fetch : h3Fetch ,
4945 }
5046}
51-
52- /** utils from nitro */
53-
54- function normalizeFetchResponse ( response : Response ) {
55- if ( ! response . headers . has ( 'set-cookie' ) ) {
56- return response
57- }
58- return new Response ( response . body , {
59- status : response . status ,
60- statusText : response . statusText ,
61- headers : normalizeCookieHeaders ( response . headers ) ,
62- } )
63- }
64-
65- function normalizeCookieHeader ( header : number | string | string [ ] = '' ) {
66- return splitCookiesString ( joinHeaders ( header ) )
67- }
68-
69- function normalizeCookieHeaders ( headers : Headers ) {
70- const outgoingHeaders = new Headers ( )
71- for ( const [ name , header ] of headers ) {
72- if ( name === 'set-cookie' ) {
73- for ( const cookie of normalizeCookieHeader ( header ) ) {
74- outgoingHeaders . append ( 'set-cookie' , cookie )
75- }
76- }
77- else {
78- outgoingHeaders . set ( name , joinHeaders ( header ) )
79- }
80- }
81- return outgoingHeaders
82- }
83-
84- function joinHeaders ( value : number | string | string [ ] ) {
85- return Array . isArray ( value ) ? value . join ( ', ' ) : String ( value )
86- }
0 commit comments