title: Unit test webapi controller with response headers
body: <p>I'm trying to learn webapi and have stumbled across a problem. The training course I was doing showed how to do paging by returning a response header with the next and previous link. However it uses <code>HttpContext.Current.Response.Headers.Add()</code> to send back the next link, previous link, and total pages.</p>
  
  <p>I am also trying to implement unit tests for the controllers. Problem seems to be that the <code>HttpContext.Current</code> is <code>null</code> when running through a unit test. I read somewhere that I shouldn't be <code>HttpContext.Current</code> for webapi as it's not testable, but I'm not sure what I should be using instead.</p>
  
  <p>Here is my contoller code:</p>
  
  <pre><code>public partial class CandidateManagerController
  {
      private readonly ICandidateManager _candidateManagerV2;
  
      public CandidateManagerController(ICandidateManager candidateManager)
      {
          _candidateManagerV2 = candidateManager;
      }
  
      [VersionedRoute("CandidateManager", 2, Name="CandidateManagerV2")]
      public IHttpActionResult Get(int page = 1, int pageSize = 1)
      {
          try
          {
              var totalCount = 0;
              var totalPages = 0;
  
              var result = _candidateManagerV2.GetCandidates(out totalCount, out totalPages, page, pageSize);
  
              var urlHelper = new UrlHelper(Request);
  
  
              var prevLink = page &gt; 1
                  ? urlHelper.Link("CandidateManagerV2",
                      new
                      {
                          page = page - 1,
                          pageSize = pageSize,
                      })
                  : "";
  
  
              var nextLink = page &lt; totalPages ? urlHelper.Link("CandidateManagerV2",
                  new
                  {
                      page = page + 1,
                      pageSize = pageSize
                  }) : "";
  
              var paginationHeader = new
              {
                  currentPage = page,
                  pageSize = pageSize,
                  totalCount = totalCount,
                  totalPages = totalPages,
                  previousPageLink = prevLink,
                  nextPageLink = nextLink
              };
  
              HttpContext.Current.Response.Headers.Add("X-Pagination", Newtonsoft.Json.JsonConvert.SerializeObject(paginationHeader));
  
  
  
              return Ok(result);
          }
          catch (Exception exp)
          {
              return InternalServerError();
          }
      }
  
  }
  </code></pre>
  
  <p>Here is my unit test. Please note I'm using Nunit and Moq:</p>
  
  <pre><code>[TestFixture]
  public class CandidateManagerControllerV2Tests
  {
  
  
      [Test]
      [Category("CandidateManagerController Unit Tests")]
      public void Should_Return_List_Of_Candidate_Objects()
      {
  
          var testList = new List&lt;Candidate&gt;();
          testList.Add(new Candidate() { CandidateId = 1, Name = "Mr", Surname = "Flibble" });
          testList.Add(new Candidate() { CandidateId = 2, Name = "Arnold", Surname = "Rimmer" });
  
          var totalCount = 0;
          var totalPages = 0;
          var mockManager = new Mock&lt;ICandidateManager&gt;();
          mockManager.Setup(x =&gt; x.GetCandidates(out totalCount, out totalPages, It.IsAny&lt;int&gt;(), It.IsAny&lt;int&gt;())).Returns(testList);
  
          var controller = new CandidateManagerController(mockManager.Object);
          SetupControllerForTests(controller);
  
          var result = controller.Get(1, 1);
      }
  
      private static void SetupControllerForTests(ApiController controller)
      {
          var config = new HttpConfiguration();
          var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost/api/candidatemanager");
          var route = config.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{id}");
          var routeData = new HttpRouteData(route, new HttpRouteValueDictionary { { "controller", "products" } });
  
          controller.ControllerContext = new HttpControllerContext(config, routeData, request);
          controller.Request = request;
          controller.Request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config;
          controller.ActionContext=new HttpActionContext();
      }
  
  }
  </code></pre>
  
  <p>I'm hoping someone will be able to help me. It could be that I've been led down a wrong path with the way to implement paging. However it is likely that I'd need to add a response header for something any way.</p>