mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-22 17:40:33 +01:00
@@ -12,6 +12,7 @@ namespace WireMock.Matchers.Request
|
|||||||
public class RequestMessageCookieMatcher : IRequestMatcher
|
public class RequestMessageCookieMatcher : IRequestMatcher
|
||||||
{
|
{
|
||||||
private readonly MatchBehaviour _matchBehaviour;
|
private readonly MatchBehaviour _matchBehaviour;
|
||||||
|
private readonly bool _ignoreCase;
|
||||||
|
|
||||||
/// <value>
|
/// <value>
|
||||||
/// The funcs.
|
/// The funcs.
|
||||||
@@ -41,6 +42,7 @@ namespace WireMock.Matchers.Request
|
|||||||
Check.NotNull(pattern, nameof(pattern));
|
Check.NotNull(pattern, nameof(pattern));
|
||||||
|
|
||||||
_matchBehaviour = matchBehaviour;
|
_matchBehaviour = matchBehaviour;
|
||||||
|
_ignoreCase = ignoreCase;
|
||||||
Name = name;
|
Name = name;
|
||||||
Matchers = new IStringMatcher[] { new WildcardMatcher(matchBehaviour, pattern, ignoreCase) };
|
Matchers = new IStringMatcher[] { new WildcardMatcher(matchBehaviour, pattern, ignoreCase) };
|
||||||
}
|
}
|
||||||
@@ -84,9 +86,12 @@ namespace WireMock.Matchers.Request
|
|||||||
return MatchBehaviourHelper.Convert(_matchBehaviour, MatchScores.Mismatch);
|
return MatchBehaviourHelper.Convert(_matchBehaviour, MatchScores.Mismatch);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check if we want to use IgnoreCase to compare the Cookie-Name and Cookie-Value
|
||||||
|
var cookies = !_ignoreCase ? requestMessage.Cookies : new Dictionary<string, string>(requestMessage.Cookies, StringComparer.OrdinalIgnoreCase);
|
||||||
|
|
||||||
if (Funcs != null)
|
if (Funcs != null)
|
||||||
{
|
{
|
||||||
return MatchScores.ToScore(Funcs.Any(f => f(requestMessage.Cookies)));
|
return MatchScores.ToScore(Funcs.Any(f => f(cookies)));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Matchers == null)
|
if (Matchers == null)
|
||||||
@@ -94,12 +99,12 @@ namespace WireMock.Matchers.Request
|
|||||||
return MatchScores.Mismatch;
|
return MatchScores.Mismatch;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!requestMessage.Cookies.ContainsKey(Name))
|
if (!cookies.ContainsKey(Name))
|
||||||
{
|
{
|
||||||
return MatchBehaviourHelper.Convert(_matchBehaviour, MatchScores.Mismatch);
|
return MatchBehaviourHelper.Convert(_matchBehaviour, MatchScores.Mismatch);
|
||||||
}
|
}
|
||||||
|
|
||||||
string value = requestMessage.Cookies[Name];
|
string value = cookies[Name];
|
||||||
return Matchers.Max(m => m.IsMatch(value));
|
return Matchers.Max(m => m.IsMatch(value));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ namespace WireMock.Matchers.Request
|
|||||||
public class RequestMessageHeaderMatcher : IRequestMatcher
|
public class RequestMessageHeaderMatcher : IRequestMatcher
|
||||||
{
|
{
|
||||||
private readonly MatchBehaviour _matchBehaviour;
|
private readonly MatchBehaviour _matchBehaviour;
|
||||||
|
private readonly bool _ignoreCase;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The functions
|
/// The functions
|
||||||
@@ -43,6 +44,7 @@ namespace WireMock.Matchers.Request
|
|||||||
Check.NotNull(pattern, nameof(pattern));
|
Check.NotNull(pattern, nameof(pattern));
|
||||||
|
|
||||||
_matchBehaviour = matchBehaviour;
|
_matchBehaviour = matchBehaviour;
|
||||||
|
_ignoreCase = ignoreCase;
|
||||||
Name = name;
|
Name = name;
|
||||||
Matchers = new IStringMatcher[] { new WildcardMatcher(matchBehaviour, pattern, ignoreCase) };
|
Matchers = new IStringMatcher[] { new WildcardMatcher(matchBehaviour, pattern, ignoreCase) };
|
||||||
}
|
}
|
||||||
@@ -60,6 +62,7 @@ namespace WireMock.Matchers.Request
|
|||||||
Check.NotNull(patterns, nameof(patterns));
|
Check.NotNull(patterns, nameof(patterns));
|
||||||
|
|
||||||
_matchBehaviour = matchBehaviour;
|
_matchBehaviour = matchBehaviour;
|
||||||
|
_ignoreCase = ignoreCase;
|
||||||
Name = name;
|
Name = name;
|
||||||
Matchers = patterns.Select(pattern => new WildcardMatcher(matchBehaviour, pattern, ignoreCase)).Cast<IStringMatcher>().ToArray();
|
Matchers = patterns.Select(pattern => new WildcardMatcher(matchBehaviour, pattern, ignoreCase)).Cast<IStringMatcher>().ToArray();
|
||||||
}
|
}
|
||||||
@@ -103,9 +106,12 @@ namespace WireMock.Matchers.Request
|
|||||||
return MatchBehaviourHelper.Convert(_matchBehaviour, MatchScores.Mismatch);
|
return MatchBehaviourHelper.Convert(_matchBehaviour, MatchScores.Mismatch);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check if we want to use IgnoreCase to compare the Header-Name and Header-Value(s)
|
||||||
|
var headers = !_ignoreCase ? requestMessage.Headers : new Dictionary<string, WireMockList<string>>(requestMessage.Headers, StringComparer.OrdinalIgnoreCase);
|
||||||
|
|
||||||
if (Funcs != null)
|
if (Funcs != null)
|
||||||
{
|
{
|
||||||
return MatchScores.ToScore(Funcs.Any(f => f(requestMessage.Headers.ToDictionary(entry => entry.Key, entry => entry.Value.ToArray()))));
|
return MatchScores.ToScore(Funcs.Any(f => f(headers.ToDictionary(entry => entry.Key, entry => entry.Value.ToArray()))));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Matchers == null)
|
if (Matchers == null)
|
||||||
@@ -113,12 +119,12 @@ namespace WireMock.Matchers.Request
|
|||||||
return MatchScores.Mismatch;
|
return MatchScores.Mismatch;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!requestMessage.Headers.ContainsKey(Name))
|
if (!headers.ContainsKey(Name))
|
||||||
{
|
{
|
||||||
return MatchBehaviourHelper.Convert(_matchBehaviour, MatchScores.Mismatch);
|
return MatchBehaviourHelper.Convert(_matchBehaviour, MatchScores.Mismatch);
|
||||||
}
|
}
|
||||||
|
|
||||||
WireMockList<string> list = requestMessage.Headers[Name];
|
WireMockList<string> list = headers[Name];
|
||||||
return Matchers.Max(m => list.Max(value => m.IsMatch(value))); // TODO : is this correct ?
|
return Matchers.Max(m => list.Max(value => m.IsMatch(value))); // TODO : is this correct ?
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
using System;
|
using System.Collections.Generic;
|
||||||
using System.Collections.Generic;
|
|
||||||
using NFluent;
|
using NFluent;
|
||||||
using WireMock.Matchers;
|
using WireMock.Matchers;
|
||||||
using WireMock.Matchers.Request;
|
using WireMock.Matchers.Request;
|
||||||
@@ -134,5 +133,37 @@ namespace WireMock.Net.Tests.RequestMatchers
|
|||||||
// Assert
|
// Assert
|
||||||
Check.That(score).IsEqualTo(1.0d);
|
Check.That(score).IsEqualTo(1.0d);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void RequestMessageCookieMatcher_GetMatchingScore_CaseIgnoreForCookieValue()
|
||||||
|
{
|
||||||
|
// Assign
|
||||||
|
var cookies = new Dictionary<string, string> { { "cook", "teST" } };
|
||||||
|
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", null, null, cookies);
|
||||||
|
var matcher = new RequestMessageCookieMatcher(MatchBehaviour.AcceptOnMatch, "cook", "test", true);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = new RequestMatchResult();
|
||||||
|
double score = matcher.GetMatchingScore(requestMessage, result);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Check.That(score).IsEqualTo(1.0d);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void RequestMessageCookieMatcher_GetMatchingScore_CaseIgnoreForCookieName()
|
||||||
|
{
|
||||||
|
// Assign
|
||||||
|
var cookies = new Dictionary<string, string> { { "cook", "teST" } };
|
||||||
|
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", null, null, cookies);
|
||||||
|
var matcher = new RequestMessageCookieMatcher(MatchBehaviour.AcceptOnMatch, "CooK", "test", true);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = new RequestMatchResult();
|
||||||
|
double score = matcher.GetMatchingScore(requestMessage, result);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Check.That(score).IsEqualTo(1.0d);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
using System;
|
using System.Collections.Generic;
|
||||||
using System.Collections.Generic;
|
|
||||||
using NFluent;
|
using NFluent;
|
||||||
using WireMock.Matchers;
|
using WireMock.Matchers;
|
||||||
using WireMock.Matchers.Request;
|
using WireMock.Matchers.Request;
|
||||||
@@ -134,5 +133,37 @@ namespace WireMock.Net.Tests.RequestMatchers
|
|||||||
// Assert
|
// Assert
|
||||||
Check.That(score).IsEqualTo(1.0d);
|
Check.That(score).IsEqualTo(1.0d);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void RequestMessageHeaderMatcher_GetMatchingScore_CaseIgnoreForHeaderValue()
|
||||||
|
{
|
||||||
|
// Assign
|
||||||
|
var headers = new Dictionary<string, string[]> { { "h", new[] { "teST" } } };
|
||||||
|
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", null, headers);
|
||||||
|
var matcher = new RequestMessageHeaderMatcher(MatchBehaviour.AcceptOnMatch, "h", "test", true);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = new RequestMatchResult();
|
||||||
|
double score = matcher.GetMatchingScore(requestMessage, result);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Check.That(score).IsEqualTo(1.0d);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void RequestMessageHeaderMatcher_GetMatchingScore_CaseIgnoreForHeaderName()
|
||||||
|
{
|
||||||
|
// Assign
|
||||||
|
var headers = new Dictionary<string, string[]> { { "teST", new[] { "x" } } };
|
||||||
|
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", null, headers);
|
||||||
|
var matcher = new RequestMessageHeaderMatcher(MatchBehaviour.AcceptOnMatch, "TEST", "x", true);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = new RequestMatchResult();
|
||||||
|
double score = matcher.GetMatchingScore(requestMessage, result);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Check.That(score).IsEqualTo(1.0d);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user