mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-24 01:08:28 +02:00
cleanup + fix tests for net8
This commit is contained in:
@@ -9,6 +9,8 @@ namespace WireMock.Owin;
|
||||
|
||||
internal partial class AspNetCoreSelfHost
|
||||
{
|
||||
private const string CorsPolicyName = "WireMock.Net - Policy";
|
||||
|
||||
public void AddCors(IServiceCollection services)
|
||||
{
|
||||
if (_wireMockMiddlewareOptions.CorsPolicyOptions > CorsPolicyOptions.None)
|
||||
|
||||
@@ -1,57 +0,0 @@
|
||||
// Copyright © WireMock.Net
|
||||
|
||||
//#if USE_ASPNETCORE && NETSTANDARD1_3
|
||||
//using System.Collections.Generic;
|
||||
//using Microsoft.AspNetCore.Hosting;
|
||||
//using Microsoft.AspNetCore.Server.Kestrel;
|
||||
//using Microsoft.AspNetCore.Server.Kestrel.Https;
|
||||
//using Microsoft.Extensions.Configuration;
|
||||
//using Microsoft.Extensions.DependencyInjection;
|
||||
//using WireMock.HttpsCertificate;
|
||||
|
||||
//namespace WireMock.Owin;
|
||||
|
||||
//internal partial class AspNetCoreSelfHost
|
||||
//{
|
||||
// private static void SetKestrelOptionsLimits(KestrelServerOptions options)
|
||||
// {
|
||||
// options.Limits.MaxRequestBufferSize = null;
|
||||
// options.Limits.MaxRequestHeaderCount = 100;
|
||||
// options.Limits.MaxResponseBufferSize = null;
|
||||
// }
|
||||
|
||||
// private static void SetHttpsAndUrls(KestrelServerOptions options, IWireMockMiddlewareOptions wireMockMiddlewareOptions, IEnumerable<HostUrlDetails> urlDetails)
|
||||
// {
|
||||
// foreach (var urlDetail in urlDetails)
|
||||
// {
|
||||
// if (urlDetail.IsHttps)
|
||||
// {
|
||||
// options.UseHttps(new HttpsConnectionFilterOptions
|
||||
// {
|
||||
// ServerCertificate = wireMockMiddlewareOptions.CustomCertificateDefined ? CertificateLoader.LoadCertificate(wireMockMiddlewareOptions, urlDetail.Host) : PublicCertificateHelper.GetX509Certificate2(),
|
||||
// ClientCertificateMode = (ClientCertificateMode) wireMockMiddlewareOptions.ClientCertificateMode,
|
||||
// ClientCertificateValidation = wireMockMiddlewareOptions.AcceptAnyClientCertificate ? (_, _, _) => true : null
|
||||
// });
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
|
||||
//internal static class IWebHostBuilderExtensions
|
||||
//{
|
||||
// internal static IWebHostBuilder ConfigureAppConfigurationUsingEnvironmentVariables(this IWebHostBuilder builder) => builder;
|
||||
|
||||
// internal static IWebHostBuilder ConfigureKestrelServerOptions(this IWebHostBuilder builder)
|
||||
// {
|
||||
// var configuration = new ConfigurationBuilder()
|
||||
// .AddEnvironmentVariables()
|
||||
// .Build();
|
||||
|
||||
// return builder.ConfigureServices(services =>
|
||||
// {
|
||||
// services.Configure<KestrelServerOptions>(configuration.GetSection("Kestrel"));
|
||||
// });
|
||||
// }
|
||||
//}
|
||||
|
||||
//#endif
|
||||
@@ -19,8 +19,6 @@ namespace WireMock.Owin;
|
||||
|
||||
internal partial class AspNetCoreSelfHost : IOwinSelfHost
|
||||
{
|
||||
private const string CorsPolicyName = "WireMock.Net - Policy";
|
||||
|
||||
private readonly CancellationTokenSource _cts = new();
|
||||
private readonly IWireMockMiddlewareOptions _wireMockMiddlewareOptions;
|
||||
private readonly IWireMockLogger _logger;
|
||||
@@ -99,36 +97,36 @@ internal partial class AspNetCoreSelfHost : IOwinSelfHost
|
||||
})
|
||||
.ConfigureKestrelServerOptions()
|
||||
|
||||
//#if NETSTANDARD1_3
|
||||
// .UseUrls(_urlOptions.GetDetails().Select(u => u.Url).ToArray())
|
||||
//#endif
|
||||
//#if NETSTANDARD1_3
|
||||
// .UseUrls(_urlOptions.GetDetails().Select(u => u.Url).ToArray())
|
||||
//#endif
|
||||
.Build();
|
||||
|
||||
return RunHost(_cts.Token);
|
||||
}
|
||||
|
||||
private Task RunHost(CancellationToken token)
|
||||
private Task RunHost(CancellationToken token)
|
||||
{
|
||||
try
|
||||
{
|
||||
try
|
||||
{
|
||||
#if NET8_0_OR_GREATER
|
||||
var appLifetime = _host.Services.GetRequiredService<Microsoft.Extensions.Hosting.IHostApplicationLifetime>();
|
||||
var appLifetime = _host.Services.GetRequiredService<Microsoft.Extensions.Hosting.IHostApplicationLifetime>();
|
||||
#else
|
||||
var appLifetime = _host.Services.GetRequiredService<IApplicationLifetime>();
|
||||
var appLifetime = _host.Services.GetRequiredService<IApplicationLifetime>();
|
||||
#endif
|
||||
appLifetime.ApplicationStarted.Register(() =>
|
||||
appLifetime.ApplicationStarted.Register(() =>
|
||||
{
|
||||
var addresses = _host.ServerFeatures
|
||||
.Get<Microsoft.AspNetCore.Hosting.Server.Features.IServerAddressesFeature>()!
|
||||
.Addresses;
|
||||
|
||||
foreach (var address in addresses)
|
||||
{
|
||||
var addresses = _host.ServerFeatures
|
||||
.Get<Microsoft.AspNetCore.Hosting.Server.Features.IServerAddressesFeature>()!
|
||||
.Addresses;
|
||||
Urls.Add(address.Replace("0.0.0.0", "localhost").Replace("[::]", "localhost"));
|
||||
|
||||
foreach (var address in addresses)
|
||||
{
|
||||
Urls.Add(address.Replace("0.0.0.0", "localhost").Replace("[::]", "localhost"));
|
||||
|
||||
PortUtils.TryExtract(address, out _, out _, out _, out _, out var port);
|
||||
Ports.Add(port);
|
||||
}
|
||||
PortUtils.TryExtract(address, out _, out _, out _, out _, out var port);
|
||||
Ports.Add(port);
|
||||
}
|
||||
|
||||
IsStarted = true;
|
||||
});
|
||||
@@ -139,14 +137,14 @@ internal partial class AspNetCoreSelfHost : IOwinSelfHost
|
||||
_logger.Info("Server using .NET Framework 4.8");
|
||||
#endif
|
||||
|
||||
//#if NETSTANDARD1_3
|
||||
// return Task.Run(() =>
|
||||
// {
|
||||
// _host.Run(token);
|
||||
// });
|
||||
//#else
|
||||
//#if NETSTANDARD1_3
|
||||
// return Task.Run(() =>
|
||||
// {
|
||||
// _host.Run(token);
|
||||
// });
|
||||
//#else
|
||||
return _host.RunAsync(token);
|
||||
//#endif
|
||||
//#endif
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
@@ -164,11 +162,11 @@ internal partial class AspNetCoreSelfHost : IOwinSelfHost
|
||||
_cts.Cancel();
|
||||
|
||||
IsStarted = false;
|
||||
//#if NETSTANDARD1_3
|
||||
// return Task.CompletedTask;
|
||||
//#else
|
||||
//#if NETSTANDARD1_3
|
||||
// return Task.CompletedTask;
|
||||
//#else
|
||||
return _host.StopAsync();
|
||||
//#endif
|
||||
//#endif
|
||||
}
|
||||
}
|
||||
//#endif
|
||||
@@ -55,10 +55,10 @@ internal class HostUrlOptions
|
||||
|
||||
private static int FindFreeTcpPort()
|
||||
{
|
||||
#if USE_ASPNETCORE || NETSTANDARD2_0 || NETSTANDARD2_1
|
||||
//#if USE_ASPNETCORE || NETSTANDARD2_0 || NETSTANDARD2_1
|
||||
return 0;
|
||||
#else
|
||||
return PortUtils.FindFreeTcpPort();
|
||||
#endif
|
||||
//#else
|
||||
//return PortUtils.FindFreeTcpPort();
|
||||
//#endif
|
||||
}
|
||||
}
|
||||
@@ -75,10 +75,10 @@ internal class OwinRequestMapper : IOwinRequestMapper
|
||||
body,
|
||||
headers,
|
||||
cookies,
|
||||
httpVersion
|
||||
#if USE_ASPNETCORE
|
||||
, await request.HttpContext.Connection.GetClientCertificateAsync()
|
||||
#endif
|
||||
httpVersion,
|
||||
//#if USE_ASPNETCORE
|
||||
await request.HttpContext.Connection.GetClientCertificateAsync()
|
||||
//#endif
|
||||
)
|
||||
{
|
||||
DateTime = DateTime.UtcNow
|
||||
|
||||
@@ -1,112 +0,0 @@
|
||||
// Copyright © WireMock.Net
|
||||
|
||||
//using Microsoft.Owin.Hosting;
|
||||
//using Owin;
|
||||
//using System;
|
||||
//using System.Collections.Generic;
|
||||
//using System.Threading;
|
||||
//using System.Threading.Tasks;
|
||||
//using JetBrains.Annotations;
|
||||
//using WireMock.Logging;
|
||||
//using WireMock.Owin.Mappers;
|
||||
//using Stef.Validation;
|
||||
//using WireMock.Services;
|
||||
//using WireMock.Util;
|
||||
|
||||
//namespace WireMock.Owin;
|
||||
|
||||
//internal class OwinSelfHost : IOwinSelfHost
|
||||
//{
|
||||
// private readonly IWireMockMiddlewareOptions _options;
|
||||
// private readonly CancellationTokenSource _cts = new();
|
||||
// private readonly IWireMockLogger _logger;
|
||||
|
||||
// private Exception? _runningException;
|
||||
|
||||
// public OwinSelfHost(IWireMockMiddlewareOptions options, HostUrlOptions urlOptions)
|
||||
// {
|
||||
// Guard.NotNull(urlOptions);
|
||||
|
||||
// _options = Guard.NotNull(options);
|
||||
// _logger = options.Logger ?? new WireMockConsoleLogger();
|
||||
|
||||
// foreach (var detail in urlOptions.GetDetails())
|
||||
// {
|
||||
// Urls.Add(detail.Url);
|
||||
// Ports.Add(detail.Port);
|
||||
// }
|
||||
// }
|
||||
|
||||
// public bool IsStarted { get; private set; }
|
||||
|
||||
// public List<string> Urls { get; } = new();
|
||||
|
||||
// public List<int> Ports { get; } = new();
|
||||
|
||||
// public Exception? RunningException => _runningException;
|
||||
|
||||
// [PublicAPI]
|
||||
// public Task StartAsync()
|
||||
// {
|
||||
// return Task.Run(StartServers, _cts.Token);
|
||||
// }
|
||||
|
||||
// [PublicAPI]
|
||||
// public Task StopAsync()
|
||||
// {
|
||||
// _cts.Cancel();
|
||||
|
||||
// return Task.FromResult(true);
|
||||
// }
|
||||
|
||||
// private void StartServers()
|
||||
// {
|
||||
//#if NET46
|
||||
// _logger.Info("Server using .net 4.6");
|
||||
//#else
|
||||
// _logger.Info("Server using .net 4.5.x");
|
||||
//#endif
|
||||
// var servers = new List<IDisposable>();
|
||||
|
||||
// try
|
||||
// {
|
||||
// var requestMapper = new OwinRequestMapper();
|
||||
// var responseMapper = new OwinResponseMapper(_options);
|
||||
// var matcher = new MappingMatcher(_options, new RandomizerDoubleBetween0And1());
|
||||
// var guidUtils = new GuidUtils();
|
||||
|
||||
// Action<IAppBuilder> startup = app =>
|
||||
// {
|
||||
// app.Use<GlobalExceptionMiddleware>(_options, responseMapper);
|
||||
// _options.PreWireMockMiddlewareInit?.Invoke(app);
|
||||
// app.Use<WireMockMiddleware>(_options, requestMapper, responseMapper, matcher, guidUtils);
|
||||
// _options.PostWireMockMiddlewareInit?.Invoke(app);
|
||||
// };
|
||||
|
||||
// foreach (var url in Urls)
|
||||
// {
|
||||
// servers.Add(WebApp.Start(url, startup));
|
||||
// }
|
||||
|
||||
// IsStarted = true;
|
||||
|
||||
// // WaitHandle is signaled when the token is cancelled,
|
||||
// // which will be more efficient than Thread.Sleep in while loop
|
||||
// _cts.Token.WaitHandle.WaitOne();
|
||||
// }
|
||||
// catch (Exception e)
|
||||
// {
|
||||
// // Expose exception of starting host, otherwise it's hard to be troubleshooting if keeping quiet
|
||||
// // For example, WebApp.Start will fail with System.MissingMemberException if Microsoft.Owin.Host.HttpListener.dll is being located
|
||||
// // https://stackoverflow.com/questions/25090211/owin-httplistener-not-located/31369857
|
||||
// _runningException = e;
|
||||
// _logger.Error(e.ToString());
|
||||
// }
|
||||
// finally
|
||||
// {
|
||||
// IsStarted = false;
|
||||
// // Dispose all servers in finally block to make sure clean up allocated resource on error happening
|
||||
// servers.ForEach(s => s.Dispose());
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
Reference in New Issue
Block a user