wynd 0.4.3

A simple websocket library for rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
#[cfg(test)]
mod tests {
    use futures::{SinkExt, StreamExt};
    use std::net::SocketAddr;
    use std::sync::atomic::Ordering;
    use std::sync::{Arc, Mutex};
    use std::time::Duration;
    use tokio::net::TcpStream;
    use tokio::time::timeout;
    use tokio_tungstenite::{connect_async, tungstenite::Message};
    use url::Url;

    use crate::wynd::{Standalone, Wynd};

    /// Helper function to create a test server with basic handlers
    fn create_test_server() -> Wynd<Standalone> {
        let mut wynd: Wynd<Standalone> = Wynd::new();

        wynd.on_connection(|conn| async move {
            conn.on_open(|handle| async move {
                println!("Test connection {} opened", handle.id());
            })
            .await;

            conn.on_text(|msg, handle| async move {
                let _ = handle.send_text(&format!("Echo: {}", msg.data)).await;
            });
        });

        wynd.on_error(|err| async move {
            eprintln!("Test server error: {}", err);
        });

        wynd
    }

    #[test]
    fn test_wynd_creation() {
        let wynd: Wynd<Standalone> = Wynd::new();

        // Verify initial state
        assert!(wynd.connection_handler.is_none());
        assert!(wynd.error_handler.is_none());
        assert!(wynd.close_handler.is_none());
        assert_eq!(wynd.next_connection_id.load(Ordering::Relaxed), 0);

        // Default address should be 0.0.0.0:8080
        let expected_addr = SocketAddr::from(([0, 0, 0, 0], 8080));
        assert_eq!(wynd.addr, expected_addr);
    }

    #[test]
    fn test_connection_handler_registration() {
        let mut wynd: Wynd<Standalone> = Wynd::new();

        // Initially no handler
        assert!(wynd.connection_handler.is_none());

        // Register handler
        wynd.on_connection(|_conn| async move {
            // Test handler
        });

        // Handler should be registered
        assert!(wynd.connection_handler.is_some());
    }

    #[test]
    fn test_error_handler_registration() {
        let mut wynd: Wynd<Standalone> = Wynd::new();

        // Initially no handler
        assert!(wynd.error_handler.is_none());

        // Register handler
        wynd.on_error(|_err| async move {
            // Test error handler
        });

        // Handler should be registered
        assert!(wynd.error_handler.is_some());
    }

    #[test]
    fn test_close_handler_registration() {
        let mut wynd: Wynd<Standalone> = Wynd::new();

        // Initially no handler
        assert!(wynd.close_handler.is_none());

        // Register handler
        wynd.on_close(|| {
            // Test close handler
        });

        // Handler should be registered
        assert!(wynd.close_handler.is_some());
    }

    #[test]
    fn test_connection_id_counter() {
        let wynd: Wynd<Standalone> = Wynd::new();

        // Initial value
        assert_eq!(wynd.next_connection_id.load(Ordering::Relaxed), 0);

        // Simulate getting next ID
        let id1 = wynd.next_connection_id.fetch_add(1, Ordering::Relaxed);
        let id2 = wynd.next_connection_id.fetch_add(1, Ordering::Relaxed);
        let id3 = wynd.next_connection_id.fetch_add(1, Ordering::Relaxed);

        assert_eq!(id1, 0);
        assert_eq!(id2, 1);
        assert_eq!(id3, 2);
        assert_eq!(wynd.next_connection_id.load(Ordering::Relaxed), 3);
    }

