Skip to content

Commit

Permalink
HTTPS by default, HTTP only if specified (#12394)
Browse files Browse the repository at this point in the history
* HTTPS by default, HTTP only if specified

* Added/Updated unit tests;Added FTPS
  • Loading branch information
chrisharris333 committed Jul 16, 2021
1 parent c05e33c commit a084ea2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -36,10 +44,14 @@ public class ExtendedUriParserTests
[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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ public bool TryParse(string input, out System.Uri result)
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;
Expand Down

0 comments on commit a084ea2

Please sign in to comment.