From ab0185a0b8d0b7a80a6169fd78a45f00a28e057d Mon Sep 17 00:00:00 2001 From: Justin Kotalik Date: Thu, 13 Jul 2017 17:45:01 -0700 Subject: [PATCH] Adds null checks to UriHelper and fixes typo --- .../UriHelper.cs | 14 +++++++++-- .../UriHelperTests.cs | 24 +++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.AspNetCore.Http.Extensions/UriHelper.cs b/src/Microsoft.AspNetCore.Http.Extensions/UriHelper.cs index 2b423b5dbb..633e591186 100644 --- a/src/Microsoft.AspNetCore.Http.Extensions/UriHelper.cs +++ b/src/Microsoft.AspNetCore.Http.Extensions/UriHelper.cs @@ -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 } /// - /// Seperates the given absolute URI string into components. Assumes no PathBase. + /// Separates the given absolute URI string into components. Assumes no PathBase. /// /// A string representation of the uri. /// http, https, etc. @@ -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 /// public static string Encode(Uri uri) { + if (uri == null) + { + throw new ArgumentNullException(nameof(uri)); + } + if (uri.IsAbsoluteUri) { return BuildAbsolute( diff --git a/test/Microsoft.AspNetCore.Http.Extensions.Tests/UriHelperTests.cs b/test/Microsoft.AspNetCore.Http.Extensions.Tests/UriHelperTests.cs index a27864c529..11b045af4f 100644 --- a/test/Microsoft.AspNetCore.Http.Extensions.Tests/UriHelperTests.cs +++ b/test/Microsoft.AspNetCore.Http.Extensions.Tests/UriHelperTests.cs @@ -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(() => 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(() => UriHelper.FromAbsolute(null, out resScheme, out resHost, out resPath, out resQuery, out resFragment)); + + } } }