title: Net Core - Integration test - TestServer - real http connection
body: <p>We have several .NET Core Microservices / API and we wish to do some integration tests.
  So, we have follow the tutorials and it work fine, except for one situation.</p>
  
  <p>Imagine I have 3 API, so 3 "TestServer" instance, each has its own HttpClient to connect with !</p>
  
  <p>I show the way I create the TestServer :</p>
  
  <pre><code>    private TestServer SetupAPI&lt;TStartup&gt;(string path, string uri) where TStartup : class
      {
          string rootPath = Path.Join(_rootPath, "path");
  
          var hostBuilder = WebHost.CreateDefaultBuilder()
              .UseUrls(uri)
              .UseIISIntegration()
              //.ConfigureServices(services =&gt; services.AddSecurityHeaders())
              .UseContentRoot(rootPath)
              .UseEnvironment("Development")
              .ConfigureAppConfiguration(cb =&gt;
              {
                  cb.AddJsonFile("appsettings.json", optional: false)
                  //.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                  .AddEnvironmentVariables();
              })
              .UseStartup&lt;TStartup&gt;();
  
          TestServer server = new TestServer(hostBuilder);
          server.BaseAddress = new Uri(uri);
  
          return server;
      }
  </code></pre>
  
  <p>I show how I create the HttpClient :</p>
  
  <pre><code>        client = ArtistsServer.CreateClient();
          client.BaseAddress = new Uri("http://localhost:5000");
          client.DefaultRequestHeaders.Accept.Clear();
          client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
  </code></pre>
  
  <p>It works perfectly, but when I try to create a new HttpClient that way, it failed:
      HttpClient httpClient = new HttpClient();
      httpClient.BaseAddress = new Uri("<a href="http://localhost:5000" rel="nofollow noreferrer">http://localhost:5000</a>");</p>
  
  <p>The real problem is that some microservices create their own HttpClient that way in order to communicate with other API ! They are setup through the appsettings.json file and try to connect to a normal and real http server, not a "TestServer" one !</p>
  
  <p>Do you have any idea to solve this ?</p>
  
  <p>Thanks</p>