@@ -305,4 +305,91 @@ describe('serverMode option', () => {
305
305
} ) ;
306
306
} ) ;
307
307
} ) ;
308
+
309
+ describe ( 'server' , ( ) => {
310
+ let MockSockJSServer ;
311
+ beforeEach ( ( done ) => {
312
+ jest . mock ( '../../lib/servers/SockJSServer' ) ;
313
+ // eslint-disable-next-line global-require
314
+ mockedTestServer = require ( '../helpers/test-server' ) ;
315
+ // eslint-disable-next-line global-require
316
+ MockSockJSServer = require ( '../../lib/servers/SockJSServer' ) ;
317
+
318
+ server = mockedTestServer . start (
319
+ config ,
320
+ {
321
+ port,
322
+ } ,
323
+ done
324
+ ) ;
325
+ } ) ;
326
+
327
+ afterEach ( ( done ) => {
328
+ mockedTestServer . close ( done ) ;
329
+ jest . resetAllMocks ( ) ;
330
+ jest . resetModules ( ) ;
331
+
332
+ server = null ;
333
+ } ) ;
334
+
335
+ it ( 'should use server implementation correctly' , ( ) => {
336
+ const mockServerInstance = MockSockJSServer . mock . instances [ 0 ] ;
337
+
338
+ const connectionObj = {
339
+ foo : 'bar' ,
340
+ } ;
341
+ // this simulates a client connecting to the server
342
+ mockServerInstance . onConnection . mock . calls [ 0 ] [ 0 ] ( connectionObj , {
343
+ host : `localhost:${ port } ` ,
344
+ origin : `http://localhost:${ port } ` ,
345
+ } ) ;
346
+
347
+ expect ( server . sockets . length ) . toEqual ( 1 ) ;
348
+ expect ( server . sockets ) . toMatchSnapshot ( ) ;
349
+
350
+ // this simulates a client leaving the server
351
+ mockServerInstance . onConnectionClose . mock . calls [ 0 ] [ 1 ] ( connectionObj ) ;
352
+
353
+ expect ( server . sockets . length ) . toEqual ( 0 ) ;
354
+
355
+ // check that the dev server was passed to the socket server implementation constructor
356
+ expect ( MockSockJSServer . mock . calls [ 0 ] . length ) . toEqual ( 1 ) ;
357
+ expect ( MockSockJSServer . mock . calls [ 0 ] [ 0 ] . options . port ) . toEqual ( port ) ;
358
+
359
+ expect ( mockServerInstance . onConnection . mock . calls ) . toMatchSnapshot ( ) ;
360
+ expect ( mockServerInstance . send . mock . calls . length ) . toEqual ( 3 ) ;
361
+ // call 0 to the send() method is liveReload
362
+ expect ( mockServerInstance . send . mock . calls [ 0 ] ) . toMatchSnapshot ( ) ;
363
+ // call 1 to the send() method is hash data, so we skip it
364
+ // call 2 to the send() method is the "ok" message
365
+ expect ( mockServerInstance . send . mock . calls [ 2 ] ) . toMatchSnapshot ( ) ;
366
+ // close should not be called because the server never forcefully closes
367
+ // a successful client connection
368
+ expect ( mockServerInstance . close . mock . calls . length ) . toEqual ( 0 ) ;
369
+ expect ( mockServerInstance . onConnectionClose . mock . calls ) . toMatchSnapshot ( ) ;
370
+ } ) ;
371
+
372
+ it ( 'should close client with bad headers' , ( ) => {
373
+ const mockServerInstance = MockSockJSServer . mock . instances [ 0 ] ;
374
+
375
+ // this simulates a client connecting to the server
376
+ mockServerInstance . onConnection . mock . calls [ 0 ] [ 0 ] (
377
+ {
378
+ foo : 'bar' ,
379
+ } ,
380
+ {
381
+ host : null ,
382
+ }
383
+ ) ;
384
+ expect ( server . sockets . length ) . toEqual ( 0 ) ;
385
+ expect ( MockSockJSServer . mock . calls [ 0 ] . length ) . toEqual ( 1 ) ;
386
+ expect ( MockSockJSServer . mock . calls [ 0 ] [ 0 ] . options . port ) . toEqual ( port ) ;
387
+ expect ( mockServerInstance . onConnection . mock . calls ) . toMatchSnapshot ( ) ;
388
+ // the only call to send() here should be an invalid header message
389
+ expect ( mockServerInstance . send . mock . calls ) . toMatchSnapshot ( ) ;
390
+ expect ( mockServerInstance . close . mock . calls ) . toMatchSnapshot ( ) ;
391
+ // onConnectionClose should never get called since the client should be closed first
392
+ expect ( mockServerInstance . onConnectionClose . mock . calls . length ) . toEqual ( 0 ) ;
393
+ } ) ;
394
+ } ) ;
308
395
} ) ;
0 commit comments