BaseTestServer
BaseTestServer provides an in-memory ASP.NET Core test host using Microsoft's TestServer. It allows integration testing of controllers and framework middleware without opening a network port.
Features
- In-memory hosting: Runs the ASP.NET application using
TestServer. - Framework middleware: Initializes the framework services and middleware pipeline.
- Custom configuration: Supports callbacks for services, application configuration, and logging.
- HTTP helpers: Sends GET requests and returns either the response body or the complete response message.
- Rebuild: Recreates the test host when services or middleware need to be reconfigured.
Behavior
The host and HttpClient are initialized lazily on first access to Client.
GetResponseText() returns the response body. Unsuccessful responses are logged but do not throw automatically.
GetResponseMessage() returns the complete HttpResponseMessage, allowing tests to inspect the status code, headers, and content.
RebuildClient() disposes the current TestServer. The next request initializes a new host and client.
Example
public class UserTests : BaseTestServer
{
[TestMethod]
public void GetUser()
{
var response = GetResponseMessage("/api/users?id=1");
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
}
}
Caveats
- Requests are sent through the in-memory test host; no real network port is opened.
- The supplied request helpers use GET.
- Non-success responses returned by GetResponseText() are logged but do not automatically fail the test.