Add test for https scheme, code cleanup

This commit is contained in:
Master T 2015-11-18 04:52:13 +01:00
parent 85eb6ab600
commit 592d802fde
2 changed files with 60 additions and 11 deletions

View File

@ -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<ITlsConnectionFeature>(new TlsConnectionFeature { ClientCertificate = clientCertificate });
}
if (clientCertificate != null)
{
features.Set<ITlsConnectionFeature>(
new TlsConnectionFeature {ClientCertificate = clientCertificate});
}
features.Get<IHttpRequestFeature>().Scheme = "https";
};
features.Get<IHttpRequestFeature>().Scheme = "https";
};
context.Connection = sslStream;
}
}

View File

@ -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
}
}
}
}