Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FakeNavigationManager removes encoded uri's #458

Closed
linkdotnet opened this issue Jul 19, 2021 · 3 comments · Fixed by #460
Closed

FakeNavigationManager removes encoded uri's #458

linkdotnet opened this issue Jul 19, 2021 · 3 comments · Fixed by #460
Labels
bug Something isn't working

Comments

@linkdotnet
Copy link
Collaborator

Describe the bug

When passing an encoded string (like: http://localhost/white%20space) to the FakeNavigationManager.NavigateTo method, the encoded part will be decoded. The "normal" NavigationManager used in Blazor pages does not have this behavior.
This is specially interesting when you use functions like Uri.EscapeDataString

Example:
The following test will fail:

[Fact]
public void ShouldConsiderEncodedData()
{
    var fake = Services.GetService<NavigationManager>();
    var encoded = "with%20whitespace"; // You could also use var encoded = Uri.EncodeDataString("with whitespace");

    fake.NavigateTo(encoded);

    fake.Uri.Should().EndWith("with%20whitespace");
}

with:

Expected fake.Uri "http://localhost/with whitespace" to end with "with%20whitespace"

If you need a small working example for the "real" NavigationManager:

@inject NavigationManager navigationManager
<button @onclick="Navigate">Navigate</button>

@code {
    private void Navigate()
    {
        var encoded = Uri.EscapeDataString("with whitespace");
        navigationManager.NavigateTo(encoded);
    }
}

This will navigate to https://localhost:5001/with%20whitespace

Expected behavior:

The FakeNavigationManager does not decode encoded uri's.

Version info:

  • bUnit version: 1.2.36-beta
  • .NET 5
  • Windows 10 21H1
@egil
Copy link
Member

egil commented Jul 21, 2021

Hmm not sure if we can fix this, take a look at the very simple implementation of the fake here: https://github.com/bUnit-dev/bUnit/blob/main/src/bunit.web/TestDoubles/NavigationManager/FakeNavigationManager.cs

Are we sure that the real navigation manager isn't doing this escaping when we pass it a string with a space in?

Any suggestions for a fix? PR welcome :)

@linkdotnet
Copy link
Collaborator Author

linkdotnet commented Jul 21, 2021

The issue is the ToString method on the URI. This will force unescaping.
See MSDN.

I would make a PR which uses AbsoluteUri instead of ToString()
Was there a reason to explicitly using ToString in NavigateToCore?

@egil
Copy link
Member

egil commented Jul 21, 2021

Was there a reason to explicitly using ToString in NavigateToCore?

Only my lack of knowledge 😂

@egil egil added the bug Something isn't working label Jul 21, 2021
@egil egil closed this as completed in #460 Jul 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants