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

View File

@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved. // 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. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Xunit; using Xunit;
namespace Microsoft.AspNetCore.Http.Extensions namespace Microsoft.AspNetCore.Http.Extensions
@ -128,5 +129,28 @@ namespace Microsoft.AspNetCore.Http.Extensions
Assert.Equal(query, resQuery); Assert.Equal(query, resQuery);
Assert.Equal(fragment, resFragment); 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));
}
} }
} }