I am trying to add keycloak as a testcontainer to my .net core (5) integration tests using the dotnet-testcontainers library .
My Problem is, I am struggling with HTTPS-Support having a container using self-signed certificates and TestServer-Class for my integration tests.
To be precise, I am using Microsofts TestServer class to create real API requests with an in-memory config for using a keycloak-testcontainer with exposed port 8443 and its self-signed certificate.
The Problem is: I can’t add a HttpClientHandler
to TestServers HttpClient(created via serverCreateClient()
) to allow non-trusted certs within this handler. I have created a concrete example here on branch apitests-https. The failing test can be found here, its in the SucceedsWhenGetRequestWithTokenReturnsListOfArticles
test method. I added some Comments to the class and the Startup.cs of DemoApi - Project that shows what i've tried.
As a result, the TestServers internal Jwt Middleware uses the default HttpClient and throws the following AuthenticationException:
---> System.IO.IOException: IDX20804: Unable to retrieve document from: 'System.String'.
---> System.Net.Http.HttpRequestException: The SSL connection could not be established, see inner exception.
---> System.Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedure: RemoteCertificateNameMismatch, RemoteCertificateChainErrors
at System.Net.Security.SslStream.SendAuthResetSignal(ProtocolToken message, ExceptionDispatchInfo exception)
at System.Net.Security.SslStream.ForceAuthenticationAsync[TIOAdapter](TIOAdapter adapter, Boolean receiveFirst, Byte[] reAuthenticationData, Boolean isApm)
at System.Net.Http.ConnectHelper.EstablishSslConnectionAsyncCore(Boolean async, Stream stream, SslClientAuthenticationOptions sslOptions, CancellationToken cancellationToken)
--- End of inner exception stack trace ---
I already tried multiple things to make it work which are commented in the code.
DemoApi/Startup.cs
: Tried to add my own "Testing" Environment with following Code:
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
| SecurityProtocolType.Tls11
| SecurityProtocolType.Tls12;
ServicePointManager.ServerCertificateValidationCallback +=
(sender, certificate, chain, sslPolicyErrors) => true;
using it via UseEnvironment("Testing")
when creating the TestServer-Instance in the Api Test. Debugging shows the code is called, but still the exception occurs.
DemoApiTestsApi/BaseFixture.cs
or DemoApiTests/Infrastructure/Persistence/KeycloakTest.cs
: See here for a working Implementation of my own HttpClient with handler to obtain the token in general (this works in the branch) - GetTestToken
is the method in BaseFixture.
So, honestly I am a bit out of ideas on how to make this work with TestServer or otherwise. Essentially, I need the handler I use in BaseFixture.GetTestToken()
/KeycloakTest.cs
to be also used within my TestServer instance, but can't apply it in the CreateClient()
which does not accept parameters. Any help is highly appreciated, may it be a solution or a hint to another way to solve this. TestServer is not necessarily fixed when there's another way to work this out.
question from:
https://stackoverflow.com/questions/66062832/calling-thirdparty-container-with-net-core-testhost-testserver-via-ssl-bypass 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…