reinhardt-testkit 0.4.0-alpha.10

Core testing infrastructure for Reinhardt framework (no functional crate dependencies)
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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
//! WebSocket test client and utilities for integration testing
//!
//! Provides WebSocket test client for end-to-end WebSocket testing with
//! support for authentication, connection management, and message assertions.
//!
//! ## Usage Examples
//!
//! ### Basic WebSocket Connection
//!
//! ```rust,no_run
//! use reinhardt_testkit::websocket::WebSocketTestClient;
//! use rstest::*;
//!
//! #[rstest]
//! #[tokio::test]
//! async fn test_websocket_connection() {
//!     let client = WebSocketTestClient::connect("ws://localhost:8080/ws").await.unwrap();
//!     client.send_text("Hello").await.unwrap();
//!     let response = client.receive_text().await.unwrap();
//!     assert_eq!(response, "Hello");
//! }
//! ```
//!
//! ### WebSocket with Authentication
//!
//! ```rust,no_run
//! use reinhardt_testkit::websocket::WebSocketTestClient;
//!
//! #[tokio::test]
//! async fn test_websocket_auth() {
//!     let client = WebSocketTestClient::connect_with_token(
//!         "ws://localhost:8080/ws",
//!         "my-auth-token"
//!     ).await.unwrap();
//!     // ...
//! }
//! ```

use futures::{SinkExt, StreamExt};
use std::io::{Error as IoError, ErrorKind};
use tokio::time::{Duration, timeout};
use tokio_tungstenite::{
	MaybeTlsStream, WebSocketStream, connect_async,
	tungstenite::{Error as WsError, Message},
};

/// WebSocket test client for integration testing
///
/// Provides high-level API for WebSocket connection management, message sending/receiving,
/// and authentication.
pub struct WebSocketTestClient {
	/// WebSocket connection stream
	stream: WebSocketStream<MaybeTlsStream<tokio::net::TcpStream>>,
	/// WebSocket URL
	url: String,
}

impl WebSocketTestClient {
	/// Connect to WebSocket server
	///
	/// # Example
	/// ```rust,no_run
	/// use reinhardt_testkit::websocket::WebSocketTestClient;
	///
	/// #[tokio::test]
	/// async fn test_connect() {
	///     let client = WebSocketTestClient::connect("ws://localhost:8080/ws")
	///         .await
	///         .unwrap();
	/// }
	/// ```
	pub async fn connect(url: &str) -> Result<Self, WsError> {
		let (stream, _response) = connect_async(url).await?;
		Ok(Self {
			stream,
			url: url.to_string(),
		})
	}

	/// Connect to WebSocket server with Bearer token authentication
	///
	/// Adds `Authorization: Bearer <token>` header to the WebSocket handshake request.
	///
	/// # Example
	/// ```rust,no_run
	/// use reinhardt_testkit::websocket::WebSocketTestClient;
	///
	/// #[tokio::test]
	/// async fn test_auth() {
	///     let client = WebSocketTestClient::connect_with_token(
	///         "ws://localhost:8080/ws",
	///         "my-secret-token"
	///     )
	///     .await
	///     .unwrap();
	/// }
	/// ```
	pub async fn connect_with_token(url: &str, token: &str) -> Result<Self, WsError> {
		use tokio_tungstenite::tungstenite::http::Request;

		let request = Request::builder()
			.uri(url)
			.header("Authorization", format!("Bearer {}", token))
			.body(())
			.expect("Failed to build WebSocket request");

		let (stream, _response) = connect_async(request).await?;
		Ok(Self {
			stream,
			url: url.to_string(),
		})
	}

	/// Connect to WebSocket server with query parameter authentication
	///
	/// Appends `?token=<token>` to the URL.
	///
	/// # Example
	/// ```rust,no_run
	/// use reinhardt_testkit::websocket::WebSocketTestClient;
	///
	/// #[tokio::test]
	/// async fn test_query_auth() {
	///     let client = WebSocketTestClient::connect_with_query_token(
	///         "ws://localhost:8080/ws",
	///         "my-token"
	///     )
	///     .await
	///     .unwrap();
	/// }
	/// ```
	// Fixes #880: URL-encode token to prevent injection via query parameter
	pub async fn connect_with_query_token(url: &str, token: &str) -> Result<Self, WsError> {
		let url_with_token = format!("{}?token={}", url, urlencoding::encode(token));
		Self::connect(&url_with_token).await
	}

