Added some more Checks

This commit is contained in:
Stef Heyenrath
2017-02-07 17:13:56 +01:00
parent ee77a5edac
commit a9a46057be
7 changed files with 59 additions and 19 deletions

View File

@@ -86,17 +86,19 @@ namespace WireMock.Matchers.Request
private double IsMatch(RequestMessage requestMessage) private double IsMatch(RequestMessage requestMessage)
{ {
if (Funcs != null)
return MatchScores.ToScore(requestMessage.Cookies != null && Funcs.Any(cf => cf(requestMessage.Cookies)));
if (requestMessage.Cookies == null) if (requestMessage.Cookies == null)
return MatchScores.Mismatch; return MatchScores.Mismatch;
if (Funcs != null)
return MatchScores.ToScore(Funcs.Any(f => f(requestMessage.Cookies)));
if (Matchers == null)
return MatchScores.Mismatch;
if (!requestMessage.Cookies.ContainsKey(Name)) if (!requestMessage.Cookies.ContainsKey(Name))
return MatchScores.Mismatch; return MatchScores.Mismatch;
string value = requestMessage.Cookies[Name]; string value = requestMessage.Cookies[Name];
return Matchers.Max(m => m.IsMatch(value)); return Matchers.Max(m => m.IsMatch(value));
} }
} }

View File

@@ -86,18 +86,20 @@ namespace WireMock.Matchers.Request
private double IsMatch(RequestMessage requestMessage) private double IsMatch(RequestMessage requestMessage)
{ {
if (Funcs != null)
return MatchScores.ToScore(requestMessage.Headers != null && Funcs.Any(hf => hf(requestMessage.Headers)));
if (requestMessage.Headers == null) if (requestMessage.Headers == null)
return MatchScores.Mismatch; return MatchScores.Mismatch;
if (Funcs != null)
return MatchScores.ToScore(Funcs.Any(f => f(requestMessage.Headers)));
if (Matchers == null)
return MatchScores.Mismatch;
if (!requestMessage.Headers.ContainsKey(Name)) if (!requestMessage.Headers.ContainsKey(Name))
return MatchScores.Mismatch; return MatchScores.Mismatch;
string headerValue = requestMessage.Headers[Name]; string value = requestMessage.Headers[Name];
return Matchers.Max(m => m.IsMatch(value));
return Matchers.Max(m => m.IsMatch(headerValue));
} }
} }
} }

View File

@@ -36,10 +36,9 @@ namespace WireMock.Matchers.Request
/// <param name="values"> /// <param name="values">
/// The values. /// The values.
/// </param> /// </param>
public RequestMessageParamMatcher([NotNull] string key, [NotNull] IEnumerable<string> values) public RequestMessageParamMatcher([NotNull] string key, [CanBeNull] IEnumerable<string> values)
{ {
Check.NotNull(key, nameof(key)); Check.NotNull(key, nameof(key));
Check.NotNull(values, nameof(values));
Key = key; Key = key;
Values = values; Values = values;

View File

@@ -35,13 +35,13 @@ namespace WireMock.RequestBuilders
/// </summary> /// </summary>
/// <param name="body">The body string function.</param> /// <param name="body">The body string function.</param>
/// <returns>The <see cref="IRequestBuilder"/>.</returns> /// <returns>The <see cref="IRequestBuilder"/>.</returns>
IRequestBuilder WithBody(Func<string, bool> body); IRequestBuilder WithBody([NotNull] Func<string, bool> body);
/// <summary> /// <summary>
/// The with body byte[] func. /// The with body byte[] func.
/// </summary> /// </summary>
/// <param name="body">The body byte[] function.</param> /// <param name="body">The body byte[] function.</param>
/// <returns>The <see cref="IRequestBuilder"/>.</returns> /// <returns>The <see cref="IRequestBuilder"/>.</returns>
IRequestBuilder WithBody(Func<byte[], bool> body); IRequestBuilder WithBody([NotNull] Func<byte[], bool> body);
} }
} }

View File

