liveBook · Manning

TestServer

As explained in ASP.NET Core in Action, 3e

TestServer is an ASP.NET Core in-memory server that accepts test requests and returns responses without requiring a network server.

TestServer is an in-memory ASP.NET Core server for sending HTTP requests and receiving responses without opening a network listener. It lets middleware and other application components run inside a framework-like request pipeline. section 36.3 section 36.5

A small TestServer integration test follows these steps:

  • Create a HostBuilder and configure the test application's middleware. section 36.3.1
  • Call UseTestServer() to replace the default Kestrel server. section 36.3.1
  • Start the host, obtain an HttpClient with GetTestClient(), and send an in-memory request. section 36.3.1

Testing middleware at /ping

Configure a HostBuilder with StatusMiddleware, enable UseTestServer(), start the host, and call client.GetAsync("/ping"). The middleware receives a request that appears equivalent to a network request, although the request remains entirely in memory.

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains setup, host startup, client creation, and response assertions, while deferring full-application customization to WebApplicationFactory.
Examples
Demonstrates StatusMiddleware responding to an in-memory GET request at /ping.
What it assumes
Assumes basic ASP.NET Core middleware and hosting concepts while keeping the test application's configuration deliberately simple.
What you can do after
Apply Microsoft.AspNetCore.TestHost to send in-memory requests and verify middleware responses.
How it connects
Connects TestServer with integration testing, middleware, HttpClient, Kestrel, HostBuilder, and WebApplicationFactory.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.