@@ -11,20 +11,21 @@ const cookieValue = 'foo=bar'
11
11
const subprotocolValue = 'foo-subprotocol'
12
12
13
13
test ( 'basic websocket proxy' , async ( t ) => {
14
- t . plan ( 4 )
14
+ t . plan ( 7 )
15
15
16
16
const origin = createServer ( )
17
17
const wss = new WebSocket . Server ( { server : origin } )
18
18
t . teardown ( wss . close . bind ( wss ) )
19
19
t . teardown ( origin . close . bind ( origin ) )
20
20
21
+ const serverMessages = [ ]
21
22
wss . on ( 'connection' , ( ws , request ) => {
22
23
t . equal ( ws . protocol , subprotocolValue )
23
24
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 ] )
26
27
// echo
27
- ws . send ( message )
28
+ ws . send ( message , { binary } )
28
29
} )
29
30
} )
30
31
@@ -41,15 +42,22 @@ test('basic websocket proxy', async (t) => {
41
42
42
43
const options = { headers : { cookie : cookieValue } }
43
44
const ws = new WebSocket ( `ws://localhost:${ server . server . address ( ) . port } ` , [ subprotocolValue ] , options )
44
-
45
45
await once ( ws , 'open' )
46
46
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 )
48
51
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 )
50
56
51
- const [ buf ] = await once ( stream , 'data' )
52
- t . equal ( buf . toString ( ) , 'hello' )
57
+ t . strictSame ( serverMessages , [
58
+ [ 'hello' , false ] ,
59
+ [ 'fastify' , true ]
60
+ ] )
53
61
54
62
await Promise . all ( [
55
63
once ( ws , 'close' ) ,
0 commit comments