title: WebApi Unit Test HttpContextHelper with Mock
body: <p>I have a method I'm trying to mock (MOQ) for unit testing but I'm having difficulty as all the props are read only and mocking fails as it is a sealed class. This method is hung off a controller and I have all that infrastructure in place.</p>
  
  <p>First here is the method I'm trying to test.</p>
  
  <pre><code>public CarResponse(Common.Models.Car car)
  {
      string currentAcceptType = HttpContextHelper.Current.Request.Headers["Accept"];
  
      ....removed for brevity.....
  
  }
  </code></pre>
  
  <p>I have tried to inject an instantiated object and that would work but my Headers are read only so I can't see how to set the headers to what I want??</p>
  
  <pre><code>HttpRequest cv = new HttpRequest(null, "http://localhost:24244", "/cars/_services/addNewPickup");
  //so far so good but then when I want to add my headers every property is read only.
  cv.Headers.Add("qewr", "adsf"); //This fails.
  </code></pre>
  
  <p><a href="https://msdn.microsoft.com/en-us/library/system.web.httprequest_properties(v=vs.110).aspx" rel="nofollow">MSDN</a></p>
  
  <p>Ultimately I would be using it as part of the larger fake "session" for testing.</p>
  
  <pre><code>HttpContext.Current = new HttpContext(cv, new HttpResponse(null));
  </code></pre>
  
  <p>Mocking of HttpRequest fails as it is a sealed class.</p>
  
  <p>So I'm kinda stumped on how to test this unit of code.</p>