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

add an "append_path" function to url #934

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
34 changes: 34 additions & 0 deletions url/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2637,6 +2637,40 @@ impl Url {
Err(())
}

/// Append path segments to the path of a Url, escaping if necesary.
///
/// This differs from Url::join in that it is insensitive to trailing slashes
/// in the url and leading slashes in the passed string. See documentation of Url::join for discussion
/// of this subtlety. Also, this function cannot change any part of the Url other than the path.
///
/// Examples:
///
/// ```
/// # use url::Url;
/// let mut my_url = Url::parse("http://www.example.com/api/v1").unwrap();
/// my_url.append_path("system/status").unwrap();
/// assert_eq!(my_url.as_str(), "http://www.example.com/api/v1/system/status");
/// ```
///
/// Fails if the Url is cannot-be-a-base.
#[allow(clippy::result_unit_err)]
#[inline]
pub fn append_path(&mut self, path: impl AsRef<str>) -> Result<(), ()> {
// This fails if self is cannot-be-a-base but succeeds otherwise.
let mut path_segments_mut = self.path_segments_mut()?;

// Remove the last segment if it is empty, this makes our code tolerate trailing `/`'s
path_segments_mut.pop_if_empty();

// Remove any leading `/` from the path we are appending, this makes our code tolerate leading `/`'s
let path = path.as_ref();
let path = path.strip_prefix('/').unwrap_or(path);
for segment in path.split('/') {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this splitting is based on similar code elsewhere in the library: https://docs.rs/url/2.5.0/src/url/lib.rs.html#1351

path_segments_mut.push(segment);
}
Ok(())
}

// Private helper methods:

#[inline]
Expand Down
39 changes: 39 additions & 0 deletions url/tests/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1316,3 +1316,42 @@ fn issue_864() {
url.set_path("x");
dbg!(&url);
}

#[test]
/// append_path is an alternative to Url::join addressing issues described in
/// https://github.com/servo/rust-url/issues/333
fn test_append_path() {
// append_path behaves as expected when path is `/` regardless of trailing & leading slashes
let mut url = Url::parse("http://test.com").unwrap();
url.append_path("/a/b/c").unwrap();
assert_eq!(url.as_str(), "http://test.com/a/b/c");

let mut url = Url::parse("http://test.com").unwrap();
url.append_path("a/b/c").unwrap();
assert_eq!(url.as_str(), "http://test.com/a/b/c");

let mut url = Url::parse("http://test.com/").unwrap();
url.append_path("/a/b/c").unwrap();
assert_eq!(url.as_str(), "http://test.com/a/b/c");

let mut url = Url::parse("http://test.com/").unwrap();
url.append_path("a/b/c").unwrap();
assert_eq!(url.as_str(), "http://test.com/a/b/c");

// append_path behaves as expected when path is `/api/v1` regardless of trailing & leading slashes
let mut url = Url::parse("http://test.com/api/v1").unwrap();
url.append_path("/a/b/c").unwrap();
assert_eq!(url.as_str(), "http://test.com/api/v1/a/b/c");

let mut url = Url::parse("http://test.com/api/v1").unwrap();
url.append_path("a/b/c").unwrap();
assert_eq!(url.as_str(), "http://test.com/api/v1/a/b/c");

let mut url = Url::parse("http://test.com/api/v1/").unwrap();
url.append_path("/a/b/c").unwrap();
assert_eq!(url.as_str(), "http://test.com/api/v1/a/b/c");

let mut url = Url::parse("http://test.com/api/v1/").unwrap();
url.append_path("a/b/c").unwrap();
assert_eq!(url.as_str(), "http://test.com/api/v1/a/b/c");
}