Skip to content

Commit fb457fd

Browse files
authored
Connection url ends in trailing forward slash (#50)
This commit fixes a bug in relation to the usage of url::join(). If A url passed to Connection does not end in a trailing forward slash, a new url will be constructed from the passed url with a trailing forward slash. When joining a Connection url with an API path, the leading forward slash of the path is trimmed, so that the path is appended to the url rather than replacing any existing path segments. Fixes #48
1 parent dc69ffb commit fb457fd

File tree

1 file changed

+40
-3
lines changed

1 file changed

+40
-3
lines changed

elasticsearch/src/http/transport.rs

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,17 @@ pub struct Connection {
175175
}
176176

177177
impl Connection {
178-
/// Creates a new instance of a [Connection]
178+
/// Creates a new instance of a [Connection].
179+
///
180+
/// If the passed [url::Url] does not have a trailing forward slash, a new
181+
/// url is constructed from the passed url, with a trailing slash.
179182
pub fn new(url: Url) -> Self {
183+
let url = if !url.path().ends_with('/') {
184+
Url::parse(format!("{}/", url.as_str()).as_ref()).unwrap()
185+
} else {
186+
url
187+
};
188+
180189
Self { url }
181190
}
182191
}
@@ -236,7 +245,7 @@ impl Transport {
236245
Q: Serialize + ?Sized,
237246
{
238247
let connection = self.conn_pool.next();
239-
let url = connection.url.join(path)?;
248+
let url = connection.url.join(path.trim_start_matches('/'))?;
240249
let reqwest_method = self.method(method);
241250
let mut request_builder = self.client.request(reqwest_method, url);
242251

@@ -460,7 +469,7 @@ impl ConnectionPool for CloudConnectionPool {
460469
#[cfg(test)]
461470
pub mod tests {
462471
use crate::auth::Credentials;
463-
use crate::http::transport::{CloudId, TransportBuilder, SingleNodeConnectionPool};
472+
use crate::http::transport::{CloudId, Connection, TransportBuilder, SingleNodeConnectionPool};
464473
use url::Url;
465474

466475
#[test]
@@ -524,4 +533,32 @@ pub mod tests {
524533
let result = CloudId::parse(&cloud_id);
525534
assert!(result.is_err());
526535
}
536+
537+
#[test]
538+
fn connection_url_with_no_trailing_slash() {
539+
let url = Url::parse("http://10.1.2.3/path_with_no_trailing_slash").unwrap();
540+
let conn = Connection::new(url);
541+
assert_eq!(conn.url.as_str(), "http://10.1.2.3/path_with_no_trailing_slash/");
542+
}
543+
544+
#[test]
545+
fn connection_url_with_trailing_slash() {
546+
let url = Url::parse("http://10.1.2.3/path_with_trailing_slash/").unwrap();
547+
let conn = Connection::new(url);
548+
assert_eq!(conn.url.as_str(), "http://10.1.2.3/path_with_trailing_slash/");
549+
}
550+
551+
#[test]
552+
fn connection_url_with_no_path_and_no_trailing_slash() {
553+
let url = Url::parse("http://10.1.2.3").unwrap();
554+
let conn = Connection::new(url);
555+
assert_eq!(conn.url.as_str(), "http://10.1.2.3/");
556+
}
557+
558+
#[test]
559+
fn connection_url_with_no_path_and_trailing_slash() {
560+
let url = Url::parse("http://10.1.2.3/").unwrap();
561+
let conn = Connection::new(url);
562+
assert_eq!(conn.url.as_str(), "http://10.1.2.3/");
563+
}
527564
}

0 commit comments

Comments
 (0)