Mock HttpContext for ASP.NET Core Unit Testing

Tiago Araújo
C# Programming
Published in
4 min readAug 25, 2021
Photo by Ferenc Almasi on Unsplash

What’s the first thing that comes to your mind when you realize your unit test needs to mock an HttpContext? 😭 It's not a difficult task, but may you agree that is boring.

I’ve often wondered if I could find a better way to write unit tests faster to decrease the resistance to writing them and even to improve their quality. So, I get down to business and wrote an HttContext Mock implementation that initializes out of the box all the properties and methods, without any setup.

HttpContextMoq

My proposal is the HttpContextMoq package available on nuget.org and source code on GitHub.

For example, If you need to mock a Referer header on your request, you need to write 5 lines of code, 6 if you consider the Mock<HttpContext> creation.

Image. Mock a Request.Header Referer with Mock<HttpContex>

With HttpContextMock you only need 1 line to the referer header setup.

Image. Mock a Request.Header Referer with HttpContextMock

What is the HttpContextMock class?

HttpContextMock is an implementation of AspNetCore.Http.HttpContext that stores a Mock<HttpContext> instance and works as a proxy for the real Mock.

Image. HttpContextMock inherits from Microsoft.AspNetCore.Http.HttpContext

The HttpContextMock constructor initializes a Mock for Request, Response, Header and etc to work as a proxy as well.

Image. HttpContextMocks class constructor

For example, the HttpRequest follows the same pattern and so one, and so one, until the last class pure method and properties of your request context.

Image. HttpContextMock class constructor

So, when you create an HttpContextMock instance all the setups are available and initialized.

How to use the HttpContextMoq?

Import the Nuget package HttpContextMoq:

Or, If you want to customize the implementation you can clone or/and copy the code to your project. See the source code here:

HttpContextMock Extensions

HttpContextMock has 5 extensions for the most uses cases of an ASP.NET Core unit testing:

  • Request Url HttpContextMock.SetupUrl()
  • Request Headers HttpContextMock.SetupRequestHeaders()
  • Request Cookies HttpContextMock.SetupRequestCookies()
  • Session HttpContextMock.SetupSession()
  • Request Services HttpContextMock.SetupRequestService()

In the image below, we can see a very useful mock of the URL https://localhost/mocks?assert=true that will set up all the request and query properties and methods.

Image. Setup entire HttpContext with Url extension

See the SetupUrl source code here.

Session extension, similarly to URL, set up the Session and Features properties of HttpContext. See the example unit test below.

Image. Setup Session extension

The Request.Headers, Request.Cookies and RequestServices extensions try to step these properties with minimal effort also.

Image. Setup Request.Headers extension
Image. Setup Request.Cookies extension
Image. Setup RequestServices extension

Use Cases

I recommend seeing the code Samples and some Unit Tests to check how you can set up your HttpContext by Url, Session, Headers, Cookies, Query and etc.

Conclusion

Maybe this is not the best solution for you, and maybe not for everyone, but in my case, it saved me a lot of time along thousands of unit tests that I wrote with HttpContext.

If you have any suggestions, improvements, or something else, open an issue on Github or left a comment. I will be happy to know that this can help more people.

Tests safe.

--

--

Tiago Araújo
C# Programming

SOFTWARE ENGINEER to make money — Listen to MUSIC for energy — RIDE for no reason to the heart — SHARE with loved ones — The DREAM: the last 3 in the same box