mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-20 16:01:39 +02:00
Merge branch 'param'
This commit is contained in:
@@ -81,8 +81,29 @@ namespace WireMock.Net.ConsoleApplication
|
|||||||
.WithParam("$filter", "(substringof(Code, 'WA')")
|
.WithParam("$filter", "(substringof(Code, 'WA')")
|
||||||
.UsingGet())
|
.UsingGet())
|
||||||
.RespondWith(Response.Create()
|
.RespondWith(Response.Create()
|
||||||
|
.WithHeader("Content-Type", "application/json")
|
||||||
.WithBody(@"{ ""result"": ""odata""}"));
|
.WithBody(@"{ ""result"": ""odata""}"));
|
||||||
|
|
||||||
|
server
|
||||||
|
.Given(Request
|
||||||
|
.Create()
|
||||||
|
.WithPath(new WildcardMatcher("/param2", true))
|
||||||
|
.WithParam("key", "test")
|
||||||
|
.UsingGet())
|
||||||
|
.RespondWith(Response.Create()
|
||||||
|
.WithHeader("Content-Type", "application/json")
|
||||||
|
.WithBodyAsJson(new { result = "param2" }));
|
||||||
|
|
||||||
|
server
|
||||||
|
.Given(Request
|
||||||
|
.Create()
|
||||||
|
.WithPath(new WildcardMatcher("/param3", true))
|
||||||
|
.WithParam("key", new WildcardMatcher("t*"))
|
||||||
|
.UsingGet())
|
||||||
|
.RespondWith(Response.Create()
|
||||||
|
.WithHeader("Content-Type", "application/json")
|
||||||
|
.WithBodyAsJson(new { result = "param3" }));
|
||||||
|
|
||||||
server
|
server
|
||||||
.Given(Request.Create().WithPath("/headers", "/headers_test").UsingPost().WithHeader("Content-Type", "application/json*"))
|
.Given(Request.Create().WithPath("/headers", "/headers_test").UsingPost().WithHeader("Content-Type", "application/json*"))
|
||||||
.RespondWith(Response.Create()
|
.RespondWith(Response.Create()
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
using System.Collections.Generic;
|
// using System.Collections.Generic;
|
||||||
|
|
||||||
namespace WireMock.Admin.Mappings
|
namespace WireMock.Admin.Mappings
|
||||||
{
|
{
|
||||||
@@ -12,10 +12,15 @@ namespace WireMock.Admin.Mappings
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public string Name { get; set; }
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
///// <summary>
|
||||||
|
///// Gets or sets the values.
|
||||||
|
///// </summary>
|
||||||
|
//public IList<string> Values { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the values.
|
/// Gets or sets the matchers.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public IList<string> Values { get; set; }
|
public MatcherModel[] Matchers { get; set; }
|
||||||
|
|
||||||
///// <summary>
|
///// <summary>
|
||||||
///// Gets or sets the functions.
|
///// Gets or sets the functions.
|
||||||
|
|||||||
@@ -25,16 +25,16 @@ namespace WireMock.Matchers.Request
|
|||||||
public string Key { get; }
|
public string Key { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The values
|
/// The matchers.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public IEnumerable<string> Values { get; }
|
public IReadOnlyList<IStringMatcher> Matchers { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="RequestMessageParamMatcher"/> class.
|
/// Initializes a new instance of the <see cref="RequestMessageParamMatcher"/> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="matchBehaviour">The match behaviour.</param>
|
/// <param name="matchBehaviour">The match behaviour.</param>
|
||||||
/// <param name="key">The key.</param>
|
/// <param name="key">The key.</param>
|
||||||
public RequestMessageParamMatcher(MatchBehaviour matchBehaviour, [NotNull] string key) : this(matchBehaviour, key, null)
|
public RequestMessageParamMatcher(MatchBehaviour matchBehaviour, [NotNull] string key) : this(matchBehaviour, key, (IStringMatcher[])null)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -44,13 +44,23 @@ namespace WireMock.Matchers.Request
|
|||||||
/// <param name="matchBehaviour">The match behaviour.</param>
|
/// <param name="matchBehaviour">The match behaviour.</param>
|
||||||
/// <param name="key">The key.</param>
|
/// <param name="key">The key.</param>
|
||||||
/// <param name="values">The values.</param>
|
/// <param name="values">The values.</param>
|
||||||
public RequestMessageParamMatcher(MatchBehaviour matchBehaviour, [NotNull] string key, [CanBeNull] IEnumerable<string> values)
|
public RequestMessageParamMatcher(MatchBehaviour matchBehaviour, [NotNull] string key, [CanBeNull] string[] values) : this(matchBehaviour, key, values?.Select(value => new ExactMatcher(matchBehaviour, value)).Cast<IStringMatcher>().ToArray())
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="RequestMessageParamMatcher"/> class.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="matchBehaviour">The match behaviour.</param>
|
||||||
|
/// <param name="key">The key.</param>
|
||||||
|
/// <param name="matchers">The matchers.</param>
|
||||||
|
public RequestMessageParamMatcher(MatchBehaviour matchBehaviour, [NotNull] string key, [CanBeNull] IStringMatcher[] matchers)
|
||||||
{
|
{
|
||||||
Check.NotNull(key, nameof(key));
|
Check.NotNull(key, nameof(key));
|
||||||
|
|
||||||
_matchBehaviour = matchBehaviour;
|
_matchBehaviour = matchBehaviour;
|
||||||
Key = key;
|
Key = key;
|
||||||
Values = values;
|
Matchers = matchers;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -78,21 +88,33 @@ namespace WireMock.Matchers.Request
|
|||||||
return MatchScores.ToScore(requestMessage.Query != null && Funcs.Any(f => f(requestMessage.Query)));
|
return MatchScores.ToScore(requestMessage.Query != null && Funcs.Any(f => f(requestMessage.Query)));
|
||||||
}
|
}
|
||||||
|
|
||||||
var values = requestMessage.GetParameter(Key);
|
WireMockList<string> valuesPresentInRequestMessage = requestMessage.GetParameter(Key);
|
||||||
if (values == null)
|
if (valuesPresentInRequestMessage == null)
|
||||||
{
|
{
|
||||||
// Key is not present, just return Mismatch
|
// Key is not present at all, just return Mismatch
|
||||||
return MatchScores.Mismatch;
|
return MatchScores.Mismatch;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (values.Count == 0 && (Values == null || !Values.Any()))
|
if (Matchers != null && Matchers.Any())
|
||||||
{
|
{
|
||||||
// Key is present, but no values or null, just return Perfect
|
// Matchers are defined, just use the matchers to calculate the match score.
|
||||||
|
var scores = new List<double>();
|
||||||
|
foreach (string valuePresentInRequestMessage in valuesPresentInRequestMessage)
|
||||||
|
{
|
||||||
|
double score = Matchers.Max(m => m.IsMatch(valuePresentInRequestMessage));
|
||||||
|
scores.Add(score);
|
||||||
|
}
|
||||||
|
|
||||||
|
return scores.Any() ? scores.Average() : MatchScores.Mismatch;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Matchers == null || !Matchers.Any())
|
||||||
|
{
|
||||||
|
// Matchers are null or not defined, and Key is present, just return Perfect.
|
||||||
return MatchScores.Perfect;
|
return MatchScores.Perfect;
|
||||||
}
|
}
|
||||||
|
|
||||||
var matches = Values.Select(v => values.Contains(v));
|
return MatchScores.Mismatch;
|
||||||
return MatchScores.ToScore(matches);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -27,6 +27,14 @@ namespace WireMock.RequestBuilders
|
|||||||
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
|
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
|
||||||
IRequestBuilder WithParam([NotNull] string key, [CanBeNull] params string[] values);
|
IRequestBuilder WithParam([NotNull] string key, [CanBeNull] params string[] values);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// WithParam: matching on key and matchers.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="key">The key.</param>
|
||||||
|
/// <param name="matchers">The matchers.</param>
|
||||||
|
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
|
||||||
|
IRequestBuilder WithParam([NotNull] string key, [CanBeNull] params IStringMatcher[] matchers);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// WithParam: matching on key, values and matchBehaviour.
|
/// WithParam: matching on key, values and matchBehaviour.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -36,6 +44,15 @@ namespace WireMock.RequestBuilders
|
|||||||
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
|
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
|
||||||
IRequestBuilder WithParam([NotNull] string key, MatchBehaviour matchBehaviour, [CanBeNull] params string[] values);
|
IRequestBuilder WithParam([NotNull] string key, MatchBehaviour matchBehaviour, [CanBeNull] params string[] values);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// WithParam: matching on key, matchers and matchBehaviour.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="key">The key.</param>
|
||||||
|
/// <param name="matchers">The matchers.</param>
|
||||||
|
/// <param name="matchBehaviour">The match behaviour.</param>
|
||||||
|
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
|
||||||
|
IRequestBuilder WithParam([NotNull] string key, MatchBehaviour matchBehaviour, [CanBeNull] params IStringMatcher[] matchers);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// WithParam: matching on functions.
|
/// WithParam: matching on functions.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -306,6 +306,12 @@ namespace WireMock.RequestBuilders
|
|||||||
return WithParam(key, MatchBehaviour.AcceptOnMatch, values);
|
return WithParam(key, MatchBehaviour.AcceptOnMatch, values);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc cref="IParamsRequestBuilder.WithParam(string, IStringMatcher[])"/>
|
||||||
|
public IRequestBuilder WithParam(string key, params IStringMatcher[] matchers)
|
||||||
|
{
|
||||||
|
return WithParam(key, MatchBehaviour.AcceptOnMatch, matchers);
|
||||||
|
}
|
||||||
|
|
||||||
/// <inheritdoc cref="IParamsRequestBuilder.WithParam(string, MatchBehaviour, string[])"/>
|
/// <inheritdoc cref="IParamsRequestBuilder.WithParam(string, MatchBehaviour, string[])"/>
|
||||||
public IRequestBuilder WithParam(string key, MatchBehaviour matchBehaviour, params string[] values)
|
public IRequestBuilder WithParam(string key, MatchBehaviour matchBehaviour, params string[] values)
|
||||||
{
|
{
|
||||||
@@ -315,6 +321,15 @@ namespace WireMock.RequestBuilders
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc cref="IParamsRequestBuilder.WithParam(string, MatchBehaviour, IStringMatcher[])"/>
|
||||||
|
public IRequestBuilder WithParam(string key, MatchBehaviour matchBehaviour, params IStringMatcher[] matchers)
|
||||||
|
{
|
||||||
|
Check.NotNull(key, nameof(key));
|
||||||
|
|
||||||
|
_requestMatchers.Add(new RequestMessageParamMatcher(matchBehaviour, key, matchers));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
/// <inheritdoc cref="IParamsRequestBuilder.WithParam(Func{IDictionary{string, WireMockList{string}}, bool}[])"/>
|
/// <inheritdoc cref="IParamsRequestBuilder.WithParam(Func{IDictionary{string, WireMockList{string}}, bool}[])"/>
|
||||||
public IRequestBuilder WithParam(params Func<IDictionary<string, WireMockList<string>>, bool>[] funcs)
|
public IRequestBuilder WithParam(params Func<IDictionary<string, WireMockList<string>>, bool>[] funcs)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -71,7 +71,7 @@ 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,
|
||||||
Values = pm.Values?.ToList()
|
Matchers = MatcherMapper.Map(pm.Matchers)
|
||||||
//Funcs = Map(pm.Funcs)
|
//Funcs = Map(pm.Funcs)
|
||||||
}).ToList() : null,
|
}).ToList() : null,
|
||||||
|
|
||||||
|
|||||||
@@ -618,9 +618,9 @@ namespace WireMock.Server
|
|||||||
|
|
||||||
if (requestModel.Params != null)
|
if (requestModel.Params != null)
|
||||||
{
|
{
|
||||||
foreach (var paramModel in requestModel.Params)
|
foreach (var paramModel in requestModel.Params.Where(c => c.Matchers != null))
|
||||||
{
|
{
|
||||||
requestBuilder = paramModel.Values == null ? requestBuilder.WithParam(paramModel.Name) : requestBuilder.WithParam(paramModel.Name, paramModel.Values.ToArray());
|
requestBuilder = requestBuilder.WithParam(paramModel.Name, paramModel.Matchers.Select(MatcherMapper.Map).Cast<IStringMatcher>().ToArray());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ namespace WireMock.Net.Tests.RequestMatchers
|
|||||||
public class RequestMessageParamMatcherTests
|
public class RequestMessageParamMatcherTests
|
||||||
{
|
{
|
||||||
[Fact]
|
[Fact]
|
||||||
public void RequestMessageParamMatcher_GetMatchingScore_AllMatch()
|
public void RequestMessageParamMatcher_GetMatchingScore_KeyWithValuesPresentInUrl_MatchExactOnStringValues()
|
||||||
{
|
{
|
||||||
// Assign
|
// Assign
|
||||||
var requestMessage = new RequestMessage(new Uri("http://localhost?key=test1,test2"), "GET", "127.0.0.1");
|
var requestMessage = new RequestMessage(new Uri("http://localhost?key=test1,test2"), "GET", "127.0.0.1");
|
||||||
@@ -24,7 +24,22 @@ namespace WireMock.Net.Tests.RequestMatchers
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void RequestMessageParamMatcher_GetMatchingScore_PartialMatch()
|
public void RequestMessageParamMatcher_GetMatchingScore_KeyWithValuesPresentInUrl_MatchExactOnExactMatchers()
|
||||||
|
{
|
||||||
|
// Assign
|
||||||
|
var requestMessage = new RequestMessage(new Uri("http://localhost?key=test1,test2"), "GET", "127.0.0.1");
|
||||||
|
var matcher = new RequestMessageParamMatcher(MatchBehaviour.AcceptOnMatch, "key", new IStringMatcher[] { new ExactMatcher("test1"), new ExactMatcher("test2") });
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = new RequestMatchResult();
|
||||||
|
double score = matcher.GetMatchingScore(requestMessage, result);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Check.That(score).IsEqualTo(1.0d);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void RequestMessageParamMatcher_GetMatchingScore_KeyWithValuesPresentInUrl_MatchOnKeyWithValues_PartialMatch()
|
||||||
{
|
{
|
||||||
// Assign
|
// Assign
|
||||||
var requestMessage = new RequestMessage(new Uri("http://localhost?key=test0,test2"), "GET", "127.0.0.1");
|
var requestMessage = new RequestMessage(new Uri("http://localhost?key=test0,test2"), "GET", "127.0.0.1");
|
||||||
@@ -39,7 +54,7 @@ namespace WireMock.Net.Tests.RequestMatchers
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void RequestMessageParamMatcher_GetMatchingScore_OnlyKeyPresent()
|
public void RequestMessageParamMatcher_GetMatchingScore_OnlyKeyPresentInUrl_MatchOnKeyWithValues_Fails()
|
||||||
{
|
{
|
||||||
// Assign
|
// Assign
|
||||||
var requestMessage = new RequestMessage(new Uri("http://localhost?key"), "GET", "127.0.0.1");
|
var requestMessage = new RequestMessage(new Uri("http://localhost?key"), "GET", "127.0.0.1");
|
||||||
@@ -54,7 +69,7 @@ namespace WireMock.Net.Tests.RequestMatchers
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void RequestMessageParamMatcher_GetMatchingScore_OnlyKeyPresent_WithNull()
|
public void RequestMessageParamMatcher_GetMatchingScore_OnlyKeyPresentInUrl_MatchOnKey()
|
||||||
{
|
{
|
||||||
// Assign
|
// Assign
|
||||||
var requestMessage = new RequestMessage(new Uri("http://localhost?key"), "GET", "127.0.0.1");
|
var requestMessage = new RequestMessage(new Uri("http://localhost?key"), "GET", "127.0.0.1");
|
||||||
@@ -69,7 +84,7 @@ namespace WireMock.Net.Tests.RequestMatchers
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void RequestMessageParamMatcher_GetMatchingScore_OnlyKeyPresent_WithEmptyArray()
|
public void RequestMessageParamMatcher_GetMatchingScore_OnlyKeyPresentInUrl_MatchOnKeyWithEmptyArray()
|
||||||
{
|
{
|
||||||
// Assign
|
// Assign
|
||||||
var requestMessage = new RequestMessage(new Uri("http://localhost?key"), "GET", "127.0.0.1");
|
var requestMessage = new RequestMessage(new Uri("http://localhost?key"), "GET", "127.0.0.1");
|
||||||
@@ -82,5 +97,20 @@ namespace WireMock.Net.Tests.RequestMatchers
|
|||||||
// Assert
|
// Assert
|
||||||
Check.That(score).IsEqualTo(1.0d);
|
Check.That(score).IsEqualTo(1.0d);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void RequestMessageParamMatcher_GetMatchingScore_KeyWithValuePresentInUrl_MatchOnKey()
|
||||||
|
{
|
||||||
|
// Assign
|
||||||
|
var requestMessage = new RequestMessage(new Uri("http://localhost?key=frank@contoso.com"), "GET", "127.0.0.1");
|
||||||
|
var matcher = new RequestMessageParamMatcher(MatchBehaviour.AcceptOnMatch, "key");
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = new RequestMatchResult();
|
||||||
|
double score = matcher.GetMatchingScore(requestMessage, result);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Check.That(score).IsEqualTo(1.0d);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user