title: How to test web API JSON response?
body: <p>I'm trying to setup unit tests for my web API. I've hacked together some test code from bits and pieces I've found on the web. I've got as far as sending the test request off and receiving a response, but I'm stuck on testing the response.</p>
  
  <p>So here's what I've got so far. This is using the xunit test package, but I don't think that matters for what I'm trying to achieve.</p>
  
  <p>(Apologies for the mash of code)</p>
  
  <pre><code>[Fact]
  public void CreateOrderTest()
  {
      string baseAddress = "http://dummyname/";
  
      // Server
      HttpConfiguration config = new HttpConfiguration();
      config.Routes.MapHttpRoute("Default", "api/{controller}/{action}/{id}",
          new { id = RouteParameter.Optional });
      config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
  
      HttpServer server = new HttpServer(config);
  
      // Client
      HttpMessageInvoker messageInvoker = new HttpMessageInvoker(new InMemoryHttpContentSerializationHandler(server));
  
      // Order to be created
      MotorInspectionAPI.Controllers.AccountController.AuthenticateRequest requestOrder = new MotorInspectionAPI.Controllers.AccountController.AuthenticateRequest() { 
          Username = "Test",
          Password = "password"
      };
  
      HttpRequestMessage request = new HttpRequestMessage();
      request.Content = new ObjectContent&lt;MotorInspectionAPI.Controllers.AccountController.AuthenticateRequest&gt;(requestOrder, new JsonMediaTypeFormatter());
      request.RequestUri = new Uri(baseAddress + "api/Account/Authenticate");
      request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
      request.Method = HttpMethod.Get;
  
      CancellationTokenSource cts = new CancellationTokenSource();
  
      using (HttpResponseMessage response = messageInvoker.SendAsync(request, cts.Token).Result)
      {
          Assert.NotNull(response.Content);
          Assert.NotNull(response.Content.Headers.ContentType);
  
          // How do I test that I received the correct response?
  
      }
  </code></pre>
  
  <p>I'm hoping I can check the response as a string, something along the lines of</p>
  
  <pre><code>response == "{\"Status\":0,\"SessionKey\":"1234",\"UserType\":0,\"Message\":\"Successfully authenticated.\"}"
  </code></pre>