@@ -194,6 +194,74 @@ test('getUpstream', async (t) => {
194
194
] )
195
195
} )
196
196
197
+ test ( 'getUpstream with unset wsUpstream' , async ( t ) => {
198
+ t . plan ( 9 )
199
+
200
+ const origin = createServer ( )
201
+ const wss = new WebSocket . Server ( { server : origin } )
202
+ t . teardown ( wss . close . bind ( wss ) )
203
+ t . teardown ( origin . close . bind ( origin ) )
204
+
205
+ const serverMessages = [ ]
206
+ wss . on ( 'connection' , ( ws , request ) => {
207
+ t . equal ( ws . protocol , subprotocolValue )
208
+ t . equal ( request . headers . cookie , cookieValue )
209
+ ws . on ( 'message' , ( message , binary ) => {
210
+ serverMessages . push ( [ message . toString ( ) , binary ] )
211
+ // echo
212
+ ws . send ( message , { binary } )
213
+ } )
214
+ } )
215
+
216
+ await promisify ( origin . listen . bind ( origin ) ) ( { port : 0 , host : '127.0.0.1' } )
217
+
218
+ const server = Fastify ( )
219
+
220
+ let _req
221
+ server . server . on ( 'upgrade' , ( req ) => {
222
+ _req = req
223
+ } )
224
+
225
+ server . register ( proxy , {
226
+ wsUpstream : '' ,
227
+ replyOptions : {
228
+ getUpstream : function ( original ) {
229
+ t . not ( original , _req )
230
+ t . equal ( original . raw , _req )
231
+ return `http://127.0.0.1:${ origin . address ( ) . port } `
232
+ }
233
+ } ,
234
+ websocket : true
235
+ } )
236
+
237
+ await server . listen ( { port : 0 , host : '127.0.0.1' } )
238
+ t . teardown ( server . close . bind ( server ) )
239
+
240
+ const options = { headers : { cookie : cookieValue } }
241
+ const ws = new WebSocket ( `ws://127.0.0.1:${ server . server . address ( ) . port } ` , [ subprotocolValue ] , options )
242
+ await once ( ws , 'open' )
243
+
244
+ ws . send ( 'hello' , { binary : false } )
245
+ const [ reply0 , binary0 ] = await once ( ws , 'message' )
246
+ t . equal ( reply0 . toString ( ) , 'hello' )
247
+ t . equal ( binary0 , false )
248
+
249
+ ws . send ( Buffer . from ( 'fastify' ) , { binary : true } )
250
+ const [ reply1 , binary1 ] = await once ( ws , 'message' )
251
+ t . equal ( reply1 . toString ( ) , 'fastify' )
252
+ t . equal ( binary1 , true )
253
+
254
+ t . strictSame ( serverMessages , [
255
+ [ 'hello' , false ] ,
256
+ [ 'fastify' , true ]
257
+ ] )
258
+
259
+ await Promise . all ( [
260
+ once ( ws , 'close' ) ,
261
+ server . close ( )
262
+ ] )
263
+ } )
264
+
197
265
test ( 'websocket proxy trigger hooks' , async ( t ) => {
198
266
t . plan ( 8 )
199
267
0 commit comments