Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
StefH committed Jul 30, 2022
1 parent ea5563d commit ac8c2a1
Show file tree
Hide file tree
Showing 15 changed files with 430 additions and 404 deletions.
4 changes: 2 additions & 2 deletions src/WireMock.Net.Abstractions/IRequestMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ public interface IRequestMessage
/// <summary>
/// Gets the headers.
/// </summary>
IDictionary<string, WireMockList<string>>? Headers { get; }
IDictionary<string, WireMockList<string>> Headers { get; }

/// <summary>
/// Gets the cookies.
/// </summary>
IDictionary<string, string>? Cookies { get; }
IDictionary<string, string> Cookies { get; }

/// <summary>
/// Gets the query.
Expand Down
59 changes: 29 additions & 30 deletions src/WireMock.Net/Owin/AspNetCoreSelfHost.NETCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,44 @@
using Microsoft.Extensions.DependencyInjection;
using WireMock.Types;

namespace WireMock.Owin
namespace WireMock.Owin;

internal partial class AspNetCoreSelfHost
{
internal partial class AspNetCoreSelfHost
public void AddCors(IServiceCollection services)
{
public void AddCors(IServiceCollection services)
if (_wireMockMiddlewareOptions.CorsPolicyOptions > CorsPolicyOptions.None)
{
if (_wireMockMiddlewareOptions.CorsPolicyOptions > CorsPolicyOptions.None)
{
/* https://stackoverflow.com/questions/31942037/how-to-enable-cors-in-asp-net-core */
/* Enable Cors */
services.AddCors(corsOptions => corsOptions
.AddPolicy(CorsPolicyName,
corsPolicyBuilder =>
/* https://stackoverflow.com/questions/31942037/how-to-enable-cors-in-asp-net-core */
/* Enable Cors */
services.AddCors(corsOptions => corsOptions
.AddPolicy(CorsPolicyName,
corsPolicyBuilder =>
{
if (_wireMockMiddlewareOptions.CorsPolicyOptions.Value.HasFlag(CorsPolicyOptions.AllowAnyHeader))
{
if (_wireMockMiddlewareOptions.CorsPolicyOptions.Value.HasFlag(CorsPolicyOptions.AllowAnyHeader))
{
corsPolicyBuilder.AllowAnyHeader();
}
corsPolicyBuilder.AllowAnyHeader();
}
if (_wireMockMiddlewareOptions.CorsPolicyOptions.Value.HasFlag(CorsPolicyOptions.AllowAnyMethod))
{
corsPolicyBuilder.AllowAnyMethod();
}
if (_wireMockMiddlewareOptions.CorsPolicyOptions.Value.HasFlag(CorsPolicyOptions.AllowAnyMethod))
{
corsPolicyBuilder.AllowAnyMethod();
}
if (_wireMockMiddlewareOptions.CorsPolicyOptions.Value.HasFlag(CorsPolicyOptions.AllowAnyOrigin))
{
corsPolicyBuilder.AllowAnyOrigin();
}
}));
}
if (_wireMockMiddlewareOptions.CorsPolicyOptions.Value.HasFlag(CorsPolicyOptions.AllowAnyOrigin))
{
corsPolicyBuilder.AllowAnyOrigin();
}
}));
}
}

