dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.22k stars 9.95k forks source link

TestServer can not resolve route for URI with HTTP encoded parameters #36598

Closed ClemensOesterle closed 3 years ago

ClemensOesterle commented 3 years ago

Describe the bug

Starting point is the .NET CLI scaffolded webapi project dotnet new webapi--name TestHostIssue

Adapt the default controller a little so it is capable of a parameter (HTTP Uri decoded)

    [ApiController]
    public class WeatherForecastController : ControllerBase
    {
        private static readonly string[] Summaries = new[]
        {
            "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
        };

        private readonly ILogger<WeatherForecastController> _logger;

        public WeatherForecastController(ILogger<WeatherForecastController> logger)
        {
            _logger = logger;
        }

        [HttpGet]
        [Route("api/[controller]/{regionPath?}")]
        public WeatherForecast Get([FromRoute] string regionPath)
        {
            var rng = new Random();
            return new WeatherForecast()
            {
                RegionPath = System.Web.HttpUtility.UrlDecode(regionPath ?? "no region specified")
            };
        }
    }

To Reproduce

This endpoint can successfully be consumed with parameters like

Doing the same with a unit test using the TestServer - it can not resolve the route when applying the parameter England/London

using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using System;
using System.Threading.Tasks;
using TestHostIssue;
using Xunit;
using FluentAssertions;

namespace TestHostIssueTest
{
    public class UnitTest1
    {
        internal static TestServer CreateTestServer()
        {
            IWebHostBuilder builder = new WebHostBuilder()
                .UseStartup<Startup>();
            return new TestServer(builder);
        }

        [Theory]
        [InlineData("London")]
        [InlineData("England/London")]
        public async Task Test1(string filePath)
        {
            // ARRANGE           
            var server = CreateTestServer();
            string path = $"/api/WeatherForecast/{ System.Web.HttpUtility.UrlEncode(filePath) }";
            var request = server.CreateRequest(path);

            // ACT
            var response = await request.GetAsync();

            // ASSERT
            response.StatusCode.Should().Be(System.Net.HttpStatusCode.OK, $"{ path } should return 200");
        }
    }
}

TestExplorer

Further technical details

Runtime Environment: OS Name: Windows OS Version: 10.0.19042 OS Platform: Windows RID: win10-x64 Base Path: C:\Program Files\dotnet\sdk\5.0.301\

Host (useful for support): Version: 5.0.7 Commit: 556582d964

.NET SDKs installed: 5.0.300 [C:\Program Files\dotnet\sdk] 5.0.301 [C:\Program Files\dotnet\sdk]

.NET runtimes installed: Microsoft.AspNetCore.All 2.1.28 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.App 2.1.28 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.1.16 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 5.0.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 2.1.28 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.16 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 5.0.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 5.0.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.WindowsDesktop.App 3.1.16 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 5.0.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 5.0.7 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

To install additional .NET runtimes or SDKs: https://aka.ms/dotnet-download

- The IDE (VS / VS Code/ VS4Mac) you're running on, and its version

Microsoft Visual Studio Enterprise 2019 Version 16.10.1

Tratcher commented 3 years ago

This looks like a duplicate of https://github.com/dotnet/aspnetcore/issues/28965 that was recently fixed. Can you test this on 6.0 rc1?

ClemensOesterle commented 3 years ago

Yes, with 6.0 RC1 everything works fine! Thanks