@@ -144,3 +144,46 @@ test('Should throw on an invalid handle parameter', (t) => {
144144 t . equal ( err . message , 'invalid handle function' )
145145 } )
146146} )
147+
148+ test ( 'Should gracefully close with a connected client' , ( t ) => {
149+ t . plan ( 6 )
150+
151+ const fastify = Fastify ( )
152+
153+ fastify . register ( fastifyWebsocket , { handle } )
154+
155+ function handle ( connection ) {
156+ connection . setEncoding ( 'utf8' )
157+ connection . write ( 'hello client' )
158+
159+ connection . once ( 'data' , ( chunk ) => {
160+ t . equal ( chunk , 'hello server' )
161+ } )
162+
163+ connection . on ( 'end' , ( ) => {
164+ t . pass ( 'end emitted on server side' )
165+ } )
166+ // this connection stays alive untile we close the server
167+ }
168+
169+ fastify . listen ( 0 , ( err ) => {
170+ t . error ( err )
171+
172+ const ws = new WebSocket ( 'ws://localhost:' + fastify . server . address ( ) . port )
173+ const client = WebSocket . createWebSocketStream ( ws , { encoding : 'utf8' } )
174+
175+ client . setEncoding ( 'utf8' )
176+ client . write ( 'hello server' )
177+
178+ client . on ( 'end' , ( ) => {
179+ t . pass ( 'end emitted on client side' )
180+ } )
181+
182+ client . once ( 'data' , ( chunk ) => {
183+ t . equal ( chunk , 'hello client' )
184+ fastify . close ( function ( err ) {
185+ t . error ( err )
186+ } )
187+ } )
188+ } )
189+ } )
0 commit comments