Refactor: extract interfaces (#484)

* .

* MatchDetail

* rm

* resp

* log

* f
This commit is contained in:
Stef Heyenrath
2020-07-05 10:51:49 +02:00
committed by GitHub
parent 9ae02823df
commit aff936e3b6
34 changed files with 1554 additions and 1313 deletions

View File

@@ -1,8 +1,9 @@
using System.Threading;
using System;
using System.Threading;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using WireMock.Admin.Requests;
using WireMock.Logging;
using WireMock.Models.Requests;
using WireMock.Server;
using WireMock.Settings;
@@ -49,6 +50,11 @@ namespace WireMock.Net.WebApplication
string message = JsonConvert.SerializeObject(logEntryModel, Formatting.Indented);
_logger.LogDebug("Admin[{0}] {1}", isAdminrequest, message);
}
public void Error(string formatString, Exception exception)
{
_logger.LogError(formatString, exception.Message);
}
}
public WireMockService(ILogger logger, WireMockServerSettings settings)

View File

@@ -0,0 +1,140 @@
using System;
using System.Collections.Generic;
using WireMock.Types;
using WireMock.Util;
namespace WireMock
{
/// <summary>
/// IRequestMessage
/// </summary>
public interface IRequestMessage
{
/// <summary>
/// Gets the Client IP Address.
/// </summary>
string ClientIP { get; }
/// <summary>
/// Gets the url (relative).
/// </summary>
string Url { get; }
/// <summary>
/// Gets the AbsoluteUrl.
/// </summary>
string AbsoluteUrl { get; }
/// <summary>
/// The ProxyUrl (if a proxy is used).
/// </summary>
string ProxyUrl { get; }
/// <summary>
/// Gets the DateTime.
/// </summary>
DateTime DateTime { get; }
/// <summary>
/// Gets the path (relative).
/// </summary>
string Path { get; }
/// <summary>
/// Gets the AbsolutePath.
/// </summary>
string AbsolutePath { get; }
/// <summary>
/// Gets the path segments.
/// </summary>
string[] PathSegments { get; }
/// <summary>
/// Gets the absolute path segments.
/// </summary>
string[] AbsolutePathSegments { get; }
/// <summary>
/// Gets the method.
/// </summary>
string Method { get; }
/// <summary>
/// Gets the headers.
/// </summary>
IDictionary<string, WireMockList<string>> Headers { get; }
/// <summary>
/// Gets the cookies.
/// </summary>
IDictionary<string, string> Cookies { get; }
/// <summary>
/// Gets the query.
/// </summary>
IDictionary<string, WireMockList<string>> Query { get; }
/// <summary>
/// Gets the raw query.
/// </summary>
string RawQuery { get; }
/// <summary>
/// The body.
/// </summary>
IBodyData BodyData { get; }
/// <summary>
/// The original body as string. Convenience getter for Handlebars.
/// </summary>
string Body { get; }
/// <summary>
/// The body (as JSON object). Convenience getter for Handlebars.
/// </summary>
object BodyAsJson { get; }
/// <summary>
/// The body (as bytearray). Convenience getter for Handlebars.
/// </summary>
byte[] BodyAsBytes { get; }
/// <summary>
/// The detected body type. Convenience getter for Handlebars.
/// </summary>
string DetectedBodyType { get; }
/// <summary>
/// The detected body type from the Content-Type header. Convenience getter for Handlebars.
/// </summary>
string DetectedBodyTypeFromContentType { get; }
/// <summary>
/// The detected compression from the Content-Encoding header. Convenience getter for Handlebars.
/// </summary>
string DetectedCompression { get; }
/// <summary>
/// Gets the Host
/// </summary>
string Host { get; }
/// <summary>
/// Gets the protocol
/// </summary>
string Protocol { get; }
/// <summary>
/// Gets the port
/// </summary>
int Port { get; }
/// <summary>
/// Gets the origin
/// </summary>
string Origin { get; }
// WireMockList<string> GetParameter(string key, bool ignoreCase = false);
}
}

View File

@@ -0,0 +1,51 @@
using System.Collections.Generic;
using WireMock.ResponseBuilders;
using WireMock.Types;
using WireMock.Util;
namespace WireMock
{
/// <summary>
/// IResponseMessage
/// </summary>
public interface IResponseMessage
{
/// <summary>
/// The Body.
/// </summary>
IBodyData BodyData { get; }
/// <summary>
/// Gets the body destination (SameAsSource, String or Bytes).
/// </summary>
string BodyDestination { get; }
/// <summary>
/// Gets or sets the body.
/// </summary>
string BodyOriginal { get; }
/// <summary>
/// Gets the Fault percentage.
/// </summary>
double? FaultPercentage { get; }
/// <summary>
/// The FaultType.
/// </summary>
FaultType FaultType { get; }
/// <summary>
/// Gets the headers.
/// </summary>
IDictionary<string, WireMockList<string>> Headers { get; }
/// <summary>
/// Gets or sets the status code.
/// </summary>
object StatusCode { get; }
//void AddHeader(string name, params string[] values);
//void AddHeader(string name, string value);
}
}

