11import { getSnapManifest } from '@metamask/snaps-utils/test-utils' ;
22import fetch from 'cross-fetch' ;
33import { promises as fs } from 'fs' ;
4- import http , { IncomingMessage , Server , ServerResponse } from 'http' ;
5- import serveMiddleware from 'serve-handler' ;
4+ import { Server } from 'http' ;
65
76import { getAllowedPaths , getServer } from './server' ;
87import { getMockConfig } from '../test-utils' ;
@@ -201,32 +200,6 @@ describe('getServer', () => {
201200 await close ( ) ;
202201 } ) ;
203202
204- it ( 'calls the serve middleware for allowed files' , async ( ) => {
205- const config = getMockConfig ( 'webpack' , {
206- input : 'src/index.js' ,
207- server : {
208- root : '/foo' ,
209- port : 0 ,
210- } ,
211- } ) ;
212-
213- const server = getServer ( config ) ;
214- const { port, close } = await server . listen ( ) ;
215-
216- const response = await fetch ( `http://localhost:${ port } /snap.manifest.json` ) ;
217-
218- expect ( response . status ) . toBe ( 200 ) ;
219- expect ( await response . text ( ) ) . toBe ( '' ) ;
220-
221- expect ( serveMiddleware ) . toHaveBeenCalledWith (
222- expect . any ( IncomingMessage ) ,
223- expect . any ( ServerResponse ) ,
224- expect . objectContaining ( { public : expect . stringContaining ( 'foo' ) } ) ,
225- ) ;
226-
227- await close ( ) ;
228- } ) ;
229-
230203 it ( 'ignores query strings' , async ( ) => {
231204 const config = getMockConfig ( 'webpack' , {
232205 input : 'src/index.js' ,
@@ -244,13 +217,7 @@ describe('getServer', () => {
244217 ) ;
245218
246219 expect ( response . status ) . toBe ( 200 ) ;
247- expect ( await response . text ( ) ) . toBe ( '' ) ;
248-
249- expect ( serveMiddleware ) . toHaveBeenCalledWith (
250- expect . any ( IncomingMessage ) ,
251- expect . any ( ServerResponse ) ,
252- expect . objectContaining ( { public : expect . stringContaining ( 'foo' ) } ) ,
253- ) ;
220+ expect ( await response . text ( ) ) . toBe ( JSON . stringify ( getSnapManifest ( ) ) ) ;
254221
255222 await close ( ) ;
256223 } ) ;
@@ -267,11 +234,10 @@ describe('getServer', () => {
267234 const server = getServer ( config ) ;
268235 const { port, close } = await server . listen ( ) ;
269236
270- const response = await fetch ( `http://localhost:${ port } /` ) ;
237+ const response = await fetch ( `http://localhost:${ port } /.env ` ) ;
271238
272239 expect ( response . status ) . toBe ( 404 ) ;
273240 expect ( await response . text ( ) ) . toBe ( '' ) ;
274- expect ( serveMiddleware ) . not . toHaveBeenCalled ( ) ;
275241
276242 await close ( ) ;
277243 } ) ;
@@ -281,21 +247,19 @@ describe('getServer', () => {
281247 input : 'src/index.js' ,
282248 server : {
283249 root : '/foo' ,
284- port : 0 ,
250+ port : 13490 ,
285251 } ,
286252 } ) ;
287253
288- const createServer = jest . spyOn ( http , 'createServer' ) ;
289- const server = getServer ( config ) ;
290- const httpServer : Server = createServer . mock . results [ 0 ] . value ;
291-
292- jest . spyOn ( httpServer , 'listen' ) . mockImplementationOnce ( ( ) => {
293- throw new Error ( 'Address already in use.' ) ;
294- } ) ;
254+ const firstServer = getServer ( config ) ;
255+ const { close } = await firstServer . listen ( ) ;
295256
296- await expect ( server . listen ( ) ) . rejects . toThrow ( 'Address already in use.' ) ;
257+ const secondServer = getServer ( config ) ;
258+ await expect ( secondServer . listen ( ) ) . rejects . toThrow (
259+ 'listen EADDRINUSE: address already in use :::13490' ,
260+ ) ;
297261
298- httpServer . close ( ) ;
262+ await close ( ) ;
299263 } ) ;
300264
301265 it ( 'throws if the server fails to close' , async ( ) => {
@@ -307,18 +271,15 @@ describe('getServer', () => {
307271 } ,
308272 } ) ;
309273
310- const createServer = jest . spyOn ( http , 'createServer' ) ;
311274 const server = getServer ( config ) ;
312- const httpServer : Server = createServer . mock . results [ 0 ] . value ;
313275
276+ const { server : httpServer , close } = await server . listen ( ) ;
314277 // @ts -expect-error - Invalid mock.
315278 jest . spyOn ( httpServer , 'close' ) . mockImplementationOnce ( ( callback ) => {
316279 return callback ?.( new Error ( 'Failed to close server.' ) ) ;
317280 } ) ;
318281
319- const { close } = await server . listen ( ) ;
320282 await expect ( close ( ) ) . rejects . toThrow ( 'Failed to close server.' ) ;
321-
322283 httpServer . close ( ) ;
323284 } ) ;
324285} ) ;
0 commit comments