title: How to get and set http headers in an Action, the testable way
body: <p>I have an action that returns either a FileContentResult or a NotModifiedResult, which is a custom result type that returns HTTP 304 to indicate that the requested resource has not been modified, like this:</p>
  
  <pre><code>[ReplaceMissingPicture(Picture = "~/Content/Images/nothumbnail.png", MimeType = "image/png")]
  public ActionResult Thumbnail(int id)
  {
      var item = Service.GetItem(id);
  
      var requestTag = Request.Headers["If-None-Match"] ?? string.Empty;
      var tag = Convert.ToBase64String(item.Version.ToArray());
  
      if (tag == requestTag)
      {
          return new NotModifiedResult();
      }
  
      if (item.Thumbnail != null)
      {
          var thumbnail = item.Thumbnail.ToArray();
          var mime = item.PictureMime;
  
          Response.AppendHeader("ETag", tag);
  
          return File(thumbnail, mime);
      }
      else
      {
          return null;
      }
  }
  </code></pre>
  
  <p>This action needs to access the Response object, which is of course not present during testing, so that makes this action untestable. I could add conditional statements around it, so that it runs during testing, but then I can't test for the headers being set correctly.</p>
  
  <p>What would be a solution to this problem?</p>
  
  <p>FYI, the ReplaceMissingPicture filter returns a specific resource in case null was returned from this action, to keep the MapPath() call out of the controller for the very same reason.</p>