View File

@@ -0,0 +1,80 @@
using System;
using WireMock.Matchers.Request;
namespace WireMock.Logging
{
public interface ILogEntry
{
/// <summary>
/// Gets the unique identifier.
/// </summary>
/// <value>
/// The unique identifier.
/// </value>
Guid Guid { get; }
/// <summary>
/// Gets the mapping unique identifier.
/// </summary>
/// <value>
/// The mapping unique identifier.
/// </value>
Guid? MappingGuid { get; }
/// <summary>
/// Gets the mapping unique title.
/// </summary>
/// <value>
/// The mapping unique title.
/// </value>
string MappingTitle { get; }
/// <summary>
/// Gets the partial mapping unique identifier.
/// </summary>
/// <value>
/// The mapping unique identifier.
/// </value>
Guid? PartialMappingGuid { get; }
/// <summary>
/// Gets the partial mapping unique title.
/// </summary>
/// <value>
/// The mapping unique title.
/// </value>
string PartialMappingTitle { get; }
/// <summary>
/// Gets the partial match result.
/// </summary>
/// <value>
/// The request match result.
/// </value>
IRequestMatchResult PartialMatchResult { get; }
/// <summary>
/// Gets the request match result.
/// </summary>
/// <value>
/// The request match result.
/// </value>
IRequestMatchResult RequestMatchResult { get; }
/// <summary>
/// Gets the request message.
/// </summary>
/// <value>
/// The request message.
/// </value>
IRequestMessage RequestMessage { get; }
/// <summary>
/// Gets the response message.
/// </summary>
/// <value>
/// The response message.
/// </value>
IResponseMessage ResponseMessage { get; }
}
}

View File

@@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
namespace WireMock.Matchers.Request
{
/// <summary>
/// IRequestMatchResult
/// </summary>
public interface IRequestMatchResult : IComparable
{
/// <summary>
/// Gets the match percentage.
/// </summary>
/// <value>
/// The match percentage.
/// </value>
double AverageTotalScore { get; }
/// <summary>
/// Gets or sets a value indicating whether this instance is perfect match.
/// </summary>
/// <value>
/// <c>true</c> if this instance is perfect match; otherwise, <c>false</c>.
/// </value>
bool IsPerfectMatch { get; }
/// <summary>
/// Gets the match details.
/// </summary>
IList<MatchDetail> MatchDetails { get; }
/// <summary>
/// Gets or sets the total number of matches.
/// </summary>
/// <value>
/// The total number of matches.
/// </value>
int TotalNumber { get; }
/// <summary>
/// Gets or sets the match-score.
/// </summary>
/// <value>
/// The match-score.
/// </value>
double TotalScore { get; }
}
}

View File

