@@ -186,6 +186,206 @@ describe('WebSocket message', () => {
186
186
await wsClosed ( ws )
187
187
app . stop ( )
188
188
} )
189
+
190
+ it ( 'should parse objects' , async ( ) => {
191
+ const app = new Elysia ( )
192
+ . ws ( '/ws' , {
193
+ message ( ws , raw ) {
194
+ ws . send ( raw )
195
+ }
196
+ } )
197
+ . listen ( 0 )
198
+
199
+ const ws = newWebsocket ( app . server ! )
200
+
201
+ await wsOpen ( ws )
202
+
203
+ const message = wsMessage ( ws )
204
+
205
+ ws . send ( JSON . stringify ( { message : 'Hello!' } ) )
206
+
207
+ const { type, data } = await message
208
+ expect ( type ) . toBe ( 'message' )
209
+ expect ( data ) . toInclude ( '{"message":"Hello!"}' )
210
+
211
+ await wsClosed ( ws )
212
+ app . stop ( )
213
+ } )
214
+
215
+ it ( 'should parse arrays' , async ( ) => {
216
+ const app = new Elysia ( )
217
+ . ws ( '/ws' , {
218
+ message ( ws , raw ) {
219
+ ws . send ( JSON . stringify ( raw ) )
220
+ }
221
+ } )
222
+ . listen ( 0 )
223
+
224
+ const ws = newWebsocket ( app . server ! )
225
+
226
+ await wsOpen ( ws )
227
+
228
+ const message = wsMessage ( ws )
229
+
230
+ ws . send ( JSON . stringify ( [ { message : 'Hello!' } ] ) )
231
+
232
+ const { type, data } = await message
233
+ expect ( type ) . toBe ( 'message' )
234
+ expect ( data ) . toInclude ( '[{"message":"Hello!"}]' )
235
+
236
+ await wsClosed ( ws )
237
+ app . stop ( )
238
+ } )
239
+
240
+ it ( 'should parse strings' , async ( ) => {
241
+ const app = new Elysia ( )
242
+ . ws ( '/ws' , {
243
+ message ( ws , raw ) {
244
+ ws . send ( JSON . stringify ( raw ) )
245
+ }
246
+ } )
247
+ . listen ( 0 )
248
+
249
+ const ws = newWebsocket ( app . server ! )
250
+
251
+ await wsOpen ( ws )
252
+
253
+ const message = wsMessage ( ws )
254
+
255
+ ws . send ( JSON . stringify ( "Hello!" ) )
256
+
257
+ const { type, data } = await message
258
+ expect ( type ) . toBe ( 'message' )
259
+ expect ( data ) . toInclude ( '"Hello!"' )
260
+
261
+ await wsClosed ( ws )
262
+ app . stop ( )
263
+ } )
264
+
265
+ it ( 'should parse numbers' , async ( ) => {
266
+ const app = new Elysia ( )
267
+ . ws ( '/ws' , {
268
+ message ( ws , raw ) {
269
+ ws . send ( JSON . stringify ( raw ) )
270
+ }
271
+ } )
272
+ . listen ( 0 )
273
+
274
+ const ws = newWebsocket ( app . server ! )
275
+
276
+ await wsOpen ( ws )
277
+
278
+ const message = wsMessage ( ws )
279
+
280
+ ws . send ( JSON . stringify ( 1234567890 ) )
281
+
282
+ const { type, data } = await message
283
+ expect ( type ) . toBe ( 'message' )
284
+ expect ( data ) . toInclude ( '1234567890' )
285
+
286
+ await wsClosed ( ws )
287
+ app . stop ( )
288
+ } )
289
+
290
+ it ( 'should parse true' , async ( ) => {
291
+ const app = new Elysia ( )
292
+ . ws ( '/ws' , {
293
+ message ( ws , raw ) {
294
+ ws . send ( JSON . stringify ( raw ) )
295
+ }
296
+ } )
297
+ . listen ( 0 )
298
+
299
+ const ws = newWebsocket ( app . server ! )
300
+
301
+ await wsOpen ( ws )
302
+
303
+ const message = wsMessage ( ws )
304
+
305
+ ws . send ( JSON . stringify ( true ) )
306
+
307
+ const { type, data } = await message
308
+ expect ( type ) . toBe ( 'message' )
309
+ expect ( data ) . toInclude ( 'true' )
310
+
311
+ await wsClosed ( ws )
312
+ app . stop ( )
313
+ } )
314
+
315
+ it ( 'should parse false' , async ( ) => {
316
+ const app = new Elysia ( )
317
+ . ws ( '/ws' , {
318
+ message ( ws , raw ) {
319
+ ws . send ( JSON . stringify ( raw ) )
320
+ }
321
+ } )
322
+ . listen ( 0 )
323
+
324
+ const ws = newWebsocket ( app . server ! )
325
+
326
+ await wsOpen ( ws )
327
+
328
+ const message = wsMessage ( ws )
329
+
330
+ ws . send ( JSON . stringify ( false ) )
331
+
332
+ const { type, data } = await message
333
+ expect ( type ) . toBe ( 'message' )
334
+ expect ( data ) . toInclude ( 'false' )
335
+
336
+ await wsClosed ( ws )
337
+ app . stop ( )
338
+ } )
339
+
340
+ it ( 'should parse null' , async ( ) => {
341
+ const app = new Elysia ( )
342
+ . ws ( '/ws' , {
343
+ message ( ws , raw ) {
344
+ ws . send ( JSON . stringify ( raw ) )
345
+ }
346
+ } )
347
+ . listen ( 0 )
348
+
349
+ const ws = newWebsocket ( app . server ! )
350
+
351
+ await wsOpen ( ws )
352
+
353
+ const message = wsMessage ( ws )
354
+
355
+ ws . send ( JSON . stringify ( null ) )
356
+
357
+ const { type, data } = await message
358
+ expect ( type ) . toBe ( 'message' )
359
+ expect ( data ) . toInclude ( 'null' )
360
+
361
+ await wsClosed ( ws )
362
+ app . stop ( )
363
+ } )
364
+
365
+ it ( 'should parse not parse /hello' , async ( ) => {
366
+ const app = new Elysia ( )
367
+ . ws ( '/ws' , {
368
+ message ( ws , raw ) {
369
+ ws . send ( JSON . stringify ( raw ) )
370
+ }
371
+ } )
372
+ . listen ( 0 )
373
+
374
+ const ws = newWebsocket ( app . server ! )
375
+
376
+ await wsOpen ( ws )
377
+
378
+ const message = wsMessage ( ws )
379
+
380
+ ws . send ( JSON . stringify ( "/hello" ) )
381
+
382
+ const { type, data } = await message
383
+ expect ( type ) . toBe ( 'message' )
384
+ expect ( data ) . toInclude ( '/hello' )
385
+
386
+ await wsClosed ( ws )
387
+ app . stop ( )
388
+ } )
189
389
190
390
it ( 'should send from plugin' , async ( ) => {
191
391
const plugin = new Elysia ( ) . ws ( '/ws' , {
0 commit comments