Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core running two TestServer for Integration Testing

I am trying to run some integration tests for a token management API. The API also requires the token issuer API to be running.

In summary, my integration test needs to run both IdentityServer4 Web/API and the Management API simultaneously. When I create two instances of TestServer, it seems like they both end up with the same BaseAddress (http://localhost).

private readonly TestServer _identityTestServer;
private readonly TestServer _mgmtTestServer;
private readonly AppMgmtConfig _config;
private readonly AdminClient _adminClient;
private const string _certificatePassword = "test";

public AdminClientTests() : base(nameof(AdminClientTests))
{
    var connectionString = GetConnectionString();
    var dbSettings = new DbSettings(connectionString);

    Environment.SetEnvironmentVariable("IdentityServer4ConnString",
        dbSettings.IdentityServerConnectionString);
    Environment.SetEnvironmentVariable("CertificatePassword", _certificatePassword);

    _identityTestServer = new TestServer(new WebHostBuilder()
        .UseStartup<USBIdentityServer.Startup>()
        .UseEnvironment("IntegrationTest"));
    USBIdentityServer.Program.InitializeDatabase(_identityTestServer.Host);

    _mgmtTestServer = new TestServer(new WebHostBuilder()
        .UseStartup<IdentityServer4.Management.Startup>()
        .UseEnvironment("IntegrationTest"));

    _config = GetConfig();
    _adminClient = new AdminClient(_config);
}

NOTE: Things I have already tried:

  • Add .UseUrls("http://localhost:5001") to see if TestServer will run on that port.
  • Add serverName.BaseAddress = new Uri("http://localhost:5001"); to see if TestServer will run on that port.

Neither of these seems to impact it.

like image 394
blgrnboy Avatar asked Nov 02 '17 18:11

blgrnboy


1 Answers

I know this is an old question, but I was having the same problem. I think the trick is to inject the HttpClient you got from "server 1" into "server 2".

From ASP.NET issues:

Test server does not open any real sockets, so running on different urls is a fictitious concept anyways. You can set BaseAddress to anything you want.

I assume your actual challenge is communicating with those test servers? Again, they don't open any real sockets, the only way to communicate with them is through their CreateClient API which creates an in-memory channel.

Example (.NET Core):

//Start and configure server 1.
IWebHostBuilder server1 = new WebHostBuilder()
 .UseStartup < Project1.Startup > ()
 .UseKestrel(options => options.Listen(IPAddress.Any, 80))
 .UseConfiguration(new ConfigurationBuilder()
  .AddJsonFile("appsettings_server1.json")
  .Build()
 );

TestServer testServer1 = new TestServer(server1);

//Get HttpClient that's able to connect to server 1.
HttpClient client1 = server1.CreateClient();

IWebHostBuilder _server2 = new WebHostBuilder()
 .UseStartup < Project2.Startup > ()
 .ConfigureTestServices(
  services => {
   //Inject HttpClient that's able to connect to server 1.
   services.AddSingleton(typeof(HttpClient), client1);
  })
 .UseKestrel(options => options.Listen(IPAddress.Any, 81))
 .UseConfiguration(new ConfigurationBuilder()
  .AddJsonFile("appsettings_server2.json")
  .Build()
 );

TestServer testServer2 = new TestServer(server2);
like image 117
Sander Avatar answered Oct 11 '22 14:10

Sander