From 1093730d721e64d71f0db18239714903d7f731b1 Mon Sep 17 00:00:00 2001 From: Chris Morgan Date: Sat, 19 Oct 2013 00:25:03 +1100 Subject: [PATCH] Fix extra::url::to_str to include the port. Fixes #9451. Fixes chris-morgan/rust-http#16. --- src/libextra/url.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/libextra/url.rs b/src/libextra/url.rs index 2afc49df43a6c..e836d3b52709c 100644 --- a/src/libextra/url.rs +++ b/src/libextra/url.rs @@ -671,9 +671,13 @@ pub fn to_str(url: &Url) -> ~str { }; let authority = if url.host.is_empty() { + // If port is Some, we're in a nonsensical situation. Too bad. ~"" } else { - format!("//{}{}", user, url.host) + match url.port { + Some(ref port) => format!("//{}{}:{}", user, url.host, *port), + None => format!("//{}{}", user, url.host), + } }; let query = if url.query.is_empty() { @@ -895,6 +899,12 @@ mod tests { assert_eq!(from_str(url).unwrap().to_str(), url); } + #[test] + fn test_url_with_port_parse_and_format() { + let url = ~"http://rust-lang.org:80/doc"; + assert_eq!(from_str(url).unwrap().to_str(), url); + } + #[test] fn test_scheme_host_only_url_parse_and_format() { let url = ~"http://rust-lang.org";