ASP.NET Core运行两个TestServer进行集成测试

时间:2017-11-02 18:05:16

标签: c# asp.net-core integration-testing

我正在尝试为令牌管理API运行一些集成测试。 API还要求令牌颁发者API正在运行。

总之,我的集成测试需要同时运行IdentityServer4 Web / API和Management API。当我创建两个hidden的实例时,看起来他们最终都使用相同的TestServerBaseAddress)。

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); } 以查看TestServer是否将在该端口上运行。
  • 添加.UseUrls("http://localhost:5001")以查看TestServer是否将在该端口上运行。

这些似乎都没有影响它。

2 个答案:

答案 0 :(得分:2)

我知道这是一个老问题,但是我遇到了同样的问题。我认为诀窍是将您从“服务器1”获得的HttpClient注入“服务器2”。

示例(.NET Core):

$elements

答案 1 :(得分:0)

我正在使用.NET Core 3.1,并且扩展了@Sander提供的答复。

在这里,您有2种微服务的有效解决方案:适配器和对象。 我想从适配器服务中调用对象API。

如果需要多个客户端,则应将其包装在特定的客户端包装器中(在我的情况下为ObjectsApiClient),设置适当的BaseAddress即可使用。

// Adapter microservice - Objects microservice client wrapper

public class ObjectsApiClient
{
    private readonly HttpClient _httpClient;

    public ObjectsApiClient(
        HttpClient httpClient,
        IAuthorizationService authorizationService, // additional services you need in your client wrapper
        IHttpHeaderAppService httpHeaderAppService)
    {
        _httpClient = httpClient;
        ...
    }
}

// Adapter microservice - Startup class 

public void ConfigureServices(IServiceCollection services)
{
    ...
    services.AddHttpClient<ObjectsApiClient>(client =>
        {
            client.BaseAddress = new Uri("http://localhost/api/v1/objects/");
        })
}

// Adapter microservice integration test class

private IHost _testServer;
private IHost _objectsTestServer;

// Adapter microservice integration test constructor

_objectsTestServer = new HostBuilder()
    .ConfigureWebHost(webHost =>
        webHost
            .UseStartup(typeof(Objects.Startup))
            .UseConfiguration(configuration)
            .UseTestServer())
    .Start();

var objectsHttpClient = _objectsTestServer.GetTestClient();
objectsHttpClient.BaseAddress = new Uri("http://localhost/api/v1/objects/");

var objectsApiClient = new ObjectsApiClient(
    objectsHttpClient,
    _objectsTestServer.Services.GetService<IAuthorizationService>(),    // inject services you need in your client wrapper
    _objectsTestServer.Services.GetService<IHttpHeaderAppService>());   

_testServer = new HostBuilder()
    .ConfigureWebHost(webHost =>
        webHost
            .UseStartup(typeof(Startup))
            .UseConfiguration(configuration)
            .ConfigureTestServices(config => config
                .AddSingleton(typeof(ObjectsApiClient), objectsApiClient)) // you can register other typed clients here
            .UseTestServer())
    .Start();