From 060f9fcddc8bf0c419d4849b4ed0df10b2eda756 Mon Sep 17 00:00:00 2001 From: Michael Egli Date: Sun, 13 Oct 2024 11:00:40 +0200 Subject: [PATCH 1/2] Add getter methods to OpenConnectionArguments. Closes #146 The getters use a `get_` prefix, even though this is discouraged by the naming conventions (see Rust API guidelines). The reason is that setters are already present with the same name. --- amqprs/src/api/connection.rs | 48 ++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/amqprs/src/api/connection.rs b/amqprs/src/api/connection.rs index 4c519c5..857cb53 100644 --- a/amqprs/src/api/connection.rs +++ b/amqprs/src/api/connection.rs @@ -349,6 +349,11 @@ impl OpenConnectionArguments { self } + /// Get the host of the server. + pub fn get_host(&self) -> &str { + &self.host + } + /// Set the port of the server. /// /// # Default @@ -359,6 +364,11 @@ impl OpenConnectionArguments { self } + /// Get the port of the server. + pub fn get_port(&self) -> u16 { + self.port + } + /// Set the virtual host. See [RabbitMQ vhosts](https://www.rabbitmq.com/vhosts.html). /// /// # Default @@ -371,6 +381,11 @@ impl OpenConnectionArguments { self } + /// Get the virtual host of the server. + pub fn get_virtual_host(&self) -> &str { + &self.virtual_host + } + /// Set the connection name. /// /// # Default @@ -381,6 +396,11 @@ impl OpenConnectionArguments { self } + /// Get the connection name. + pub fn get_connection_name(&self) -> Option<&str> { + self.connection_name.as_deref() + } + /// Set the user credentials. See [RabbitMQ access control](https://www.rabbitmq.com/access-control.html#mechanisms). /// /// # Default @@ -390,6 +410,12 @@ impl OpenConnectionArguments { self.credentials = credentials; self } + + /// Get the credentials. + pub fn get_credentials(&self) -> &SecurityCredentials { + &self.credentials + } + /// Set the heartbeat timeout in seconds. See [RabbitMQ heartbeats](https://www.rabbitmq.com/heartbeats.html). /// /// # Default @@ -400,6 +426,22 @@ impl OpenConnectionArguments { self } + /// Get the heartbeat. + pub fn get_heartbeat(&self) -> u16 { + self.heartbeat + } + + /// Set the URI scheme. + pub fn scheme(&mut self, scheme: &str) -> &mut Self { + self.scheme = Some(scheme.to_owned()); + self + } + + /// Get the connection scheme. + pub fn get_scheme(&self) -> Option<&str> { + self.scheme.as_deref() + } + /// Set SSL/TLS adaptor. Set to enable SSL/TLS connection. /// /// # Default @@ -411,6 +453,12 @@ impl OpenConnectionArguments { self } + /// Get the TLS adaptor. + #[cfg(feature = "tls")] + pub fn get_tls_adaptor(&self) -> Option<&TlsAdaptor> { + self.tls_adaptor.as_ref() + } + /// Finish chaining and returns a new argument according to chained configurations. /// /// It actually clones the resulted configurations. From eaaa4698e41579bb32c8890645b4510b680642c2 Mon Sep 17 00:00:00 2001 From: Michael Egli Date: Sun, 13 Oct 2024 14:41:15 +0200 Subject: [PATCH 2/2] 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 857cb53..f199823 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,