From b9dab503f5599dca60f5d84787285b6c089c0ddc Mon Sep 17 00:00:00 2001 From: Asger Hautop Drewsen Date: Sun, 12 May 2024 21:49:28 +0200 Subject: [PATCH] Fix parsing amqps URL without query params (#128) * Add failing test for parsing amqps * Fix parsing amqps URL without query params --- amqprs/src/api/connection.rs | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/amqprs/src/api/connection.rs b/amqprs/src/api/connection.rs index d2e5fa8..d034001 100644 --- a/amqprs/src/api/connection.rs +++ b/amqprs/src/api/connection.rs @@ -492,6 +492,17 @@ impl TryFrom<&str> for OpenConnectionArguments { args.virtual_host(&pu_path[1..]); } + if scheme == AMQPS_SCHEME { + #[cfg(feature = "tls")] + args.tls_adaptor( + TlsAdaptor::without_client_auth(None, host.to_string()) + .map_err(|e| Error::UriError(format!("error creating TLS adaptor: {}", e)))?, + ); + + #[cfg(not(feature = "tls"))] + return Err(Error::UriError("can't create amqps url without the `tls` feature enabled".to_string())); + } + // Check & apply query let pu_q = pu.query().map(|v| v.as_str()).ok_or(|| "").unwrap_or(""); @@ -520,17 +531,6 @@ impl TryFrom<&str> for OpenConnectionArguments { .unwrap_or(DEFAULT_HEARTBEAT); args.heartbeat(heartbeat); - if scheme == AMQPS_SCHEME { - #[cfg(feature = "tls")] - args.tls_adaptor( - TlsAdaptor::without_client_auth(None, host.to_string()) - .map_err(|e| Error::UriError(format!("error creating TLS adaptor: {}", e)))?, - ); - - #[cfg(not(feature = "tls"))] - return Err(Error::UriError("can't create amqps url without the `tls` feature enabled".to_string())); - } - Ok(args) } } @@ -1506,6 +1506,18 @@ mod tests { assert_eq!(tls_adaptor.domain, "localhost"); } + #[cfg(all(feature = "urispec", feature = "tls"))] + #[test] + fn test_urispec_amqps_simple() { + let args = OpenConnectionArguments::try_from("amqps://localhost") + .unwrap(); + assert_eq!(args.host, "localhost"); + assert_eq!(args.port, 5671); + assert_eq!(args.virtual_host, "/"); + let tls_adaptor = args.tls_adaptor.unwrap(); + assert_eq!(tls_adaptor.domain, "localhost"); + } + #[cfg(all(feature = "urispec", feature = "tls"))] #[tokio::test] #[should_panic(expected = "UriError")]