mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-22 08:48:46 +02:00
Cookie #9
This commit is contained in:
65
src/WireMock/Matchers/Request/RequestMessageCookieMatcher.cs
Normal file
65
src/WireMock/Matchers/Request/RequestMessageCookieMatcher.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user