@@ -16,7 +16,7 @@ namespace WireMock.RequestBuilders
/// <param name="key">The key.</param> /// <param name="key">The key.</param>
/// <param name="values">The values.</param> /// <param name="values">The values.</param>
/// <returns>The <see cref="IRequestBuilder"/>.</returns> /// <returns>The <see cref="IRequestBuilder"/>.</returns>
IRequestBuilder WithParam([NotNull] string key, params string[] values); IRequestBuilder WithParam([NotNull] string key, [CanBeNull] params string[] values);
/// <summary> /// <summary>
/// The with parameters. /// The with parameters.

View File

@@ -5,6 +5,7 @@ using System.Linq;
using WireMock.Matchers; using WireMock.Matchers;
using WireMock.Matchers.Request; using WireMock.Matchers.Request;
using WireMock.Util; using WireMock.Util;
using WireMock.Validation;
namespace WireMock.RequestBuilders namespace WireMock.RequestBuilders
{ {
@@ -60,6 +61,8 @@ namespace WireMock.RequestBuilders
/// <returns>The <see cref="IRequestBuilder"/>.</returns> /// <returns>The <see cref="IRequestBuilder"/>.</returns>
public IRequestBuilder WithPath(params IMatcher[] matchers) public IRequestBuilder WithPath(params IMatcher[] matchers)
{ {
Check.NotEmpty(matchers, nameof(matchers));
_requestMatchers.Add(new RequestMessagePathMatcher(matchers)); _requestMatchers.Add(new RequestMessagePathMatcher(matchers));
return this; return this;
} }
@@ -71,6 +74,8 @@ namespace WireMock.RequestBuilders
/// <returns>The <see cref="IRequestBuilder"/>.</returns> /// <returns>The <see cref="IRequestBuilder"/>.</returns>
public IRequestBuilder WithPath(params string[] paths) public IRequestBuilder WithPath(params string[] paths)
{ {
Check.NotEmpty(paths, nameof(paths));
_requestMatchers.Add(new RequestMessagePathMatcher(paths)); _requestMatchers.Add(new RequestMessagePathMatcher(paths));
return this; return this;
} }
@@ -82,6 +87,8 @@ namespace WireMock.RequestBuilders
/// <returns>The <see cref="IRequestBuilder"/>.</returns> /// <returns>The <see cref="IRequestBuilder"/>.</returns>
public IRequestBuilder WithPath(params Func<string, bool>[] funcs) public IRequestBuilder WithPath(params Func<string, bool>[] funcs)
{ {
Check.NotEmpty(funcs, nameof(funcs));
_requestMatchers.Add(new RequestMessagePathMatcher(funcs)); _requestMatchers.Add(new RequestMessagePathMatcher(funcs));
return this; return this;
} }
@@ -89,11 +96,13 @@ namespace WireMock.RequestBuilders
/// <summary> /// <summary>
/// The with url. /// The with url.
/// </summary> /// </summary>
/// <param name="matcher">The matcher.</param> /// <param name="matchers">The matchers.</param>
/// <returns>The <see cref="IRequestBuilder"/>.</returns> /// <returns>The <see cref="IRequestBuilder"/>.</returns>
public IRequestBuilder WithUrl(params IMatcher[] matcher) public IRequestBuilder WithUrl(params IMatcher[] matchers)
{ {
_requestMatchers.Add(new RequestMessageUrlMatcher(matcher)); Check.NotEmpty(matchers, nameof(matchers));
_requestMatchers.Add(new RequestMessageUrlMatcher(matchers));
return this; return this;
} }
@@ -104,6 +113,8 @@ namespace WireMock.RequestBuilders
/// <returns>The <see cref="IRequestBuilder"/>.</returns> /// <returns>The <see cref="IRequestBuilder"/>.</returns>
public IRequestBuilder WithUrl(params string[] urls) public IRequestBuilder WithUrl(params string[] urls)
{ {
Check.NotEmpty(urls, nameof(urls));
_requestMatchers.Add(new RequestMessageUrlMatcher(urls)); _requestMatchers.Add(new RequestMessageUrlMatcher(urls));
return this; return this;
} }
@@ -115,6 +126,8 @@ namespace WireMock.RequestBuilders
/// <returns>The <see cref="IRequestBuilder"/>.</returns> /// <returns>The <see cref="IRequestBuilder"/>.</returns>
public IRequestBuilder WithUrl(params Func<string, bool>[] funcs) public IRequestBuilder WithUrl(params Func<string, bool>[] funcs)
{ {
Check.NotEmpty(funcs, nameof(funcs));
_requestMatchers.Add(new RequestMessageUrlMatcher(funcs)); _requestMatchers.Add(new RequestMessageUrlMatcher(funcs));
return this; return this;
} }
@@ -201,6 +214,8 @@ namespace WireMock.RequestBuilders
/// <returns>The <see cref="IRequestBuilder"/>.</returns> /// <returns>The <see cref="IRequestBuilder"/>.</returns>
public IRequestBuilder UsingVerb(params string[] verbs) public IRequestBuilder UsingVerb(params string[] verbs)
{ {
Check.NotEmpty(verbs, nameof(verbs));
_requestMatchers.Add(new RequestMessageMethodMatcher(verbs)); _requestMatchers.Add(new RequestMessageMethodMatcher(verbs));
return this; return this;
} }
@@ -240,6 +255,8 @@ namespace WireMock.RequestBuilders
/// <returns>The <see cref="IRequestBuilder"/>.</returns> /// <returns>The <see cref="IRequestBuilder"/>.</returns>
public IRequestBuilder WithBody(Func<string, bool> func) public IRequestBuilder WithBody(Func<string, bool> func)
{ {
Check.NotNull(func, nameof(func));
_requestMatchers.Add(new RequestMessageBodyMatcher(func)); _requestMatchers.Add(new RequestMessageBodyMatcher(func));
return this; return this;
} }
@@ -253,6 +270,8 @@ namespace WireMock.RequestBuilders
/// <returns>The <see cref="IRequestBuilder"/>.</returns> /// <returns>The <see cref="IRequestBuilder"/>.</returns>
public IRequestBuilder WithBody(Func<byte[], bool> func) public IRequestBuilder WithBody(Func<byte[], bool> func)
{ {
Check.NotNull(func, nameof(func));
_requestMatchers.Add(new RequestMessageBodyMatcher(func)); _requestMatchers.Add(new RequestMessageBodyMatcher(func));
return this; return this;
} }
@@ -264,6 +283,8 @@ namespace WireMock.RequestBuilders
/// <returns>The <see cref="IRequestBuilder" />.</returns> /// <returns>The <see cref="IRequestBuilder" />.</returns>
public IRequestBuilder WithBody(IMatcher matcher) public IRequestBuilder WithBody(IMatcher matcher)
{ {
Check.NotNull(matcher, nameof(matcher));
_requestMatchers.Add(new RequestMessageBodyMatcher(matcher)); _requestMatchers.Add(new RequestMessageBodyMatcher(matcher));
return this; return this;
} }
@@ -280,6 +301,8 @@ namespace WireMock.RequestBuilders
/// <returns>The <see cref="IRequestBuilder"/>.</returns> /// <returns>The <see cref="IRequestBuilder"/>.</returns>
public IRequestBuilder WithParam(string key, params string[] values) public IRequestBuilder WithParam(string key, params string[] values)
{ {
Check.NotNull(key, nameof(key));
_requestMatchers.Add(new RequestMessageParamMatcher(key, values)); _requestMatchers.Add(new RequestMessageParamMatcher(key, values));
return this; return this;
} }
@@ -291,6 +314,8 @@ namespace WireMock.RequestBuilders
/// <returns>The <see cref="IRequestBuilder"/>.</returns> /// <returns>The <see cref="IRequestBuilder"/>.</returns>
public IRequestBuilder WithParam(params Func<IDictionary<string, WireMockList<string>>, bool>[] funcs) public IRequestBuilder WithParam(params Func<IDictionary<string, WireMockList<string>>, bool>[] funcs)
{ {
Check.NotEmpty(funcs, nameof(funcs));
_requestMatchers.Add(new RequestMessageParamMatcher(funcs)); _requestMatchers.Add(new RequestMessageParamMatcher(funcs));
return this; return this;
} }
@@ -304,6 +329,9 @@ namespace WireMock.RequestBuilders
/// <returns>The <see cref="IRequestBuilder"/>.</returns> /// <returns>The <see cref="IRequestBuilder"/>.</returns>
public IRequestBuilder WithHeader(string name, string pattern, bool ignoreCase = true) public IRequestBuilder WithHeader(string name, string pattern, bool ignoreCase = true)
{ {
Check.NotNull(name, nameof(name));
Check.NotNull(pattern, nameof(pattern));
_requestMatchers.Add(new RequestMessageHeaderMatcher(name, pattern, ignoreCase)); _requestMatchers.Add(new RequestMessageHeaderMatcher(name, pattern, ignoreCase));
return this; return this;
} }
@@ -316,6 +344,9 @@ namespace WireMock.RequestBuilders
/// <returns>The <see cref="IRequestBuilder"/>.</returns> /// <returns>The <see cref="IRequestBuilder"/>.</returns>
public IRequestBuilder WithHeader(string name, params IMatcher[] matchers) public IRequestBuilder WithHeader(string name, params IMatcher[] matchers)
{ {
Check.NotNull(name, nameof(name));
Check.NotEmpty(matchers, nameof(matchers));
_requestMatchers.Add(new RequestMessageHeaderMatcher(name, matchers)); _requestMatchers.Add(new RequestMessageHeaderMatcher(name, matchers));
return this; return this;
} }
@@ -327,6 +358,8 @@ namespace WireMock.RequestBuilders
/// <returns>The <see cref="IRequestBuilder"/>.</returns> /// <returns>The <see cref="IRequestBuilder"/>.</returns>
public IRequestBuilder WithHeader(params Func<IDictionary<string, string>, bool>[] funcs) public IRequestBuilder WithHeader(params Func<IDictionary<string, string>, bool>[] funcs)
{ {
Check.NotEmpty(funcs, nameof(funcs));
_requestMatchers.Add(new RequestMessageHeaderMatcher(funcs)); _requestMatchers.Add(new RequestMessageHeaderMatcher(funcs));
return this; return this;
} }
@@ -352,6 +385,8 @@ namespace WireMock.RequestBuilders
/// <returns>The <see cref="IRequestBuilder"/>.</returns> /// <returns>The <see cref="IRequestBuilder"/>.</returns>
public IRequestBuilder WithCookie(string name, params IMatcher[] matchers) public IRequestBuilder WithCookie(string name, params IMatcher[] matchers)
{ {
Check.NotEmpty(matchers, nameof(matchers));
_requestMatchers.Add(new RequestMessageCookieMatcher(name, matchers)); _requestMatchers.Add(new RequestMessageCookieMatcher(name, matchers));
return this; return this;
} }
@@ -363,6 +398,8 @@ namespace WireMock.RequestBuilders
/// <returns>The <see cref="IRequestBuilder"/>.</returns> /// <returns>The <see cref="IRequestBuilder"/>.</returns>
public IRequestBuilder WithCookie(params Func<IDictionary<string, string>, bool>[] funcs) public IRequestBuilder WithCookie(params Func<IDictionary<string, string>, bool>[] funcs)
{ {
Check.NotEmpty(funcs, nameof(funcs));
_requestMatchers.Add(new RequestMessageCookieMatcher(funcs)); _requestMatchers.Add(new RequestMessageCookieMatcher(funcs));
return this; return this;
} }

View File

@@ -85,7 +85,7 @@ namespace WireMock.Server
.WithGuid(guid) .WithGuid(guid)
.RespondWith(responseBuilder); .RespondWith(responseBuilder);
return new ResponseMessage { Body = "Mapping updated" }; return new ResponseMessage { Body = "Mapping added or updated" };
} }
private ResponseMessage MappingDelete(RequestMessage requestMessage) private ResponseMessage MappingDelete(RequestMessage requestMessage)