Skip to content

Commit

Permalink
More fixes for #106
Browse files Browse the repository at this point in the history
  • Loading branch information
StefH committed Mar 11, 2018
1 parent ff012be commit 83d71bb
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 7 deletions.
20 changes: 17 additions & 3 deletions src/WireMock.Net/Matchers/Request/RequestMessageParamMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ public class RequestMessageParamMatcher : IRequestMatcher
/// </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>
Expand Down Expand Up @@ -66,13 +74,19 @@ private double IsMatch(RequestMessage requestMessage)
}

var values = requestMessage.GetParameter(Key);
if (values == null && !Values.Any())
if (values == null)
{
// Key is not present, just return Mismatch
return MatchScores.Mismatch;
}

if (values.Count == 0 && (Values == null || !Values.Any()))
{
// Key is present, but no values, just return match
// 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);
}
}
Expand Down
13 changes: 10 additions & 3 deletions src/WireMock.Net/RequestBuilders/IParamsRequestBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,27 @@
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>
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
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>
Expand Down
9 changes: 9 additions & 0 deletions src/WireMock.Net/RequestBuilders/Request.cs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,15 @@ public IRequestBuilder WithBody(Func<object, bool> func)
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)
{
Expand Down
2 changes: 1 addition & 1 deletion src/WireMock.Net/RequestMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public RequestMessage([NotNull] Uri url, [NotNull] string method, [NotNull] stri
/// </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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,35 @@ public void RequestMessageParamMatcher_GetMatchingScore_OnlyKeyPresent()
// 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);
}
}
}

0 comments on commit 83d71bb

Please sign in to comment.