예제 #1
0
        public async Task UseCallback_SelfSignedCertificate_ExpectedPolicyErrors()
        {
            using (HttpClientHandler handler = CreateHttpClientHandler())
                using (HttpClient client = CreateHttpClient(handler))
                {
                    bool             callbackCalled = false;
                    X509Certificate2 certificate    = TestHelper.CreateServerSelfSignedCertificate();

                    handler.ServerCertificateCustomValidationCallback = (request, cert, chain, errors) =>
                    {
                        callbackCalled = true;
                        Assert.NotNull(request);
                        Assert.NotNull(cert);
                        Assert.NotNull(chain);
                        Assert.Equal(SslPolicyErrors.RemoteCertificateChainErrors, errors);
                        return(true);
                    };

                    var options = new LoopbackServer.Options {
                        UseSsl = true, Certificate = certificate
                    };

                    await LoopbackServer.CreateServerAsync(async (server, url) =>
                    {
                        await TestHelper.WhenAllCompletedOrAnyFailed(
                            server.AcceptConnectionSendResponseAndCloseAsync(),
                            client.GetAsync($"https://{certificate.GetNameInfo(X509NameType.SimpleName, false)}:{url.Port}/"));
                    }, options);

                    Assert.True(callbackCalled);
                }
        }