use tungstenite::protocol::WebSocketConfig as TungsteniteConfig;
pub const DEFAULT_MAX_MESSAGE_SIZE: usize = 1_048_576;
pub const DEFAULT_MAX_FRAME_SIZE: usize = 65_536;
pub fn default_websocket_config() -> TungsteniteConfig {
let mut config = TungsteniteConfig::default();
config.max_message_size = Some(DEFAULT_MAX_MESSAGE_SIZE);
config.max_frame_size = Some(DEFAULT_MAX_FRAME_SIZE);
config
}
pub fn websocket_config_with_limits(
max_message_size: Option<usize>,
max_frame_size: Option<usize>,
) -> TungsteniteConfig {
let mut config = TungsteniteConfig::default();
config.max_message_size = max_message_size;
config.max_frame_size = max_frame_size;
config
}
#[cfg(test)]
mod tests {
use super::*;
use rstest::rstest;
#[rstest]
fn test_default_websocket_config_has_message_size_limit() {
let config = default_websocket_config();
assert_eq!(config.max_message_size, Some(DEFAULT_MAX_MESSAGE_SIZE));
assert_eq!(config.max_message_size, Some(1_048_576));
}
#[rstest]
fn test_default_websocket_config_has_frame_size_limit() {
let config = default_websocket_config();
assert_eq!(config.max_frame_size, Some(DEFAULT_MAX_FRAME_SIZE));
assert_eq!(config.max_frame_size, Some(65_536));
}
#[rstest]
fn test_custom_limits() {
let max_msg = 2 * 1024 * 1024; let max_frame = 128 * 1024;
let config = websocket_config_with_limits(Some(max_msg), Some(max_frame));
assert_eq!(config.max_message_size, Some(max_msg));
assert_eq!(config.max_frame_size, Some(max_frame));
}
#[rstest]
fn test_no_limits() {
let config = websocket_config_with_limits(None, None);
assert_eq!(config.max_message_size, None);
assert_eq!(config.max_frame_size, None);
}
#[rstest]
fn test_default_constants() {
assert_eq!(DEFAULT_MAX_MESSAGE_SIZE, 1_048_576);
assert_eq!(DEFAULT_MAX_FRAME_SIZE, 65_536);
}
}