title: Unit test WebApi2 passing header values
body: <p>I am working on a project using WebApi2. With my test project I am using Moq and XUnit.</p>
  
  <p>So far testing an api has been pretty straight forward to do a GET like </p>
  
  <pre><code>  [Fact()]
      public void GetCustomer()
      {
          var id = 2;
  
          _customerMock.Setup(c =&gt; c.FindSingle(id))
              .Returns(FakeCustomers()
              .Single(cust =&gt; cust.Id == id));
  
          var result = new CustomersController(_customerMock.Object).Get(id);
  
          var negotiatedResult = result as OkContentActionResult&lt;Customer&gt;;
          Assert.NotNull(negotiatedResult);
          Assert.IsType&lt;OkNegotiatedContentResult&lt;Customer&gt;&gt;(negotiatedResult);
          Assert.Equal(negotiatedResult.Content.Id,id);
      }
  </code></pre>
  
  <p>Now I am moving onto something a little complicated where I need to access value from the request header.</p>
  
  <p>I have created my own Ok() result by extending the IHttpActionResult</p>
  
  <pre><code>   public OkContentActionResult(T content,HttpRequestMessage request)
      {
          _request = request;
          _content = content;
      }
  </code></pre>
  
  <p>This allows me to have a small helper that reads the header value from the request.</p>
  
  <pre><code> public virtual IHttpActionResult Post(Customer customer)
      {
          var header = RequestHeader.GetHeaderValue("customerId", this.Request);
  
          if (header != "1234")
  </code></pre>
  
  <p>How am I meant to setup Moq with a dummy Request?</p>
  
  <p>I have spent the last hour or so hunting for an example that allows me to do this with webapi however I cant seem to find anything. </p>
  
  <p>So far.....and I am pretty sure its wrong for the api but I have</p>
  
  <pre><code>      // arrange
          var context = new Mock&lt;HttpContextBase&gt;();
          var request = new Mock&lt;HttpRequestBase&gt;();
          var headers = new NameValueCollection
          {
              { "customerId", "111111" }
          };
          request.Setup(x =&gt; x.Headers).Returns(headers);
          request.Setup(x =&gt; x.HttpMethod).Returns("GET");
          request.Setup(x =&gt; x.Url).Returns(new Uri("http://foo.com"));
          request.Setup(x =&gt; x.RawUrl).Returns("/foo");
          context.Setup(x =&gt; x.Request).Returns(request.Object);
          var controller = new Mock&lt;ControllerBase&gt;();
          _customerController = new CustomerController()
          {
              //  Request = request,
  
          };
  </code></pre>
  
  <p>I am not really sure what next I need to do as I havent needed to setup a mock HttpRequestBase in the past.</p>
  
  <p>Can anyone suggest a good article or point me in the right direction?</p>
  
  <p>Thank you!!! </p>