title: Unit Testing / Integration Testing Web API with HttpClient in Visual Studio 2013
body: <p>I am having a hard time trying to test my API controller with Visual Studio 2013.  My one solution has a Web API Project and a Test project. In my test project, I have a Unit Test with this:</p>
  
  <pre><code>[TestMethod]
  public void GetProduct()
  {
      HttpConfiguration config = new HttpConfiguration();
      HttpServer _server = new HttpServer(config);
  
      var client = new HttpClient(_server);
  
      var request = new HttpRequestMessage
      {
          RequestUri = new Uri("http://localhost:50892/api/product/hello"),
          Method = HttpMethod.Get
      };
  
      request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
  
      using (var response = client.SendAsync(request).Result)
      {
          Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
  
          var test = response.Content.ReadAsAsync&lt;CollectionListDTO&gt;().Result;
      }
  }
  </code></pre>
  
  <p><strong>I keep getting a 404</strong>. I have tried running my API with one instance of Visual Studio (IIS Express), and trying to debug this Unit Test in another instance. But no luck.  I have verified that I can put this URL in a browser (when one Visual Studio is debugging) and I see my JSON response.  But I can't figure out how to get it to work with my unit test and <code>HttpClient</code>. I have tried to find examples online but can't seem to find one. Can someone help?</p>
  
  <p><strong>UPDATE 1:</strong>
  I tried adding in a route but nothing happened.</p>
  
  <pre><code>HttpConfiguration config = new HttpConfiguration();
  
  // Added this line
  config.Routes.MapHttpRoute(name: "Default", routeTemplate: "api/product/hello/");
  
  HttpServer _server = new HttpServer(config);
  
  var client = new HttpClient(_server);
  
  [...rest of code is the same]
  </code></pre>
  
  <p>Here is my API Controller</p>
  
  <pre><code>[HttpGet]
  [Route("api/product/hello/")]
  public IHttpActionResult Hello()
  {
       return Ok();
  }
  </code></pre>
  
  <p><strong>UPDATE Resolution:</strong>
  I was able to get it to work if I new up <code>HttpClient</code> without a <code>HttpServer</code> object.  I would still need to have two instances of VS running though. 1 running my API code and another to run the Unit Test. </p>
  
  <p>Here is a working method.</p>
  
  <pre><code>[TestMethod]
  public void Works()
  {
      var client = new HttpClient(); // no HttpServer
  
      var request = new HttpRequestMessage
      {
          RequestUri = new Uri("http://localhost:50892/api/product/hello"),
          Method = HttpMethod.Get
      };
  
      request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
  
      using (var response = client.SendAsync(request).Result)
      {
          Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
      }
  }
  </code></pre>
  
  <p>Anyone know why it doesn't work with a <code>HttpServer</code> and a <code>HttpConfiguration</code> passed into <code>HttpClient</code>? I have seen many examples that use this.</p>