mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-28 11:47:50 +02:00
* Version 2.x * Setup .NET 9 * 12 * cleanup some #if for NETSTANDARD1_3 * cleanup + fix tests for net8 * openapi * NO ConfigureAwait(false) + cleanup * . * #endif * HashSet * WireMock.Net.NUnit * HttpContext * Add WebSockets (#1423) * Add WebSockets * Add tests * fix * more tests * Add tests * ... * remove IOwin * - * tests * fluent * ok * match * . * byte[] * x * func * func * byte * trans * ... * frameworks......... * jmes * xxx * sc * using var httpClient = new HttpClient(); * usings * maxRetries * up * xunit v3 * ct * --- * ct * ct2 * T Unit * WireMock.Net.TUnitTests / 10 * t unit first * --project * no tunit * t2 * --project * --project * ci - --project * publish ./test/wiremock-coverage.xml * windows * . * log * ... * log * goed * BodyType * . * . * --scenario * ... * pact * ct * . * WireMock.Net.RestClient.AwesomeAssertions (#1427) * WireMock.Net.RestClient.AwesomeAssertions * ok * atpath * fix test * sonar fixes * ports * proxy test * FIX? * --- * await Task.Delay(100, _ct); * ? * --project * Aspire: use IDistributedApplicationEventingSubscriber (#1428) * broadcast * ok * more tsts * . * Collection * up * . * 2 * remove nfluent * <VersionPrefix>2.0.0-preview-02</VersionPrefix> * ... * . * nuget icon * . * <PackageReference Include="JmesPath.Net" Version="1.1.0" /> * x * 500 * . * fix some warnings * ws
116 lines
4.1 KiB
C#
116 lines
4.1 KiB
C#
// Copyright © WireMock.Net
|
|
|
|
using System.Net;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.AspNetCore.Server.Kestrel.Core;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using CertificateLoader = WireMock.HttpsCertificate.CertificateLoader;
|
|
|
|
namespace WireMock.Owin;
|
|
|
|
internal partial class AspNetCoreSelfHost
|
|
{
|
|
private static void SetKestrelOptionsLimits(KestrelServerOptions options)
|
|
{
|
|
options.Limits.MaxRequestBodySize = null; // https://stackoverflow.com/questions/46738364/increase-upload-request-length-limit-in-kestrel
|
|
options.Limits.MaxRequestBufferSize = null;
|
|
options.Limits.MaxRequestHeaderCount = 100;
|
|
options.Limits.MaxResponseBufferSize = null;
|
|
}
|
|
|
|
private static void SetHttpsAndUrls(KestrelServerOptions kestrelOptions, IWireMockMiddlewareOptions wireMockMiddlewareOptions, IEnumerable<HostUrlDetails> urlDetails)
|
|
{
|
|
foreach (var urlDetail in urlDetails)
|
|
{
|
|
if (urlDetail.IsHttps)
|
|
{
|
|
Listen(kestrelOptions, urlDetail, listenOptions =>
|
|
{
|
|
listenOptions.UseHttps(options =>
|
|
{
|
|
if (wireMockMiddlewareOptions.CustomCertificateDefined)
|
|
{
|
|
options.ServerCertificate = CertificateLoader.LoadCertificate(wireMockMiddlewareOptions, urlDetail.Host);
|
|
}
|
|
|
|
options.ClientCertificateMode = wireMockMiddlewareOptions.ClientCertificateMode;
|
|
if (wireMockMiddlewareOptions.AcceptAnyClientCertificate)
|
|
{
|
|
options.ClientCertificateValidation = (_, _, _) => true;
|
|
}
|
|
});
|
|
|
|
if (urlDetail.IsHttp2)
|
|
{
|
|
SetHttp2AsProtocolsOnListenOptions(listenOptions);
|
|
}
|
|
});
|
|
continue;
|
|
}
|
|
|
|
if (urlDetail.IsHttp2)
|
|
{
|
|
Listen(kestrelOptions, urlDetail, SetHttp2AsProtocolsOnListenOptions);
|
|
continue;
|
|
}
|
|
|
|
Listen(kestrelOptions, urlDetail, _ => { });
|
|
}
|
|
}
|
|
|
|
private static void SetHttp2AsProtocolsOnListenOptions(ListenOptions listenOptions)
|
|
{
|
|
#if NET8_0_OR_GREATER
|
|
listenOptions.Protocols = HttpProtocols.Http2;
|
|
#else
|
|
throw new NotSupportedException("HTTP/2 is only supported in .NET 8 or greater.");
|
|
#endif
|
|
}
|
|
|
|
private static void Listen(KestrelServerOptions kestrelOptions, HostUrlDetails urlDetail, Action<ListenOptions> configure)
|
|
{
|
|
// Listens on any IP with the given port.
|
|
if (urlDetail is { Port: > 0, Host: "0.0.0.0" })
|
|
{
|
|
kestrelOptions.ListenAnyIP(urlDetail.Port, configure);
|
|
return;
|
|
}
|
|
|
|
// Listens on ::1 and 127.0.0.1 with the given port.
|
|
if (urlDetail is { Port: > 0, Host: "localhost" or "127.0.0.1" or "::1" })
|
|
{
|
|
kestrelOptions.ListenLocalhost(urlDetail.Port, configure);
|
|
return;
|
|
}
|
|
|
|
// Try to parse the host as a valid IP address and bind to the given IP address and port.
|
|
if (IPAddress.TryParse(urlDetail.Host, out var ipAddress))
|
|
{
|
|
kestrelOptions.Listen(ipAddress, urlDetail.Port, configure);
|
|
return;
|
|
}
|
|
|
|
// Otherwise, listen on all IPs.
|
|
kestrelOptions.ListenAnyIP(urlDetail.Port, configure);
|
|
}
|
|
}
|
|
|
|
internal static class IWebHostBuilderExtensions
|
|
{
|
|
internal static IWebHostBuilder ConfigureAppConfigurationUsingEnvironmentVariables(this IWebHostBuilder builder)
|
|
{
|
|
return builder.ConfigureAppConfiguration(config =>
|
|
{
|
|
config.AddEnvironmentVariables();
|
|
});
|
|
}
|
|
|
|
internal static IWebHostBuilder ConfigureKestrelServerOptions(this IWebHostBuilder builder)
|
|
{
|
|
return builder.ConfigureServices((context, services) =>
|
|
{
|
|
services.Configure<KestrelServerOptions>(context.Configuration.GetSection("Kestrel"));
|
|
});
|
|
}
|
|
} |