301 (Moved Permanently) error ASP.NET Core integration tests and XUnit

1.5k Views Asked by At

I have a project with integration tests that use the Microsoft.AspNetCore.TestHost as test server.

On my local development machine, when I run dotnet test on my project, all tests pass. I have setup a GitLab-CI runner on our server, when I run 'dotnet test' on the same project on that server, all tests with an HTTP request to the TestHost fail with the same error:

Response status code does not indicate success: 301 (Moved Permanently).

Test setup:

_server = new TestServer(new WebHostBuilder().UseStartup(startup));
_client = _server.CreateClient();

Example of a failing test:

_response = _client.GetAsync($"/api/inventory").Result;
var responseResult = _response.Content?.ReadAsStringAsync().Result;
_response.EnsureSuccessStatusCode();

Test fails at _response.EnsureSuccessStatusCode();

The same and only version of the .NETCore SDK (2.1.302) is installed on both machines. Project targets .NETCoreApp v2.1. IIS is configured on the server only with default web site (IIS shouldn't matter with AspNetCore.TestHost anyway).

1

There are 1 best solutions below

0
On

I have found the problem. The ASPNETCORE_ENVIRONMENT environment variable was not set on the server so the test was picking out the production configuration file instead of the development configuration file, which specified to use HTTP to HTTPS redirection.