ServerAddress.FromUrl() should throw if Host is missing (#860)

This commit is contained in:
Mike Harder 2016-05-31 11:54:38 -07:00
parent 6b25ee7343
commit 306084356e
2 changed files with 22 additions and 1 deletions

View File

@ -141,6 +141,11 @@ namespace Microsoft.AspNetCore.Server.Kestrel
serverAddress.Host = url.Substring(schemeDelimiterEnd, pathDelimiterStart - schemeDelimiterEnd);
}
if (string.IsNullOrEmpty(serverAddress.Host))
{
throw new FormatException($"Invalid URL: {url}");
}
// Path should not end with a / since it will be used as PathBase later
if (url[url.Length - 1] == '/')
{

View File

@ -14,13 +14,29 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
[InlineData("")]
[InlineData("5000")]
[InlineData("//noscheme")]
public void FromUriThrowsForSchemelessUrls(string url)
public void FromUriThrowsForUrlsWithoutSchemeDelimiter(string url)
{
Assert.Throws<FormatException>(() => ServerAddress.FromUrl(url));
}
[Theory]
[InlineData("://")]
[InlineData("://:5000")]
[InlineData("http://")]
[InlineData("http://:5000")]
[InlineData("http:///")]
[InlineData("http:///:5000")]
[InlineData("http:////")]
[InlineData("http:////:5000")]
public void FromUriThrowsForUrlsWithoutHost(string url)
{
Assert.Throws<FormatException>(() => ServerAddress.FromUrl(url));
}
[Theory]
[InlineData("://emptyscheme", "", "emptyscheme", 0, "", "://emptyscheme:0")]
[InlineData("http://+", "http", "+", 80, "", "http://+:80")]
[InlineData("http://*", "http", "*", 80, "", "http://*:80")]
[InlineData("http://localhost", "http", "localhost", 80, "", "http://localhost:80")]
[InlineData("http://www.example.com", "http", "www.example.com", 80, "", "http://www.example.com:80")]
[InlineData("https://www.example.com", "https", "www.example.com", 443, "", "https://www.example.com:443")]