	/// Connect to WebSocket server with cookie authentication
	///
	/// Adds `Cookie: <cookie_name>=<cookie_value>` header to the WebSocket handshake request.
	///
	/// # Example
	/// ```rust,no_run
	/// use reinhardt_testkit::websocket::WebSocketTestClient;
	///
	/// #[tokio::test]
	/// async fn test_cookie_auth() {
	///     let client = WebSocketTestClient::connect_with_cookie(
	///         "ws://localhost:8080/ws",
	///         "session_id",
	///         "abc123"
	///     )
	///     .await
	///     .unwrap();
	/// }
	/// ```
	pub async fn connect_with_cookie(
		url: &str,
		cookie_name: &str,
		cookie_value: &str,
	) -> Result<Self, WsError> {
		use tokio_tungstenite::tungstenite::http::Request;

		let request = Request::builder()
			.uri(url)
			.header("Cookie", format!("{}={}", cookie_name, cookie_value))
			.body(())
			.expect("Failed to build WebSocket request");

		let (stream, _response) = connect_async(request).await?;
		Ok(Self {
			stream,
			url: url.to_string(),
		})
	}

	/// Send text message to WebSocket server
	///
	/// # Example
	/// ```rust,no_run
	/// use reinhardt_testkit::websocket::WebSocketTestClient;
	///
	/// #[tokio::test]
	/// async fn test_send() {
	///     let mut client = WebSocketTestClient::connect("ws://localhost:8080/ws")
	///         .await
	///         .unwrap();
	///     client.send_text("Hello").await.unwrap();
	/// }
	/// ```
	pub async fn send_text(&mut self, text: &str) -> Result<(), WsError> {
		self.stream.send(Message::text(text)).await
	}

	/// Send binary message to WebSocket server
	pub async fn send_binary(&mut self, data: &[u8]) -> Result<(), WsError> {
		self.stream.send(Message::binary(data.to_vec())).await
	}

	/// Send ping message to WebSocket server
	pub async fn send_ping(&mut self, payload: &[u8]) -> Result<(), WsError> {
		self.stream
			.send(Message::Ping(payload.to_vec().into()))
			.await
	}

	/// Send pong message to WebSocket server
	pub async fn send_pong(&mut self, payload: &[u8]) -> Result<(), WsError> {
		self.stream
			.send(Message::Pong(payload.to_vec().into()))
			.await
	}

	/// Receive next message from WebSocket server
	///
	/// Returns `None` if connection is closed.
	pub async fn receive(&mut self) -> Option<Result<Message, WsError>> {
		self.stream.next().await
	}

	/// Receive text message from WebSocket server with timeout
	///
	/// # Example
	/// ```rust,no_run
	/// use reinhardt_testkit::websocket::WebSocketTestClient;
	///
	/// #[tokio::test]
	/// async fn test_receive() {
	///     let mut client = WebSocketTestClient::connect("ws://localhost:8080/ws")
	///         .await
	///         .unwrap();
	///     let text = client.receive_text().await.unwrap();
	///     assert_eq!(text, "Welcome");
	/// }
	/// ```
	pub async fn receive_text(&mut self) -> Result<String, WsError> {
		self.receive_text_with_timeout(Duration::from_secs(5)).await
	}

	/// Receive text message with custom timeout
	pub async fn receive_text_with_timeout(
		&mut self,
		duration: Duration,
	) -> Result<String, WsError> {
		match timeout(duration, self.stream.next()).await {
			Ok(Some(Ok(Message::Text(text)))) => Ok(text.to_string()),
			Ok(Some(Ok(msg))) => Err(WsError::Io(IoError::new(
				ErrorKind::InvalidData,
				format!("Expected text message, got {:?}", msg),
			))),
			Ok(Some(Err(e))) => Err(e),
			Ok(None) => Err(WsError::ConnectionClosed),
			Err(_) => Err(WsError::Io(IoError::new(
				ErrorKind::TimedOut,
				"Receive timeout",
			))),
		}
	}

	/// Receive binary message from WebSocket server with timeout
	pub async fn receive_binary(&mut self) -> Result<Vec<u8>, WsError> {
		self.receive_binary_with_timeout(Duration::from_secs(5))
			.await
	}

	/// Receive binary message with custom timeout
	pub async fn receive_binary_with_timeout(
		&mut self,
		duration: Duration,
	) -> Result<Vec<u8>, WsError> {
		match timeout(duration, self.stream.next()).await {
			Ok(Some(Ok(Message::Binary(data)))) => Ok(data.to_vec()),
			Ok(Some(Ok(msg))) => Err(WsError::Io(IoError::new(
				ErrorKind::InvalidData,
				format!("Expected binary message, got {:?}", msg),
			))),
			Ok(Some(Err(e))) => Err(e),
			Ok(None) => Err(WsError::ConnectionClosed),
			Err(_) => Err(WsError::Io(IoError::new(
				ErrorKind::TimedOut,
				"Receive timeout",
			))),
		}
	}

