From e8c21186e9ba2eebe3cf1e188c4116b47575174f Mon Sep 17 00:00:00 2001 From: Michael Egli Date: Sun, 13 Oct 2024 14:41:15 +0200 Subject: [PATCH] Add tests for OpenConnectionArguments getters. --- amqprs/src/api/connection.rs | 38 +++++++++++++++++++++++++++++++++++- amqprs/src/api/security.rs | 4 ++-- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/amqprs/src/api/connection.rs b/amqprs/src/api/connection.rs index be3f5cd..054f224 100644 --- a/amqprs/src/api/connection.rs +++ b/amqprs/src/api/connection.rs @@ -1306,8 +1306,8 @@ fn generate_connection_name(domain: &str) -> String { #[cfg(test)] mod tests { use super::{generate_connection_name, Connection, OpenConnectionArguments}; - use crate::security::SecurityCredentials; use crate::test_utils::setup_logging; + use crate::{connection::AMQPS_SCHEME, security::SecurityCredentials}; use std::{collections::HashSet, thread}; use tokio::time; @@ -1329,6 +1329,42 @@ mod tests { time::sleep(time::Duration::from_millis(100)).await; } + #[test] + fn test_connection_getters() { + let args = OpenConnectionArguments::new("localhost", 5672, "user", "bitnami"); + assert_eq!(args.get_host(), "localhost"); + assert_eq!(args.get_port(), 5672); + assert_eq!(args.get_virtual_host(), "/"); + assert!(args.get_connection_name().is_none()); + assert!(args.get_credentials() == &SecurityCredentials::new_plain("user", "bitnami")); + assert_eq!(args.get_heartbeat(), 60); + assert!(args.get_scheme().is_none()); + #[cfg(feature = "tls")] + assert!(args.get_tls_adaptor().is_none()); + } + + #[test] + fn test_custom_connection_getters() { + let mut default_args = OpenConnectionArguments { + ..Default::default() + }; + let args = default_args + .host("localhost") + .port(1234) + .virtual_host("/vhost") + .connection_name("test") + .credentials(SecurityCredentials::new_plain("user", "bitnami")) + .heartbeat(30) + .scheme("amqps"); + assert_eq!(args.get_host(), "localhost"); + assert_eq!(args.get_port(), 1234); + assert_eq!(args.get_virtual_host(), "/vhost"); + assert!(args.get_connection_name() == Some("test")); + assert!(args.get_credentials() == &SecurityCredentials::new_plain("user", "bitnami")); + assert_eq!(args.get_heartbeat(), 30); + assert!(args.get_scheme() == Some(AMQPS_SCHEME)); + } + #[tokio::test(flavor = "multi_thread", worker_threads = 10)] async fn test_multi_conn_open_close() { setup_logging(); diff --git a/amqprs/src/api/security.rs b/amqprs/src/api/security.rs index b5c4e3d..c4c1139 100644 --- a/amqprs/src/api/security.rs +++ b/amqprs/src/api/security.rs @@ -11,7 +11,7 @@ use amqp_serde::{ use bytes::BytesMut; /// Credentials used to open a connection. -#[derive(Clone)] +#[derive(Clone, PartialEq)] pub struct SecurityCredentials { username: String, password: String, @@ -19,7 +19,7 @@ pub struct SecurityCredentials { } #[allow(clippy::upper_case_acronyms)] -#[derive(Debug, Clone)] +#[derive(Clone, Debug, PartialEq)] #[non_exhaustive] enum AuthenticationMechanism { PLAIN,