Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions aspnetcore-features/UrlEncodingAndDecoding/Tests/Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

Expand All @@ -10,10 +10,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.10" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
<PackageReference Include="coverlet.collector" Version="3.1.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.9.0" />
<PackageReference Include="MSTest.TestAdapter" Version="4.3.3" />
<PackageReference Include="MSTest.TestFramework" Version="4.3.3" />
<PackageReference Include="coverlet.collector" Version="10.0.1" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Net;
using System.Text.Encodings.Web;
using System.Web;

namespace Tests
Expand All @@ -22,7 +23,7 @@ public void GivenAUrl_WhenEncodingWithHttpUtility_ThenCharactersEncoded()
[DataRow(EncodedUrlLowerPlus)]
[DataRow(EncodedUrlUpperPercent)]
[DataRow(EncodedUrlUpperPlus)]
[DataTestMethod]
[TestMethod]
public void GivenAUrl_WhenDecodingWithHttpUtility_ThenCharactersDecoded(string encodedUrl)
{
var decoded = HttpUtility.UrlDecode(encodedUrl);
Expand All @@ -41,7 +42,7 @@ public void GivenAUrl_WhenEncodingWithWebUtility_ThenCharactersEncoded()
[DataRow(EncodedUrlLowerPlus)]
[DataRow(EncodedUrlUpperPercent)]
[DataRow(EncodedUrlUpperPlus)]
[DataTestMethod]
[TestMethod]
public void GivenAUrl_WhenDecodingWithWebUtility_ThenCharactersDecoded(string encodedUrl)
{
var decoded = WebUtility.UrlDecode(encodedUrl);
Expand All @@ -58,7 +59,7 @@ public void GivenAUrl_WhenEncodingWithUri_ThenCharactersEncoded()
}

[DataRow(EncodedUrlUpperPercent)]
[DataTestMethod]
[TestMethod]
public void GivenAUrl_WhenDecodingWithUri_ThenCharactersDecoded(string encodedUrl)
{
var decoded = Uri.UnescapeDataString(encodedUrl);
Expand All @@ -68,12 +69,73 @@ public void GivenAUrl_WhenDecodingWithUri_ThenCharactersDecoded(string encodedUr

[DataRow(EncodedUrlLowerPlus)]
[DataRow(EncodedUrlUpperPlus)]
[DataTestMethod]
[TestMethod]
public void GivenAUrl_WhenDecodingWithUri_ThenCharactersNotDecoded(string encodedUrl)
{
var decoded = Uri.UnescapeDataString(encodedUrl);

Assert.AreNotEqual(Url, decoded); //Uri.UnescapeDataString does not decode + character to space
}

[TestMethod]
public void GivenAUrl_WhenEncodingWithUrlEncoder_ThenCharactersEncoded()
{
var encoded = UrlEncoder.Default.Encode(Url);

Assert.AreEqual(EncodedUrlUpperPercent, encoded);
}

[TestMethod]
public void GivenAVeryLongString_WhenEscapingWithUri_ThenNoLengthLimitApplies()
{
var longValue = new string('a', 100_000) + " ";

var encoded = Uri.EscapeDataString(longValue);

Assert.AreEqual(100_003, encoded.Length);
}

[TestMethod]
public void GivenTheEncodeUriComponentSafeCharacters_WhenEscapingWithUri_ThenTheyAreEscaped()
{
var encoded = Uri.EscapeDataString("!'()*~");

Assert.AreEqual("%21%27%28%29%2A~", encoded);
}

[TestMethod]
public void GivenABarePath_WhenConstructingAUri_ThenTheOutcomeIsPlatformDependent()
{
if (OperatingSystem.IsWindows())
{
//A bare path is not an absolute URI on Windows and not a rooted local path either.
Assert.ThrowsExactly<UriFormatException>(() => new Uri("/foo"));
Assert.IsFalse(Uri.TryCreate("/foo", UriKind.Absolute, out _));
}
else
{
//On Unix a bare path IS a rooted local path, so it parses as an absolute file URI.
var uri = new Uri("/foo");

Assert.AreEqual("file", uri.Scheme);
Assert.AreEqual("file:///foo", uri.AbsoluteUri);
Assert.IsTrue(Uri.TryCreate("/foo", UriKind.Absolute, out _));
}

//The relative remedy works the same way on every platform.
Assert.IsTrue(Uri.TryCreate("/foo", UriKind.Relative, out _));
Assert.IsFalse(new Uri("/foo", UriKind.Relative).IsAbsoluteUri);
}

[TestMethod]
public void GivenAProtocolRelativeUrl_WhenConstructingAUri_ThenItParsesAsAFileUri()
{
//Same on Windows and on Unix: this does not throw, it becomes a file URI.
var uri = new Uri("//example.com");

Assert.AreEqual("file", uri.Scheme);
Assert.AreEqual("file://example.com/", uri.AbsoluteUri);
Assert.IsTrue(Uri.TryCreate("//example.com", UriKind.Absolute, out _));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Net;
using System.Text.Encodings.Web;
using System.Web;

var url = @"http://example.com/resource?foo=bar with space#fragment";
Expand All @@ -12,10 +13,18 @@
var uriEncoded = Uri.EscapeDataString(url);
var uriDecoded = Uri.UnescapeDataString(uriEncoded);

var urlEncoderEncoded = UrlEncoder.Default.Encode(url);

Console.WriteLine(httpUtilityEncoded);
Console.WriteLine(webUtilityEncoded);
Console.WriteLine(uriEncoded);
Console.WriteLine(urlEncoderEncoded);

Console.WriteLine(httpUtilityDecoded);
Console.WriteLine(webUtilityDecoded);
Console.WriteLine(uriDecoded);
Console.WriteLine(uriDecoded);

// In real code we encode a single value, not the address it goes into:
var searchUrl = $"https://example.com/search?q={Uri.EscapeDataString("bar with space")}";

Console.WriteLine(searchUrl);
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
Loading