@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Collections.Specialized;
using JetBrains.Annotations;
using WireMock.Admin.Mappings;
using WireMock.Logging;
namespace WireMock.Server
{
@@ -16,13 +17,20 @@ namespace WireMock.Server
/// </summary>
bool IsStarted { get; }
//IEnumerable<LogEntry> LogEntries { get; }
/// <summary>
/// Gets the request logs.
/// </summary>
IEnumerable<ILogEntry> LogEntries { get; }
/// <summary>
/// Gets the mappings as MappingModels.
/// </summary>
IEnumerable<MappingModel> MappingModels { get; }
/// <summary>
/// Gets the mappings.
/// </summary>
//[PublicAPI]
//IEnumerable<IMapping> Mappings { get; }
/// <summary>

View File

@@ -0,0 +1,61 @@
using System.Text;
using WireMock.Types;
namespace WireMock.Util
{
/// <summary>
/// IBodyData
/// </summary>
public interface IBodyData
{
/// <summary>
/// The body (as bytearray).
/// </summary>
byte[] BodyAsBytes { get; set; }
/// <summary>
/// Gets or sets the body as a file.
/// </summary>
string BodyAsFile { get; set; }
/// <summary>
/// Is the body as file cached?
/// </summary>
bool? BodyAsFileIsCached { get; set; }
/// <summary>
/// The body (as JSON object).
/// </summary>
object BodyAsJson { get; set; }
/// <summary>
/// Gets or sets a value indicating whether child objects to be indented according to the Newtonsoft.Json.JsonTextWriter.Indentation and Newtonsoft.Json.JsonTextWriter.IndentChar settings.
/// </summary>
bool? BodyAsJsonIndented { get; set; }
/// <summary>
/// The body as string, this is defined when BodyAsString or BodyAsJson are not null.
/// </summary>
string BodyAsString { get; set; }
/// <summary>
/// The detected body type (detection based on body content).
/// </summary>
BodyType DetectedBodyType { get; set; }
/// <summary>
/// The detected body type (detection based on Content-Type).
/// </summary>
BodyType DetectedBodyTypeFromContentType { get; set; }
/// <summary>
/// The detected compression.
/// </summary>
string DetectedCompression { get; set; }
/// <summary>
/// The body encoding.
/// </summary>
Encoding Encoding { get; set; }
}
}

View File

@@ -5,10 +5,10 @@ namespace WireMock.FluentAssertions
{
public class WireMockANumberOfCallsAssertions
{
private readonly WireMockServer _server;
private readonly IWireMockServer _server;
private readonly int _callsCount;
public WireMockANumberOfCallsAssertions(WireMockServer server, int callsCount)
public WireMockANumberOfCallsAssertions(IWireMockServer server, int callsCount)
{
_server = server;
_callsCount = callsCount;

View File

@@ -8,9 +8,9 @@ namespace WireMock.FluentAssertions
{
public class WireMockAssertions
{
private readonly WireMockServer _instance;
private readonly IWireMockServer _instance;
public WireMockAssertions(WireMockServer instance, int? callsCount)
public WireMockAssertions(IWireMockServer instance, int? callsCount)
{
_instance = instance;
}

View File

@@ -4,9 +4,9 @@ using WireMock.Server;
// ReSharper disable once CheckNamespace
namespace WireMock.FluentAssertions
{
public class WireMockReceivedAssertions : ReferenceTypeAssertions<WireMockServer, WireMockReceivedAssertions>
public class WireMockReceivedAssertions : ReferenceTypeAssertions<IWireMockServer, WireMockReceivedAssertions>
{
public WireMockReceivedAssertions(WireMockServer server)
public WireMockReceivedAssertions(IWireMockServer server)
{
Subject = server;
}

View File

@@ -5,7 +5,7 @@ namespace WireMock.FluentAssertions
{
public static class WireMockExtensions
{
public static WireMockReceivedAssertions Should(this WireMockServer instance)
public static WireMockReceivedAssertions Should(this IWireMockServer instance)
{
return new WireMockReceivedAssertions(instance);
}

View File

@@ -35,7 +35,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\WireMock.Net\WireMock.Net.csproj" />
<ProjectReference Include="..\WireMock.Net.Abstractions\WireMock.Net.Abstractions.csproj" />
</ItemGroup>
</Project>

View File

@@ -99,7 +99,7 @@ namespace WireMock
/// </summary>
/// <param name="requestMessage">The request message.</param>
/// <param name="nextState">The Next State.</param>
/// <returns>The <see cref="RequestMatchResult"/>.</returns>
/// <returns>The <see cref="IRequestMatchResult"/>.</returns>
RequestMatchResult GetRequestMatchResult(RequestMessage requestMessage, [CanBeNull] string nextState);
}
}

View File

@@ -6,78 +6,33 @@ namespace WireMock.Logging
/// <summary>
/// LogEntry
/// </summary>
public class LogEntry
public class LogEntry : ILogEntry
{
/// <summary>
/// Gets or sets the unique identifier.
/// </summary>
/// <value>
/// The unique identifier.
/// </value>
/// <inheritdoc cref="ILogEntry.Guid" />
public Guid Guid { get; set; }
/// <summary>
/// Gets or sets the request message.
/// </summary>
/// <value>
/// The request message.
/// </value>
public RequestMessage RequestMessage { get; set; }
/// <inheritdoc cref="ILogEntry.RequestMessage" />
public IRequestMessage RequestMessage { get; set; }
/// <summary>
/// Gets or sets the response message.
/// </summary>
/// <value>
/// The response message.
/// </value>
public ResponseMessage ResponseMessage { get; set; }
/// <inheritdoc cref="ILogEntry.ResponseMessage" />
public IResponseMessage ResponseMessage { get; set; }
/// <summary>
/// Gets or sets the request match result.
/// </summary>
/// <value>
/// The request match result.
/// </value>
public RequestMatchResult RequestMatchResult { get; set; }
/// <inheritdoc cref="ILogEntry.RequestMatchResult" />
public IRequestMatchResult RequestMatchResult { get; set; }
/// <summary>
/// Gets or sets the mapping unique identifier.
/// </summary>
/// <value>
/// The mapping unique identifier.
/// </value>
/// <inheritdoc cref="ILogEntry.MappingGuid" />
public Guid? MappingGuid { get; set; }
/// <summary>
/// Gets or sets the mapping unique title.
/// </summary>
/// <value>
/// The mapping unique title.
/// </value>
/// <inheritdoc cref="ILogEntry.MappingTitle" />
public string MappingTitle { get; set; }
/// <summary>
/// Gets or sets the partial mapping unique identifier.
/// </summary>
/// <value>
/// The mapping unique identifier.
/// </value>
/// <inheritdoc cref="ILogEntry.PartialMappingGuid" />
public Guid? PartialMappingGuid { get; set; }
/// <summary>
/// Gets or sets the partial mapping unique title.
/// </summary>
/// <value>
/// The mapping unique title.
/// </value>
/// <inheritdoc cref="ILogEntry.PartialMappingTitle" />
public string PartialMappingTitle { get; set; }
/// <summary>
/// Gets or sets the partial match result.
/// </summary>
/// <value>
/// The request match result.
/// </value>
public RequestMatchResult PartialMatchResult { get; set; }
/// <inheritdoc cref="ILogEntry.PartialMatchResult" />
public IRequestMatchResult PartialMatchResult { get; set; }
}
}

View File

@@ -15,6 +15,6 @@ namespace WireMock.Matchers.Request
/// <returns>
/// A value between 0.0 - 1.0 of the similarity.
/// </returns>
double GetMatchingScore([NotNull] RequestMessage requestMessage, [NotNull] RequestMatchResult requestMatchResult);
double GetMatchingScore([NotNull] IRequestMessage requestMessage, [NotNull] RequestMatchResult requestMatchResult);
}
}

View File

@@ -7,43 +7,21 @@ namespace WireMock.Matchers.Request
/// <summary>
/// RequestMatchResult
/// </summary>
public class RequestMatchResult : IComparable
public class RequestMatchResult : IRequestMatchResult
{
/// <summary>
/// Gets or sets the match-score.
/// </summary>
/// <value>
/// The match-score.
/// </value>
/// <inheritdoc cref="IRequestMatchResult.TotalScore" />
public double TotalScore => MatchDetails.Sum(md => md.Score);
/// <summary>
/// Gets or sets the total number of matches.
/// </summary>
/// <value>
/// The total number of matches.
/// </value>
/// <inheritdoc cref="IRequestMatchResult.TotalNumber" />
public int TotalNumber => MatchDetails.Count;
/// <summary>
/// Gets or sets a value indicating whether this instance is perfect match.
/// </summary>
/// <value>
/// <c>true</c> if this instance is perfect match; otherwise, <c>false</c>.
/// </value>
/// <inheritdoc cref="IRequestMatchResult.IsPerfectMatch" />
public bool IsPerfectMatch => Math.Abs(TotalScore - TotalNumber) < MatchScores.Tolerance;
/// <summary>
/// Gets the match percentage.
/// </summary>
/// <value>
/// The match percentage.
/// </value>
/// <inheritdoc cref="IRequestMatchResult.AverageTotalScore" />
public double AverageTotalScore => TotalNumber == 0 ? 0.0 : TotalScore / TotalNumber;
/// <summary>
/// Gets the match details.
/// </summary>
/// <inheritdoc cref="IRequestMatchResult.MatchDetails" />
public IList<MatchDetail> MatchDetails { get; } = new List<MatchDetail>();
/// <summary>

View File

@@ -99,13 +99,13 @@ namespace WireMock.Matchers.Request
}
/// <see cref="IRequestMatcher.GetMatchingScore"/>
public double GetMatchingScore(RequestMessage requestMessage, RequestMatchResult requestMatchResult)
public double GetMatchingScore(IRequestMessage requestMessage, RequestMatchResult requestMatchResult)
{
double score = CalculateMatchScore(requestMessage);
return requestMatchResult.AddScore(GetType(), score);
}
private double CalculateMatchScore(RequestMessage requestMessage, IMatcher matcher)
private double CalculateMatchScore(IRequestMessage requestMessage, IMatcher matcher)
{
// Check if the matcher is a IObjectMatcher
if (matcher is IObjectMatcher objectMatcher)
@@ -136,7 +136,7 @@ namespace WireMock.Matchers.Request
return MatchScores.Mismatch;
}
private double CalculateMatchScore(RequestMessage requestMessage)
private double CalculateMatchScore(IRequestMessage requestMessage)
{
if (Matchers != null && Matchers.Any())
{

View File

@@ -51,13 +51,13 @@ namespace WireMock.Matchers.Request
}
/// <inheritdoc cref="IRequestMatcher.GetMatchingScore"/>
public double GetMatchingScore(RequestMessage requestMessage, RequestMatchResult requestMatchResult)
public double GetMatchingScore(IRequestMessage requestMessage, RequestMatchResult requestMatchResult)
{
double score = IsMatch(requestMessage);
return requestMatchResult.AddScore(GetType(), score);
}
private double IsMatch(RequestMessage requestMessage)
private double IsMatch(IRequestMessage requestMessage)
{
if (Matchers != null)
{

View File

@@ -34,7 +34,7 @@ namespace WireMock.Matchers.Request
}
/// <inheritdoc cref="IRequestMatcher.GetMatchingScore"/>
public double GetMatchingScore(RequestMessage requestMessage, RequestMatchResult requestMatchResult)
public double GetMatchingScore(IRequestMessage requestMessage, RequestMatchResult requestMatchResult)
{
if (!RequestMatchers.Any())
{

View File

@@ -91,13 +91,13 @@ namespace WireMock.Matchers.Request
}
/// <inheritdoc cref="IRequestMatcher.GetMatchingScore"/>
public double GetMatchingScore(RequestMessage requestMessage, RequestMatchResult requestMatchResult)
public double GetMatchingScore(IRequestMessage requestMessage, RequestMatchResult requestMatchResult)
{
double score = IsMatch(requestMessage);
return requestMatchResult.AddScore(GetType(), score);
}
private double IsMatch(RequestMessage requestMessage)
private double IsMatch(IRequestMessage requestMessage)
{
if (requestMessage.Cookies == null)
{

View File

@@ -92,13 +92,13 @@ namespace WireMock.Matchers.Request
}
/// <inheritdoc cref="IRequestMatcher.GetMatchingScore"/>
public double GetMatchingScore(RequestMessage requestMessage, RequestMatchResult requestMatchResult)
public double GetMatchingScore(IRequestMessage requestMessage, RequestMatchResult requestMatchResult)
{
double score = IsMatch(requestMessage);
return requestMatchResult.AddScore(GetType(), score);
}
private double IsMatch(RequestMessage requestMessage)
private double IsMatch(IRequestMessage requestMessage)
{
if (requestMessage.Headers == null)
{

View File

@@ -31,13 +31,13 @@ namespace WireMock.Matchers.Request
}
/// <inheritdoc cref="IRequestMatcher.GetMatchingScore"/>
public double GetMatchingScore(RequestMessage requestMessage, RequestMatchResult requestMatchResult)
public double GetMatchingScore(IRequestMessage requestMessage, RequestMatchResult requestMatchResult)
{
double score = MatchBehaviourHelper.Convert(_matchBehaviour, IsMatch(requestMessage));
return requestMatchResult.AddScore(GetType(), score);
}
private double IsMatch(RequestMessage requestMessage)
private double IsMatch(IRequestMessage requestMessage)
{
return MatchScores.ToScore(Methods.Contains(requestMessage.Method, StringComparer.OrdinalIgnoreCase));
}

View File

@@ -84,20 +84,20 @@ namespace WireMock.Matchers.Request
}
/// <inheritdoc cref="IRequestMatcher.GetMatchingScore"/>
public double GetMatchingScore(RequestMessage requestMessage, RequestMatchResult requestMatchResult)
public double GetMatchingScore(IRequestMessage requestMessage, RequestMatchResult requestMatchResult)
{
double score = MatchBehaviourHelper.Convert(_matchBehaviour, IsMatch(requestMessage));
return requestMatchResult.AddScore(GetType(), score);
}
private double IsMatch(RequestMessage requestMessage)
private double IsMatch(IRequestMessage requestMessage)
{
if (Funcs != null)
{
return MatchScores.ToScore(requestMessage.Query != null && Funcs.Any(f => f(requestMessage.Query)));
}
WireMockList<string> valuesPresentInRequestMessage = requestMessage.GetParameter(Key, IgnoreCase ?? false);
WireMockList<string> valuesPresentInRequestMessage = ((RequestMessage) requestMessage).GetParameter(Key, IgnoreCase ?? false);
if (valuesPresentInRequestMessage == null)
{
// Key is not present at all, just return Mismatch

View File

@@ -53,13 +53,13 @@ namespace WireMock.Matchers.Request
}
/// <inheritdoc cref="IRequestMatcher.GetMatchingScore"/>
public double GetMatchingScore(RequestMessage requestMessage, RequestMatchResult requestMatchResult)
public double GetMatchingScore(IRequestMessage requestMessage, RequestMatchResult requestMatchResult)
{
double score = IsMatch(requestMessage);
return requestMatchResult.AddScore(GetType(), score);
}
private double IsMatch(RequestMessage requestMessage)
private double IsMatch(IRequestMessage requestMessage)
{
if (Matchers != null)
{

View File

@@ -32,7 +32,7 @@ namespace WireMock.Matchers.Request
}
/// <inheritdoc />
public double GetMatchingScore(RequestMessage requestMessage, RequestMatchResult requestMatchResult)
public double GetMatchingScore(IRequestMessage requestMessage, RequestMatchResult requestMatchResult)
{
double score = IsMatch();
return requestMatchResult.AddScore(GetType(), score);

View File

@@ -51,13 +51,13 @@ namespace WireMock.Matchers.Request
}
/// <inheritdoc cref="IRequestMatcher.GetMatchingScore"/>
public double GetMatchingScore(RequestMessage requestMessage, RequestMatchResult requestMatchResult)
public double GetMatchingScore(IRequestMessage requestMessage, RequestMatchResult requestMatchResult)
{
double score = IsMatch(requestMessage);
return requestMatchResult.AddScore(GetType(), score);
}
private double IsMatch(RequestMessage requestMessage)
private double IsMatch(IRequestMessage requestMessage)
{
if (Matchers != null)
{

View File

@@ -15,131 +15,81 @@ namespace WireMock
/// <summary>
/// The RequestMessage.
/// </summary>
public class RequestMessage
public class RequestMessage : IRequestMessage
{
/// <summary>
/// Gets the Client IP Address.
/// </summary>
/// <inheritdoc cref="IRequestMessage.ClientIP" />
public string ClientIP { get; }
/// <summary>
/// Gets the url (relative).
/// </summary>
/// <inheritdoc cref="IRequestMessage.Url" />
public string Url { get; }
/// <summary>
/// Gets the AbsoluteUrl.
/// </summary>
/// <inheritdoc cref="IRequestMessage.AbsoluteUrl" />
public string AbsoluteUrl { get; }
/// <summary>
/// The ProxyUrl (if a proxy is used).
/// </summary>
/// <inheritdoc cref="IRequestMessage.ProxyUrl" />
public string ProxyUrl { get; set; }
/// <summary>
/// Gets the DateTime.
/// </summary>
/// <inheritdoc cref="IRequestMessage.DateTime" />
public DateTime DateTime { get; set; }
/// <summary>
/// Gets the path (relative).
/// </summary>
/// <inheritdoc cref="IRequestMessage.Path" />
public string Path { get; }
/// <summary>
/// Gets the AbsolutePath.
/// </summary>
/// <inheritdoc cref="IRequestMessage.AbsolutePath" />
public string AbsolutePath { get; }
/// <summary>
/// Gets the path segments.
/// </summary>
/// <inheritdoc cref="IRequestMessage.PathSegments" />
public string[] PathSegments { get; }
/// <summary>
/// Gets the absolute path segments.
/// </summary>
/// <inheritdoc cref="IRequestMessage.AbsolutePathSegments" />
public string[] AbsolutePathSegments { get; }
/// <summary>
/// Gets the method.
/// </summary>
/// <inheritdoc cref="IRequestMessage.Method" />
public string Method { get; }
/// <summary>
/// Gets the headers.
/// </summary>
/// <inheritdoc cref="IRequestMessage.Headers" />
public IDictionary<string, WireMockList<string>> Headers { get; }
/// <summary>
/// Gets the cookies.
/// </summary>
/// <inheritdoc cref="IRequestMessage.Cookies" />
public IDictionary<string, string> Cookies { get; }
/// <summary>
/// Gets the query.
/// </summary>
/// <inheritdoc cref="IRequestMessage.Query" />
public IDictionary<string, WireMockList<string>> Query { get; }
/// <summary>
/// Gets the raw query.
/// </summary>
/// <inheritdoc cref="IRequestMessage.RawQuery" />
public string RawQuery { get; }
/// <summary>
/// The body.
/// </summary>
public BodyData BodyData { get; }
/// <inheritdoc cref="IRequestMessage.BodyData" />
public IBodyData BodyData { get; }
/// <summary>
/// The original body as string. Convenience getter for Handlebars.
/// </summary>
/// <inheritdoc cref="IRequestMessage.Body" />
public string Body { get; }
/// <summary>
/// The body (as JSON object). Convenience getter for Handlebars.
/// </summary>
/// <inheritdoc cref="IRequestMessage.BodyAsJson" />
public object BodyAsJson { get; }
/// <summary>
/// The body (as bytearray). Convenience getter for Handlebars.
/// </summary>
/// <inheritdoc cref="IRequestMessage.BodyAsBytes" />
public byte[] BodyAsBytes { get; }
/// <summary>
/// The detected body type. Convenience getter for Handlebars.
/// </summary>
/// <inheritdoc cref="IRequestMessage.DetectedBodyType" />
public string DetectedBodyType { get; }
/// <summary>
/// The detected body type from the Content-Type header. Convenience getter for Handlebars.
/// </summary>
/// <inheritdoc cref="IRequestMessage.DetectedBodyTypeFromContentType" />
public string DetectedBodyTypeFromContentType { get; }
/// <summary>
/// The detected compression from the Content-Encoding header. Convenience getter for Handlebars.
/// </summary>
/// <inheritdoc cref="IRequestMessage.DetectedCompression" />
public string DetectedCompression { get; }
/// <summary>
/// Gets the Host
/// </summary>
/// <inheritdoc cref="IRequestMessage.Host" />
public string Host { get; }
/// <summary>
/// Gets the protocol
/// </summary>
/// <inheritdoc cref="IRequestMessage.Protocol" />
public string Protocol { get; }
/// <summary>
/// Gets the port
/// </summary>
/// <inheritdoc cref="IRequestMessage.Port" />
public int Port { get; }
/// <summary>
/// Gets the origin
/// </summary>
/// <inheritdoc cref="IRequestMessage.Origin" />
public string Origin { get; }
/// <summary>

View File

@@ -12,41 +12,27 @@ namespace WireMock
/// <summary>
/// The ResponseMessage.
/// </summary>
public class ResponseMessage
public class ResponseMessage : IResponseMessage
{
/// <summary>
/// Gets the headers.
/// </summary>
/// <inheritdoc cref="IResponseMessage.Headers" />
public IDictionary<string, WireMockList<string>> Headers { get; set; } = new Dictionary<string, WireMockList<string>>();
/// <summary>
/// Gets or sets the status code.
/// </summary>
/// <inheritdoc cref="IResponseMessage.StatusCode" />
public object StatusCode { get; set; }
/// <summary>
/// Gets or sets the body.
/// </summary>
/// <inheritdoc cref="IResponseMessage.BodyOriginal" />
public string BodyOriginal { get; set; }
/// <summary>
/// Gets or sets the body destination (SameAsSource, String or Bytes).
/// </summary>
/// <inheritdoc cref="IResponseMessage.BodyDestination" />
public string BodyDestination { get; set; }
/// <summary>
/// The Body.
/// </summary>
public BodyData BodyData { get; set; }
/// <inheritdoc cref="IResponseMessage.BodyData" />
public IBodyData BodyData { get; set; }
/// <summary>
/// The FaultType.
/// </summary>
/// <inheritdoc cref="IResponseMessage.FaultType" />
public FaultType FaultType { get; set; }
/// <summary>
/// Gets or sets the Fault percentage.
/// </summary>
/// <inheritdoc cref="IResponseMessage.FaultPercentage" />
public double? FaultPercentage { get; set; }
/// <summary>

View File

@@ -10,7 +10,7 @@ namespace WireMock.Serialization
{
internal static class LogEntryMapper
{
public static LogEntryModel Map(LogEntry logEntry)
public static LogEntryModel Map(ILogEntry logEntry)
{
var logRequestModel = new LogRequestModel
{
@@ -124,7 +124,7 @@ namespace WireMock.Serialization
};
}
private static LogRequestMatchModel Map(RequestMatchResult matchResult)
private static LogRequestMatchModel Map(IRequestMatchResult matchResult)
{
if (matchResult == null)
{

File diff suppressed because it is too large Load Diff

View File

@@ -1,93 +1,91 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using JetBrains.Annotations;
using WireMock.Logging;
using WireMock.Matchers;
using WireMock.Matchers.Request;
namespace WireMock.Server
{
public partial class WireMockServer
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using JetBrains.Annotations;
using WireMock.Logging;
using WireMock.Matchers;
using WireMock.Matchers.Request;
namespace WireMock.Server
{
public partial class WireMockServer
{
/// <inheritdoc cref="IWireMockServer.LogEntriesChanged" />
[PublicAPI]
public event NotifyCollectionChangedEventHandler LogEntriesChanged
{
add
{
_options.LogEntries.CollectionChanged += (sender, eventRecordArgs) =>
{
try
{
value(sender, eventRecordArgs);
}
catch (Exception exception)
{
_options.Logger.Error("Error calling the LogEntriesChanged event handler: {0}", exception.Message);
}
};
}
remove => _options.LogEntries.CollectionChanged -= value;
/// <inheritdoc cref="IWireMockServer.LogEntriesChanged" />
[PublicAPI]
public event NotifyCollectionChangedEventHandler LogEntriesChanged
{
add
{
_options.LogEntries.CollectionChanged += (sender, eventRecordArgs) =>
{
try
{
value(sender, eventRecordArgs);
}
catch (Exception exception)
{
_options.Logger.Error("Error calling the LogEntriesChanged event handler: {0}", exception.Message);
}
};
}
remove => _options.LogEntries.CollectionChanged -= value;
}
/// <summary>
/// Gets the request logs.
/// </summary>
[PublicAPI]
public IEnumerable<LogEntry> LogEntries => new ReadOnlyCollection<LogEntry>(_options.LogEntries.ToList());
/// <inheritdoc cref="IWireMockServer.LogEntries" />
[PublicAPI]
public IEnumerable<ILogEntry> LogEntries => new ReadOnlyCollection<LogEntry>(_options.LogEntries.ToList());
/// <summary>
/// The search log-entries based on matchers.
/// </summary>
/// <param name="matchers">The matchers.</param>
/// <returns>The <see cref="IEnumerable"/>.</returns>
[PublicAPI]
public IEnumerable<LogEntry> FindLogEntries([NotNull] params IRequestMatcher[] matchers)
{
var results = new Dictionary<LogEntry, RequestMatchResult>();
foreach (var log in _options.LogEntries.ToList())
{
var requestMatchResult = new RequestMatchResult();
foreach (var matcher in matchers)
{
matcher.GetMatchingScore(log.RequestMessage, requestMatchResult);
}
if (requestMatchResult.AverageTotalScore > MatchScores.AlmostPerfect)
{
results.Add(log, requestMatchResult);
}
}
return new ReadOnlyCollection<LogEntry>(results.OrderBy(x => x.Value).Select(x => x.Key).ToList());
/// <summary>
/// The search log-entries based on matchers.
/// </summary>
/// <param name="matchers">The matchers.</param>
/// <returns>The <see cref="IEnumerable"/>.</returns>
[PublicAPI]
public IEnumerable<LogEntry> FindLogEntries([NotNull] params IRequestMatcher[] matchers)
{
var results = new Dictionary<LogEntry, RequestMatchResult>();
foreach (var log in _options.LogEntries.ToList())
{
var requestMatchResult = new RequestMatchResult();
foreach (var matcher in matchers)
{
matcher.GetMatchingScore(log.RequestMessage, requestMatchResult);
}
if (requestMatchResult.AverageTotalScore > MatchScores.AlmostPerfect)
{
results.Add(log, requestMatchResult);
}
}
return new ReadOnlyCollection<LogEntry>(results.OrderBy(x => x.Value).Select(x => x.Key).ToList());
}
/// <inheritdoc cref="IWireMockServer.ResetLogEntries" />
[PublicAPI]
public void ResetLogEntries()
{
_options.LogEntries.Clear();
/// <inheritdoc cref="IWireMockServer.ResetLogEntries" />
[PublicAPI]
public void ResetLogEntries()
{
_options.LogEntries.Clear();
}
/// <inheritdoc cref="IWireMockServer.DeleteLogEntry" />
[PublicAPI]
public bool DeleteLogEntry(Guid guid)
{
// Check a logentry exists with the same GUID, if so, remove it.
var existing = _options.LogEntries.ToList().FirstOrDefault(m => m.Guid == guid);
if (existing != null)
{
_options.LogEntries.Remove(existing);
return true;
}
return false;
}
}
/// <inheritdoc cref="IWireMockServer.DeleteLogEntry" />
[PublicAPI]
public bool DeleteLogEntry(Guid guid)
{
// Check a logentry exists with the same GUID, if so, remove it.
var existing = _options.LogEntries.ToList().FirstOrDefault(m => m.Guid == guid);
if (existing != null)
{
_options.LogEntries.Remove(existing);
return true;
}
return false;
}
}
}

View File

@@ -6,56 +6,36 @@ namespace WireMock.Util
/// <summary>
/// BodyData
/// </summary>
public class BodyData
public class BodyData : IBodyData
{
/// <summary>
/// The body encoding.
/// </summary>
/// <inheritdoc cref="IBodyData.Encoding" />
public Encoding Encoding { get; set; }
/// <summary>
/// The body as string, this is defined when BodyAsString or BodyAsJson are not null.
/// </summary>
/// <inheritdoc cref="IBodyData.BodyAsBytes" />
public string BodyAsString { get; set; }
/// <summary>
/// The body (as JSON object).
/// </summary>
/// <inheritdoc cref="IBodyData.BodyAsJson" />
public object BodyAsJson { get; set; }
/// <summary>
/// The body (as bytearray).
/// </summary>
/// <inheritdoc cref="IBodyData.BodyAsBytes" />
public byte[] BodyAsBytes { get; set; }
/// <summary>
/// Gets or sets a value indicating whether child objects to be indented according to the Newtonsoft.Json.JsonTextWriter.Indentation and Newtonsoft.Json.JsonTextWriter.IndentChar settings.
/// </summary>
/// <inheritdoc cref="IBodyData.BodyAsJsonIndented" />
public bool? BodyAsJsonIndented { get; set; }
/// <summary>
/// Gets or sets the body as a file.
/// </summary>
/// <inheritdoc cref="IBodyData.BodyAsFile" />
public string BodyAsFile { get; set; }
/// <summary>
/// Is the body as file cached?
/// </summary>
/// <inheritdoc cref="IBodyData.BodyAsFileIsCached" />
public bool? BodyAsFileIsCached { get; set; }
/// <summary>
/// The detected body type (detection based on body content).
/// </summary>
/// <inheritdoc cref="IBodyData.DetectedBodyType" />
public BodyType DetectedBodyType { get; set; }
/// <summary>
/// The detected body type (detection based on Content-Type).
/// </summary>
/// <inheritdoc cref="IBodyData.DetectedBodyTypeFromContentType" />
public BodyType DetectedBodyTypeFromContentType { get; set; }
/// <summary>
/// The detected compression.
/// </summary>
/// <inheritdoc cref="IRequestMessage.DetectedCompression" />
public string DetectedCompression { get; set; }
}
}