This commit is contained in:
Stef Heyenrath
2017-01-23 19:43:30 +01:00
parent 363d96e615
commit 45aa83ee91
11 changed files with 208 additions and 92 deletions

View File

@@ -0,0 +1,65 @@
using System;
using System.Collections.Generic;
using JetBrains.Annotations;
using WireMock.Validation;
namespace WireMock.Matchers.Request
{
/// <summary>
/// The request cookie matcher.
/// </summary>
public class RequestMessageCookieMatcher : IRequestMatcher
{
private readonly string _name;
private readonly IMatcher _matcher;
private readonly Func<IDictionary<string, string>, bool> _cookieFunc;
/// <summary>
/// Initializes a new instance of the <see cref="RequestMessageCookieMatcher"/> class.
/// </summary>
/// <param name="name">The name.</param>
/// <param name="pattern">The pattern.</param>
/// <param name="ignoreCase">The ignoreCase.</param>
public RequestMessageCookieMatcher([NotNull] string name, [NotNull] string pattern, bool ignoreCase = true)
{
Check.NotNull(name, nameof(name));
Check.NotNull(pattern, nameof(pattern));
_name = name;
_matcher = new WildcardMatcher(pattern, ignoreCase);
}
/// <summary>
/// Initializes a new instance of the <see cref="RequestMessageCookieMatcher"/> class.
/// </summary>
/// <param name="func">
/// The func.
/// </param>
public RequestMessageCookieMatcher([NotNull] Func<IDictionary<string, string>, bool> func)
{
Check.NotNull(func, nameof(func));
_cookieFunc = func;
}
/// <summary>
/// Determines whether the specified RequestMessage is match.
/// </summary>
/// <param name="requestMessage">The RequestMessage.</param>
/// <returns>
/// <c>true</c> if the specified RequestMessage is match; otherwise, <c>false</c>.
/// </returns>
public bool IsMatch(RequestMessage requestMessage)
{
if (_cookieFunc != null)
return _cookieFunc(requestMessage.Cookies);
if (requestMessage.Cookies == null)
return false;
string headerValue = requestMessage.Cookies[_name];
return _matcher.IsMatch(headerValue);
}
}
}

View File

@@ -64,6 +64,9 @@ namespace WireMock.Matchers.Request
if (_headerFunc != null)
return _headerFunc(requestMessage.Headers);
if (requestMessage.Headers == null)
return false;
string headerValue = requestMessage.Headers[_name];
return _matcher.IsMatch(headerValue);
}

View File

@@ -40,12 +40,12 @@ namespace WireMock.Matchers
/// <summary>
/// Copy/paste from http://www.codeproject.com/Tips/57304/Use-wildcard-characters-and-to-compare-strings
/// </summary>
private bool MatchWildcardString(string pattern, string input)
private bool MatchWildcardString([NotNull] string pattern, string input)
{
if (input != null && _ignoreCase)
input = input.ToLower();
if (pattern != null && _ignoreCase)
if (_ignoreCase)
pattern = pattern.ToLower();
if (string.CompareOrdinal(pattern, input) == 0)