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
"London" or
"England/London" (encoded)
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");
}
}
}
Further technical details
ASP.NET Core version
net5.0
Include the output of dotnet --info
.NET SDK (reflecting any global.json):
Version: 5.0.301
Commit: ef17233f86
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
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)
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
Further technical details
net5.0
dotnet --info
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
Microsoft Visual Studio Enterprise 2019 Version 16.10.1