Adds null checks to UriHelper and fixes typo

This commit is contained in:
Justin Kotalik 2017-07-13 17:45:01 -07:00
parent 3202387d3f
commit ab0185a0b8
2 changed files with 36 additions and 2 deletions

View File

@ -53,6 +53,11 @@ namespace Microsoft.AspNetCore.Http.Extensions
QueryString query = new QueryString(),
FragmentString fragment = new FragmentString())
{
if (scheme == null)
{
throw new ArgumentNullException(nameof(scheme));
}
var combinedPath = (pathBase.HasValue || path.HasValue) ? (pathBase + path).ToString() : "/";
var encodedHost = host.ToString();
@ -74,7 +79,7 @@ namespace Microsoft.AspNetCore.Http.Extensions
}
/// <summary>
/// Seperates the given absolute URI string into components. Assumes no PathBase.
/// Separates the given absolute URI string into components. Assumes no PathBase.
/// </summary>
/// <param name="uri">A string representation of the uri.</param>
/// <param name="scheme">http, https, etc.</param>
@ -94,7 +99,7 @@ namespace Microsoft.AspNetCore.Http.Extensions
{
throw new ArgumentNullException(nameof(uri));
}
// Satisfy the out parameters
path = new PathString();
query = new QueryString();
fragment = new FragmentString();
@ -142,6 +147,11 @@ namespace Microsoft.AspNetCore.Http.Extensions
/// <returns></returns>
public static string Encode(Uri uri)
{
if (uri == null)
{
throw new ArgumentNullException(nameof(uri));
}
if (uri.IsAbsoluteUri)
{
return BuildAbsolute(

View File

@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Xunit;
namespace Microsoft.AspNetCore.Http.Extensions
@ -128,5 +129,28 @@ namespace Microsoft.AspNetCore.Http.Extensions
Assert.Equal(query, resQuery);
Assert.Equal(fragment, resFragment);
}
[Fact]
public void BuildAbsoluteNullInputThrowsArgumentNullException()
{
var resHost = new HostString();
var resPath = new PathString();
var resQuery = new QueryString();
var resFragment = new FragmentString();
Assert.Throws<ArgumentNullException>(() => UriHelper.BuildAbsolute(null, resHost, resPath, resPath, resQuery, resFragment));
}
[Fact]
public void FromAbsoluteNullInputThrowsArgumentNullException()
{
string resScheme = null;
var resHost = new HostString();
var resPath = new PathString();
var resQuery = new QueryString();
var resFragment = new FragmentString();
Assert.Throws<ArgumentNullException>(() => UriHelper.FromAbsolute(null, out resScheme, out resHost, out resPath, out resQuery, out resFragment));
}
}
}