DefaultHttpContext is an ASP.NET Core implementation of HttpContext for unit-testing custom middleware. Create one, configure its request, invoke the middleware, and inspect the resulting behavior. section 36.1
A typical middleware test uses it to:
- Set Request.Path to exercise a matching or nonmatching request. section 36.1
- Pass the context to middleware and verify that the next RequestDelegate was called when the path does not match. section 36.1
- Replace Response.Body with a MemoryStream when the test must read response content; the default Stream.Null discards it. section 36.5 section 36.1
Testing a ping response
Create a DefaultHttpContext, assign a MemoryStream to Response.Body, set Request.Path to "/ping", invoke the middleware, then rewind and read the stream to verify the response body contains "pong". The same test can check the status code and text/plain content type. section 36.1
