ASP.NET Core – How to unit test your middleware class

There are three requirements for unit testing a middleware class: Mock out RequestDelegate by defining a lambda that returns a Task (Task.FromException, Task.FromResult, or Task.FromCanceled). Mock out HttpContext by using DefaultHttpContext. The middleware function needs to be awaited, so your unit test needs to be defined with async Task. Here’s an example: This is a … Read more

ASP.NET Core – How to add your own middleware function

Middleware functions have access to requests before they are sent to the controllers. Likewise, they have access to responses from the controllers before they are returned to the client. This is useful for doing things like logging the request and response, generating stats about requests, handling exceptions, and many more scenarios. In this article, first … Read more