mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-29 12:17:38 +02:00
Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9b09a0ae00 | ||
|
|
ad040fb63c | ||
|
|
9210957e55 | ||
|
|
c135854cbe | ||
|
|
54fe0823dc | ||
|
|
ef9baf3472 | ||
|
|
22f9647e88 | ||
|
|
d5fa385a46 |
@@ -1,3 +1,11 @@
|
|||||||
|
# 1.5.52 (06 April 2024)
|
||||||
|
- [#1091](https://github.com/WireMock-Net/WireMock.Net/pull/1091) - Add RegEx support to JsonMatcher [feature] contributed by [StefH](https://github.com/StefH)
|
||||||
|
- [#1088](https://github.com/WireMock-Net/WireMock.Net/issues/1088) - Regex support for JsonMatcher [feature]
|
||||||
|
|
||||||
|
# 1.5.51 (20 March 2024)
|
||||||
|
- [#1085](https://github.com/WireMock-Net/WireMock.Net/pull/1085) - Fix FluentAssertions (actual body is not displayed in error message) [bug] contributed by [StefH](https://github.com/StefH)
|
||||||
|
- [#1084](https://github.com/WireMock-Net/WireMock.Net/issues/1084) - FluentAssertions - Actual body is not displayed in error message when using Json Body [bug]
|
||||||
|
|
||||||
# 1.5.50 (12 March 2024)
|
# 1.5.50 (12 March 2024)
|
||||||
- [#1080](https://github.com/WireMock-Net/WireMock.Net/pull/1080) - Fix FluentAssertions on Header(s) [bug] contributed by [StefH](https://github.com/StefH)
|
- [#1080](https://github.com/WireMock-Net/WireMock.Net/pull/1080) - Fix FluentAssertions on Header(s) [bug] contributed by [StefH](https://github.com/StefH)
|
||||||
- [#1082](https://github.com/WireMock-Net/WireMock.Net/pull/1082) - Make WireMockAssertions extendable [feature] contributed by [StefH](https://github.com/StefH)
|
- [#1082](https://github.com/WireMock-Net/WireMock.Net/pull/1082) - Make WireMockAssertions extendable [feature] contributed by [StefH](https://github.com/StefH)
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<VersionPrefix>1.5.50</VersionPrefix>
|
<VersionPrefix>1.5.52</VersionPrefix>
|
||||||
<PackageIcon>WireMock.Net-Logo.png</PackageIcon>
|
<PackageIcon>WireMock.Net-Logo.png</PackageIcon>
|
||||||
<PackageProjectUrl>https://github.com/WireMock-Net/WireMock.Net</PackageProjectUrl>
|
<PackageProjectUrl>https://github.com/WireMock-Net/WireMock.Net</PackageProjectUrl>
|
||||||
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
|
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
rem https://github.com/StefH/GitHubReleaseNotes
|
rem https://github.com/StefH/GitHubReleaseNotes
|
||||||
|
|
||||||
SET version=1.5.50
|
SET version=1.5.52
|
||||||
|
|
||||||
GitHubReleaseNotes --output CHANGELOG.md --skip-empty-releases --exclude-labels question invalid doc duplicate example --version %version% --token %GH_TOKEN%
|
GitHubReleaseNotes --output CHANGELOG.md --skip-empty-releases --exclude-labels question invalid doc duplicate example --version %version% --token %GH_TOKEN%
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
# 1.5.50 (12 March 2024)
|
# 1.5.52 (06 April 2024)
|
||||||
- #1080 Fix FluentAssertions on Header(s) [bug]
|
- #1091 Add RegEx support to JsonMatcher [feature]
|
||||||
- #1082 Make WireMockAssertions extendable [feature]
|
- #1088 Regex support for JsonMatcher [feature]
|
||||||
- #1074 FluentAssertions extensions do not filter headers correctly [bug]
|
|
||||||
- #1075 FluentAssertions extensions are not open for extension [feature]
|
|
||||||
|
|
||||||
The full release notes can be found here: https://github.com/WireMock-Net/WireMock.Net/blob/master/CHANGELOG.md
|
The full release notes can be found here: https://github.com/WireMock-Net/WireMock.Net/blob/master/CHANGELOG.md
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
using System.Net;
|
using System.Net;
|
||||||
|
using System.Net.Http.Headers;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using FluentAssertions;
|
using FluentAssertions;
|
||||||
|
using MimeKit;
|
||||||
using WireMock.Logging;
|
using WireMock.Logging;
|
||||||
using WireMock.RequestBuilders;
|
using WireMock.RequestBuilders;
|
||||||
using WireMock.ResponseBuilders;
|
using WireMock.ResponseBuilders;
|
||||||
@@ -18,17 +20,90 @@ internal class Program
|
|||||||
Logger = new WireMockConsoleLogger(),
|
Logger = new WireMockConsoleLogger(),
|
||||||
});
|
});
|
||||||
|
|
||||||
server.Given(Request.Create().UsingPost().WithPath("/some/endpoint"))
|
server
|
||||||
.RespondWith(Response.Create().WithStatusCode(HttpStatusCode.Created));
|
.Given(Request.Create()
|
||||||
|
.UsingPost()
|
||||||
|
.WithPath("/test")
|
||||||
|
)
|
||||||
|
.RespondWith(Response.Create()
|
||||||
|
.WithBody(requestMessage => requestMessage.BodyAsMimeMessage != null ?
|
||||||
|
"BodyAsMimeMessage is present" :
|
||||||
|
"BodyAsMimeMessage is not present")
|
||||||
|
);
|
||||||
|
|
||||||
var httpClient = new HttpClient { BaseAddress = new Uri(server.Url!) };
|
server
|
||||||
var requestUri = new Uri(httpClient.BaseAddress!, "some/endpoint");
|
.Given(Request.Create()
|
||||||
var content = new StringContent(string.Empty, Encoding.UTF8, "application/json");
|
.UsingPost()
|
||||||
|
.WithPath("/some/endpoint")
|
||||||
|
)
|
||||||
|
.RespondWith(Response.Create()
|
||||||
|
.WithStatusCode(HttpStatusCode.Created)
|
||||||
|
);
|
||||||
|
|
||||||
|
var httpClient = server.CreateClient();
|
||||||
|
var content = new StringContent("abc", Encoding.UTF8, "application/json");
|
||||||
|
|
||||||
|
await TestAsync(httpClient, content);
|
||||||
|
|
||||||
|
await TestNoMultiPartAsync(httpClient, content);
|
||||||
|
|
||||||
|
await TestMultiPartAsync(server);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static async Task TestNoMultiPartAsync(HttpClient httpClient, StringContent content)
|
||||||
|
{
|
||||||
|
var response = await httpClient.PostAsync("/test", content);
|
||||||
|
|
||||||
|
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||||
|
(await response.Content.ReadAsStringAsync()).Should().Be("BodyAsMimeMessage is not present");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static async Task TestAsync(HttpClient httpClient, StringContent content)
|
||||||
|
{
|
||||||
|
var response = await httpClient.PostAsync("some/endpoint", content);
|
||||||
|
|
||||||
|
response.StatusCode.Should().Be(HttpStatusCode.Created);
|
||||||
|
(await response.Content.ReadAsStringAsync()).Should().BeEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static async Task TestMultiPartAsync(WireMockServer server)
|
||||||
|
{
|
||||||
|
var textPlainContent = "This is some plain text";
|
||||||
|
var textPlainContentType = "text/plain";
|
||||||
|
|
||||||
|
var textJson = "{ \"Key\" : \"Value\" }";
|
||||||
|
var textJsonContentType = "text/json";
|
||||||
|
|
||||||
|
var imagePngBytes = Convert.FromBase64String("iVBORw0KGgoAAAANSUhEUgAAAAIAAAACAgMAAAAP2OW3AAAADFBMVEX/tID/vpH/pWX/sHidUyjlAAAADElEQVR4XmMQYNgAAADkAMHebX3mAAAAAElFTkSuQmCC");
|
||||||
|
|
||||||
|
server
|
||||||
|
.Given(
|
||||||
|
Request.Create()
|
||||||
|
.UsingPost()
|
||||||
|
.WithPath("/multipart")
|
||||||
|
)
|
||||||
|
.RespondWith(Response.Create()
|
||||||
|
.WithBody(requestMessage => requestMessage.BodyAsMimeMessage is MimeMessage mm ?
|
||||||
|
"BodyAsMimeMessage is present: " + ((MimePart)mm.BodyParts.Last()).FileName :
|
||||||
|
"BodyAsMimeMessage is not present")
|
||||||
|
);
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var actual = await httpClient.PostAsync(requestUri, content);
|
var formDataContent = new MultipartFormDataContent
|
||||||
|
{
|
||||||
|
{ new StringContent(textPlainContent, Encoding.UTF8, textPlainContentType), "text" },
|
||||||
|
{ new StringContent(textJson, Encoding.UTF8, textJsonContentType), "json" }
|
||||||
|
};
|
||||||
|
|
||||||
// Assert
|
var fileContent = new ByteArrayContent(imagePngBytes);
|
||||||
actual.StatusCode.Should().Be(HttpStatusCode.Created);
|
fileContent.Headers.ContentType = new MediaTypeHeaderValue("image/png");
|
||||||
|
formDataContent.Add(fileContent, "somefile", "image.png");
|
||||||
|
|
||||||
|
var client = server.CreateClient();
|
||||||
|
|
||||||
|
var response = await client.PostAsync("/multipart", formDataContent);
|
||||||
|
|
||||||
|
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||||
|
(await response.Content.ReadAsStringAsync()).Should().Be("BodyAsMimeMessage is present: image.png");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -8,8 +8,8 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="FluentAssertions" Version="6.11.0" />
|
<PackageReference Include="FluentAssertions" Version="6.12.0" />
|
||||||
<PackageReference Include="WireMock.Net" Version="1.5.42" />
|
<PackageReference Include="WireMock.Net" Version="1.5.51" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ public class MatcherModel
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public string? MatchOperator { get; set; }
|
public string? MatchOperator { get; set; }
|
||||||
|
|
||||||
#region JsonPartialMatcher and JsonPartialWildcardMatcher
|
#region JsonMatcher, JsonPartialMatcher and JsonPartialWildcardMatcher
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Support Regex.
|
/// Support Regex.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -1,7 +1,12 @@
|
|||||||
#pragma warning disable CS1591
|
#pragma warning disable CS1591
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using AnyOfTypes;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Newtonsoft.Json.Linq;
|
||||||
|
using WireMock.Extensions;
|
||||||
using WireMock.Matchers;
|
using WireMock.Matchers;
|
||||||
|
using WireMock.Models;
|
||||||
|
|
||||||
// ReSharper disable once CheckNamespace
|
// ReSharper disable once CheckNamespace
|
||||||
namespace WireMock.FluentAssertions;
|
namespace WireMock.FluentAssertions;
|
||||||
@@ -9,7 +14,7 @@ namespace WireMock.FluentAssertions;
|
|||||||
public partial class WireMockAssertions
|
public partial class WireMockAssertions
|
||||||
{
|
{
|
||||||
private const string MessageFormatNoCalls = "Expected {context:wiremockserver} to have been called using body {0}{reason}, but no calls were made.";
|
private const string MessageFormatNoCalls = "Expected {context:wiremockserver} to have been called using body {0}{reason}, but no calls were made.";
|
||||||
private const string MessageFormat = "Expected {context:wiremockserver} to have been called using body {0}{reason}, but didn't find it among the body {1}.";
|
private const string MessageFormat = "Expected {context:wiremockserver} to have been called using body {0}{reason}, but didn't find it among the body/bodies {1}.";
|
||||||
|
|
||||||
[CustomAssertion]
|
[CustomAssertion]
|
||||||
public AndConstraint<WireMockAssertions> WithBody(string body, string because = "", params object[] becauseArgs)
|
public AndConstraint<WireMockAssertions> WithBody(string body, string because = "", params object[] becauseArgs)
|
||||||
@@ -56,7 +61,7 @@ public partial class WireMockAssertions
|
|||||||
{
|
{
|
||||||
var (filter, condition) = BuildFilterAndCondition(r => r.BodyAsBytes, matcher);
|
var (filter, condition) = BuildFilterAndCondition(r => r.BodyAsBytes, matcher);
|
||||||
|
|
||||||
return ExecuteAssertionWithBodyAsBytesExactObjectMatcher(matcher, because, becauseArgs, condition, filter, r => r.BodyAsBytes);
|
return ExecuteAssertionWithBodyAsIObjectMatcher(matcher, because, becauseArgs, condition, filter, r => r.BodyAsBytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
private AndConstraint<WireMockAssertions> ExecuteAssertionWithBodyStringMatcher(
|
private AndConstraint<WireMockAssertions> ExecuteAssertionWithBodyStringMatcher(
|
||||||
@@ -74,14 +79,14 @@ public partial class WireMockAssertions
|
|||||||
.ForCondition(requests => CallsCount == 0 || requests.Any())
|
.ForCondition(requests => CallsCount == 0 || requests.Any())
|
||||||
.FailWith(
|
.FailWith(
|
||||||
MessageFormatNoCalls,
|
MessageFormatNoCalls,
|
||||||
matcher.GetPatterns()
|
FormatBody(matcher.GetPatterns())
|
||||||
)
|
)
|
||||||
.Then
|
.Then
|
||||||
.ForCondition(condition)
|
.ForCondition(condition)
|
||||||
.FailWith(
|
.FailWith(
|
||||||
MessageFormat,
|
MessageFormat,
|
||||||
_ => matcher.GetPatterns(),
|
_ => FormatBody(matcher.GetPatterns()),
|
||||||
requests => requests.Select(expression)
|
requests => FormatBodies(requests.Select(expression))
|
||||||
);
|
);
|
||||||
|
|
||||||
FilterRequestMessages(filter);
|
FilterRequestMessages(filter);
|
||||||
@@ -104,14 +109,14 @@ public partial class WireMockAssertions
|
|||||||
.ForCondition(requests => CallsCount == 0 || requests.Any())
|
.ForCondition(requests => CallsCount == 0 || requests.Any())
|
||||||
.FailWith(
|
.FailWith(
|
||||||
MessageFormatNoCalls,
|
MessageFormatNoCalls,
|
||||||
matcher.Value
|
FormatBody(matcher.Value)
|
||||||
)
|
)
|
||||||
.Then
|
.Then
|
||||||
.ForCondition(condition)
|
.ForCondition(condition)
|
||||||
.FailWith(
|
.FailWith(
|
||||||
MessageFormat,
|
MessageFormat,
|
||||||
_ => matcher.Value,
|
_ => FormatBody(matcher.Value),
|
||||||
requests => requests.Select(expression)
|
requests => FormatBodies(requests.Select(expression))
|
||||||
);
|
);
|
||||||
|
|
||||||
FilterRequestMessages(filter);
|
FilterRequestMessages(filter);
|
||||||
@@ -119,33 +124,22 @@ public partial class WireMockAssertions
|
|||||||
return new AndConstraint<WireMockAssertions>(this);
|
return new AndConstraint<WireMockAssertions>(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
private AndConstraint<WireMockAssertions> ExecuteAssertionWithBodyAsBytesExactObjectMatcher(
|
private static string? FormatBody(object? body)
|
||||||
ExactObjectMatcher matcher,
|
|
||||||
string because,
|
|
||||||
object[] becauseArgs,
|
|
||||||
Func<IReadOnlyList<IRequestMessage>, bool> condition,
|
|
||||||
Func<IReadOnlyList<IRequestMessage>, IReadOnlyList<IRequestMessage>> filter,
|
|
||||||
Func<IRequestMessage, object?> expression
|
|
||||||
)
|
|
||||||
{
|
{
|
||||||
Execute.Assertion
|
return body switch
|
||||||
.BecauseOf(because, becauseArgs)
|
{
|
||||||
.Given(() => RequestMessages)
|
null => null,
|
||||||
.ForCondition(requests => CallsCount == 0 || requests.Any())
|
string str => str,
|
||||||
.FailWith(
|
AnyOf<string, StringPattern>[] stringPatterns => FormatBodies(stringPatterns.Select(p => p.GetPattern())),
|
||||||
MessageFormatNoCalls,
|
byte[] bytes => $"byte[{bytes.Length}] {{...}}",
|
||||||
matcher.Value
|
JToken jToken => jToken.ToString(Formatting.None),
|
||||||
)
|
_ => JToken.FromObject(body).ToString(Formatting.None)
|
||||||
.Then
|
};
|
||||||
.ForCondition(condition)
|
}
|
||||||
.FailWith(
|
|
||||||
MessageFormat,
|
|
||||||
_ => matcher.Value,
|
|
||||||
requests => requests.Select(expression)
|
|
||||||
);
|
|
||||||
|
|
||||||
FilterRequestMessages(filter);
|
private static string? FormatBodies(IEnumerable<object?> bodies)
|
||||||
|
{
|
||||||
return new AndConstraint<WireMockAssertions>(this);
|
var valueAsArray = bodies as object[] ?? bodies.ToArray();
|
||||||
|
return valueAsArray.Length == 1 ? FormatBody(valueAsArray.First()) : $"[ {string.Join(", ", valueAsArray.Select(FormatBody))} ]";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -5,18 +5,36 @@ using WireMock.Models;
|
|||||||
|
|
||||||
namespace WireMock.Extensions;
|
namespace WireMock.Extensions;
|
||||||
|
|
||||||
internal static class AnyOfExtensions
|
/// <summary>
|
||||||
|
/// Some extensions for AnyOf.
|
||||||
|
/// </summary>
|
||||||
|
public static class AnyOfExtensions
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the pattern.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="value">AnyOf type</param>
|
||||||
|
/// <returns>string value</returns>
|
||||||
public static string GetPattern(this AnyOf<string, StringPattern> value)
|
public static string GetPattern(this AnyOf<string, StringPattern> value)
|
||||||
{
|
{
|
||||||
return value.IsFirst ? value.First : value.Second.Pattern;
|
return value.IsFirst ? value.First : value.Second.Pattern;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Converts a string-patterns to AnyOf patterns.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="patterns">The string patterns</param>
|
||||||
|
/// <returns>The AnyOf patterns</returns>
|
||||||
public static AnyOf<string, StringPattern>[] ToAnyOfPatterns(this IEnumerable<string> patterns)
|
public static AnyOf<string, StringPattern>[] ToAnyOfPatterns(this IEnumerable<string> patterns)
|
||||||
{
|
{
|
||||||
return patterns.Select(p => p.ToAnyOfPattern()).ToArray();
|
return patterns.Select(p => p.ToAnyOfPattern()).ToArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Converts a string-pattern to AnyOf pattern.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="pattern">The string pattern</param>
|
||||||
|
/// <returns>The AnyOf pattern</returns>
|
||||||
public static AnyOf<string, StringPattern> ToAnyOfPattern(this string pattern)
|
public static AnyOf<string, StringPattern> ToAnyOfPattern(this string pattern)
|
||||||
{
|
{
|
||||||
return new AnyOf<string, StringPattern>(pattern);
|
return new AnyOf<string, StringPattern>(pattern);
|
||||||
|
|||||||
@@ -10,20 +10,15 @@ namespace WireMock.Matchers;
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public abstract class AbstractJsonPartialMatcher : JsonMatcher
|
public abstract class AbstractJsonPartialMatcher : JsonMatcher
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// Support Regex
|
|
||||||
/// </summary>
|
|
||||||
public bool Regex { get; }
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="AbstractJsonPartialMatcher"/> class.
|
/// Initializes a new instance of the <see cref="AbstractJsonPartialMatcher"/> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="value">The string value to check for equality.</param>
|
/// <param name="value">The string value to check for equality.</param>
|
||||||
/// <param name="ignoreCase">Ignore the case from the PropertyName and PropertyValue (string only).</param>
|
/// <param name="ignoreCase">Ignore the case from the PropertyName and PropertyValue (string only).</param>
|
||||||
/// <param name="regex">Support Regex.</param>
|
/// <param name="regex">Support Regex.</param>
|
||||||
protected AbstractJsonPartialMatcher(string value, bool ignoreCase = false, bool regex = false) : base(value, ignoreCase)
|
protected AbstractJsonPartialMatcher(string value, bool ignoreCase = false, bool regex = false) :
|
||||||
|
base(value, ignoreCase, regex)
|
||||||
{
|
{
|
||||||
Regex = regex;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -32,9 +27,9 @@ public abstract class AbstractJsonPartialMatcher : JsonMatcher
|
|||||||
/// <param name="value">The object value to check for equality.</param>
|
/// <param name="value">The object value to check for equality.</param>
|
||||||
/// <param name="ignoreCase">Ignore the case from the PropertyName and PropertyValue (string only).</param>
|
/// <param name="ignoreCase">Ignore the case from the PropertyName and PropertyValue (string only).</param>
|
||||||
/// <param name="regex">Support Regex.</param>
|
/// <param name="regex">Support Regex.</param>
|
||||||
protected AbstractJsonPartialMatcher(object value, bool ignoreCase = false, bool regex = false) : base(value, ignoreCase)
|
protected AbstractJsonPartialMatcher(object value, bool ignoreCase = false, bool regex = false) :
|
||||||
|
base(value, ignoreCase, regex)
|
||||||
{
|
{
|
||||||
Regex = regex;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -44,15 +39,15 @@ public abstract class AbstractJsonPartialMatcher : JsonMatcher
|
|||||||
/// <param name="value">The value to check for equality.</param>
|
/// <param name="value">The value to check for equality.</param>
|
||||||
/// <param name="ignoreCase">Ignore the case from the PropertyName and PropertyValue (string only).</param>
|
/// <param name="ignoreCase">Ignore the case from the PropertyName and PropertyValue (string only).</param>
|
||||||
/// <param name="regex">Support Regex.</param>
|
/// <param name="regex">Support Regex.</param>
|
||||||
protected AbstractJsonPartialMatcher(MatchBehaviour matchBehaviour, object value, bool ignoreCase = false, bool regex = false) : base(matchBehaviour, value, ignoreCase)
|
protected AbstractJsonPartialMatcher(MatchBehaviour matchBehaviour, object value, bool ignoreCase = false, bool regex = false) :
|
||||||
|
base(matchBehaviour, value, ignoreCase, regex)
|
||||||
{
|
{
|
||||||
Regex = regex;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
protected override bool IsMatch(JToken? value, JToken? input)
|
protected override bool IsMatch(JToken value, JToken? input)
|
||||||
{
|
{
|
||||||
if (value == null || value == input)
|
if (value == input)
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -72,7 +67,7 @@ public abstract class AbstractJsonPartialMatcher : JsonMatcher
|
|||||||
((value.Type == JTokenType.Guid && input.Type == JTokenType.String) ||
|
((value.Type == JTokenType.Guid && input.Type == JTokenType.String) ||
|
||||||
(value.Type == JTokenType.String && input.Type == JTokenType.Guid)))
|
(value.Type == JTokenType.String && input.Type == JTokenType.Guid)))
|
||||||
{
|
{
|
||||||
return IsMatch(value.ToString(), input.ToString());
|
return IsMatch(value.ToString().ToUpperInvariant(), input.ToString().ToUpperInvariant());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (input == null || value.Type != input.Type)
|
if (input == null || value.Type != input.Type)
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Newtonsoft.Json.Linq;
|
using Newtonsoft.Json.Linq;
|
||||||
using Stef.Validation;
|
using Stef.Validation;
|
||||||
using WireMock.Util;
|
using WireMock.Util;
|
||||||
|
using JsonUtils = WireMock.Util.JsonUtils;
|
||||||
|
|
||||||
namespace WireMock.Matchers;
|
namespace WireMock.Matchers;
|
||||||
|
|
||||||
@@ -23,6 +25,11 @@ public class JsonMatcher : IJsonMatcher
|
|||||||
/// <inheritdoc cref="IIgnoreCaseMatcher.IgnoreCase"/>
|
/// <inheritdoc cref="IIgnoreCaseMatcher.IgnoreCase"/>
|
||||||
public bool IgnoreCase { get; }
|
public bool IgnoreCase { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Support Regex
|
||||||
|
/// </summary>
|
||||||
|
public bool Regex { get; }
|
||||||
|
|
||||||
private readonly JToken _valueAsJToken;
|
private readonly JToken _valueAsJToken;
|
||||||
private readonly Func<JToken, JToken> _jTokenConverter;
|
private readonly Func<JToken, JToken> _jTokenConverter;
|
||||||
|
|
||||||
@@ -31,7 +38,8 @@ public class JsonMatcher : IJsonMatcher
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="value">The string value to check for equality.</param>
|
/// <param name="value">The string value to check for equality.</param>
|
||||||
/// <param name="ignoreCase">Ignore the case from the PropertyName and PropertyValue (string only).</param>
|
/// <param name="ignoreCase">Ignore the case from the PropertyName and PropertyValue (string only).</param>
|
||||||
public JsonMatcher(string value, bool ignoreCase = false) : this(MatchBehaviour.AcceptOnMatch, value, ignoreCase)
|
/// <param name="regex">Support Regex.</param>
|
||||||
|
public JsonMatcher(string value, bool ignoreCase = false, bool regex = false) : this(MatchBehaviour.AcceptOnMatch, value, ignoreCase, regex)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -40,7 +48,8 @@ public class JsonMatcher : IJsonMatcher
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="value">The object value to check for equality.</param>
|
/// <param name="value">The object value to check for equality.</param>
|
||||||
/// <param name="ignoreCase">Ignore the case from the PropertyName and PropertyValue (string only).</param>
|
/// <param name="ignoreCase">Ignore the case from the PropertyName and PropertyValue (string only).</param>
|
||||||
public JsonMatcher(object value, bool ignoreCase = false) : this(MatchBehaviour.AcceptOnMatch, value, ignoreCase)
|
/// <param name="regex">Support Regex.</param>
|
||||||
|
public JsonMatcher(object value, bool ignoreCase = false, bool regex = false) : this(MatchBehaviour.AcceptOnMatch, value, ignoreCase, regex)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -50,12 +59,14 @@ public class JsonMatcher : IJsonMatcher
|
|||||||
/// <param name="matchBehaviour">The match behaviour.</param>
|
/// <param name="matchBehaviour">The match behaviour.</param>
|
||||||
/// <param name="value">The value to check for equality.</param>
|
/// <param name="value">The value to check for equality.</param>
|
||||||
/// <param name="ignoreCase">Ignore the case from the PropertyName and PropertyValue (string only).</param>
|
/// <param name="ignoreCase">Ignore the case from the PropertyName and PropertyValue (string only).</param>
|
||||||
public JsonMatcher(MatchBehaviour matchBehaviour, object value, bool ignoreCase = false)
|
/// <param name="regex">Support Regex.</param>
|
||||||
|
public JsonMatcher(MatchBehaviour matchBehaviour, object value, bool ignoreCase = false, bool regex = false)
|
||||||
{
|
{
|
||||||
Guard.NotNull(value);
|
Guard.NotNull(value);
|
||||||
|
|
||||||
MatchBehaviour = matchBehaviour;
|
MatchBehaviour = matchBehaviour;
|
||||||
IgnoreCase = ignoreCase;
|
IgnoreCase = ignoreCase;
|
||||||
|
Regex = regex;
|
||||||
|
|
||||||
Value = value;
|
Value = value;
|
||||||
_valueAsJToken = JsonUtils.ConvertValueToJToken(value);
|
_valueAsJToken = JsonUtils.ConvertValueToJToken(value);
|
||||||
@@ -93,10 +104,80 @@ public class JsonMatcher : IJsonMatcher
|
|||||||
/// <param name="value">Matcher value</param>
|
/// <param name="value">Matcher value</param>
|
||||||
/// <param name="input">Input value</param>
|
/// <param name="input">Input value</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
protected virtual bool IsMatch(JToken value, JToken input)
|
protected virtual bool IsMatch(JToken value, JToken? input)
|
||||||
{
|
{
|
||||||
|
// If equal, return true.
|
||||||
|
if (input == value)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If input is null, return false.
|
||||||
|
if (input == null)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If using Regex and the value is a string, use the MatchRegex method.
|
||||||
|
if (Regex && value.Type == JTokenType.String)
|
||||||
|
{
|
||||||
|
var valueAsString = value.ToString();
|
||||||
|
|
||||||
|
var (valid, result) = RegexUtils.MatchRegex(valueAsString, input.ToString());
|
||||||
|
if (valid)
|
||||||
|
{
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the value is a Guid and the input is a string, or vice versa, convert them to strings and compare the string values.
|
||||||
|
if ((value.Type == JTokenType.Guid && input.Type == JTokenType.String) || (value.Type == JTokenType.String && input.Type == JTokenType.Guid))
|
||||||
|
{
|
||||||
|
return JToken.DeepEquals(value.ToString().ToUpperInvariant(), input.ToString().ToUpperInvariant());
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (value.Type)
|
||||||
|
{
|
||||||
|
// If the value is an object, compare all properties.
|
||||||
|
case JTokenType.Object:
|
||||||
|
var valueProperties = value.ToObject<Dictionary<string, JToken>>() ?? new Dictionary<string, JToken>();
|
||||||
|
var inputProperties = input.ToObject<Dictionary<string, JToken>>() ?? new Dictionary<string, JToken>();
|
||||||
|
|
||||||
|
// If the number of properties is different, return false.
|
||||||
|
if (valueProperties.Count != inputProperties.Count)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Compare all properties. The input must match all properties of the value.
|
||||||
|
foreach (var pair in valueProperties)
|
||||||
|
{
|
||||||
|
if (!IsMatch(pair.Value, inputProperties[pair.Key]))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
|
||||||
|
// If the value is an array, compare all elements.
|
||||||
|
case JTokenType.Array:
|
||||||
|
var valueArray = value.ToObject<JToken[]>() ?? EmptyArray<JToken>.Value;
|
||||||
|
var inputArray = input.ToObject<JToken[]>() ?? EmptyArray<JToken>.Value;
|
||||||
|
|
||||||
|
// If the number of elements is different, return false.
|
||||||
|
if (valueArray.Length != inputArray.Length)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return !valueArray.Where((valueToken, index) => !IsMatch(valueToken, inputArray[index])).Any();
|
||||||
|
|
||||||
|
default:
|
||||||
|
// Use JToken.DeepEquals() for all other types.
|
||||||
return JToken.DeepEquals(value, input);
|
return JToken.DeepEquals(value, input);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static string? ToUpper(string? input)
|
private static string? ToUpper(string? input)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -84,7 +84,7 @@ internal class MatcherMapper
|
|||||||
|
|
||||||
case nameof(JsonMatcher):
|
case nameof(JsonMatcher):
|
||||||
var valueForJsonMatcher = matcherModel.Pattern ?? matcherModel.Patterns;
|
var valueForJsonMatcher = matcherModel.Pattern ?? matcherModel.Patterns;
|
||||||
return new JsonMatcher(matchBehaviour, valueForJsonMatcher!, ignoreCase);
|
return new JsonMatcher(matchBehaviour, valueForJsonMatcher!, ignoreCase, useRegex);
|
||||||
|
|
||||||
case nameof(JsonPartialMatcher):
|
case nameof(JsonPartialMatcher):
|
||||||
var valueForJsonPartialMatcher = matcherModel.Pattern ?? matcherModel.Patterns;
|
var valueForJsonPartialMatcher = matcherModel.Pattern ?? matcherModel.Patterns;
|
||||||
@@ -152,12 +152,8 @@ internal class MatcherMapper
|
|||||||
|
|
||||||
switch (matcher)
|
switch (matcher)
|
||||||
{
|
{
|
||||||
case JsonPartialMatcher jsonPartialMatcher:
|
case JsonMatcher jsonMatcher:
|
||||||
model.Regex = jsonPartialMatcher.Regex;
|
model.Regex = jsonMatcher.Regex;
|
||||||
break;
|
|
||||||
|
|
||||||
case JsonPartialWildcardMatcher jsonPartialWildcardMatcher:
|
|
||||||
model.Regex = jsonPartialWildcardMatcher.Regex;
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case XPathMatcher xpathMatcher:
|
case XPathMatcher xpathMatcher:
|
||||||
|
|||||||
@@ -72,7 +72,6 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup Condition=" '$(TargetFramework)' != 'netstandard1.3' ">
|
<ItemGroup Condition=" '$(TargetFramework)' != 'netstandard1.3' ">
|
||||||
<PackageReference Include="XPath2.Extensions" Version="1.1.4" />
|
|
||||||
<PackageReference Include="Microsoft.IdentityModel.Protocols.OpenIdConnect" Version="6.34.0" />
|
<PackageReference Include="Microsoft.IdentityModel.Protocols.OpenIdConnect" Version="6.34.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
@@ -182,13 +181,13 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Handlebars.Net.Helpers" Version="2.4.1.4" />
|
<PackageReference Include="Handlebars.Net.Helpers" Version="2.4.2.1" />
|
||||||
<PackageReference Include="Handlebars.Net.Helpers.DynamicLinq" Version="2.4.1.4" />
|
<PackageReference Include="Handlebars.Net.Helpers.DynamicLinq" Version="2.4.2.1" />
|
||||||
<PackageReference Include="Handlebars.Net.Helpers.Humanizer" Version="2.4.1.4" />
|
<PackageReference Include="Handlebars.Net.Helpers.Humanizer" Version="2.4.2.1" />
|
||||||
<PackageReference Include="Handlebars.Net.Helpers.Json" Version="2.4.1.4" />
|
<PackageReference Include="Handlebars.Net.Helpers.Json" Version="2.4.2.1" />
|
||||||
<PackageReference Include="Handlebars.Net.Helpers.Random" Version="2.4.1.4" />
|
<PackageReference Include="Handlebars.Net.Helpers.Random" Version="2.4.2.1" />
|
||||||
<PackageReference Include="Handlebars.Net.Helpers.XPath" Version="2.4.1.4" />
|
<PackageReference Include="Handlebars.Net.Helpers.XPath" Version="2.4.2.1" />
|
||||||
<PackageReference Include="Handlebars.Net.Helpers.Xeger" Version="2.4.1.4" />
|
<PackageReference Include="Handlebars.Net.Helpers.Xeger" Version="2.4.2.1" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -702,7 +702,11 @@ public class WireMockAssertionsTests : IDisposable
|
|||||||
// Act
|
// Act
|
||||||
var httpClient = new HttpClient();
|
var httpClient = new HttpClient();
|
||||||
|
|
||||||
await httpClient.PostAsync($"{server.Url}/a", new StringContent(@"{ ""x"": ""y"" }"));
|
var requestBody = new
|
||||||
|
{
|
||||||
|
x = "y"
|
||||||
|
};
|
||||||
|
await httpClient.PostAsJsonAsync($"{server.Url}/a", requestBody);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
server
|
server
|
||||||
@@ -740,6 +744,103 @@ public class WireMockAssertionsTests : IDisposable
|
|||||||
server.Stop();
|
server.Stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task WithBodyAsJson_When_NoMatch_ShouldHaveCorrectErrorMessage()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var server = WireMockServer.Start();
|
||||||
|
|
||||||
|
server
|
||||||
|
.Given(Request.Create().WithPath("/a").UsingPost())
|
||||||
|
.RespondWith(Response.Create().WithBody("A response"));
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var httpClient = new HttpClient();
|
||||||
|
|
||||||
|
var requestBody = new
|
||||||
|
{
|
||||||
|
x = "123"
|
||||||
|
};
|
||||||
|
await httpClient.PostAsJsonAsync($"{server.Url}/a", requestBody);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Action act = () => server
|
||||||
|
.Should()
|
||||||
|
.HaveReceived(1)
|
||||||
|
.Calls()
|
||||||
|
.WithBodyAsJson(new { x = "y" })
|
||||||
|
.And
|
||||||
|
.UsingPost();
|
||||||
|
|
||||||
|
act.Should()
|
||||||
|
.Throw<Exception>()
|
||||||
|
.WithMessage("""Expected wiremockserver to have been called using body "{"x":"y"}", but didn't find it among the body/bodies "{"x":"123"}".""");
|
||||||
|
|
||||||
|
server.Stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task WithBodyAsString_When_NoMatch_ShouldHaveCorrectErrorMessage()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var server = WireMockServer.Start();
|
||||||
|
|
||||||
|
server
|
||||||
|
.Given(Request.Create().WithPath("/a").UsingPost())
|
||||||
|
.RespondWith(Response.Create().WithBody("A response"));
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var httpClient = new HttpClient();
|
||||||
|
|
||||||
|
await httpClient.PostAsync($"{server.Url}/a", new StringContent("123"));
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Action act = () => server
|
||||||
|
.Should()
|
||||||
|
.HaveReceived(1)
|
||||||
|
.Calls()
|
||||||
|
.WithBody("abc")
|
||||||
|
.And
|
||||||
|
.UsingPost();
|
||||||
|
|
||||||
|
act.Should()
|
||||||
|
.Throw<Exception>()
|
||||||
|
.WithMessage("""Expected wiremockserver to have been called using body "abc", but didn't find it among the body/bodies "123".""");
|
||||||
|
|
||||||
|
server.Stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task WithBodyAsBytes_When_NoMatch_ShouldHaveCorrectErrorMessage()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var server = WireMockServer.Start();
|
||||||
|
|
||||||
|
server
|
||||||
|
.Given(Request.Create().WithPath("/a").UsingPost())
|
||||||
|
.RespondWith(Response.Create().WithBody("A response"));
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var httpClient = new HttpClient();
|
||||||
|
|
||||||
|
await httpClient.PostAsync($"{server.Url}/a", new ByteArrayContent(new byte[] { 5 }));
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Action act = () => server
|
||||||
|
.Should()
|
||||||
|
.HaveReceived(1)
|
||||||
|
.Calls()
|
||||||
|
.WithBodyAsBytes(new byte[] { 1 })
|
||||||
|
.And
|
||||||
|
.UsingPost();
|
||||||
|
|
||||||
|
act.Should()
|
||||||
|
.Throw<Exception>()
|
||||||
|
.WithMessage("""Expected wiremockserver to have been called using body "byte[1] {...}", but didn't find it among the body/bodies "byte[1] {...}".""");
|
||||||
|
|
||||||
|
server.Stop();
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task HaveReceived1Call_WithBodyAsBytes()
|
public async Task HaveReceived1Call_WithBodyAsBytes()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ public class JsonMatcherTests
|
|||||||
var matcher = new JsonMatcher("{}");
|
var matcher = new JsonMatcher("{}");
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
string name = matcher.Name;
|
var name = matcher.Name;
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Check.That(name).Equals("JsonMatcher");
|
Check.That(name).Equals("JsonMatcher");
|
||||||
@@ -53,7 +53,7 @@ public class JsonMatcherTests
|
|||||||
var matcher = new JsonMatcher("{}");
|
var matcher = new JsonMatcher("{}");
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
object value = matcher.Value;
|
var value = matcher.Value;
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Check.That(value).Equals("{}");
|
Check.That(value).Equals("{}");
|
||||||
@@ -103,7 +103,7 @@ public class JsonMatcherTests
|
|||||||
var matcher = new JsonMatcher("");
|
var matcher = new JsonMatcher("");
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
double match = matcher.IsMatch(bytes).Score;
|
var match = matcher.IsMatch(bytes).Score;
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Check.That(match).IsEqualTo(0);
|
Check.That(match).IsEqualTo(0);
|
||||||
@@ -117,7 +117,7 @@ public class JsonMatcherTests
|
|||||||
var matcher = new JsonMatcher("");
|
var matcher = new JsonMatcher("");
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
double match = matcher.IsMatch(s).Score;
|
var match = matcher.IsMatch(s).Score;
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Check.That(match).IsEqualTo(0);
|
Check.That(match).IsEqualTo(0);
|
||||||
@@ -131,7 +131,7 @@ public class JsonMatcherTests
|
|||||||
var matcher = new JsonMatcher("");
|
var matcher = new JsonMatcher("");
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
double match = matcher.IsMatch(o).Score;
|
var match = matcher.IsMatch(o).Score;
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Check.That(match).IsEqualTo(0);
|
Check.That(match).IsEqualTo(0);
|
||||||
@@ -149,14 +149,14 @@ public class JsonMatcherTests
|
|||||||
"x",
|
"x",
|
||||||
"y"
|
"y"
|
||||||
};
|
};
|
||||||
double match = matcher.IsMatch(jArray).Score;
|
var match = matcher.IsMatch(jArray).Score;
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.Equal(1.0, match);
|
Assert.Equal(1.0, match);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void JsonMatcher_IsMatch_JObject()
|
public void JsonMatcher_IsMatch_JObject_ShouldMatch()
|
||||||
{
|
{
|
||||||
// Assign
|
// Assign
|
||||||
var matcher = new JsonMatcher(new { Id = 1, Name = "Test" });
|
var matcher = new JsonMatcher(new { Id = 1, Name = "Test" });
|
||||||
@@ -167,12 +167,31 @@ public class JsonMatcherTests
|
|||||||
{ "Id", new JValue(1) },
|
{ "Id", new JValue(1) },
|
||||||
{ "Name", new JValue("Test") }
|
{ "Name", new JValue("Test") }
|
||||||
};
|
};
|
||||||
double match = matcher.IsMatch(jObject).Score;
|
var match = matcher.IsMatch(jObject).Score;
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.Equal(1.0, match);
|
Assert.Equal(1.0, match);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void JsonMatcher_IsMatch_JObject_ShouldNotMatch()
|
||||||
|
{
|
||||||
|
// Assign
|
||||||
|
var matcher = new JsonMatcher(new { Id = 1, Name = "Test" });
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var jObject = new JObject
|
||||||
|
{
|
||||||
|
{ "Id", new JValue(1) },
|
||||||
|
{ "Name", new JValue("Test") },
|
||||||
|
{ "Other", new JValue("abc") }
|
||||||
|
};
|
||||||
|
var score = matcher.IsMatch(jObject).Score;
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Equal(MatchScores.Mismatch, score);
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void JsonMatcher_IsMatch_WithIgnoreCaseTrue_JObject()
|
public void JsonMatcher_IsMatch_WithIgnoreCaseTrue_JObject()
|
||||||
{
|
{
|
||||||
@@ -185,7 +204,7 @@ public class JsonMatcherTests
|
|||||||
{ "Id", new JValue(1) },
|
{ "Id", new JValue(1) },
|
||||||
{ "NaMe", new JValue("Test") }
|
{ "NaMe", new JValue("Test") }
|
||||||
};
|
};
|
||||||
double match = matcher.IsMatch(jObject).Score;
|
var match = matcher.IsMatch(jObject).Score;
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.Equal(1.0, match);
|
Assert.Equal(1.0, match);
|
||||||
@@ -199,7 +218,7 @@ public class JsonMatcherTests
|
|||||||
|
|
||||||
// Act
|
// Act
|
||||||
var jObject = JObject.Parse("{ \"Id\" : 1, \"Name\" : \"Test\" }");
|
var jObject = JObject.Parse("{ \"Id\" : 1, \"Name\" : \"Test\" }");
|
||||||
double match = matcher.IsMatch(jObject).Score;
|
var match = matcher.IsMatch(jObject).Score;
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.Equal(1.0, match);
|
Assert.Equal(1.0, match);
|
||||||
@@ -213,7 +232,7 @@ public class JsonMatcherTests
|
|||||||
|
|
||||||
// Act
|
// Act
|
||||||
var jObject = JObject.Parse("{ \"Id\" : 1, \"Name\" : \"Test\" }");
|
var jObject = JObject.Parse("{ \"Id\" : 1, \"Name\" : \"Test\" }");
|
||||||
double match = matcher.IsMatch(jObject).Score;
|
var match = matcher.IsMatch(jObject).Score;
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.Equal(1.0, match);
|
Assert.Equal(1.0, match);
|
||||||
@@ -231,7 +250,7 @@ public class JsonMatcherTests
|
|||||||
"x",
|
"x",
|
||||||
"y"
|
"y"
|
||||||
};
|
};
|
||||||
double match = matcher.IsMatch(jArray).Score;
|
var match = matcher.IsMatch(jArray).Score;
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.Equal(1.0, match);
|
Assert.Equal(1.0, match);
|
||||||
@@ -249,7 +268,7 @@ public class JsonMatcherTests
|
|||||||
{ "Id", new JValue(1) },
|
{ "Id", new JValue(1) },
|
||||||
{ "Name", new JValue("Test") }
|
{ "Name", new JValue("Test") }
|
||||||
};
|
};
|
||||||
double match = matcher.IsMatch(jObject).Score;
|
var match = matcher.IsMatch(jObject).Score;
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.Equal(1.0, match);
|
Assert.Equal(1.0, match);
|
||||||
@@ -267,7 +286,7 @@ public class JsonMatcherTests
|
|||||||
{ "Id", new JValue(1) },
|
{ "Id", new JValue(1) },
|
||||||
{ "Name", new JValue("Test") }
|
{ "Name", new JValue("Test") }
|
||||||
};
|
};
|
||||||
double match = matcher.IsMatch(jObject).Score;
|
var match = matcher.IsMatch(jObject).Score;
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.Equal(1.0, match);
|
Assert.Equal(1.0, match);
|
||||||
@@ -285,7 +304,7 @@ public class JsonMatcherTests
|
|||||||
{ "Id", new JValue(1) },
|
{ "Id", new JValue(1) },
|
||||||
{ "Name", new JValue("Test") }
|
{ "Name", new JValue("Test") }
|
||||||
};
|
};
|
||||||
double match = matcher.IsMatch(jObject).Score;
|
var match = matcher.IsMatch(jObject).Score;
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.Equal(0.0, match);
|
Assert.Equal(0.0, match);
|
||||||
@@ -302,7 +321,7 @@ public class JsonMatcherTests
|
|||||||
{
|
{
|
||||||
{ "preferredAt", new JValue("2019-11-21T10:32:53.2210009+00:00") }
|
{ "preferredAt", new JValue("2019-11-21T10:32:53.2210009+00:00") }
|
||||||
};
|
};
|
||||||
double match = matcher.IsMatch(jObject).Score;
|
var match = matcher.IsMatch(jObject).Score;
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.Equal(1.0, match);
|
Assert.Equal(1.0, match);
|
||||||
@@ -312,14 +331,14 @@ public class JsonMatcherTests
|
|||||||
public void JsonMatcher_IsMatch_NormalEnum()
|
public void JsonMatcher_IsMatch_NormalEnum()
|
||||||
{
|
{
|
||||||
// Assign
|
// Assign
|
||||||
var matcher = new JsonMatcher(new Test1 { NormalEnum = NormalEnum.Abc});
|
var matcher = new JsonMatcher(new Test1 { NormalEnum = NormalEnum.Abc });
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var jObject = new JObject
|
var jObject = new JObject
|
||||||
{
|
{
|
||||||
{ "NormalEnum", new JValue(0) }
|
{ "NormalEnum", new JValue(0) }
|
||||||
};
|
};
|
||||||
double match = matcher.IsMatch(jObject).Score;
|
var match = matcher.IsMatch(jObject).Score;
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
match.Should().Be(1.0);
|
match.Should().Be(1.0);
|
||||||
@@ -336,9 +355,174 @@ public class JsonMatcherTests
|
|||||||
{
|
{
|
||||||
{ "EnumWithJsonConverter", new JValue("Type1") }
|
{ "EnumWithJsonConverter", new JValue("Type1") }
|
||||||
};
|
};
|
||||||
double match = matcher.IsMatch(jObject).Score;
|
var match = matcher.IsMatch(jObject).Score;
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
match.Should().Be(1.0);
|
match.Should().Be(1.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void JsonMatcher_IsMatch_WithRegexTrue_ShouldMatch()
|
||||||
|
{
|
||||||
|
// Assign
|
||||||
|
var matcher = new JsonMatcher(new { Id = "^\\d+$", Name = "Test" }, regex: true);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var jObject = new JObject
|
||||||
|
{
|
||||||
|
{ "Id", new JValue(42) },
|
||||||
|
{ "Name", new JValue("Test") }
|
||||||
|
};
|
||||||
|
var score = matcher.IsMatch(jObject).Score;
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Equal(1.0, score);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void JsonMatcher_IsMatch_WithRegexTrue_Complex_ShouldMatch()
|
||||||
|
{
|
||||||
|
// Assign
|
||||||
|
var matcher = new JsonMatcher(new
|
||||||
|
{
|
||||||
|
Complex = new
|
||||||
|
{
|
||||||
|
Id = "^\\d+$",
|
||||||
|
Name = ".*"
|
||||||
|
}
|
||||||
|
}, regex: true);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var jObject = new JObject
|
||||||
|
{
|
||||||
|
{
|
||||||
|
"Complex", new JObject
|
||||||
|
{
|
||||||
|
{ "Id", new JValue(42) },
|
||||||
|
{ "Name", new JValue("Test") }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
var score = matcher.IsMatch(jObject).Score;
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Equal(1.0, score);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void JsonMatcher_IsMatch_WithRegexTrue_Complex_ShouldNotMatch()
|
||||||
|
{
|
||||||
|
// Assign
|
||||||
|
var matcher = new JsonMatcher(new
|
||||||
|
{
|
||||||
|
Complex = new
|
||||||
|
{
|
||||||
|
Id = "^\\d+$",
|
||||||
|
Name = ".*"
|
||||||
|
}
|
||||||
|
}, regex: true);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var jObject = new JObject
|
||||||
|
{
|
||||||
|
{
|
||||||
|
"Complex", new JObject
|
||||||
|
{
|
||||||
|
{ "Id", new JValue(42) },
|
||||||
|
{ "Name", new JValue("Test") },
|
||||||
|
{ "Other", new JValue("Other") }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
var score = matcher.IsMatch(jObject).Score;
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Equal(MatchScores.Mismatch, score);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void JsonMatcher_IsMatch_WithRegexTrue_Array_ShouldMatch()
|
||||||
|
{
|
||||||
|
// Assign
|
||||||
|
var matcher = new JsonMatcher(new
|
||||||
|
{
|
||||||
|
Array = new[]
|
||||||
|
{
|
||||||
|
"^\\d+$",
|
||||||
|
".*"
|
||||||
|
}
|
||||||
|
}, regex: true);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var jObject = new JObject
|
||||||
|
{
|
||||||
|
{ "Array", new JArray("42", "test") }
|
||||||
|
};
|
||||||
|
var score = matcher.IsMatch(jObject).Score;
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Equal(1.0, score);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void JsonMatcher_IsMatch_WithRegexTrue_Array_ShouldNotMatch()
|
||||||
|
{
|
||||||
|
// Assign
|
||||||
|
var matcher = new JsonMatcher(new
|
||||||
|
{
|
||||||
|
Array = new[]
|
||||||
|
{
|
||||||
|
"^\\d+$",
|
||||||
|
".*"
|
||||||
|
}
|
||||||
|
}, regex: true);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var jObject = new JObject
|
||||||
|
{
|
||||||
|
{ "Array", new JArray("42", "test", "other") }
|
||||||
|
};
|
||||||
|
var score = matcher.IsMatch(jObject).Score;
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Equal(MatchScores.Mismatch, score);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void JsonMatcher_IsMatch_GuidAndString()
|
||||||
|
{
|
||||||
|
// Assign
|
||||||
|
var id = Guid.NewGuid();
|
||||||
|
var idAsString = id.ToString();
|
||||||
|
var matcher = new JsonMatcher(new { Id = id });
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var jObject = new JObject
|
||||||
|
{
|
||||||
|
{ "Id", new JValue(idAsString) }
|
||||||
|
};
|
||||||
|
var score = matcher.IsMatch(jObject).Score;
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Equal(1.0, score);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void JsonMatcher_IsMatch_StringAndGuid()
|
||||||
|
{
|
||||||
|
// Assign
|
||||||
|
var id = Guid.NewGuid();
|
||||||
|
var idAsString = id.ToString();
|
||||||
|
var matcher = new JsonMatcher(new { Id = idAsString });
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var jObject = new JObject
|
||||||
|
{
|
||||||
|
{ "Id", new JValue(id) }
|
||||||
|
};
|
||||||
|
var score = matcher.IsMatch(jObject).Score;
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Equal(1.0, score);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -25,7 +25,8 @@
|
|||||||
Pattern: {
|
Pattern: {
|
||||||
name: stef
|
name: stef
|
||||||
},
|
},
|
||||||
IgnoreCase: false
|
IgnoreCase: false,
|
||||||
|
Regex: false
|
||||||
},
|
},
|
||||||
ProtoBufMessageType: greet.HelloRequest
|
ProtoBufMessageType: greet.HelloRequest
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,7 +42,8 @@ message HelloReply {
|
|||||||
Pattern: {
|
Pattern: {
|
||||||
name: stef
|
name: stef
|
||||||
},
|
},
|
||||||
IgnoreCase: false
|
IgnoreCase: false,
|
||||||
|
Regex: false
|
||||||
},
|
},
|
||||||
ProtoBufMessageType: greet.HelloRequest
|
ProtoBufMessageType: greet.HelloRequest
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -967,4 +967,44 @@ public class WireMockServerProxyTests
|
|||||||
|
|
||||||
server.Dispose();
|
server.Dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// #1097
|
||||||
|
// https://stackoverflow.com/questions/52106567/how-to-stop-httprequestmessage-from-unencoding-3a-to-a-colon-in-the-request-uri
|
||||||
|
[Fact]
|
||||||
|
public async Task WireMockServer_Proxy_Should_Keep_UrlEncoding()
|
||||||
|
{
|
||||||
|
// Assign
|
||||||
|
string path = $"/prx_{Guid.NewGuid()}";
|
||||||
|
var serverForProxyForwarding = WireMockServer.Start();
|
||||||
|
serverForProxyForwarding
|
||||||
|
.Given(Request.Create().WithPath(path))
|
||||||
|
.RespondWith(Response.Create());
|
||||||
|
|
||||||
|
var settings = new WireMockServerSettings
|
||||||
|
{
|
||||||
|
ProxyAndRecordSettings = new ProxyAndRecordSettings
|
||||||
|
{
|
||||||
|
Url = serverForProxyForwarding.Urls[0],
|
||||||
|
SaveMapping = true,
|
||||||
|
SaveMappingToFile = false
|
||||||
|
}
|
||||||
|
};
|
||||||
|
var server = WireMockServer.Start(settings);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var requestUri = $"{server.Urls[0]}{path}with%20space_and_%3A_colon";
|
||||||
|
var requestMessage = new HttpRequestMessage
|
||||||
|
{
|
||||||
|
Method = HttpMethod.Get,
|
||||||
|
RequestUri = new Uri(requestUri)
|
||||||
|
};
|
||||||
|
await new HttpClient().SendAsync(requestMessage).ConfigureAwait(false);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
var receivedRequest = serverForProxyForwarding.LogEntries.First().RequestMessage;
|
||||||
|
receivedRequest.AbsolutePath.Should().EndWith("with space_and_:_colon");
|
||||||
|
|
||||||
|
// check that new proxied mapping is added
|
||||||
|
server.Mappings.Should().HaveCount(2);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user