Add support for WithoutDefaultPort as WithPort doesn't allow "-1" to be sent to hide DefaultPorts

This commit is contained in:
Peter Dolkens
2017-07-05 11:57:23 +01:00
parent 9b1d037a88
commit 9dc6c4a466
2 changed files with 27 additions and 0 deletions

View File

@@ -78,6 +78,22 @@ namespace FluentUriBuilder.Tests
Assert.Equal(url.Port, 22);
}
[Fact]
public void WithoutDefaultPort()
{
var url = new UriBuilder("http://awesome.com:80")
.WithoutDefaultPort();
Assert.Equal("http://awesome.com", url.Uri.ToString());
url = new UriBuilder("http://awesome.com:443")
.WithoutDefaultPort();
Assert.Equal("http://awesome.com:443", url.Uri.ToString());
url = new UriBuilder("https://awesome.com:443")
.WithoutDefaultPort();
Assert.Equal("https://awesome.com", url.Uri.ToString());
}
[Fact]
public void WithHttps()
{

View File

@@ -81,6 +81,17 @@ namespace System
return bld;
}
/// <summary>
/// Removes the port number for default ports
/// </summary>
/// <param name="bld"></param>
/// <returns></returns>
public static UriBuilder WithoutDefaultPort(this UriBuilder bld)
{
if (bld.Uri.IsDefaultPort) bld.Port = -1;
return bld;
}
/// <summary>
/// appends a path segment to the path. Can be called multiple times to append multiple segments
/// </summary>