This post is about unit testing your middleware components. For testing middleware ASP.NET team introduced a TestHost package, this package contains a TestServer which can create an ASP.NET request processing pipeline and helpers to submit requests to the pipeline as if it is a real server. Requests made with these helpers never hit the network as they are processed directly in memory. . You can verify the content type, http status code etc. To write unit test, you require “Microsoft.AspNet.TestHost” package and dnx supported XUnit packages as well.
Here is the unit test for HelloWorld middleware component.
In this you are creating an instance of Test server, requesting for “/” url using client. And verifying the Http Status code. This is very minimal, here is the unit test for Html Minification middleware.
In this code, server is responding with a HTML content with whitespace, and verifying whether the middleware removes it successfully.
Here is the project.json file
Happy Programming :)
What do you think? I would like to hear your thoughts, suggestions, and questions in the comments section below.