Mock HttpContext for unit testing a .NET core MVC controller?
Asked Answered
C

3

147

I have a function in a controller that I am unit testing that expects values in the header of the http request. I can't initialize the HttpContext because it is readonly.

My controller function expects a http request header value for "device-id"

[TestMethod]
public void TestValuesController()
{
    ValuesController controller = new ValuesController();

    //not valid controller.HttpContext is readonly
    //controller.HttpContext = new DefaultHttpContext(); 

    var result = controller.Get();
    Assert.AreEqual(result.Count(), 2);
}

Is there a straight-forward way to do this without using a third party library?

Cheryle answered 30/12, 2016 at 17:15 Comment(4)
Don't use HttpContext? The whole point of using controllers is that the data comes through the controller's parameters. If your controller uses the HttpContext to read data as if it was a WebForms page, you have a problem.Jews
@PanagiotisKanavos The value in the header is a piece of information indicating which mobile device the call comes from. This is necassary to retrieve the correct data. The device ID is in the header because the ID is needed for authentication, which is handled by a custom action filter. I could could pass the device ID as a route parameter but it would be redundantCheryle
Check FromHeaderAttribute but also check the duplicate. HttpContext is injectable through configuration nowJews
I suggest you edit your question to specify exactly what you want (access to header fields to identify mobile devices). The ASP.NET documentation seems to be going through a .... "transition" period to put it kindly, with documentation pages missing. Check this almost identical question that asks how to route mobile devicesJews
C
316

I was able to initialize the httpcontext and header in this way:

[TestMethod]
public void TestValuesController()
{
    ValuesController controller = new ValuesController();
    controller.ControllerContext = new ControllerContext();
    controller.ControllerContext.HttpContext = new DefaultHttpContext();
    controller.ControllerContext.HttpContext.Request.Headers["device-id"] = "20317";
    var result = controller.Get();
    //the controller correctly receives the http header key value pair device-id:20317
    ...
}
Cheryle answered 30/12, 2016 at 17:32 Comment(1)
Simple perfect solution.Myogenic
D
22

Rather than mocking out the HTTPContext, it is probably a better idea to map the header into a parameter on the method. For example, in the controller at the bottom of this answer, the id parameter is set to the value header with a name equal to "device-id"... The unit test then becomes

[TestMethod]
public void TestValuesController()
{
    ValuesController controller = new ValuesController();
    var result = controller.GetHeaderValue("27");
    Assert.AreEqual(result, "27");
}

While you can mock the HttpContext, in my opinion it is something that should be avoided unless you have no choice. The documentation for the FromHeaderAttribute can be found here FromHeaderAttribute Class.

public class ValuesController: Controller
{
    public string GetHeaderValue([FromHeader(Name = "device-id")] string id)
    {
        return id;
    }
}
Drew answered 30/12, 2016 at 21:53 Comment(1)
In my case, IIRC, it was a requirement to include it in http header because the same value needed to be evaluated in a .net core middleware componentCheryle
H
7

For people in need of a header but also additional data in their HttpContext, you can do so by initializing the context with features thanks to the second constructor of the DefaultHttpContext class:

1. Create a header dictionary with the headers you need:

var headers = new Dictionary<string, StringValues>
{
   { "myHeaderKey", "myHeaderValue" },
};
var headerDictionary = new HeaderDictionary(headers)

2. Create an HttpRequestFeature with the previously created header dictionary:

var requestFeature = new HttpRequestFeature()
{
    Headers = headerDictionary,
};

3. Create a Feature collection containing the feature previously created :

 var features = new FeatureCollection();

features.Set<IHttpRequestFeature>(requestFeature);

4. Initialize the DefaultHttpContext with the feature collection, and set it as the HttpContext of your controller:

var httpContext = new DefaultHttpContext(features);

var controller = new MyController();
controller.ControllerContext = new ControllerContext();
controller.ControllerContext.HttpContext = httpContext;

The controller's context will have the correct headers set, and you can still feed the context with more data as needed by setting additional HttpContext attributes to the featureCollection before instantiating the DefaultHttpContext (like feature.Set<IQueryFeature>(new QueryFeature(...)) for the query string for instance).

PS: For a more in-depth explanation on using features to mock (and unit testing in general) an HttpContext, see: https://weblogs.asp.net/ricardoperes/unit-testing-the-httpcontext-in-controllers

Hillier answered 31/12, 2021 at 19:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.