#510 parsing bug with Uri + default port

This commit is contained in:
Todd 2020-09-27 12:28:01 -05:00
parent f3551eace0
commit a8d540c272
2 changed files with 10 additions and 1 deletions

View File

@ -559,5 +559,14 @@ namespace Flurl.Test.UrlBuilder
Assert.IsInstanceOf<Url>(url); Assert.IsInstanceOf<Url>(url);
Assert.AreEqual("http://mysite.com", url.ToString()); Assert.AreEqual("http://mysite.com", url.ToString());
} }
[Test] // https://github.com/tmenier/Flurl/issues/510
public void uri_with_default_port_parses_correctly() {
var originalString = "https://someurl.net:443/api/somepath";
var uri = new Uri(originalString);
var url = new Url(uri);
Assert.AreEqual(443, url.Port);
Assert.AreEqual(originalString, url.ToString());
}
} }
} }

View File

@ -181,7 +181,7 @@ namespace Flurl
_scheme = uri.Scheme; _scheme = uri.Scheme;
_userInfo = uri.UserInfo; _userInfo = uri.UserInfo;
_host = uri.Host; _host = uri.Host;
_port = uri.Authority.OrdinalEndsWith($":{uri.Port}") ? uri.Port : (int?)null; // don't default Port if not included _port = _baseUrl.OrdinalStartsWith($"{Root}:{uri.Port}") ? uri.Port : (int?)null; // don't default Port if not included explicitly
_pathSegments = new List<string>(); _pathSegments = new List<string>();
if (uri.AbsolutePath.Length > 0 && uri.AbsolutePath != "/") if (uri.AbsolutePath.Length > 0 && uri.AbsolutePath != "/")
AppendPathSegment(uri.AbsolutePath); AppendPathSegment(uri.AbsolutePath);