Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getter methods for OpenConnectionArguments #147

Merged
merged 2 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 85 additions & 1 deletion amqprs/src/api/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -1258,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;

Expand All @@ -1281,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();
Expand Down
4 changes: 2 additions & 2 deletions amqprs/src/api/security.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ use amqp_serde::{
use bytes::BytesMut;

/// Credentials used to open a connection.
#[derive(Clone)]
#[derive(Clone, PartialEq)]
pub struct SecurityCredentials {
username: String,
password: String,
mechanism: AuthenticationMechanism,
}

#[allow(clippy::upper_case_acronyms)]
#[derive(Debug, Clone)]
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
enum AuthenticationMechanism {
PLAIN,
Expand Down
Loading