Skip to content

Commit

Permalink
Fix parsing amqps URL without query params (#128)
Browse files Browse the repository at this point in the history
* Add failing test for parsing amqps
* Fix parsing amqps URL without query params
  • Loading branch information
tyilo committed May 12, 2024
1 parent 8ce01c3 commit b9dab50
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions amqprs/src/api/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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("");

Expand Down Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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")]
Expand Down

0 comments on commit b9dab50

Please sign in to comment.