Skip to content

Commit 0523a68

Browse files
authored
Additional CloudId tests (#145)
This commit adds additional tests for CloudId format. The cloud id from the cloud console will contain a trailing $ when no Kibana instance is defined.
1 parent a4f1281 commit 0523a68

File tree

1 file changed

+37
-9
lines changed

1 file changed

+37
-9
lines changed

elasticsearch/src/http/transport.rs

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -290,14 +290,13 @@ pub struct Connection {
290290
impl Connection {
291291
/// Creates a new instance of a [Connection].
292292
///
293-
/// If the passed [url::Url] does not have a trailing forward slash, a new
294-
/// url is constructed from the passed url, with a trailing slash.
293+
/// If the passed [url::Url] path does not have a trailing forward slash, a trailing
294+
/// forward slash will be appended
295295
pub fn new(url: Url) -> Self {
296-
let url = if !url.path().ends_with('/') {
297-
Url::parse(format!("{}/", url.as_str()).as_ref()).unwrap()
298-
} else {
299-
url
300-
};
296+
let mut url = url;
297+
if !url.path().ends_with('/') {
298+
url.set_path(&format!("{}/", url.path()));
299+
}
301300

302301
Self { url }
303302
}
@@ -630,7 +629,7 @@ pub mod tests {
630629
}
631630

632631
#[test]
633-
fn can_parse_cloud_id() {
632+
fn can_parse_cloud_id_with_kibana_uuid() {
634633
let base64 = base64::encode("cloud-endpoint.example$3dadf823f05388497ea684236d918a1a$3f26e1609cf54a0f80137a80de560da4");
635634
let cloud_id = format!("my_cluster:{}", base64);
636635
let result = CloudId::parse(&cloud_id);
@@ -643,6 +642,35 @@ pub mod tests {
643642
);
644643
}
645644

645+
#[test]
646+
fn can_parse_cloud_id_without_kibana_uuid() {
647+
let base64 = base64::encode("cloud-endpoint.example$3dadf823f05388497ea684236d918a1a$");
648+
let cloud_id = format!("my_cluster:{}", base64);
649+
let result = CloudId::parse(&cloud_id);
650+
assert!(result.is_ok());
651+
let cloud = result.unwrap();
652+
assert_eq!("my_cluster", cloud.name);
653+
assert_eq!(
654+
Url::parse("https://3dadf823f05388497ea684236d918a1a.cloud-endpoint.example").unwrap(),
655+
cloud.url
656+
);
657+
}
658+
659+
#[test]
660+
fn can_parse_cloud_id_with_different_port() {
661+
let base64 = base64::encode("cloud-endpoint.example:4463$3dadf823f05388497ea684236d918a1a$3f26e1609cf54a0f80137a80de560da4");
662+
let cloud_id = format!("my_cluster:{}", base64);
663+
let result = CloudId::parse(&cloud_id);
664+
assert!(result.is_ok());
665+
let cloud = result.unwrap();
666+
assert_eq!("my_cluster", cloud.name);
667+
assert_eq!(
668+
Url::parse("https://3dadf823f05388497ea684236d918a1a.cloud-endpoint.example:4463")
669+
.unwrap(),
670+
cloud.url
671+
);
672+
}
673+
646674
#[test]
647675
fn cloud_id_must_contain_colon() {
648676
let base64 = base64::encode("cloud-endpoint.example$3dadf823f05388497ea684236d918a1a$3f26e1609cf54a0f80137a80de560da4");
@@ -659,7 +687,7 @@ pub mod tests {
659687
}
660688

661689
#[test]
662-
fn cloud_id_first_cannot_be_empty() {
690+
fn cloud_id_first_part_cannot_be_empty() {
663691
let base64 = base64::encode("cloud-endpoint.example$3dadf823f05388497ea684236d918a1a$3f26e1609cf54a0f80137a80de560da4");
664692
let cloud_id = format!(":{}", base64);
665693
let cloud = CloudId::parse(&cloud_id);

0 commit comments

Comments
 (0)