mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-18 23:20:11 +02:00
When posting new mapping, use DateParseHandling.None (#348)
* . * OfType + test * remove line
This commit is contained in:
@@ -4,7 +4,7 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<VersionPrefix>1.0.31</VersionPrefix>
|
<VersionPrefix>1.0.32</VersionPrefix>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<Choose>
|
<Choose>
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ namespace WireMock.RequestBuilders
|
|||||||
/// <returns>A List{T}</returns>
|
/// <returns>A List{T}</returns>
|
||||||
public IList<T> GetRequestMessageMatchers<T>() where T : IRequestMatcher
|
public IList<T> GetRequestMessageMatchers<T>() where T : IRequestMatcher
|
||||||
{
|
{
|
||||||
return new ReadOnlyCollection<T>(_requestMatchers.Where(rm => rm is T).Cast<T>().ToList());
|
return new ReadOnlyCollection<T>(_requestMatchers.OfType<T>().ToList());
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -50,7 +50,7 @@ namespace WireMock.RequestBuilders
|
|||||||
/// <returns>A RequestMatcher</returns>
|
/// <returns>A RequestMatcher</returns>
|
||||||
public T GetRequestMessageMatcher<T>() where T : IRequestMatcher
|
public T GetRequestMessageMatcher<T>() where T : IRequestMatcher
|
||||||
{
|
{
|
||||||
return _requestMatchers.Where(rm => rm is T).Cast<T>().FirstOrDefault();
|
return _requestMatchers.OfType<T>().FirstOrDefault();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc cref="IClientIPRequestBuilder.WithClientIP(IStringMatcher[])"/>
|
/// <inheritdoc cref="IClientIPRequestBuilder.WithClientIP(IStringMatcher[])"/>
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
using JetBrains.Annotations;
|
using System;
|
||||||
using SimMetrics.Net;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using JetBrains.Annotations;
|
||||||
|
using SimMetrics.Net;
|
||||||
using WireMock.Admin.Mappings;
|
using WireMock.Admin.Mappings;
|
||||||
using WireMock.Matchers;
|
using WireMock.Matchers;
|
||||||
using WireMock.Settings;
|
using WireMock.Settings;
|
||||||
@@ -36,7 +36,7 @@ namespace WireMock.Serialization
|
|||||||
string matcherName = parts[0];
|
string matcherName = parts[0];
|
||||||
string matcherType = parts.Length > 1 ? parts[1] : null;
|
string matcherType = parts.Length > 1 ? parts[1] : null;
|
||||||
|
|
||||||
string[] stringPatterns = matcher.Patterns != null ? matcher.Patterns.Cast<string>().ToArray() : new[] { matcher.Pattern as string };
|
string[] stringPatterns = matcher.Patterns != null ? matcher.Patterns.OfType<string>().ToArray() : new[] { matcher.Pattern as string };
|
||||||
MatchBehaviour matchBehaviour = matcher.RejectOnMatch == true ? MatchBehaviour.RejectOnMatch : MatchBehaviour.AcceptOnMatch;
|
MatchBehaviour matchBehaviour = matcher.RejectOnMatch == true ? MatchBehaviour.RejectOnMatch : MatchBehaviour.AcceptOnMatch;
|
||||||
|
|
||||||
switch (matcherName)
|
switch (matcherName)
|
||||||
@@ -48,8 +48,7 @@ namespace WireMock.Serialization
|
|||||||
return new ExactMatcher(matchBehaviour, stringPatterns);
|
return new ExactMatcher(matchBehaviour, stringPatterns);
|
||||||
|
|
||||||
case "ExactObjectMatcher":
|
case "ExactObjectMatcher":
|
||||||
var bytePattern = Convert.FromBase64String(stringPatterns[0]);
|
return CreateExactObjectMatcher(matchBehaviour, stringPatterns[0]);
|
||||||
return new ExactObjectMatcher(matchBehaviour, bytePattern);
|
|
||||||
|
|
||||||
case "RegexMatcher":
|
case "RegexMatcher":
|
||||||
return new RegexMatcher(matchBehaviour, stringPatterns, matcher.IgnoreCase == true);
|
return new RegexMatcher(matchBehaviour, stringPatterns, matcher.IgnoreCase == true);
|
||||||
@@ -64,7 +63,7 @@ namespace WireMock.Serialization
|
|||||||
return new JmesPathMatcher(matchBehaviour, stringPatterns);
|
return new JmesPathMatcher(matchBehaviour, stringPatterns);
|
||||||
|
|
||||||
case "XPathMatcher":
|
case "XPathMatcher":
|
||||||
return new XPathMatcher(matchBehaviour, (string)matcher.Pattern);
|
return new XPathMatcher(matchBehaviour, stringPatterns);
|
||||||
|
|
||||||
case "WildcardMatcher":
|
case "WildcardMatcher":
|
||||||
return new WildcardMatcher(matchBehaviour, stringPatterns, matcher.IgnoreCase == true);
|
return new WildcardMatcher(matchBehaviour, stringPatterns, matcher.IgnoreCase == true);
|
||||||
@@ -76,13 +75,28 @@ namespace WireMock.Serialization
|
|||||||
throw new NotSupportedException($"Matcher '{matcherName}' with Type '{matcherType}' is not supported.");
|
throw new NotSupportedException($"Matcher '{matcherName}' with Type '{matcherType}' is not supported.");
|
||||||
}
|
}
|
||||||
|
|
||||||
return new SimMetricsMatcher(matchBehaviour, (string)matcher.Pattern, type);
|
return new SimMetricsMatcher(matchBehaviour, stringPatterns, type);
|
||||||
|
|
||||||
default:
|
default:
|
||||||
throw new NotSupportedException($"Matcher '{matcherName}' is not supported.");
|
throw new NotSupportedException($"Matcher '{matcherName}' is not supported.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private ExactObjectMatcher CreateExactObjectMatcher(MatchBehaviour matchBehaviour, string stringPattern)
|
||||||
|
{
|
||||||
|
byte[] bytePattern;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
bytePattern = Convert.FromBase64String(stringPattern);
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
throw new ArgumentException($"Matcher 'ExactObjectMatcher' has invalid pattern. The pattern value '{stringPattern}' is not a Base64String.", nameof(stringPattern));
|
||||||
|
}
|
||||||
|
|
||||||
|
return new ExactObjectMatcher(matchBehaviour, bytePattern);
|
||||||
|
}
|
||||||
|
|
||||||
public MatcherModel[] Map([CanBeNull] IEnumerable<IMatcher> matchers)
|
public MatcherModel[] Map([CanBeNull] IEnumerable<IMatcher> matchers)
|
||||||
{
|
{
|
||||||
return matchers?.Select(Map).Where(m => m != null).ToArray();
|
return matchers?.Select(Map).Where(m => m != null).ToArray();
|
||||||
|
|||||||
@@ -653,7 +653,7 @@ namespace WireMock.Server
|
|||||||
var clientIPModel = JsonUtils.ParseJTokenToObject<ClientIPModel>(requestModel.ClientIP);
|
var clientIPModel = JsonUtils.ParseJTokenToObject<ClientIPModel>(requestModel.ClientIP);
|
||||||
if (clientIPModel?.Matchers != null)
|
if (clientIPModel?.Matchers != null)
|
||||||
{
|
{
|
||||||
requestBuilder = requestBuilder.WithPath(clientIPModel.Matchers.Select(_matcherMapper.Map).Cast<IStringMatcher>().ToArray());
|
requestBuilder = requestBuilder.WithPath(clientIPModel.Matchers.Select(_matcherMapper.Map).OfType<IStringMatcher>().ToArray());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -671,7 +671,7 @@ namespace WireMock.Server
|
|||||||
var pathModel = JsonUtils.ParseJTokenToObject<PathModel>(requestModel.Path);
|
var pathModel = JsonUtils.ParseJTokenToObject<PathModel>(requestModel.Path);
|
||||||
if (pathModel?.Matchers != null)
|
if (pathModel?.Matchers != null)
|
||||||
{
|
{
|
||||||
requestBuilder = requestBuilder.WithPath(pathModel.Matchers.Select(_matcherMapper.Map).Cast<IStringMatcher>().ToArray());
|
requestBuilder = requestBuilder.WithPath(pathModel.Matchers.Select(_matcherMapper.Map).OfType<IStringMatcher>().ToArray());
|
||||||
pathOrUrlmatchersValid = true;
|
pathOrUrlmatchersValid = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -688,7 +688,7 @@ namespace WireMock.Server
|
|||||||
var urlModel = JsonUtils.ParseJTokenToObject<UrlModel>(requestModel.Url);
|
var urlModel = JsonUtils.ParseJTokenToObject<UrlModel>(requestModel.Url);
|
||||||
if (urlModel?.Matchers != null)
|
if (urlModel?.Matchers != null)
|
||||||
{
|
{
|
||||||
requestBuilder = requestBuilder.WithUrl(urlModel.Matchers.Select(_matcherMapper.Map).Cast<IStringMatcher>().ToArray());
|
requestBuilder = requestBuilder.WithUrl(urlModel.Matchers.Select(_matcherMapper.Map).OfType<IStringMatcher>().ToArray());
|
||||||
pathOrUrlmatchersValid = true;
|
pathOrUrlmatchersValid = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -709,7 +709,7 @@ namespace WireMock.Server
|
|||||||
{
|
{
|
||||||
foreach (var headerModel in requestModel.Headers.Where(h => h.Matchers != null))
|
foreach (var headerModel in requestModel.Headers.Where(h => h.Matchers != null))
|
||||||
{
|
{
|
||||||
requestBuilder = requestBuilder.WithHeader(headerModel.Name, headerModel.Matchers.Select(_matcherMapper.Map).Cast<IStringMatcher>().ToArray());
|
requestBuilder = requestBuilder.WithHeader(headerModel.Name, headerModel.Matchers.Select(_matcherMapper.Map).OfType<IStringMatcher>().ToArray());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -717,7 +717,7 @@ namespace WireMock.Server
|
|||||||
{
|
{
|
||||||
foreach (var cookieModel in requestModel.Cookies.Where(c => c.Matchers != null))
|
foreach (var cookieModel in requestModel.Cookies.Where(c => c.Matchers != null))
|
||||||
{
|
{
|
||||||
requestBuilder = requestBuilder.WithCookie(cookieModel.Name, cookieModel.Matchers.Select(_matcherMapper.Map).Cast<IStringMatcher>().ToArray());
|
requestBuilder = requestBuilder.WithCookie(cookieModel.Name, cookieModel.Matchers.Select(_matcherMapper.Map).OfType<IStringMatcher>().ToArray());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -726,7 +726,7 @@ namespace WireMock.Server
|
|||||||
foreach (var paramModel in requestModel.Params.Where(c => c.Matchers != null))
|
foreach (var paramModel in requestModel.Params.Where(c => c.Matchers != null))
|
||||||
{
|
{
|
||||||
bool ignoreCase = paramModel?.IgnoreCase ?? false;
|
bool ignoreCase = paramModel?.IgnoreCase ?? false;
|
||||||
requestBuilder = requestBuilder.WithParam(paramModel.Name, ignoreCase, paramModel.Matchers.Select(_matcherMapper.Map).Cast<IStringMatcher>().ToArray());
|
requestBuilder = requestBuilder.WithParam(paramModel.Name, ignoreCase, paramModel.Matchers.Select(_matcherMapper.Map).OfType<IStringMatcher>().ToArray());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -57,6 +57,8 @@ namespace WireMock.Util
|
|||||||
new WildcardMatcher("application/x-www-form-urlencoded", true)
|
new WildcardMatcher("application/x-www-form-urlencoded", true)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
private static readonly JsonSerializerSettings JsonSerializerSettings = new JsonSerializerSettings { DateParseHandling = DateParseHandling.None };
|
||||||
|
|
||||||
public static bool ShouldParseBody([CanBeNull] string method)
|
public static bool ShouldParseBody([CanBeNull] string method)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(method))
|
if (string.IsNullOrEmpty(method))
|
||||||
@@ -135,12 +137,12 @@ namespace WireMock.Util
|
|||||||
data.Encoding = DefaultEncoding;
|
data.Encoding = DefaultEncoding;
|
||||||
data.DetectedBodyType = BodyType.String;
|
data.DetectedBodyType = BodyType.String;
|
||||||
|
|
||||||
// If string is not null or empty, try to get as Json
|
// If string is not null or empty, try to deserialize the string to a JObject
|
||||||
if (!string.IsNullOrEmpty(data.BodyAsString))
|
if (!string.IsNullOrEmpty(data.BodyAsString))
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
data.BodyAsJson = JsonConvert.DeserializeObject(data.BodyAsString, new JsonSerializerSettings { Formatting = Formatting.Indented });
|
data.BodyAsJson = JsonConvert.DeserializeObject(data.BodyAsString, JsonSerializerSettings);
|
||||||
data.DetectedBodyType = BodyType.Json;
|
data.DetectedBodyType = BodyType.Json;
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
using NFluent;
|
using System;
|
||||||
using System;
|
|
||||||
using Moq;
|
using Moq;
|
||||||
|
using NFluent;
|
||||||
using WireMock.Admin.Mappings;
|
using WireMock.Admin.Mappings;
|
||||||
using WireMock.Matchers;
|
using WireMock.Matchers;
|
||||||
using WireMock.Serialization;
|
using WireMock.Serialization;
|
||||||
@@ -68,7 +68,7 @@ namespace WireMock.Net.Tests.Serialization
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void MatcherModelMapper_Map_ExactObjectMatcher_Pattern()
|
public void MatcherModelMapper_Map_ExactObjectMatcher_ValidBase64StringPattern()
|
||||||
{
|
{
|
||||||
// Assign
|
// Assign
|
||||||
var model = new MatcherModel
|
var model = new MatcherModel
|
||||||
@@ -84,6 +84,20 @@ namespace WireMock.Net.Tests.Serialization
|
|||||||
Check.That(matcher.ValueAsBytes).ContainsExactly(new byte[] { 115, 116, 101, 102 });
|
Check.That(matcher.ValueAsBytes).ContainsExactly(new byte[] { 115, 116, 101, 102 });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void MatcherModelMapper_Map_ExactObjectMatcher_InvalidBase64StringPattern()
|
||||||
|
{
|
||||||
|
// Assign
|
||||||
|
var model = new MatcherModel
|
||||||
|
{
|
||||||
|
Name = "ExactObjectMatcher",
|
||||||
|
Patterns = new object[] { "_" }
|
||||||
|
};
|
||||||
|
|
||||||
|
// Act & Assert
|
||||||
|
Check.ThatCode(() => _sut.Map(model)).Throws<ArgumentException>();
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void MatcherModelMapper_Map_RegexMatcher()
|
public void MatcherModelMapper_Map_RegexMatcher()
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user