How to setup Request.Header in FakeHttpContext for Unit Testing

I just discovered that with HttpRequestMessage class, you can easily add headers for testing your WebAPI controllers without having to create any fake HttpContext.

var request = new HttpRequestMessage(HttpMethod.Get, "http://stackoverflow");
request.Headers.Add("deviceId","1234");
_myController.Request = request;

I'm using .Net Core 3.1 and the Moq framework. I created a DefaultHttpContext, set the headers there, finally assigned it to the controller ControllerContext constructor.

var httpContext = new DefaultHttpContext();
httpContext.Request.Headers["X-Example"] = "test-header";
var controller = new ExampleController(mockClient.Object, mockLogger.Object) 
{ 
    ControllerContext = new ControllerContext()
    {
        HttpContext = httpContext
    }
};

Source: https://www.thecodebuzz.com/unit-test-mock-httprequest-in-asp-net-core-controller/