title: C# - Unit testing a WebApi controller
body: <p>I am used to test Web controllers in Java in which a real request is being sent to the controller even under a unit test.</p>
  
  <p>When it comes to C# the doc I am seeing like <a href="https://docs.microsoft.com/en-us/aspnet/web-api/overview/testing-and-debugging/unit-testing-with-aspnet-web-api" rel="nofollow noreferrer">here</a> or <a href="https://www.c-sharpcorner.com/article/unit-testing-controllers-in-web-api/" rel="nofollow noreferrer">here</a> it seems to check the call workflow to the controller methods but not an entire http message workflow.</p>
  
  <p>In particular, say i have the following controller:</p>
  
  <pre><code>public class SessionController : ApiController
  {
      [HttpDelete]
      [ResponseType(typeof(IHttpActionResult))]
      [Route("api/v1/sessions/{session-id}")]
      [ActionName("stop_session")]
      [ValidateModel]
      public IHttpActionResult StopSession([FromUri(Name = "session-id")] Guid sessionId)
      {
          Console.WriteLine($"Stopping session {sessionId}");
  
          SessionRepository.Instance.Remove(sessionId);
  
          return Ok();
      }
  }
  </code></pre>
  
  <p>If i create the following unit test:</p>
  
  <pre><code>    [TestMethod]
      public void TestStopSession()
      {
          SessionController controller = new SessionController()
          {
              Request = new HttpRequestMessage
              {
                  Method = HttpMethod.Get,
                  RequestUri = new Uri("http://localhost:9000/api/v1/zada")
              }
          };
          controller.Configuration = new HttpConfiguration();
  
          var res = controller.StopSession(new Guid());
  
          Assert.IsInstanceOfType(res, typeof(OkResult));
      }
  </code></pre>
  
  <p>As you can see, in test I am putting a different URL and even a different method and still the unit test passes successfully.</p>
  
  <p>What is the way to unit test the entire HTTP workflow?</p>