	/// Close WebSocket connection
	pub async fn close(mut self) -> Result<(), WsError> {
		self.stream.close(None).await
	}

	/// Get WebSocket URL
	pub fn url(&self) -> &str {
		&self.url
	}
}

/// WebSocket message assertion utilities
pub mod assertions {
	use tokio_tungstenite::tungstenite::Message;

	/// Assert that WebSocket message is text with expected content
	///
	/// # Example
	/// ```rust,no_run
	/// use reinhardt_testkit::websocket::assertions::assert_message_text;
	/// use tokio_tungstenite::tungstenite::Message;
	///
	/// let msg = Message::text("Hello");
	/// assert_message_text(&msg, "Hello");
	/// ```
	pub fn assert_message_text(msg: &Message, expected: &str) {
		match msg {
			Message::Text(text) => assert_eq!(text.as_str(), expected),
			_ => panic!("Expected text message, got {:?}", msg),
		}
	}

	/// Assert that WebSocket message is text containing substring
	pub fn assert_message_contains(msg: &Message, substring: &str) {
		match msg {
			Message::Text(text) => assert!(
				text.contains(substring),
				"Message '{}' does not contain '{}'",
				text,
				substring
			),
			_ => panic!("Expected text message, got {:?}", msg),
		}
	}

	/// Assert that WebSocket message is binary with expected data
	pub fn assert_message_binary(msg: &Message, expected: &[u8]) {
		match msg {
			Message::Binary(data) => assert_eq!(data.as_ref(), expected),
			_ => panic!("Expected binary message, got {:?}", msg),
		}
	}

	/// Assert that WebSocket message is ping
	pub fn assert_message_ping(msg: &Message) {
		match msg {
			Message::Ping(_) => {}
			_ => panic!("Expected ping message, got {:?}", msg),
		}
	}

	/// Assert that WebSocket message is pong
	pub fn assert_message_pong(msg: &Message) {
		match msg {
			Message::Pong(_) => {}
			_ => panic!("Expected pong message, got {:?}", msg),
		}
	}
}

#[cfg(test)]
mod tests {
	use super::*;
	use std::sync::{Arc, Mutex};

	use rstest::rstest;
	use tokio::net::TcpListener;
	use tokio::sync::Notify;
	use tokio::task::JoinHandle;
	use tokio_tungstenite::accept_async;

	struct WebSocketServerGuard {
		handle: Option<JoinHandle<()>>,
	}

	impl WebSocketServerGuard {
		async fn join(mut self) {
			if let Some(handle) = self.handle.take() {
				handle.await.unwrap();
			}
		}
	}

	impl Drop for WebSocketServerGuard {
		fn drop(&mut self) {
			if let Some(handle) = self.handle.take() {
				handle.abort();
			}
		}
	}

	async fn start_echo_server(
		initial_message: Option<Message>,
		close_messages: Arc<Mutex<Vec<Message>>>,
	) -> (String, WebSocketServerGuard) {
		let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
		let address = listener.local_addr().unwrap();
		let handle = tokio::spawn(async move {
			let (stream, _) = listener.accept().await.unwrap();
			let mut socket = accept_async(stream).await.unwrap();
			if let Some(message) = initial_message {
				socket.send(message).await.unwrap();
			}
			while let Some(result) = socket.next().await {
				let message = result.unwrap();
				match message {
					Message::Close(frame) => {
						close_messages.lock().unwrap().push(Message::Close(frame));
						break;
					}
					Message::Text(text) => socket.send(Message::Text(text)).await.unwrap(),
					Message::Binary(bytes) => socket.send(Message::Binary(bytes)).await.unwrap(),
					Message::Ping(bytes) => socket.send(Message::Pong(bytes)).await.unwrap(),
					Message::Pong(bytes) => socket.send(Message::Ping(bytes)).await.unwrap(),
					Message::Frame(_) => unreachable!("raw frames are not yielded by tungstenite"),
				}
			}
		});

		(
			format!("ws://{address}/"),
			WebSocketServerGuard {
				handle: Some(handle),
			},
		)
	}

	async fn start_timeout_server() -> (String, WebSocketServerGuard, Arc<Notify>) {
		let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
		let address = listener.local_addr().unwrap();
		let release = Arc::new(Notify::new());
		let server_release = Arc::clone(&release);
		let handle = tokio::spawn(async move {
			let (stream, _) = listener.accept().await.unwrap();
			let _socket = accept_async(stream).await.unwrap();
			server_release.notified().await;
		});

		(
			format!("ws://{address}"),
			WebSocketServerGuard {
				handle: Some(handle),
			},
			release,
		)
	}

