nautilus_network/transport/
mod.rs1pub mod error;
32pub mod message;
33pub mod stream;
34pub mod tungstenite;
35
36#[cfg(feature = "transport-sockudo")]
37pub mod sockudo;
38
39pub use error::TransportError;
40pub use message::{CloseFrame, Message};
41pub use stream::{BoxedWsTransport, WsTransport};
42
43#[cfg(test)]
44mod tests {
45 use bytes::Bytes;
46 use proptest::prelude::*;
47 use rstest::rstest;
48 #[cfg(feature = "transport-sockudo")]
49 use sockudo_ws::protocol::Message as SockudoMessage;
50 use tokio_tungstenite::tungstenite::Message as TungsteniteMessage;
51
52 use super::*;
53
54 fn message_strategy() -> impl Strategy<Value = Message> {
55 prop_oneof![
56 any::<String>().prop_map(Message::text),
57 prop::collection::vec(any::<u8>(), 0..256).prop_map(Message::binary),
58 prop::collection::vec(any::<u8>(), 0..=125)
59 .prop_map(|bytes| Message::ping(Bytes::from(bytes))),
60 prop::collection::vec(any::<u8>(), 0..=125)
61 .prop_map(|bytes| Message::pong(Bytes::from(bytes))),
62 prop::option::of((any::<u16>(), any::<String>())).prop_map(|frame| {
63 Message::Close(frame.map(|(code, reason)| CloseFrame::new(code, reason)))
64 }),
65 ]
66 }
67
68 proptest! {
69 #[rstest]
70 fn message_conversions_round_trip(message in message_strategy()) {
71 let tungstenite = TungsteniteMessage::try_from(message.clone()).unwrap();
72 let tungstenite_round_trip = Message::from(tungstenite);
73 prop_assert_eq!(&tungstenite_round_trip, &message);
74
75 #[cfg(feature = "transport-sockudo")]
76 {
77 let sockudo = SockudoMessage::from(message.clone());
78 prop_assert_eq!(Message::from(sockudo), message);
79 }
80 }
81
82 #[rstest]
83 fn invalid_text_conversion_is_backend_specific(
84 bytes in prop::collection::vec(any::<u8>(), 1..256)
85 .prop_filter("invalid UTF-8", |bytes| std::str::from_utf8(bytes).is_err())
86 ) {
87 let message = Message::Text(Bytes::from(bytes));
88 let tungstenite = TungsteniteMessage::try_from(message.clone());
89
90 prop_assert!(matches!(tungstenite, Err(TransportError::InvalidUtf8)));
91
92 #[cfg(feature = "transport-sockudo")]
93 {
94 let sockudo = SockudoMessage::from(message.clone());
95 prop_assert_eq!(Message::from(sockudo), message);
96 }
97 }
98 }
99}