mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-01 07:03:29 +02:00
Support WithBody with multiple matchers (#304)
This commit is contained in:
@@ -4,7 +4,7 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<VersionPrefix>1.0.24</VersionPrefix>
|
<VersionPrefix>1.0.25</VersionPrefix>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<Choose>
|
<Choose>
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
https://github.com/StefH/GitHubReleaseNotes
|
https://github.com/StefH/GitHubReleaseNotes
|
||||||
|
|
||||||
GitHubReleaseNotes.exe --output CHANGELOG.md --skip-empty-releases --version 1.0.24.0
|
GitHubReleaseNotes.exe --output CHANGELOG.md --skip-empty-releases --version 1.0.25.0
|
||||||
@@ -94,6 +94,13 @@ namespace WireMock.Net.ConsoleApplication
|
|||||||
)
|
)
|
||||||
.RespondWith(Response.Create().WithBody("XPathMatcher!"));
|
.RespondWith(Response.Create().WithBody("XPathMatcher!"));
|
||||||
|
|
||||||
|
server
|
||||||
|
.Given(Request.Create()
|
||||||
|
.WithPath("/xpaths").UsingPost()
|
||||||
|
.WithBody(new[] { new XPathMatcher("/todo-list[count(todo-item) = 3]"), new XPathMatcher("/todo-list[count(todo-item) = 4]") })
|
||||||
|
)
|
||||||
|
.RespondWith(Response.Create().WithBody("xpaths!"));
|
||||||
|
|
||||||
server
|
server
|
||||||
.Given(Request
|
.Given(Request
|
||||||
.Create()
|
.Create()
|
||||||
|
|||||||
@@ -9,5 +9,10 @@
|
|||||||
/// Gets or sets the matcher.
|
/// Gets or sets the matcher.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public MatcherModel Matcher { get; set; }
|
public MatcherModel Matcher { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the matchers.
|
||||||
|
/// </summary>
|
||||||
|
public MatcherModel[] Matchers { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
using System;
|
|
||||||
using JetBrains.Annotations;
|
using JetBrains.Annotations;
|
||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
using WireMock.Util;
|
using WireMock.Util;
|
||||||
using WireMock.Validation;
|
using WireMock.Validation;
|
||||||
|
|
||||||
@@ -26,16 +27,16 @@ namespace WireMock.Matchers.Request
|
|||||||
public Func<object, bool> JsonFunc { get; }
|
public Func<object, bool> JsonFunc { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The matcher.
|
/// The matchers.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public IMatcher Matcher { get; }
|
public IMatcher[] Matchers { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="RequestMessageBodyMatcher"/> class.
|
/// Initializes a new instance of the <see cref="RequestMessageBodyMatcher"/> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="matchBehaviour">The match behaviour.</param>
|
/// <param name="matchBehaviour">The match behaviour.</param>
|
||||||
/// <param name="body">The body.</param>
|
/// <param name="body">The body.</param>
|
||||||
public RequestMessageBodyMatcher(MatchBehaviour matchBehaviour, [NotNull] string body) : this(new WildcardMatcher(matchBehaviour, body))
|
public RequestMessageBodyMatcher(MatchBehaviour matchBehaviour, [NotNull] string body) : this(new[] { new WildcardMatcher(matchBehaviour, body) }.Cast<IMatcher>().ToArray())
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -44,7 +45,7 @@ namespace WireMock.Matchers.Request
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="matchBehaviour">The match behaviour.</param>
|
/// <param name="matchBehaviour">The match behaviour.</param>
|
||||||
/// <param name="body">The body.</param>
|
/// <param name="body">The body.</param>
|
||||||
public RequestMessageBodyMatcher(MatchBehaviour matchBehaviour, [NotNull] byte[] body) : this(new ExactObjectMatcher(matchBehaviour, body))
|
public RequestMessageBodyMatcher(MatchBehaviour matchBehaviour, [NotNull] byte[] body) : this(new[] { new ExactObjectMatcher(matchBehaviour, body) }.Cast<IMatcher>().ToArray())
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,7 +54,7 @@ namespace WireMock.Matchers.Request
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="matchBehaviour">The match behaviour.</param>
|
/// <param name="matchBehaviour">The match behaviour.</param>
|
||||||
/// <param name="body">The body.</param>
|
/// <param name="body">The body.</param>
|
||||||
public RequestMessageBodyMatcher(MatchBehaviour matchBehaviour, [NotNull] object body) : this(new ExactObjectMatcher(matchBehaviour, body))
|
public RequestMessageBodyMatcher(MatchBehaviour matchBehaviour, [NotNull] object body) : this(new[] { new ExactObjectMatcher(matchBehaviour, body) }.Cast<IMatcher>().ToArray())
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,25 +91,24 @@ namespace WireMock.Matchers.Request
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="RequestMessageBodyMatcher"/> class.
|
/// Initializes a new instance of the <see cref="RequestMessageBodyMatcher"/> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="matcher">The matcher.</param>
|
/// <param name="matchers">The matchers.</param>
|
||||||
public RequestMessageBodyMatcher([NotNull] IMatcher matcher)
|
public RequestMessageBodyMatcher([NotNull] params IMatcher[] matchers)
|
||||||
{
|
{
|
||||||
Check.NotNull(matcher, nameof(matcher));
|
Check.NotNull(matchers, nameof(matchers));
|
||||||
|
Matchers = matchers;
|
||||||
Matcher = matcher;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <see cref="IRequestMatcher.GetMatchingScore"/>
|
/// <see cref="IRequestMatcher.GetMatchingScore"/>
|
||||||
public double GetMatchingScore(RequestMessage requestMessage, RequestMatchResult requestMatchResult)
|
public double GetMatchingScore(RequestMessage requestMessage, RequestMatchResult requestMatchResult)
|
||||||
{
|
{
|
||||||
double score = IsMatch(requestMessage);
|
double score = CalculateMatchScore(requestMessage);
|
||||||
return requestMatchResult.AddScore(GetType(), score);
|
return requestMatchResult.AddScore(GetType(), score);
|
||||||
}
|
}
|
||||||
|
|
||||||
private double IsMatch(RequestMessage requestMessage)
|
private double CalculateMatchScore(RequestMessage requestMessage, IMatcher matcher)
|
||||||
{
|
{
|
||||||
// Check if the matcher is a IObjectMatcher
|
// Check if the matcher is a IObjectMatcher
|
||||||
if (Matcher is IObjectMatcher objectMatcher)
|
if (matcher is IObjectMatcher objectMatcher)
|
||||||
{
|
{
|
||||||
// If the body is a JSON object, try to match.
|
// If the body is a JSON object, try to match.
|
||||||
if (requestMessage?.BodyData?.DetectedBodyType == BodyType.Json)
|
if (requestMessage?.BodyData?.DetectedBodyType == BodyType.Json)
|
||||||
@@ -124,7 +124,7 @@ namespace WireMock.Matchers.Request
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Check if the matcher is a IStringMatcher
|
// Check if the matcher is a IStringMatcher
|
||||||
if (Matcher is IStringMatcher stringMatcher)
|
if (matcher is IStringMatcher stringMatcher)
|
||||||
{
|
{
|
||||||
// If the body is a Json or a String, use the BodyAsString to match on.
|
// If the body is a Json or a String, use the BodyAsString to match on.
|
||||||
if (requestMessage?.BodyData?.DetectedBodyType == BodyType.Json || requestMessage?.BodyData?.DetectedBodyType == BodyType.String)
|
if (requestMessage?.BodyData?.DetectedBodyType == BodyType.Json || requestMessage?.BodyData?.DetectedBodyType == BodyType.String)
|
||||||
@@ -133,6 +133,16 @@ namespace WireMock.Matchers.Request
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return MatchScores.Mismatch;
|
||||||
|
}
|
||||||
|
|
||||||
|
private double CalculateMatchScore(RequestMessage requestMessage)
|
||||||
|
{
|
||||||
|
if (Matchers != null && Matchers.Any())
|
||||||
|
{
|
||||||
|
return Matchers.Max(matcher => CalculateMatchScore(requestMessage, matcher));
|
||||||
|
}
|
||||||
|
|
||||||
if (Func != null)
|
if (Func != null)
|
||||||
{
|
{
|
||||||
return MatchScores.ToScore(requestMessage?.BodyData?.DetectedBodyType == BodyType.String && Func(requestMessage.BodyData.BodyAsString));
|
return MatchScores.ToScore(requestMessage?.BodyData?.DetectedBodyType == BodyType.String && Func(requestMessage.BodyData.BodyAsString));
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
using System;
|
using JetBrains.Annotations;
|
||||||
using JetBrains.Annotations;
|
using System;
|
||||||
using WireMock.Matchers;
|
using WireMock.Matchers;
|
||||||
|
|
||||||
namespace WireMock.RequestBuilders
|
namespace WireMock.RequestBuilders
|
||||||
@@ -16,6 +16,13 @@ namespace WireMock.RequestBuilders
|
|||||||
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
|
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
|
||||||
IRequestBuilder WithBody([NotNull] IMatcher matcher);
|
IRequestBuilder WithBody([NotNull] IMatcher matcher);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// WithBody: IMatcher[]
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="matchers">The matchers.</param>
|
||||||
|
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
|
||||||
|
IRequestBuilder WithBody([NotNull] IMatcher[] matchers);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// WithBody: Body as string
|
/// WithBody: Body as string
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
73
src/WireMock.Net/RequestBuilders/Request.WithBody.cs
Normal file
73
src/WireMock.Net/RequestBuilders/Request.WithBody.cs
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
using System;
|
||||||
|
using WireMock.Matchers;
|
||||||
|
using WireMock.Matchers.Request;
|
||||||
|
using WireMock.Validation;
|
||||||
|
|
||||||
|
namespace WireMock.RequestBuilders
|
||||||
|
{
|
||||||
|
public partial class Request
|
||||||
|
{
|
||||||
|
/// <inheritdoc cref="IBodyRequestBuilder.WithBody(string, MatchBehaviour)"/>
|
||||||
|
public IRequestBuilder WithBody(string body, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch)
|
||||||
|
{
|
||||||
|
_requestMatchers.Add(new RequestMessageBodyMatcher(matchBehaviour, body));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc cref="IBodyRequestBuilder.WithBody(byte[], MatchBehaviour)"/>
|
||||||
|
public IRequestBuilder WithBody(byte[] body, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch)
|
||||||
|
{
|
||||||
|
_requestMatchers.Add(new RequestMessageBodyMatcher(matchBehaviour, body));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc cref="IBodyRequestBuilder.WithBody(object, MatchBehaviour)"/>
|
||||||
|
public IRequestBuilder WithBody(object body, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch)
|
||||||
|
{
|
||||||
|
_requestMatchers.Add(new RequestMessageBodyMatcher(matchBehaviour, body));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc cref="IBodyRequestBuilder.WithBody(IMatcher[])"/>
|
||||||
|
public IRequestBuilder WithBody(IMatcher matcher)
|
||||||
|
{
|
||||||
|
return WithBody(new[] { matcher });
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc cref="IBodyRequestBuilder.WithBody(IMatcher[])"/>
|
||||||
|
public IRequestBuilder WithBody(IMatcher[] matchers)
|
||||||
|
{
|
||||||
|
Check.NotNull(matchers, nameof(matchers));
|
||||||
|
|
||||||
|
_requestMatchers.Add(new RequestMessageBodyMatcher(matchers));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc cref="IBodyRequestBuilder.WithBody(Func{string, bool})"/>
|
||||||
|
public IRequestBuilder WithBody(Func<string, bool> func)
|
||||||
|
{
|
||||||
|
Check.NotNull(func, nameof(func));
|
||||||
|
|
||||||
|
_requestMatchers.Add(new RequestMessageBodyMatcher(func));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc cref="IBodyRequestBuilder.WithBody(Func{byte[], bool})"/>
|
||||||
|
public IRequestBuilder WithBody(Func<byte[], bool> func)
|
||||||
|
{
|
||||||
|
Check.NotNull(func, nameof(func));
|
||||||
|
|
||||||
|
_requestMatchers.Add(new RequestMessageBodyMatcher(func));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc cref="IBodyRequestBuilder.WithBody(Func{object, bool})"/>
|
||||||
|
public IRequestBuilder WithBody(Func<object, bool> func)
|
||||||
|
{
|
||||||
|
Check.NotNull(func, nameof(func));
|
||||||
|
|
||||||
|
_requestMatchers.Add(new RequestMessageBodyMatcher(func));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -233,63 +233,6 @@ namespace WireMock.RequestBuilders
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc cref="IBodyRequestBuilder.WithBody(string, MatchBehaviour)"/>
|
|
||||||
public IRequestBuilder WithBody(string body, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch)
|
|
||||||
{
|
|
||||||
_requestMatchers.Add(new RequestMessageBodyMatcher(matchBehaviour, body));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc cref="IBodyRequestBuilder.WithBody(byte[], MatchBehaviour)"/>
|
|
||||||
public IRequestBuilder WithBody(byte[] body, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch)
|
|
||||||
{
|
|
||||||
_requestMatchers.Add(new RequestMessageBodyMatcher(matchBehaviour, body));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc cref="IBodyRequestBuilder.WithBody(object, MatchBehaviour)"/>
|
|
||||||
public IRequestBuilder WithBody(object body, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch)
|
|
||||||
{
|
|
||||||
_requestMatchers.Add(new RequestMessageBodyMatcher(matchBehaviour, body));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc cref="IBodyRequestBuilder.WithBody(IMatcher)"/>
|
|
||||||
public IRequestBuilder WithBody(IMatcher matcher)
|
|
||||||
{
|
|
||||||
Check.NotNull(matcher, nameof(matcher));
|
|
||||||
|
|
||||||
_requestMatchers.Add(new RequestMessageBodyMatcher(matcher));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc cref="IBodyRequestBuilder.WithBody(Func{string, bool})"/>
|
|
||||||
public IRequestBuilder WithBody(Func<string, bool> func)
|
|
||||||
{
|
|
||||||
Check.NotNull(func, nameof(func));
|
|
||||||
|
|
||||||
_requestMatchers.Add(new RequestMessageBodyMatcher(func));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc cref="IBodyRequestBuilder.WithBody(Func{byte[], bool})"/>
|
|
||||||
public IRequestBuilder WithBody(Func<byte[], bool> func)
|
|
||||||
{
|
|
||||||
Check.NotNull(func, nameof(func));
|
|
||||||
|
|
||||||
_requestMatchers.Add(new RequestMessageBodyMatcher(func));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc cref="IBodyRequestBuilder.WithBody(Func{object, bool})"/>
|
|
||||||
public IRequestBuilder WithBody(Func<object, bool> func)
|
|
||||||
{
|
|
||||||
Check.NotNull(func, nameof(func));
|
|
||||||
|
|
||||||
_requestMatchers.Add(new RequestMessageBodyMatcher(func));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc cref="IHeadersAndCookiesRequestBuilder.WithHeader(string, string, MatchBehaviour)"/>
|
/// <inheritdoc cref="IHeadersAndCookiesRequestBuilder.WithHeader(string, string, MatchBehaviour)"/>
|
||||||
public IRequestBuilder WithHeader(string name, string pattern, MatchBehaviour matchBehaviour)
|
public IRequestBuilder WithHeader(string name, string pattern, MatchBehaviour matchBehaviour)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -21,14 +21,14 @@ namespace WireMock.Serialization
|
|||||||
var headerMatchers = request.GetRequestMessageMatchers<RequestMessageHeaderMatcher>();
|
var headerMatchers = request.GetRequestMessageMatchers<RequestMessageHeaderMatcher>();
|
||||||
var cookieMatchers = request.GetRequestMessageMatchers<RequestMessageCookieMatcher>();
|
var cookieMatchers = request.GetRequestMessageMatchers<RequestMessageCookieMatcher>();
|
||||||
var paramsMatchers = request.GetRequestMessageMatchers<RequestMessageParamMatcher>();
|
var paramsMatchers = request.GetRequestMessageMatchers<RequestMessageParamMatcher>();
|
||||||
var bodyMatcher = request.GetRequestMessageMatcher<RequestMessageBodyMatcher>();
|
|
||||||
var methodMatcher = request.GetRequestMessageMatcher<RequestMessageMethodMatcher>();
|
var methodMatcher = request.GetRequestMessageMatcher<RequestMessageMethodMatcher>();
|
||||||
|
var bodyMatcher = request.GetRequestMessageMatcher<RequestMessageBodyMatcher>();
|
||||||
|
|
||||||
var mappingModel = new MappingModel
|
var mappingModel = new MappingModel
|
||||||
{
|
{
|
||||||
Guid = mapping.Guid,
|
Guid = mapping.Guid,
|
||||||
Title = mapping.Title,
|
Title = mapping.Title,
|
||||||
Priority = mapping.Priority != 0 ? mapping.Priority : (int?) null,
|
Priority = mapping.Priority != 0 ? mapping.Priority : (int?)null,
|
||||||
Scenario = mapping.Scenario,
|
Scenario = mapping.Scenario,
|
||||||
WhenStateIs = mapping.ExecutionConditionState,
|
WhenStateIs = mapping.ExecutionConditionState,
|
||||||
SetStateTo = mapping.NextState,
|
SetStateTo = mapping.NextState,
|
||||||
@@ -66,14 +66,9 @@ namespace WireMock.Serialization
|
|||||||
Params = paramsMatchers != null && paramsMatchers.Any() ? paramsMatchers.Select(pm => new ParamModel
|
Params = paramsMatchers != null && paramsMatchers.Any() ? paramsMatchers.Select(pm => new ParamModel
|
||||||
{
|
{
|
||||||
Name = pm.Key,
|
Name = pm.Key,
|
||||||
IgnoreCase = pm.IgnoreCase == true ? true : (bool?) null,
|
IgnoreCase = pm.IgnoreCase == true ? true : (bool?)null,
|
||||||
Matchers = MatcherMapper.Map(pm.Matchers)
|
Matchers = MatcherMapper.Map(pm.Matchers)
|
||||||
}).ToList() : null,
|
}).ToList() : null
|
||||||
|
|
||||||
Body = methodMatcher?.Methods != null && methodMatcher.Methods.Any(m => m == "get") ? null : new BodyModel
|
|
||||||
{
|
|
||||||
Matcher = bodyMatcher != null ? MatcherMapper.Map(bodyMatcher.Matcher) : null
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
Response = new ResponseModel
|
Response = new ResponseModel
|
||||||
{
|
{
|
||||||
@@ -81,6 +76,20 @@ namespace WireMock.Serialization
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (methodMatcher?.Methods != null && methodMatcher.Methods.All(m => m != "get") && bodyMatcher?.Matchers != null)
|
||||||
|
{
|
||||||
|
mappingModel.Request.Body = new BodyModel();
|
||||||
|
|
||||||
|
if (bodyMatcher.Matchers.Length == 1)
|
||||||
|
{
|
||||||
|
mappingModel.Request.Body.Matcher = MatcherMapper.Map(bodyMatcher.Matchers[0]);
|
||||||
|
}
|
||||||
|
else if (bodyMatcher.Matchers.Length > 1)
|
||||||
|
{
|
||||||
|
mappingModel.Request.Body.Matchers = MatcherMapper.Map(bodyMatcher.Matchers);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(response.ProxyUrl))
|
if (!string.IsNullOrEmpty(response.ProxyUrl))
|
||||||
{
|
{
|
||||||
mappingModel.Response.StatusCode = null;
|
mappingModel.Response.StatusCode = null;
|
||||||
|
|||||||
@@ -10,6 +10,11 @@ namespace WireMock.Serialization
|
|||||||
{
|
{
|
||||||
internal static class MatcherMapper
|
internal static class MatcherMapper
|
||||||
{
|
{
|
||||||
|
public static IMatcher[] Map([CanBeNull] IEnumerable<MatcherModel> matchers)
|
||||||
|
{
|
||||||
|
return matchers?.Select(Map).Where(m => m != null).ToArray();
|
||||||
|
}
|
||||||
|
|
||||||
public static IMatcher Map([CanBeNull] MatcherModel matcher)
|
public static IMatcher Map([CanBeNull] MatcherModel matcher)
|
||||||
{
|
{
|
||||||
if (matcher == null)
|
if (matcher == null)
|
||||||
@@ -70,7 +75,7 @@ namespace WireMock.Serialization
|
|||||||
|
|
||||||
public static MatcherModel[] Map([CanBeNull] IEnumerable<IMatcher> matchers)
|
public static MatcherModel[] Map([CanBeNull] IEnumerable<IMatcher> matchers)
|
||||||
{
|
{
|
||||||
return matchers?.Select(Map).Where(x => x != null).ToArray();
|
return matchers?.Select(Map).Where(m => m != null).ToArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static MatcherModel Map([CanBeNull] IMatcher matcher)
|
public static MatcherModel Map([CanBeNull] IMatcher matcher)
|
||||||
|
|||||||
@@ -716,8 +716,11 @@ namespace WireMock.Server
|
|||||||
|
|
||||||
if (requestModel.Body?.Matcher != null)
|
if (requestModel.Body?.Matcher != null)
|
||||||
{
|
{
|
||||||
var bodyMatcher = MatcherMapper.Map(requestModel.Body.Matcher);
|
requestBuilder = requestBuilder.WithBody(MatcherMapper.Map(requestModel.Body.Matcher));
|
||||||
requestBuilder = requestBuilder.WithBody(bodyMatcher);
|
}
|
||||||
|
else if (requestModel.Body?.Matchers != null)
|
||||||
|
{
|
||||||
|
requestBuilder = requestBuilder.WithBody(MatcherMapper.Map(requestModel.Body.Matchers));
|
||||||
}
|
}
|
||||||
|
|
||||||
return requestBuilder;
|
return requestBuilder;
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using System.Collections.Generic;
|
using FluentAssertions;
|
||||||
using NFluent;
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
using WireMock.Matchers;
|
using WireMock.Matchers;
|
||||||
using WireMock.Matchers.Request;
|
using WireMock.Matchers.Request;
|
||||||
using WireMock.RequestBuilders;
|
using WireMock.RequestBuilders;
|
||||||
@@ -20,8 +21,24 @@ namespace WireMock.Net.Tests.RequestBuilders
|
|||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
var matchers = requestBuilder.GetPrivateFieldValue<IList<IRequestMatcher>>("_requestMatchers");
|
var matchers = requestBuilder.GetPrivateFieldValue<IList<IRequestMatcher>>("_requestMatchers");
|
||||||
Check.That(matchers.Count).IsEqualTo(1);
|
matchers.Should().HaveCount(1);
|
||||||
Check.That(((RequestMessageBodyMatcher) matchers[0]).Matcher).IsEqualTo(matcher);
|
((RequestMessageBodyMatcher)matchers[0]).Matchers.Should().Contain(matcher);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void RequestBuilder_WithBody_IMatchers()
|
||||||
|
{
|
||||||
|
// Assign
|
||||||
|
var matcher1 = new WildcardMatcher("x");
|
||||||
|
var matcher2 = new WildcardMatcher("y");
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var requestBuilder = (Request)Request.Create().WithBody(new[] { matcher1, matcher2 }.Cast<IMatcher>().ToArray());
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
var matchers = requestBuilder.GetPrivateFieldValue<IList<IRequestMatcher>>("_requestMatchers");
|
||||||
|
matchers.Should().HaveCount(1);
|
||||||
|
((RequestMessageBodyMatcher)matchers[0]).Matchers.Should().Contain(new[] { matcher1, matcher2 });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
using Moq;
|
using System.Linq;
|
||||||
|
using Moq;
|
||||||
using NFluent;
|
using NFluent;
|
||||||
using WireMock.Matchers;
|
using WireMock.Matchers;
|
||||||
using WireMock.Matchers.Request;
|
using WireMock.Matchers.Request;
|
||||||
@@ -38,6 +39,39 @@ namespace WireMock.Net.Tests.RequestMatchers
|
|||||||
stringMatcherMock.Verify(m => m.IsMatch("b"), Times.Once);
|
stringMatcherMock.Verify(m => m.IsMatch("b"), Times.Once);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void RequestMessageBodyMatcher_GetMatchingScore_BodyAsString_IStringMatchers()
|
||||||
|
{
|
||||||
|
// Assign
|
||||||
|
var body = new BodyData
|
||||||
|
{
|
||||||
|
BodyAsString = "b",
|
||||||
|
DetectedBodyType = BodyType.String
|
||||||
|
};
|
||||||
|
var stringMatcherMock1 = new Mock<IStringMatcher>();
|
||||||
|
stringMatcherMock1.Setup(m => m.IsMatch(It.IsAny<string>())).Returns(0.2d);
|
||||||
|
var stringMatcherMock2 = new Mock<IStringMatcher>();
|
||||||
|
stringMatcherMock2.Setup(m => m.IsMatch(It.IsAny<string>())).Returns(0.8d);
|
||||||
|
var matchers = new[] { stringMatcherMock1.Object, stringMatcherMock2.Object };
|
||||||
|
|
||||||
|
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", body);
|
||||||
|
|
||||||
|
var matcher = new RequestMessageBodyMatcher(matchers.Cast<IMatcher>().ToArray());
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = new RequestMatchResult();
|
||||||
|
double score = matcher.GetMatchingScore(requestMessage, result);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Check.That(score).IsEqualTo(0.8d);
|
||||||
|
|
||||||
|
// Verify
|
||||||
|
stringMatcherMock1.Verify(m => m.GetPatterns(), Times.Never);
|
||||||
|
stringMatcherMock1.Verify(m => m.IsMatch("b"), Times.Once);
|
||||||
|
stringMatcherMock2.Verify(m => m.GetPatterns(), Times.Never);
|
||||||
|
stringMatcherMock2.Verify(m => m.IsMatch("b"), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void RequestMessageBodyMatcher_GetMatchingScore_BodyAsBytes_IStringMatcher()
|
public void RequestMessageBodyMatcher_GetMatchingScore_BodyAsBytes_IStringMatcher()
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user