	#[test]
	fn test_url_with_query_token() {
		let url = "ws://localhost:8080/ws";
		let token = "my-token";
		let expected = "ws://localhost:8080/ws?token=my-token";

		let url_with_token = format!("{}?token={}", url, urlencoding::encode(token));
		assert_eq!(url_with_token, expected);
	}

	#[test]
	fn test_url_with_query_token_special_chars() {
		let url = "ws://localhost:8080/ws";
		let token = "token with spaces&special=chars";
		let url_with_token = format!("{}?token={}", url, urlencoding::encode(token));
		assert_eq!(
			url_with_token,
			"ws://localhost:8080/ws?token=token%20with%20spaces%26special%3Dchars"
		);
	}

	#[test]
	fn test_message_assertions() {
		use assertions::*;

		let text_msg = Message::text("Hello");
		assert_message_text(&text_msg, "Hello");
		assert_message_contains(&text_msg, "ell");

		let binary_msg = Message::Binary(vec![1, 2, 3].into());
		assert_message_binary(&binary_msg, &[1, 2, 3]);

		let ping_msg = Message::Ping(vec![].into());
		assert_message_ping(&ping_msg);

		let pong_msg = Message::Pong(vec![].into());
		assert_message_pong(&pong_msg);
	}

	#[rstest]
	#[tokio::test]
	async fn websocket_client_roundtrips_frames_and_reports_timeout_and_type_errors() {
		// Arrange
		let close_messages = Arc::new(Mutex::new(Vec::new()));
		let (url, echo_server) = start_echo_server(None, Arc::clone(&close_messages)).await;
		let (wrong_url, wrong_server) = start_echo_server(
			Some(Message::binary(&b"not text"[..])),
			Arc::new(Mutex::new(Vec::new())),
		)
		.await;
		let (timeout_url, timeout_server, timeout_release) = start_timeout_server().await;
		let mut client = WebSocketTestClient::connect(&url).await.unwrap();
		let client_url = client.url().to_string();
		let mut wrong_client = WebSocketTestClient::connect(&wrong_url).await.unwrap();
		let mut timeout_client = WebSocketTestClient::connect(&timeout_url).await.unwrap();

		// Act
		client.send_text("hello websocket").await.unwrap();
		let text = client.receive_text().await.unwrap();
		client.send_binary(&[1, 2, 3]).await.unwrap();
		let binary = client.receive_binary().await.unwrap();
		client.send_ping(b"ping-data").await.unwrap();
		let pong = client.receive().await.unwrap().unwrap();
		client.send_pong(b"pong-data").await.unwrap();
		let ping = client.receive().await.unwrap().unwrap();
		let wrong_frame = wrong_client
			.receive_text_with_timeout(Duration::from_millis(50))
			.await
			.unwrap_err();
		let timeout_error = timeout_client
			.receive_text_with_timeout(Duration::from_millis(10))
			.await
			.unwrap_err();
		timeout_release.notify_one();
		wrong_client.close().await.unwrap();
		client.close().await.unwrap();

		// Assert
		assert_eq!(client_url, url);
		assert_eq!(text, "hello websocket");
		assert_eq!(binary, vec![1, 2, 3]);
		assert_eq!(pong, Message::Pong(b"ping-data".to_vec().into()));
		assert_eq!(ping, Message::Ping(b"pong-data".to_vec().into()));
		match wrong_frame {
			WsError::Io(error) => {
				assert_eq!(error.kind(), ErrorKind::InvalidData);
				assert_eq!(
					error.to_string(),
					"Expected text message, got Binary(b\"not text\")"
				);
			}
			other => panic!("expected invalid data error, got {other:?}"),
		}
		match timeout_error {
			WsError::Io(error) => {
				assert_eq!(error.kind(), ErrorKind::TimedOut);
				assert_eq!(error.to_string(), "Receive timeout");
			}
			other => panic!("expected timeout error, got {other:?}"),
		}
		echo_server.join().await;
		wrong_server.join().await;
		timeout_server.join().await;
		assert_eq!(*close_messages.lock().unwrap(), vec![Message::Close(None)]);
	}

	#[tokio::test]
	async fn websocket_client_query_auth_encodes_token_in_connected_url() {
		// Arrange
		let close_messages = Arc::new(Mutex::new(Vec::new()));
		let (url, server) = start_echo_server(None, Arc::clone(&close_messages)).await;

		// Act
		let query_client = WebSocketTestClient::connect_with_query_token(&url, "space & equals=")
			.await
			.unwrap();
		let query_url = query_client.url().to_string();
		query_client.close().await.unwrap();
		server.join().await;

		// Assert
		assert_eq!(query_url, format!("{url}?token=space%20%26%20equals%3D"));
		assert_eq!(*close_messages.lock().unwrap(), vec![Message::Close(None)]);
	}
}