HTTPS by default, HTTP only if specified (#12394)

* HTTPS by default, HTTP only if specified

* Added/Updated unit tests;Added FTPS
This commit is contained in:
Chris 2021-07-16 06:38:26 -05:00 committed by GitHub
parent c05e33cae3
commit a084ea24b3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 13 deletions

View file

@ -11,20 +11,28 @@ namespace Microsoft.Plugin.Uri.UnitTests.UriHelper
[TestFixture]
public class ExtendedUriParserTests
{
[TestCase("google.com", true, "http://google.com/")]
[TestCase("localhost", true, "http://localhost/")]
[TestCase("127.0.0.1", true, "http://127.0.0.1/")]
[TestCase("127.0.0.1:80", true, "http://127.0.0.1/")]
[TestCase("google.com", true, "https://google.com/")]
[TestCase("http://google.com", true, "http://google.com/")]
[TestCase("localhost", true, "https://localhost/")]
[TestCase("http://localhost", true, "http://localhost/")]
[TestCase("127.0.0.1", true, "https://127.0.0.1/")]
[TestCase("http://127.0.0.1", true, "http://127.0.0.1/")]
[TestCase("http://127.0.0.1:80", true, "http://127.0.0.1/")]
[TestCase("127", false, null)]
[TestCase("", false, null)]
[TestCase("https://google.com", true, "https://google.com/")]
[TestCase("ftps://google.com", true, "ftps://google.com/")]
[TestCase(null, false, null)]
[TestCase("bing.com/search?q=gmx", true, "http://bing.com/search?q=gmx")]
[TestCase("h", true, "http://h/")]
[TestCase("ht", true, "http://ht/")]
[TestCase("htt", true, "http://htt/")]
[TestCase("http", true, "http://http/")]
[TestCase("bing.com/search?q=gmx", true, "https://bing.com/search?q=gmx")]
[TestCase("http://bing.com/search?q=gmx", true, "http://bing.com/search?q=gmx")]
[TestCase("h", true, "https://h/")]
[TestCase("http://h", true, "http://h/")]
[TestCase("ht", true, "https://ht/")]
[TestCase("http://ht", true, "http://ht/")]
[TestCase("htt", true, "https://htt/")]
[TestCase("http://htt", true, "http://htt/")]
[TestCase("http", true, "https://http/")]
[TestCase("http://http", true, "http://http/")]
[TestCase("http:", false, null)]
[TestCase("http:/", false, null)]
[TestCase("http://", false, null)]
@ -36,10 +44,14 @@ namespace Microsoft.Plugin.Uri.UnitTests.UriHelper
[TestCase("http://test.c", true, "http://test.c/")]
[TestCase("http://test.co", true, "http://test.co/")]
[TestCase("http://test.com", true, "http://test.com/")]
[TestCase("http:3", true, "http://http:3/")]
[TestCase("[::]", true, "http://[::]/")]
[TestCase("[2001:0DB8::1]", true, "http://[2001:db8::1]/")]
[TestCase("[2001:0DB8::1]:80", true, "http://[2001:db8::1]/")]
[TestCase("http:3", true, "https://http:3/")]
[TestCase("http://http:3", true, "http://http:3/")]
[TestCase("[::]", true, "https://[::]/")]
[TestCase("http://[::]", true, "http://[::]/")]
[TestCase("[2001:0DB8::1]", true, "https://[2001:db8::1]/")]
[TestCase("http://[2001:0DB8::1]", true, "http://[2001:db8::1]/")]
[TestCase("[2001:0DB8::1]:80", true, "https://[2001:db8::1]/")]
[TestCase("http://[2001:0DB8::1]:80", true, "http://[2001:db8::1]/")]
public void TryParseCanParseHostName(string query, bool expectedSuccess, string expectedResult)
{
// Arrange

View file

@ -32,6 +32,14 @@ namespace Microsoft.Plugin.Uri.UriHelper
try
{
var urlBuilder = new UriBuilder(input);
var hadDefaultPort = urlBuilder.Uri.IsDefaultPort;
urlBuilder.Port = hadDefaultPort ? -1 : urlBuilder.Port;
if (!input.Contains("HTTP://", StringComparison.OrdinalIgnoreCase) &&
!input.Contains("FTPS://", StringComparison.OrdinalIgnoreCase))
{
urlBuilder.Scheme = System.Uri.UriSchemeHttps;
}
result = urlBuilder.Uri;
return true;