Skip to content
This repository was archived by the owner on Nov 20, 2018. It is now read-only.

Commit ab0185a

Browse files
committed
Adds null checks to UriHelper and fixes typo
1 parent 3202387 commit ab0185a

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

src/Microsoft.AspNetCore.Http.Extensions/UriHelper.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ public static string BuildAbsolute(
5353
QueryString query = new QueryString(),
5454
FragmentString fragment = new FragmentString())
5555
{
56+
if (scheme == null)
57+
{
58+
throw new ArgumentNullException(nameof(scheme));
59+
}
60+
5661
var combinedPath = (pathBase.HasValue || path.HasValue) ? (pathBase + path).ToString() : "/";
5762

5863
var encodedHost = host.ToString();
@@ -74,7 +79,7 @@ public static string BuildAbsolute(
7479
}
7580

7681
/// <summary>
77-
/// Seperates the given absolute URI string into components. Assumes no PathBase.
82+
/// Separates the given absolute URI string into components. Assumes no PathBase.
7883
/// </summary>
7984
/// <param name="uri">A string representation of the uri.</param>
8085
/// <param name="scheme">http, https, etc.</param>
@@ -94,7 +99,7 @@ public static void FromAbsolute(
9499
{
95100
throw new ArgumentNullException(nameof(uri));
96101
}
97-
// Satisfy the out parameters
102+
98103
path = new PathString();
99104
query = new QueryString();
100105
fragment = new FragmentString();
@@ -142,6 +147,11 @@ public static void FromAbsolute(
142147
/// <returns></returns>
143148
public static string Encode(Uri uri)
144149
{
150+
if (uri == null)
151+
{
152+
throw new ArgumentNullException(nameof(uri));
153+
}
154+
145155
if (uri.IsAbsoluteUri)
146156
{
147157
return BuildAbsolute(

test/Microsoft.AspNetCore.Http.Extensions.Tests/UriHelperTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System;
45
using Xunit;
56

67
namespace Microsoft.AspNetCore.Http.Extensions
@@ -128,5 +129,28 @@ public void FromAbsoluteToBuildAbsolute()
128129
Assert.Equal(query, resQuery);
129130
Assert.Equal(fragment, resFragment);
130131
}
132+
133+
[Fact]
134+
public void BuildAbsoluteNullInputThrowsArgumentNullException()
135+
{
136+
var resHost = new HostString();
137+
var resPath = new PathString();
138+
var resQuery = new QueryString();
139+
var resFragment = new FragmentString();
140+
Assert.Throws<ArgumentNullException>(() => UriHelper.BuildAbsolute(null, resHost, resPath, resPath, resQuery, resFragment));
141+
142+
}
143+
144+
[Fact]
145+
public void FromAbsoluteNullInputThrowsArgumentNullException()
146+
{
147+
string resScheme = null;
148+
var resHost = new HostString();
149+
var resPath = new PathString();
150+
var resQuery = new QueryString();
151+
var resFragment = new FragmentString();
152+
Assert.Throws<ArgumentNullException>(() => UriHelper.FromAbsolute(null, out resScheme, out resHost, out resPath, out resQuery, out resFragment));
153+
154+
}
131155
}
132156
}

0 commit comments

Comments
 (0)