@@ -11,20 +11,21 @@ const cookieValue = 'foo=bar'
1111const subprotocolValue = 'foo-subprotocol'
1212
1313test ( 'basic websocket proxy' , async ( t ) => {
14- t . plan ( 4 )
14+ t . plan ( 7 )
1515
1616 const origin = createServer ( )
1717 const wss = new WebSocket . Server ( { server : origin } )
1818 t . teardown ( wss . close . bind ( wss ) )
1919 t . teardown ( origin . close . bind ( origin ) )
2020
21+ const serverMessages = [ ]
2122 wss . on ( 'connection' , ( ws , request ) => {
2223 t . equal ( ws . protocol , subprotocolValue )
2324 t . equal ( request . headers . cookie , cookieValue )
24- ws . on ( 'message' , ( message ) => {
25- t . equal ( message . toString ( ) , 'hello' )
25+ ws . on ( 'message' , ( message , binary ) => {
26+ serverMessages . push ( [ message . toString ( ) , binary ] )
2627 // echo
27- ws . send ( message )
28+ ws . send ( message , { binary } )
2829 } )
2930 } )
3031
@@ -41,15 +42,22 @@ test('basic websocket proxy', async (t) => {
4142
4243 const options = { headers : { cookie : cookieValue } }
4344 const ws = new WebSocket ( `ws://localhost:${ server . server . address ( ) . port } ` , [ subprotocolValue ] , options )
44-
4545 await once ( ws , 'open' )
4646
47- const stream = WebSocket . createWebSocketStream ( ws )
47+ ws . send ( 'hello' , { binary : false } )
48+ const [ reply0 , binary0 ] = await once ( ws , 'message' )
49+ t . equal ( reply0 . toString ( ) , 'hello' )
50+ t . equal ( binary0 , false )
4851
49- stream . write ( 'hello' )
52+ ws . send ( Buffer . from ( 'fastify' ) , { binary : true } )
53+ const [ reply1 , binary1 ] = await once ( ws , 'message' )
54+ t . equal ( reply1 . toString ( ) , 'fastify' )
55+ t . equal ( binary1 , true )
5056
51- const [ buf ] = await once ( stream , 'data' )
52- t . equal ( buf . toString ( ) , 'hello' )
57+ t . strictSame ( serverMessages , [
58+ [ 'hello' , false ] ,
59+ [ 'fastify' , true ]
60+ ] )
5361
5462 await Promise . all ( [
5563 once ( ws , 'close' ) ,
0 commit comments