Skip to content

URL encoding and decoding: retarget net10.0, add UrlEncoder and Uri behaviour tests - #2163

Open
vladimir-pecanac-main wants to merge 2 commits into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/88650-uri-encode
Open

URL encoding and decoding: retarget net10.0, add UrlEncoder and Uri behaviour tests#2163
vladimir-pecanac-main wants to merge 2 commits into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/88650-uri-encode

Conversation

@vladimir-pecanac-main

@vladimir-pecanac-main vladimir-pecanac-main commented Aug 30, 2026

Copy link
Copy Markdown
Collaborator

Updates the aspnetcore-features/UrlEncodingAndDecoding sample for the article refresh.

  • Retargets both projects from net7.0 to net10.0.
  • Bumps test packages: Microsoft.NET.Test.Sdk 18.9.0, MSTest.TestAdapter and MSTest.TestFramework 4.3.3, coverlet.collector 10.0.1. The obsolete DataTestMethod attribute is replaced with TestMethod (MSTEST0044), so the build is warning-free.
  • Adds the UrlEncoder.Default.Encode() example to Program.cs with a test asserting its exact output, and a value-level encoding line showing the shape real code should use (the existing examples encode a whole URL, which is good for comparing encoders and wrong as a pattern to copy).
  • Adds tests for Uri.EscapeDataString() on a 100,000-character string, on the encodeURIComponent-safe characters !'()*~, and for the Uri constructor with a bare path (/foo) and with a protocol-relative address (//example.com).

Build and test locally, SDK 10.0.302, Windows:

Build succeeded.
    0 Warning(s)
    0 Error(s)

Passed!  - Failed:     0, Passed:    17, Skipped:     0, Total:    17

Console output:

http%3a%2f%2fexample.com%2fresource%3ffoo%3dbar+with+space%23fragment
http%3A%2F%2Fexample.com%2Fresource%3Ffoo%3Dbar+with+space%23fragment
http%3A%2F%2Fexample.com%2Fresource%3Ffoo%3Dbar%20with%20space%23fragment
http%3A%2F%2Fexample.com%2Fresource%3Ffoo%3Dbar%20with%20space%23fragment
http://example.com/resource?foo=bar with space#fragment
http://example.com/resource?foo=bar with space#fragment
http://example.com/resource?foo=bar with space#fragment
https://example.com/search?q=bar%20with%20space

On the two Uri constructor tests

The two literals do not behave the way they are usually described, and they do not behave the same way as each other.

new Uri("//example.com") does not throw on either platform. It parses the leading double slash as a host and yields the absolute file URI file://example.com/, and Uri.TryCreate(..., UriKind.Absolute, out _) reports success. Confirmed on Windows locally and on ubuntu in CI, where the same assertions pass unchanged.

new Uri("/foo") is platform-dependent. It throws UriFormatException on Windows, but on Unix a bare path is a rooted local path, so it parses as the absolute file URI file:///foo. The first CI run on this branch asserted the Windows outcome everywhere and failed on ubuntu with "Expected exception of exact type UriFormatException but no exception was thrown", which is how the difference was found; the test now asserts each platform's real behaviour and both legs are green.

UriKind.Relative behaves the same everywhere and is asserted outside the platform branch.

…behaviour tests

- Retarget UrlEncodingAndDecoding and Tests from net7.0 to net10.0.
- Bump test packages: Microsoft.NET.Test.Sdk 18.9.0, MSTest.TestAdapter and
  MSTest.TestFramework 4.3.3, coverlet.collector 10.0.1. Replace the obsolete
  DataTestMethod attribute with TestMethod (MSTEST0044).
- Add the UrlEncoder.Default.Encode() example to Program.cs and a test asserting
  its exact output, plus a value-level encoding line showing the shape real code
  should use.
- Add tests for Uri.EscapeDataString on a 100,000-character string (no length
  limit on modern .NET), on the encodeURIComponent-safe characters, and for the
  Uri constructor with a bare path and with a protocol-relative address.
new Uri("/foo") throws UriFormatException on Windows but parses as the
absolute file URI file:///foo on Unix, where a bare path is a rooted local
path. The test now asserts the real behaviour on each platform instead of the
Windows one everywhere. new Uri("//example.com") is unchanged: it yields
file://example.com/ on both.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant