Search code examples
c#.netunit-testinghttp-headersfakeiteasy

How to build an HttpResponseHeaders for the FakeItEasy


I'm using C# and I need to Test one of my methods that accept System.Net.Http.Headers.HttpRequestHeaders as a parameter. We're using FakeItEasy for the test.

But it seems that the HttpResponseHeaders - does not have the construcotr (and it's sealed), and it uses HttpHeader as base. HttpHeader - has constructor but the Header property only allows get.

Is there a way to build dummy/fake HttpResponseHeaders or HttpResponseMessage and preset the required Header value in it?


Solution

  • FakeItEasy can't fake a sealed class, nor create a Dummy out of one that doesn't have an accessible constructor, but you could try this:

    var message = new HttpResponseMessage();
    var headers = message.Headers;
    headers.Add("HeaderKey", "HeaderValue");
    

    Just because the Headers are get-only doesn't mean you can't mutate the list.