title: .NET Core Integration tests fails because of HttpRequestException
body: <p>I am trying to test my API by calling an endpoint which should return an error message (json response). When testing this in Postman, the API indeed returns correct JSON and I am able to see that the status code is not 200. However, when trying to test this while using xunit and HttpClient, I am getting the following error message:</p>
  
  <p><code>System.Net.Http.HttpRequestException : Error while copying content to a stream.</code></p>
  
  <p>I am trying to figure out why this is happening. In my API I am checking credentials and if they are not correct, I will throw an Exception. This exception will get caught by a global exception handler, which will set the correct status code and create a json response.</p>
  
  <p>Exception handler:</p>
  
  <pre><code>public class ExceptionHandler : IExceptionFilter
  {
      public void OnException(ExceptionContext context)
      {
          int status = (int)HttpStatusCode.InternalServerError;
          String message = context.Exception.Message;
          var exception = context.Exception;
  
          if (exception is ArgumentNullException) 
          {
              message = ((ArgumentNullException)exception).ParamName + " is required";
              status = (int)HttpStatusCode.BadRequest;
          }
  
          if (exception is NotFoundException)
          {
              message = exception.Message;
              status = (int)HttpStatusCode.NotFound;
          }
  
          if (exception is AuthenticationException)
          {
              message = exception.Message;
              status = (int)HttpStatusCode.Unauthorized;
          }
  
          if (exception is DuplicateEntryException)
          {
              message = exception.Message;
              status = 422; // Unprocessable entity, not supported in HttpStatusCode class
          }
  
          HttpResponse response = context.HttpContext.Response;
          response.StatusCode = status;
          response.ContentType = "application/json";
  
          response.WriteAsync(JsonConvert.SerializeObject(new ErrorMessage(message, (int)status)));
      }
  }
  
  class ErrorMessage
  {
      public string Message { get; set; }
      public int Code { get; set; }
  
      public ErrorMessage(string message, int code)
      {
          Message = message;
          Code = code;
      }
  }
  </code></pre>
  
  <p>Integration test:</p>
  
  <pre><code>    [Fact]
      public async Task ItReturnsAnErrorWhenCredentialsAreIncorrect()
      {
          var request = new UserAuthenticationRequest();
          request.Username = "JohnDoe";
          request.Password = "TestPasswordIncorrect";
  
          var stringPayload = JsonConvert.SerializeObject(request);
  
          var httpContent = new StringContent(stringPayload, Encoding.UTF8, "application/json");
  
          _client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
          var response = await _client.PostAsync("api/authentication/GetUserAppToken", httpContent);
  
  
          Assert.Equal(401, (int)response.StatusCode);
      }
  </code></pre>
  
  <p>Anyone having any idea why it is throwing the HttpRequestException?</p>
  
  <p><strong>Update:</strong></p>
  
  <p>Client setup:</p>
  
  <pre><code>    public AuthenticationControllerTest()
      {
          _testServer = new TestServer(new WebHostBuilder().UseStartup&lt;TestStartup&gt;());
          _client = _testServer.CreateClient();
      }
  </code></pre>
  
  <p>I did a test by setting up a new client per test method. Unfortunately that also didn't solve the problem</p>
  
  <p><strong>Update 2:</strong></p>
  
  <p>I think it has to do something with either the HttpClient setup or the request message I am sending. Instead of making use of the exception handler, I decided to return a BadRequest() from the controller right away.</p>
  
  <pre><code>    public BadRequestResult Post([FromBody]UserAuthenticationRequest userAuthenticationRequest)
      {
          return BadRequest();
      }
  </code></pre>
  
  <p>When doing that, I am getting a HttpRequestException again, instead of getting a HttpResponse with status code 400.</p>