More fixes for #106

This commit is contained in:
Stef Heyenrath
2018-03-11 12:18:27 +01:00
parent ff012be173
commit 83d71bb24e
5 changed files with 67 additions and 7 deletions

View File

@@ -27,6 +27,14 @@ namespace WireMock.Matchers.Request
/// </summary> /// </summary>
public IEnumerable<string> Values { get; } public IEnumerable<string> Values { get; }
/// <summary>
/// Initializes a new instance of the <see cref="RequestMessageParamMatcher"/> class.
/// </summary>
/// <param name="key">The key.</param>
public RequestMessageParamMatcher([NotNull] string key) : this(key, null)
{
}
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="RequestMessageParamMatcher"/> class. /// Initializes a new instance of the <see cref="RequestMessageParamMatcher"/> class.
/// </summary> /// </summary>
@@ -66,13 +74,19 @@ namespace WireMock.Matchers.Request
} }
var values = requestMessage.GetParameter(Key); var values = requestMessage.GetParameter(Key);
if (values == null && !Values.Any()) if (values == null)
{ {
// Key is present, but no values, just return match // Key is not present, just return Mismatch
return MatchScores.Mismatch;
}
if (values.Count == 0 && (Values == null || !Values.Any()))
{
// Key is present, but no values or null, just return Perfect
return MatchScores.Perfect; return MatchScores.Perfect;
} }
var matches = Values.Select(v => values != null && values.Contains(v)); var matches = Values.Select(v => values.Contains(v));
return MatchScores.ToScore(matches); return MatchScores.ToScore(matches);
} }
} }

View File

@@ -6,12 +6,19 @@ using WireMock.Util;
namespace WireMock.RequestBuilders namespace WireMock.RequestBuilders
{ {
/// <summary> /// <summary>
/// The ParametersRequestBuilder interface. /// The ParamsRequestBuilder interface.
/// </summary> /// </summary>
public interface IParamsRequestBuilder public interface IParamsRequestBuilder
{ {
/// <summary> /// <summary>
/// The with parameters. /// WithParam (key only)
/// </summary>
/// <param name="key">The key.</param>
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
IRequestBuilder WithParam([NotNull] string key);
/// <summary>
/// WithParam (values)
/// </summary> /// </summary>
/// <param name="key">The key.</param> /// <param name="key">The key.</param>
/// <param name="values">The values.</param> /// <param name="values">The values.</param>
@@ -19,7 +26,7 @@ namespace WireMock.RequestBuilders
IRequestBuilder WithParam([NotNull] string key, [CanBeNull] params string[] values); IRequestBuilder WithParam([NotNull] string key, [CanBeNull] params string[] values);
/// <summary> /// <summary>
/// The with parameters. /// WithParam (funcs)
/// </summary> /// </summary>
/// <param name="funcs">The funcs.</param> /// <param name="funcs">The funcs.</param>
/// <returns>The <see cref="IRequestBuilder"/>.</returns> /// <returns>The <see cref="IRequestBuilder"/>.</returns>

View File

@@ -291,6 +291,15 @@ namespace WireMock.RequestBuilders
return this; return this;
} }
/// <inheritdoc cref="IParamsRequestBuilder.WithParam(string)"/>
public IRequestBuilder WithParam(string key)
{
Check.NotNull(key, nameof(key));
_requestMatchers.Add(new RequestMessageParamMatcher(key));
return this;
}
/// <inheritdoc cref="IParamsRequestBuilder.WithParam(string, string[])"/> /// <inheritdoc cref="IParamsRequestBuilder.WithParam(string, string[])"/>
public IRequestBuilder WithParam(string key, params string[] values) public IRequestBuilder WithParam(string key, params string[] values)
{ {

View File

@@ -206,7 +206,7 @@ namespace WireMock
/// </summary> /// </summary>
/// <param name="key">The key.</param> /// <param name="key">The key.</param>
/// <returns>The query parameter.</returns> /// <returns>The query parameter.</returns>
public List<string> GetParameter(string key) public WireMockList<string> GetParameter(string key)
{ {
if (Query == null) if (Query == null)
{ {

View File

@@ -51,5 +51,35 @@ namespace WireMock.Net.Tests.RequestMatchers
// Assert // Assert
Check.That(score).IsEqualTo(0.0d); Check.That(score).IsEqualTo(0.0d);
} }
[Fact]
public void RequestMessageParamMatcher_GetMatchingScore_OnlyKeyPresent_WithNull()
{
// Assign
var requestMessage = new RequestMessage(new Uri("http://localhost?key"), "GET", "127.0.0.1");
var matcher = new RequestMessageParamMatcher("key");
// Act
var result = new RequestMatchResult();
double score = matcher.GetMatchingScore(requestMessage, result);
// Assert
Check.That(score).IsEqualTo(1.0d);
}
[Fact]
public void RequestMessageParamMatcher_GetMatchingScore_OnlyKeyPresent_WithEmptyArray()
{
// Assign
var requestMessage = new RequestMessage(new Uri("http://localhost?key"), "GET", "127.0.0.1");
var matcher = new RequestMessageParamMatcher("key", new string[] { });
// Act
var result = new RequestMatchResult();
double score = matcher.GetMatchingScore(requestMessage, result);
// Assert
Check.That(score).IsEqualTo(1.0d);
}
} }
} }