From 592d802fde77ec375cd2b7227d0874466fd40c83 Mon Sep 17 00:00:00 2001 From: Master T Date: Wed, 18 Nov 2015 04:52:13 +0100 Subject: [PATCH] Add test for https scheme, code cleanup --- .../HttpsConnectionFilter.cs | 17 +++--- .../HttpsConnectionFilterTests.cs | 54 ++++++++++++++++++- 2 files changed, 60 insertions(+), 11 deletions(-) diff --git a/src/Microsoft.AspNet.Server.Kestrel.Https/HttpsConnectionFilter.cs b/src/Microsoft.AspNet.Server.Kestrel.Https/HttpsConnectionFilter.cs index b7606438f0..1d13d6fb2e 100644 --- a/src/Microsoft.AspNet.Server.Kestrel.Https/HttpsConnectionFilter.cs +++ b/src/Microsoft.AspNet.Server.Kestrel.Https/HttpsConnectionFilter.cs @@ -97,17 +97,16 @@ namespace Microsoft.AspNet.Server.Kestrel.Https var previousPrepareRequest = context.PrepareRequest; context.PrepareRequest = features => + { + previousPrepareRequest?.Invoke(features); + + if (clientCertificate != null) { - previousPrepareRequest?.Invoke(features); + features.Set(new TlsConnectionFeature { ClientCertificate = clientCertificate }); + } - if (clientCertificate != null) - { - features.Set( - new TlsConnectionFeature {ClientCertificate = clientCertificate}); - } - - features.Get().Scheme = "https"; - }; + features.Get().Scheme = "https"; + }; context.Connection = sslStream; } } diff --git a/test/Microsoft.AspNet.Server.KestrelTests/HttpsConnectionFilterTests.cs b/test/Microsoft.AspNet.Server.KestrelTests/HttpsConnectionFilterTests.cs index 8b766fba8e..1f7ca8c4d2 100644 --- a/test/Microsoft.AspNet.Server.KestrelTests/HttpsConnectionFilterTests.cs +++ b/test/Microsoft.AspNet.Server.KestrelTests/HttpsConnectionFilterTests.cs @@ -258,8 +258,58 @@ namespace Microsoft.AspNet.Server.KestrelTests #if DNX451 ServicePointManager.ServerCertificateValidationCallback -= validationCallback; #endif - } -} + } + } + // https://github.com/aspnet/KestrelHttpServer/issues/240 + // This test currently fails on mono because of an issue with SslStream. + [ConditionalFact] + [OSSkipCondition(OperatingSystems.Linux)] + [OSSkipCondition(OperatingSystems.MacOSX)] + public async Task HttpsSchemePassedToRequestFeature() + { + RemoteCertificateValidationCallback validationCallback = + (sender, cert, chain, sslPolicyErrors) => true; + + try + { +#if DNX451 + var handler = new HttpClientHandler(); + ServicePointManager.ServerCertificateValidationCallback += validationCallback; +#else + var handler = new WinHttpHandler(); + handler.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true; +#endif + + var serverAddress = "https://localhost:54321/"; + var serviceContext = new TestServiceContext() + { + ConnectionFilter = new HttpsConnectionFilter( + new HttpsConnectionFilterOptions + { + ServerCertificate = new X509Certificate2(@"TestResources/testCert.pfx", "testPassword") + }, + new NoOpConnectionFilter()) + }; + + RequestDelegate app = context => context.Response.WriteAsync(context.Request.Scheme); + + using (var server = new TestServer(app, serviceContext, serverAddress)) + { + using (var client = new HttpClient(handler)) + { + var result = await client.GetAsync(serverAddress); + + Assert.Equal("https", await result.Content.ReadAsStringAsync()); + } + } + } + finally + { +#if DNX451 + ServicePointManager.ServerCertificateValidationCallback -= validationCallback; +#endif + } + } } }