@@ -45,13 +45,9 @@ type RequestCallback = (
4545 } ,
4646) => void
4747const mockHttpsRequest = vi . hoisted ( ( ) => vi . fn ( ) )
48- const mockHttpRequest = vi . hoisted ( ( ) => vi . fn ( ) )
4948const MockHttpsAgent = vi . hoisted ( ( ) =>
5049 vi . fn ( ) . mockImplementation ( opts => ( { ...opts , _isHttpsAgent : true } ) ) ,
5150)
52- vi . mock ( 'node:http' , ( ) => ( {
53- request : mockHttpRequest ,
54- } ) )
5551vi . mock ( 'node:https' , ( ) => ( {
5652 Agent : MockHttpsAgent ,
5753 request : mockHttpsRequest ,
@@ -160,48 +156,6 @@ describe('apiFetch with extra CA certificates', () => {
160156 expect ( result . ok ) . toBe ( true )
161157 } )
162158
163- it ( 'should use http.request for plain HTTP API URLs' , async ( ) => {
164- const mockReq = {
165- end : vi . fn ( ) ,
166- on : vi . fn ( ) ,
167- write : vi . fn ( ) ,
168- }
169-
170- mockHttpRequest . mockImplementation (
171- ( _url : string , _opts : unknown , callback : RequestCallback ) => {
172- setTimeout ( ( ) => {
173- const mockRes = {
174- headers : { 'content-type' : 'text/plain' } ,
175- on : vi . fn ( ) ,
176- statusCode : 200 ,
177- statusMessage : 'OK' ,
178- }
179- const handlers : Record < string , Function > = { }
180- mockRes . on . mockImplementation ( ( event : string , handler : Function ) => {
181- handlers [ event ] = handler
182- return mockRes
183- } )
184- callback ( mockRes )
185- handlers [ 'data' ] ?.( Buffer . from ( 'local response' ) )
186- handlers [ 'end' ] ?.( )
187- } , 0 )
188- return mockReq
189- } ,
190- )
191-
192- const { apiFetch } = await import ( './api.mts' )
193- const response = await apiFetch ( 'http://localhost:3000/v0/report' )
194-
195- expect ( response . status ) . toBe ( 200 )
196- expect ( await response . text ( ) ) . toBe ( 'local response' )
197- expect ( mockHttpRequest ) . toHaveBeenCalledWith (
198- 'http://localhost:3000/v0/report' ,
199- expect . objectContaining ( { agent : undefined , method : 'GET' } ) ,
200- expect . any ( Function ) ,
201- )
202- expect ( mockHttpsRequest ) . not . toHaveBeenCalled ( )
203- } )
204-
205159 it ( 'should use https.request when extra CA certs are available' , async ( ) => {
206160 const caCerts = [ 'ROOT_CERT' , 'EXTRA_CERT' ]
207161 mockGetExtraCaCerts . mockReturnValue ( caCerts )
0 commit comments