-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify logic for SafeGetPathAndQuery by converting relative URIs in…
…to absolute ones #163
- Loading branch information
Showing
2 changed files
with
104 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace KubeClient.Tests | ||
{ | ||
using Utilities; | ||
using Xunit; | ||
|
||
/// <summary> | ||
/// Tests for <see cref="UriHelper"/>. | ||
/// </summary> | ||
public class UriHelperTests | ||
{ | ||
static readonly Uri BaseUri = new Uri("https://localhost"); | ||
|
||
[Theory] | ||
[InlineData("/")] | ||
[InlineData("/?param1=value1¶m2=value2")] | ||
[InlineData("path1")] | ||
[InlineData("path1?param1=value1¶m2=value2")] | ||
[InlineData("path1/path2")] | ||
[InlineData("path1/path2?param1=value1¶m2=value2")] | ||
[InlineData("path1/path2/")] | ||
[InlineData("path1/path2/?param1=value1¶m2=value2")] | ||
[InlineData("/path1")] | ||
[InlineData("/path1?param1=value1¶m2=value2")] | ||
[InlineData("/path1/path2")] | ||
[InlineData("/path1/path2?param1=value1¶m2=value2")] | ||
[InlineData("/path1/path2/")] | ||
[InlineData("/path1/path2/?param1=value1¶m2=value2")] | ||
public void Can_SafeGetPathAndQuery_RelativeUri(string input) | ||
{ | ||
Uri uri = new Uri(input, UriKind.RelativeOrAbsolute); | ||
Assert.False(uri.IsAbsoluteUri); | ||
|
||
string pathAndQuery = uri.SafeGetPathAndQuery(); | ||
Assert.Equal(pathAndQuery, | ||
NormalizePath(input) | ||
); | ||
} | ||
|
||
[Theory] | ||
[InlineData("/")] | ||
[InlineData("/?param1=value1¶m2=value2")] | ||
[InlineData("path1")] | ||
[InlineData("path1?param1=value1¶m2=value2")] | ||
[InlineData("path1/path2")] | ||
[InlineData("path1/path2?param1=value1¶m2=value2")] | ||
[InlineData("path1/path2/")] | ||
[InlineData("path1/path2/?param1=value1¶m2=value2")] | ||
[InlineData("/path1")] | ||
[InlineData("/path1?param1=value1¶m2=value2")] | ||
[InlineData("/path1/path2")] | ||
[InlineData("/path1/path2?param1=value1¶m2=value2")] | ||
[InlineData("/path1/path2/")] | ||
[InlineData("/path1/path2/?param1=value1¶m2=value2")] | ||
public void Can_SafeGetPathAndQuery_AbsoluteUri(string input) | ||
{ | ||
Uri relativeUri = new Uri(input, UriKind.RelativeOrAbsolute); | ||
Assert.False(relativeUri.IsAbsoluteUri); | ||
|
||
Uri uri = new Uri(BaseUri, relativeUri); | ||
|
||
string pathAndQuery = uri.SafeGetPathAndQuery(); | ||
Assert.Equal(pathAndQuery, | ||
NormalizePath(input) | ||
); | ||
} | ||
|
||
/// <summary> | ||
/// Normalise the specified path and query for comparisons in tests. | ||
/// </summary> | ||
/// <param name="pathAndQuery"> | ||
/// The URI path and query components. | ||
/// </param> | ||
/// <returns> | ||
/// The normalised path and query. | ||
/// </returns> | ||
/// <remarks> | ||
/// System.Uri treats the path component of an absolute URI as an absolute path. | ||
/// </remarks> | ||
static string NormalizePath(string pathAndQuery) | ||
{ | ||
if (pathAndQuery == null) | ||
throw new ArgumentNullException(nameof(pathAndQuery)); | ||
|
||
if (pathAndQuery.StartsWith("/")) | ||
return pathAndQuery; | ||
|
||
return $"/{pathAndQuery}"; | ||
} | ||
} | ||
} |