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>
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>
/// Initializes a new instance of the <see cref="RequestMessageParamMatcher"/> class.
/// </summary>
@@ -66,13 +74,19 @@ namespace WireMock.Matchers.Request
}
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;
}
var matches = Values.Select(v => values != null && values.Contains(v));
var matches = Values.Select(v => values.Contains(v));
return MatchScores.ToScore(matches);
}
}

View File

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

View File

@@ -291,6 +291,15 @@ namespace WireMock.RequestBuilders
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[])"/>
public IRequestBuilder WithParam(string key, params string[] values)
{

View File

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

View File

@@ -51,5 +51,35 @@ namespace WireMock.Net.Tests.RequestMatchers
// Assert
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);
}
}
}