Skip to content

Commit

Permalink
Merge pull request #252 from CrockAgile/fix/base-url
Browse files Browse the repository at this point in the history
Fix client base URL documentation
  • Loading branch information
Fishrock123 authored Oct 22, 2020
2 parents b856efd + 9313eb6 commit 14ce915
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -507,13 +507,17 @@ impl Client {

/// Sets the base URL for this client. All request URLs will be relative to this URL.
///
/// Note: a trailing slash is significant.
/// Without it, the last path component is considered to be a “file” name
/// to be removed to get at the “directory” that is used as the base.
///
/// # Examples
/// ```no_run
/// # use http_types::Url;
/// # fn main() -> http_types::Result<()> { async_std::task::block_on(async {
/// let mut client = surf::client();
/// client.set_base_url(Url::parse("http://example.com/api/v1")?);
/// client.get("/posts.json").recv_json().await?; /// http://example.com/api/v1/posts.json
/// client.set_base_url(Url::parse("http://example.com/api/v1/")?);
/// client.get("posts.json").recv_json().await?; /// http://example.com/api/v1/posts.json
/// # Ok(()) }) }
/// ```
pub fn set_base_url(&mut self, base: Url) {
Expand All @@ -528,3 +532,17 @@ impl Client {
}
}
}

#[cfg(test)]
mod client_tests {
use super::Client;
use crate::Url;

#[test]
fn base_url() {
let mut client = Client::new();
client.set_base_url(Url::parse("http://example.com/api/v1/").unwrap());
let url = client.url("posts.json");
assert_eq!(url.as_str(), "http://example.com/api/v1/posts.json");
}
}

0 comments on commit 14ce915

Please sign in to comment.