Skip to content

Commit 14ce915

Browse files
authored
Merge pull request #252 from CrockAgile/fix/base-url
Fix client base URL documentation
2 parents b856efd + 9313eb6 commit 14ce915

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

src/client.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -507,13 +507,17 @@ impl Client {
507507

508508
/// Sets the base URL for this client. All request URLs will be relative to this URL.
509509
///
510+
/// Note: a trailing slash is significant.
511+
/// Without it, the last path component is considered to be a “file” name
512+
/// to be removed to get at the “directory” that is used as the base.
513+
///
510514
/// # Examples
511515
/// ```no_run
512516
/// # use http_types::Url;
513517
/// # fn main() -> http_types::Result<()> { async_std::task::block_on(async {
514518
/// let mut client = surf::client();
515-
/// client.set_base_url(Url::parse("http://example.com/api/v1")?);
516-
/// client.get("/posts.json").recv_json().await?; /// http://example.com/api/v1/posts.json
519+
/// client.set_base_url(Url::parse("http://example.com/api/v1/")?);
520+
/// client.get("posts.json").recv_json().await?; /// http://example.com/api/v1/posts.json
517521
/// # Ok(()) }) }
518522
/// ```
519523
pub fn set_base_url(&mut self, base: Url) {
@@ -528,3 +532,17 @@ impl Client {
528532
}
529533
}
530534
}
535+
536+
#[cfg(test)]
537+
mod client_tests {
538+
use super::Client;
539+
use crate::Url;
540+
541+
#[test]
542+
fn base_url() {
543+
let mut client = Client::new();
544+
client.set_base_url(Url::parse("http://example.com/api/v1/").unwrap());
545+
let url = client.url("posts.json");
546+
assert_eq!(url.as_str(), "http://example.com/api/v1/posts.json");
547+
}
548+
}

0 commit comments

Comments
 (0)