    #[tokio::test]
    async fn test_server_startup_and_shutdown() {
        let wynd = create_test_server();
        let port = 8081; // Use different port to avoid conflicts

        // Test server startup with timeout to prevent hanging
        let server_future = wynd.listen(port, move || {
            println!("Test server started on port {}", port);
        });

        // Run server for a short time then cancel
        let result = timeout(Duration::from_millis(100), server_future).await;

        // Should timeout (which means server started successfully)
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_websocket_connection_and_echo() {
        let port = 8082;
        let connection_count = Arc::new(Mutex::new(0));
        let connection_count_clone = Arc::clone(&connection_count);

        let mut wynd: Wynd<Standalone> = Wynd::new();

        wynd.on_connection(move |conn| {
            let count_clone = Arc::clone(&connection_count_clone);
            async move {
                {
                    let mut count = count_clone.lock().unwrap();
                    *count += 1;
                }

                conn.on_open(|handle| async move {
                    println!("Connection {} opened", handle.id());
                })
                .await;

                conn.on_text(|msg, handle| async move {
                    let response = format!("Echo: {}", msg.data);
                    let _ = handle.send_text(&response).await;
                });
            }
        });

        // Start server in background
        let server_handle = tokio::spawn(async move {
            let _ = wynd
                .listen(port, || {
                    println!("Echo test server started");
                })
                .await;
        });

        // Give server time to start
        tokio::time::sleep(Duration::from_millis(50)).await;

        // Connect as client and test echo
        let url = Url::parse(&format!("ws://127.0.0.1:{}", port)).unwrap();
        if let Ok(ws_stream) = timeout(Duration::from_millis(1000), connect_async(url)).await {
            if let Ok(_) = ws_stream {
                // Send test message
                let test_message = "Hello WebSocket!";
                let (mut ws_stream, _) = ws_stream.unwrap();
                if ws_stream
                    .send(Message::Text(test_message.to_string()))
                    .await
                    .is_ok()
                {
                    // Read response
                    if let Ok(Some(response)) =
                        timeout(Duration::from_millis(500), ws_stream.next()).await
                    {
                        if let Ok(msg) = response {
                            if let Message::Text(text) = msg {
                                assert_eq!(text, format!("Echo: {}", test_message));
                            }
                        }
                    }
                }
            }

            // Check connection count
            let count = connection_count.lock().unwrap();
            assert_eq!(*count, 1);
        }

        // Clean up
        server_handle.abort();
    }

    #[tokio::test]
    async fn test_multiple_connections() {
        let port = 8083;
        let connection_count = Arc::new(Mutex::new(0));
        let connection_count_clone = Arc::clone(&connection_count);

        let mut wynd: Wynd<Standalone> = Wynd::new();

        wynd.on_connection(move |conn| {
            let count_clone = Arc::clone(&connection_count_clone);
            async move {
                {
                    let mut count = count_clone.lock().unwrap();
                    *count += 1;
                }

                conn.on_open(|handle| async move {
                    println!("Connection {} opened", handle.id());
                })
                .await;

                conn.on_text(|msg, handle| async move {
                    let _ = handle.send_text(&msg.data).await;
                });
            }
        });

        // Start server
        let server_handle = tokio::spawn(async move {
            let _ = wynd
                .listen(port, || {
                    println!("Multi-connection test server started");
                })
                .await;
        });

        // Give server time to start
        tokio::time::sleep(Duration::from_millis(50)).await;

        // Create multiple connections
        let mut connection_tasks = Vec::new();
        for i in 0..3 {
            let task = tokio::spawn(async move {
                let url = Url::parse(&format!("ws://127.0.0.1:{}", port)).unwrap();
                if let Ok(Ok((mut ws_stream, _))) =
                    timeout(Duration::from_millis(1000), connect_async(url)).await
                {
                    let message = format!("Message from connection {}", i);
                    let _ = ws_stream.send(Message::Text(message)).await;

                    // Read response
                    if let Ok(Some(_)) = timeout(Duration::from_millis(500), ws_stream.next()).await
                    {
                        return true;
                    }
                }
                false
            });
            connection_tasks.push(task);
        }

        // Wait for all connections to complete
        let mut successful_connections = 0;
        for task in connection_tasks {
            if let Ok(success) = task.await {
                if success {
                    successful_connections += 1;
                }
            }
        }

        // Give time for connection handlers to execute
        tokio::time::sleep(Duration::from_millis(100)).await;

        // Verify multiple connections were handled
        let count = connection_count.lock().unwrap();
        assert!(successful_connections > 0);
        assert!(*count >= successful_connections);

        // Clean up
        server_handle.abort();
    }

    #[tokio::test]
    async fn test_error_handling() {
        let error_count = Arc::new(Mutex::new(0));
        let error_count_clone = Arc::clone(&error_count);

        let mut wynd: Wynd<Standalone> = Wynd::new();

        wynd.on_error(move |_err| {
            let count_clone = Arc::clone(&error_count_clone);
            async move {
                let mut count = count_clone.lock().unwrap();
                *count += 1;
            }
        });

        wynd.on_connection(|conn| async move {
            conn.on_open(|_handle| async move {}).await;
        });

        // Force bind failure by holding the port open
        let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
        let port = listener.local_addr().unwrap().port();

        // listen() should fail immediately with EADDRINUSE
        let result = wynd.listen(port, || {}).await;
        assert!(result.is_err());
    }

    #[test]
    fn test_drop_behavior() {
        let close_called = Arc::new(Mutex::new(false));
        let close_called_clone = Arc::clone(&close_called);

        {
            let mut wynd: Wynd<Standalone> = Wynd::new();

            wynd.on_close(move || {
                let mut called = close_called_clone.lock().unwrap();
                *called = true;
            });

            // wynd will be dropped here
        }

        // Verify close handler was called
        let called = close_called.lock().unwrap();
        assert!(*called);
    }

    #[tokio::test]
    // TODO: Add test for connection timeout behavior once proper mocking infrastructure is in place

    #[cfg(feature = "with-ripress")]
    mod ripress_tests {
        use super::*;
        use crate::wynd::WithRipress;

        #[test]
        fn test_ripress_wynd_creation() {
            let wynd: Wynd<WithRipress> = Wynd::new();

            // Verify initial state is the same as Standalone
            assert!(wynd.connection_handler.is_none());
            assert!(wynd.error_handler.is_none());
            assert!(wynd.close_handler.is_none());
            assert_eq!(wynd.next_connection_id.load(Ordering::Relaxed), 0);
        }

        #[test]
        fn test_ripress_handler_creation() {
            let wynd: Wynd<WithRipress> = Wynd::new();

            // Test that handler can be created
            let _handler = wynd.handler();

            // If we get here without panic, handler creation works
            assert!(true);
        }

        #[tokio::test]
        async fn test_ripress_websocket_upgrade_detection() {
            let wynd: Wynd<WithRipress> = Wynd::new();
            let handler = wynd.handler();

            // Create a non-WebSocket request
            let req = hyper::Request::builder()
                .method("GET")
                .uri("/")
                .body(hyper::Body::empty())
                .unwrap();

            let response = handler(req).await.unwrap();

            // Should return 400 for non-WebSocket requests
            assert_eq!(response.status(), 400);
        }

        #[tokio::test]
        async fn test_ripress_websocket_upgrade_headers() {
            let wynd: Wynd<WithRipress> = Wynd::new();
            let handler = wynd.handler();

            // Create a request with WebSocket headers
            let req = hyper::Request::builder()
                .method("GET")
                .uri("/")
                .header("upgrade", "websocket")
                .header("sec-websocket-key", "dGhlIHNhbXBsZSBub25jZQ==")
                .header("sec-websocket-version", "13")
                .body(hyper::Body::empty())
                .unwrap();

            // This should attempt WebSocket upgrade
            // The actual upgrade will fail due to test environment limitations,
            // but we can verify it gets past the header checks
            let response = handler(req).await.unwrap();

            // Should fail during upgrade (not header check): expect 400 with specific body
            assert_eq!(response.status(), 400);
            let body = hyper::body::to_bytes(response.into_body()).await.unwrap();
            assert_eq!(&body[..], b"WebSocket upgrade failed");
        }
    }

    // Integration test helper functions
    mod helpers {
        use super::*;

        pub async fn wait_for_server_start(port: u16, max_attempts: u32) -> bool {
            for _ in 0..max_attempts {
                if let Ok(_) = TcpStream::connect(format!("127.0.0.1:{}", port)).await {
                    return true;
                }
                tokio::time::sleep(Duration::from_millis(10)).await;
            }
            false
        }

        pub fn get_available_port() -> u16 {
            use std::net::TcpListener;
            let listener = TcpListener::bind("127.0.0.1:0").unwrap();
            let port = listener.local_addr().unwrap().port();
            drop(listener);
            port
        }
    }

    #[tokio::test]
    async fn test_concurrent_connections() {
        use helpers::*;

        let port = get_available_port();
        let connection_ids = Arc::new(Mutex::new(Vec::new()));
        let ids_clone = Arc::clone(&connection_ids);

        let mut wynd: Wynd<Standalone> = Wynd::new();

        wynd.on_connection(move |conn| {
            let ids = Arc::clone(&ids_clone);
            async move {
                let conn_id = conn.id();
                {
                    let mut ids_vec = ids.lock().unwrap();
                    ids_vec.push(conn_id);
                }

                conn.on_open(|_handle| async move {}).await;

                conn.on_text(|msg, handle| async move {
                    let response = format!("Connection {}: {}", handle.id(), msg.data);
                    let _ = handle.send_text(&response).await;
                });
            }
        });

        // Start server
        let server_handle = tokio::spawn(async move {
            let _ = wynd.listen(port, || {}).await;
        });

        // Wait for server to start
        assert!(wait_for_server_start(port, 100).await);

        // Create multiple concurrent connections
        let connection_futures: Vec<_> = (0..5)
            .map(|i| {
                tokio::spawn(async move {
                    let url = Url::parse(&format!("ws://127.0.0.1:{}", port)).unwrap();
                    match connect_async(url).await {
                        Ok((mut ws_stream, _)) => {
                            let message = format!("Test message {}", i);
                            if ws_stream.send(Message::Text(message)).await.is_ok() {
                                if let Some(Ok(Message::Text(response))) = ws_stream.next().await {
                                    return Some(response);
                                }
                            }
                        }
                        Err(_) => {}
                    }
                    None
                })
            })
            .collect();

        // Wait for all connections
        let mut responses = Vec::new();
        for future in connection_futures {
            if let Ok(Some(response)) = future.await {
                responses.push(response);
            }
        }

        // Give time for connection handlers to complete
        tokio::time::sleep(Duration::from_millis(100)).await;

        // Verify we got responses and unique connection IDs were assigned
        assert!(responses.len() > 0);

        let ids = connection_ids.lock().unwrap();
        assert!(ids.len() >= responses.len());

        // Verify all connection IDs are unique
        let mut sorted_ids = ids.clone();
        sorted_ids.sort();
        sorted_ids.dedup();
        assert_eq!(sorted_ids.len(), ids.len());

        // Clean up
        server_handle.abort();
    }
}