public void UseCors(IApplicationBuilder appBuilder)
public void UseCors(IApplicationBuilder appBuilder)
{
if (_wireMockMiddlewareOptions.CorsPolicyOptions > CorsPolicyOptions.None)
{
if (_wireMockMiddlewareOptions.CorsPolicyOptions > CorsPolicyOptions.None)
{
/* Use Cors */
appBuilder.UseCors(CorsPolicyName);
}
/* Use Cors */
appBuilder.UseCors(CorsPolicyName);
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/WireMock.Net/Owin/AspNetCoreSelfHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ internal partial class AspNetCoreSelfHost : IOwinSelfHost

public Exception RunningException => _runningException;

public AspNetCoreSelfHost([NotNull] IWireMockMiddlewareOptions wireMockMiddlewareOptions, [NotNull] HostUrlOptions urlOptions)
public AspNetCoreSelfHost(IWireMockMiddlewareOptions wireMockMiddlewareOptions, HostUrlOptions urlOptions)
{
Guard.NotNull(wireMockMiddlewareOptions, nameof(wireMockMiddlewareOptions));
Guard.NotNull(urlOptions, nameof(urlOptions));
Guard.NotNull(wireMockMiddlewareOptions);
Guard.NotNull(urlOptions);

_logger = wireMockMiddlewareOptions.Logger ?? new WireMockConsoleLogger();

Expand Down Expand Up @@ -119,7 +119,7 @@ private Task RunHost(CancellationToken token)
{
Urls.Add(address.Replace("0.0.0.0", "localhost").Replace("[::]", "localhost"));
PortUtils.TryExtract(address, out bool isHttps, out string protocol, out string host, out int port);
PortUtils.TryExtract(address, out _, out _, out _, out int port);
Ports.Add(port);
}
Expand Down
17 changes: 8 additions & 9 deletions src/WireMock.Net/Owin/HostUrlDetails.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
namespace WireMock.Owin
namespace WireMock.Owin;

internal struct HostUrlDetails
{
internal class HostUrlDetails
{
public bool IsHttps { get; set; }
public bool IsHttps { get; set; }

public string Url { get; set; }
public string Url { get; set; }

public string Protocol { get; set; }
public string Protocol { get; set; }

public string Host { get; set; }
public string Host { get; set; }

public int Port { get; set; }
}
public int Port { get; set; }
}
53 changes: 27 additions & 26 deletions src/WireMock.Net/Owin/HostUrlOptions.cs
Original file line number Diff line number Diff line change
@@ -1,46 +1,47 @@
using System.Collections.Generic;
using System.Collections.Generic;
using WireMock.Util;

namespace WireMock.Owin
namespace WireMock.Owin;

internal class HostUrlOptions
{
internal class HostUrlOptions
{
private const string LOCALHOST = "localhost";
private const string LOCALHOST = "localhost";

public ICollection<string> Urls { get; set; }
public ICollection<string>? Urls { get; set; }

public int? Port { get; set; }
public int? Port { get; set; }

public bool UseSSL { get; set; }
public bool UseSSL { get; set; }

public ICollection<HostUrlDetails> GetDetails()
public ICollection<HostUrlDetails> GetDetails()
{
var list = new List<HostUrlDetails>();
if (Urls == null)
{
var list = new List<HostUrlDetails>();
if (Urls == null)
{
int port = Port > 0 ? Port.Value : FindFreeTcpPort();
string protocol = UseSSL ? "https" : "http";
list.Add(new HostUrlDetails { IsHttps = UseSSL, Url = $"{protocol}://{LOCALHOST}:{port}", Protocol = protocol, Host = LOCALHOST, Port = port });
}
else
int port = Port > 0 ? Port.Value : FindFreeTcpPort();
string protocol = UseSSL ? "https" : "http";
list.Add(new HostUrlDetails { IsHttps = UseSSL, Url = $"{protocol}://{LOCALHOST}:{port}", Protocol = protocol, Host = LOCALHOST, Port = port });
}
else
{
foreach (string url in Urls)
{
foreach (string url in Urls)
if (PortUtils.TryExtract(url, out bool isHttps, out var protocol, out var host, out int port))
{
PortUtils.TryExtract(url, out bool isHttps, out string protocol, out string host, out int port);
list.Add(new HostUrlDetails { IsHttps = isHttps, Url = url, Protocol = protocol, Host = host, Port = port });
}
}

return list;
}

private int FindFreeTcpPort()
{
return list;
}

private static int FindFreeTcpPort()
{
#if USE_ASPNETCORE || NETSTANDARD2_0 || NETSTANDARD2_1
return 0;
return 0;
#else
return PortUtils.FindFreeTcpPort();
return PortUtils.FindFreeTcpPort();
#endif
}
}
}
9 changes: 4 additions & 5 deletions src/WireMock.Net/Owin/IMappingMatcher.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
namespace WireMock.Owin
namespace WireMock.Owin;

internal interface IMappingMatcher
{
internal interface IMappingMatcher
{
(MappingMatcherResult Match, MappingMatcherResult Partial) FindBestMatch(RequestMessage request);
}
(MappingMatcherResult? Match, MappingMatcherResult? Partial) FindBestMatch(RequestMessage request);
}
51 changes: 25 additions & 26 deletions src/WireMock.Net/Owin/IOwinSelfHost.cs
Original file line number Diff line number Diff line change
@@ -1,36 +1,35 @@
using System.Collections.Generic;
using System.Collections.Generic;
using System.Threading.Tasks;
using System;

namespace WireMock.Owin
namespace WireMock.Owin;

interface IOwinSelfHost
{
interface IOwinSelfHost
{
/// <summary>
/// Gets a value indicating whether this server is started.
/// </summary>
/// <value>
/// <c>true</c> if this server is started; otherwise, <c>false</c>.
/// </value>
bool IsStarted { get; }
/// <summary>
/// Gets a value indicating whether this server is started.
/// </summary>
/// <value>
/// <c>true</c> if this server is started; otherwise, <c>false</c>.
/// </value>
bool IsStarted { get; }

/// <summary>
/// Gets the urls.
/// </summary>
List<string> Urls { get; }
/// <summary>
/// Gets the urls.
/// </summary>
List<string> Urls { get; }

/// <summary>
/// Gets the ports.
/// </summary>
List<int> Ports { get; }
/// <summary>
/// Gets the ports.
/// </summary>
List<int> Ports { get; }

/// <summary>
/// The exception occurred when the host is running
/// </summary>
Exception RunningException { get; }
/// <summary>
/// The exception occurred when the host is running.
/// </summary>
Exception? RunningException { get; }

Task StartAsync();
Task StartAsync();

Task StopAsync();
}
Task StopAsync();
}
23 changes: 17 additions & 6 deletions src/WireMock.Net/Owin/Mappers/OwinRequestMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public async Task<RequestMessage> MapAsync(IRequest request, IWireMockMiddleware

string method = request.Method;

Dictionary<string, string[]>? headers = null;
var headers = new Dictionary<string, string[]>();
IEnumerable<string>? contentEncodingHeader = null;
if (request.Headers.Any())
{
Expand All @@ -43,7 +43,7 @@ public async Task<RequestMessage> MapAsync(IRequest request, IWireMockMiddleware
}
}

IDictionary<string, string>? cookies = null;
var cookies = new Dictionary<string, string>();
if (request.Cookies.Any())
{
cookies = new Dictionary<string, string>();
Expand Down Expand Up @@ -75,13 +75,24 @@ private static (UrlDetails UrlDetails, string ClientIP) ParseRequest(IRequest re
{
#if !USE_ASPNETCORE
var urlDetails = UrlUtils.Parse(request.Uri, request.PathBase);
string clientIP = request.RemoteIpAddress;
var clientIP = request.RemoteIpAddress;
#else
var urlDetails = UrlUtils.Parse(new Uri(request.GetEncodedUrl()), request.PathBase);

var connection = request.HttpContext.Connection;
string clientIP = connection.RemoteIpAddress.IsIPv4MappedToIPv6
? connection.RemoteIpAddress.MapToIPv4().ToString()
: connection.RemoteIpAddress.ToString();
string clientIP;
if (connection.RemoteIpAddress is null)
{
clientIP = string.Empty;
}
else if (connection.RemoteIpAddress.IsIPv4MappedToIPv6)
{
clientIP = connection.RemoteIpAddress.MapToIPv4().ToString();
}
else
{
clientIP = connection.RemoteIpAddress.ToString();
}
#endif
return (urlDetails, clientIP);
}
Expand Down
Loading

0 comments on commit ac8c2a1

Please sign in to comment.