Compare commits

..

2 Commits

Author SHA1 Message Date
Stef Heyenrath
c0c07ea127 1.5.53 2024-05-08 17:43:21 +02:00
Stef Heyenrath
e20a90b615 Fix MappingConverter to support Body with JsonMatcher (#1101)
* Fix MappingBuilder for Body

* .

* .

* Fix MappingConverter
2024-05-08 17:40:53 +02:00
16 changed files with 311 additions and 115 deletions

View File

@@ -1,3 +1,8 @@
# 1.5.53 (08 May 2024)
- [#1093](https://github.com/WireMock-Net/WireMock.Net/pull/1093) - Update Handlebars.Net [feature] contributed by [StefH](https://github.com/StefH)
- [#1101](https://github.com/WireMock-Net/WireMock.Net/pull/1101) - Fix MappingConverter to support Body with JsonMatcher [bug] contributed by [StefH](https://github.com/StefH)
- [#1095](https://github.com/WireMock-Net/WireMock.Net/issues/1095) - When using C# code generation WithBody() matcher is not generated for POST Request [bug]
# 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]

View File

@@ -4,7 +4,7 @@
</PropertyGroup>
<PropertyGroup>
<VersionPrefix>1.5.52</VersionPrefix>
<VersionPrefix>1.5.53</VersionPrefix>
<PackageIcon>WireMock.Net-Logo.png</PackageIcon>
<PackageProjectUrl>https://github.com/WireMock-Net/WireMock.Net</PackageProjectUrl>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>

View File

@@ -1,6 +1,6 @@
rem https://github.com/StefH/GitHubReleaseNotes
SET version=1.5.52
SET version=1.5.53
GitHubReleaseNotes --output CHANGELOG.md --skip-empty-releases --exclude-labels question invalid doc duplicate example --version %version% --token %GH_TOKEN%

View File

@@ -1,5 +1,6 @@
# 1.5.52 (06 April 2024)
- #1091 Add RegEx support to JsonMatcher [feature]
- #1088 Regex support for JsonMatcher [feature]
# 1.5.53 (08 May 2024)
- #1093 Update Handlebars.Net [feature]
- #1101 Fix MappingConverter to support Body with JsonMatcher [bug]
- #1095 When using C# code generation WithBody() matcher is not generated for POST Request [bug]
The full release notes can be found here: https://github.com/WireMock-Net/WireMock.Net/blob/master/CHANGELOG.md

View File

@@ -143,25 +143,21 @@ internal class MappingConverter
if (requestMessageBodyMatcher is { Matchers: { } })
{
if (requestMessageBodyMatcher.Matchers.OfType<WildcardMatcher>().FirstOrDefault() is { } wildcardMatcher && wildcardMatcher.GetPatterns().Any())
var firstMatcher = requestMessageBodyMatcher.Matchers.FirstOrDefault();
if (firstMatcher is WildcardMatcher wildcardMatcher && wildcardMatcher.GetPatterns().Any())
{
sb.AppendLine($" .WithBody({GetString(wildcardMatcher)})");
}
else if (requestMessageBodyMatcher.Matchers.OfType<JsonPartialMatcher>().FirstOrDefault() is { Value: { } } jsonPartialMatcher)
if (firstMatcher is JsonMatcher jsonMatcher)
{
sb.AppendLine(@$" .WithBody(new JsonPartialMatcher(
value: {ToCSharpStringLiteral(jsonPartialMatcher.Value.ToString())},
ignoreCase: {ToCSharpBooleanLiteral(jsonPartialMatcher.IgnoreCase)},
regex: {ToCSharpBooleanLiteral(jsonPartialMatcher.Regex)}
))");
}
else if (requestMessageBodyMatcher.Matchers.OfType<JsonPartialWildcardMatcher>().FirstOrDefault() is { Value: { } } jsonPartialWildcardMatcher)
{
sb.AppendLine(@$" .WithBody(new JsonPartialWildcardMatcher(
value: {ToCSharpStringLiteral(jsonPartialWildcardMatcher.Value.ToString())},
ignoreCase: {ToCSharpBooleanLiteral(jsonPartialWildcardMatcher.IgnoreCase)},
regex: {ToCSharpBooleanLiteral(jsonPartialWildcardMatcher.Regex)}
))");
var matcherType = jsonMatcher.GetType().Name;
sb.AppendLine($" .WithBody(new {matcherType}(");
sb.AppendLine($" value: {ConvertToAnonymousObjectDefinition(jsonMatcher.Value, 3)},");
sb.AppendLine($" ignoreCase: {ToCSharpBooleanLiteral(jsonMatcher.IgnoreCase)},");
sb.AppendLine($" regex: {ToCSharpBooleanLiteral(jsonMatcher.Regex)}");
sb.AppendLine(@" ))");
}
}

View File

@@ -344,15 +344,13 @@ public partial class WireMockServer
private static MappingConverterType GetMappingConverterType(IRequestMessage requestMessage)
{
var mappingConverterType = MappingConverterType.Server;
if (requestMessage.QueryIgnoreCase?.TryGetValue(nameof(MappingConverterType), out var values) == true &&
Enum.TryParse(values.FirstOrDefault(), true, out MappingConverterType parsed))
{
mappingConverterType = parsed;
return parsed;
}
return mappingConverterType;
return MappingConverterType.Server;
}
private IMapping? FindMappingByGuid(IRequestMessage requestMessage)

View File

@@ -10,6 +10,8 @@ namespace WireMock.Util;
internal static class CSharpFormatter
{
private const string Null = "null";
#region Reserved Keywords
private static readonly HashSet<string> CSharpReservedKeywords = new(new[]
{
@@ -92,17 +94,15 @@ internal static class CSharpFormatter
"while"
});
#endregion
private const string Null = "null";
public static object ConvertToAnonymousObjectDefinition(object jsonBody)
public static object ConvertToAnonymousObjectDefinition(object jsonBody, int ind = 2)
{
var serializedBody = JsonConvert.SerializeObject(jsonBody);
using var jsonReader = new JsonTextReader(new StringReader(serializedBody));
jsonReader.DateParseHandling = DateParseHandling.None;
var deserializedBody = JObject.Load(jsonReader);
return ConvertJsonToAnonymousObjectDefinition(deserializedBody, 2);
return ConvertJsonToAnonymousObjectDefinition(deserializedBody, ind);
}
public static string ConvertJsonToAnonymousObjectDefinition(JToken token, int ind = 0)

View File

@@ -1,11 +1,49 @@
var server = WireMockServer.Start();
server
.Given(Request.Create()
.UsingMethod("POST")
.WithPath("/users/post1")
.WithBody(new JsonMatcher(
value: new
{
city = "Amsterdam",
country = "The Netherlands"
},
ignoreCase: false,
regex: false
))
)
.WithGuid("90356dba-b36c-469a-a17e-669cd84f1f05")
.RespondWith(Response.Create()
);
server
.Given(Request.Create()
.UsingMethod("POST")
.WithPath("/users/post2")
.WithBody(new JsonPartialMatcher(
value: new
{
city = "City",
country = "Country"
},
ignoreCase: false,
regex: false
))
)
.WithGuid("1b731398-4a5b-457f-a6e3-d65e541c428f")
.RespondWith(Response.Create()
.WithBody(@"Line1
Some ""value"" in Line2")
);
server
.Given(Request.Create()
.UsingMethod("GET")
.WithPath("/foo1")
.WithParam("p1", "xyz")
)
.WithGuid("90356dba-b36c-469a-a17e-669cd84f1f05")
.WithGuid("f74fd144-df53-404f-8e35-da22a640bd5f")
.RespondWith(Response.Create()
.WithStatusCode(200)
.WithBody("1")
@@ -18,7 +56,7 @@ server
.WithParam("p2", "abc")
.WithHeader("h1", "W/\"234f2q3r\"", true)
)
.WithGuid("1b731398-4a5b-457f-a6e3-d65e541c428f")
.WithGuid("4126dec8-470b-4eff-93bb-c24f83b8b1fd")
.RespondWith(Response.Create()
.WithStatusCode("201")
.WithHeader("hk", "hv")
@@ -31,7 +69,7 @@ server
.UsingMethod("DELETE")
.WithUrl("https://localhost/test")
)
.WithGuid("f74fd144-df53-404f-8e35-da22a640bd5f")
.WithGuid("c9929240-7ae8-4a5d-8ed8-0913479f6eeb")
.RespondWith(Response.Create()
.WithStatusCode(208)
.WithBodyAsJson(new
@@ -70,20 +108,3 @@ text
})
);
server
.Given(Request.Create()
.UsingMethod("POST")
.WithPath("/foo3")
.WithBody(new JsonPartialMatcher(
value: "{ a = 1, b = 2 }",
ignoreCase: false,
regex: false
))
)
.WithGuid("4126dec8-470b-4eff-93bb-c24f83b8b1fd")
.RespondWith(Response.Create()
.WithStatusCode(200)
.WithBody(@"Line1
Some ""value"" in Line2")
);

View File

@@ -491,7 +491,7 @@ public partial class WireMockAdminApiTests
server.Stop();
}
[Fact]
public async Task IWireMockAdminApi_GetRequestsAsync_Json()
{
@@ -862,8 +862,37 @@ public partial class WireMockAdminApiTests
var guid2 = Guid.Parse("1b731398-4a5b-457f-a6e3-d65e541c428f");
var guid3 = Guid.Parse("f74fd144-df53-404f-8e35-da22a640bd5f");
var guid4 = Guid.Parse("4126DEC8-470B-4EFF-93BB-C24F83B8B1FD");
var guid5 = Guid.Parse("c9929240-7ae8-4a5d-8ed8-0913479f6eeb");
var server = WireMockServer.StartWithAdminInterface();
server
.Given(
Request.Create()
.WithPath("/users/post1")
.UsingPost()
.WithBody(new JsonMatcher(new
{
city = "Amsterdam",
country = "The Netherlands"
}))
)
.WithGuid(guid1)
.RespondWith(Response.Create());
server
.Given(
Request.Create()
.WithPath("/users/post2")
.UsingPost()
.WithBody(new JsonPartialMatcher(new
{
city = "City",
country = "Country"
}))
)
.WithGuid(guid2)
.RespondWith(Response.Create().WithBody("Line1\r\nSome \"value\" in Line2"));
server
.Given(
Request.Create()
@@ -871,7 +900,7 @@ public partial class WireMockAdminApiTests
.WithParam("p1", "xyz")
.UsingGet()
)
.WithGuid(guid1)
.WithGuid(guid3)
.RespondWith(
Response.Create()
.WithStatusCode(200)
@@ -886,7 +915,7 @@ public partial class WireMockAdminApiTests
.WithHeader("h1", "W/\"234f2q3r\"")
.UsingPost()
)
.WithGuid(guid2)
.WithGuid(guid4)
.RespondWith(
Response.Create()
.WithStatusCode("201")
@@ -901,36 +930,43 @@ public partial class WireMockAdminApiTests
.WithUrl("https://localhost/test")
.UsingDelete()
)
.WithGuid(guid3)
.WithGuid(guid5)
.RespondWith(
Response.Create()
.WithStatusCode(HttpStatusCode.AlreadyReported)
.WithBodyAsJson(new { @as = 1, b = 1.2, d = true, e = false, f = new[] { 1, 2, 3, 4 }, g = new { z1 = 1, z2 = 2, z3 = new[] { "a", "b", "c" }, z4 = new[] { new { a = 1, b = 2 }, new { a = 2, b = 3 } } }, date_field = new DateTime(2023, 05, 08, 11, 20, 19), string_field_with_date = "2021-03-13T21:04:00Z", multiline_text = @"This
.WithBodyAsJson(new
{
@as = 1,
b = 1.2,
d = true,
e = false,
f = new[] { 1, 2, 3, 4 },
g = new
{
z1 = 1,
z2 = 2,
z3 = new[] { "a", "b", "c" },
z4 = new[]
{
new { a = 1, b = 2 },
new { a = 2, b = 3 }
}
},
date_field = new DateTime(2023, 05, 08, 11, 20, 19),
string_field_with_date = "2021-03-13T21:04:00Z",
multiline_text = @"This
is
multiline
text
" })
);
server
.Given(
Request.Create()
.WithPath("/foo3")
.WithBody(new JsonPartialMatcher(new { a = 1, b = 2 }))
.UsingPost()
)
.WithGuid(guid4)
.RespondWith(
Response.Create()
.WithStatusCode(200)
.WithBody("Line1\r\nSome \"value\" in Line2")
"
})
);
// Act
var api = RestClient.For<IWireMockAdminApi>(server.Url);
var mappings = await api.GetMappingsAsync().ConfigureAwait(false);
mappings.Should().HaveCount(4);
mappings.Should().HaveCount(5);
var code = await api.GetMappingsCodeAsync().ConfigureAwait(false);

View File

@@ -31,5 +31,35 @@
BodyDestination: SameAsSource,
Body: { msg: "Hello world!"}
}
},
{
Guid: Guid_2,
UpdatedAt: 2023-01-14 15:16:17,
Request: {
Path: {
Matchers: [
{
Name: WildcardMatcher,
Pattern: /users/post2,
IgnoreCase: false
}
]
},
Methods: [
POST
],
Body: {
Matcher: {
Name: JsonMatcher,
Pattern: {
city: Amsterdam,
country: The Netherlands
},
IgnoreCase: false,
Regex: false
}
}
},
Response: {}
}
]

View File

@@ -0,0 +1,30 @@
var builder = new MappingBuilder();
builder
.Given(Request.Create()
.UsingMethod("GET")
.WithPath("/foo")
.WithParam("test", "it.Length < 10")
)
.WithGuid("41372914-1838-4c67-916b-b9aacdd096ce")
.RespondWith(Response.Create()
.WithBody("{ msg: \"Hello world!\"}")
);
builder
.Given(Request.Create()
.UsingMethod("POST")
.WithPath("/users/post2")
.WithBody(new JsonMatcher(
value: new
{
city = "Amsterdam",
country = "The Netherlands"
},
ignoreCase: false,
regex: false
))
)
.WithGuid("98fae52e-76df-47d9-876f-2ee32e931d9b")
.RespondWith(Response.Create()
);

View File

@@ -0,0 +1,30 @@
var server = WireMockServer.Start();
server
.Given(Request.Create()
.UsingMethod("GET")
.WithPath("/foo")
.WithParam("test", "it.Length < 10")
)
.WithGuid("41372914-1838-4c67-916b-b9aacdd096ce")
.RespondWith(Response.Create()
.WithBody("{ msg: \"Hello world!\"}")
);
server
.Given(Request.Create()
.UsingMethod("POST")
.WithPath("/users/post2")
.WithBody(new JsonMatcher(
value: new
{
city = "Amsterdam",
country = "The Netherlands"
},
ignoreCase: false,
regex: false
))
)
.WithGuid("98fae52e-76df-47d9-876f-2ee32e931d9b")
.RespondWith(Response.Create()
);

View File

@@ -31,5 +31,34 @@
BodyDestination: SameAsSource,
Body: { msg: "Hello world!"}
}
},
{
Guid: Guid_2,
UpdatedAt: 2023-01-14T15:16:17,
Request: {
Path: {
Matchers: [
{
Name: WildcardMatcher,
Pattern: /users/post2,
IgnoreCase: false
}
]
},
Methods: [
POST
],
Body: {
Matcher: {
Name: JsonMatcher,
Pattern: {
city: Amsterdam,
country: The Netherlands
},
IgnoreCase: false,
Regex: false
}
}
}
}
]

View File

@@ -13,6 +13,7 @@ using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
using WireMock.Serialization;
using WireMock.Settings;
using WireMock.Types;
using WireMock.Util;
using Xunit;
@@ -73,6 +74,25 @@ public class MappingBuilderTests
.RespondWith(Response.Create()
.WithBody(@"{ msg: ""Hello world!""}")
);
_sut.Given(Request.Create()
.WithPath("/users/post1")
.UsingPost()
.WithBodyAsJson(new
{
Request = "Hello?"
})
).RespondWith(Response.Create());
_sut.Given(Request.Create()
.WithPath("/users/post2")
.UsingPost()
.WithBody(new JsonMatcher(new
{
city = "Amsterdam",
country = "The Netherlands"
}))
).RespondWith(Response.Create());
}
[Fact]
@@ -95,6 +115,26 @@ public class MappingBuilderTests
return Verifier.VerifyJson(json, VerifySettings);
}
[Fact]
public Task ToCSharpCode_Server()
{
// Act
var code = _sut.ToCSharpCode(MappingConverterType.Server);
// Verify
return Verifier.Verify(code, VerifySettings);
}
[Fact]
public Task ToCSharpCode_Builder()
{
// Act
var code = _sut.ToCSharpCode(MappingConverterType.Builder);
// Verify
return Verifier.Verify(code, VerifySettings);
}
[Fact]
public void SaveMappingsToFile_FolderExists_IsFalse()
{
@@ -141,9 +181,9 @@ public class MappingBuilderTests
_sut.SaveMappingsToFolder(null);
// Verify
_fileSystemHandlerMock.Verify(fs => fs.GetMappingFolder(), Times.Once);
_fileSystemHandlerMock.Verify(fs => fs.FolderExists(mappingFolder), Times.Once);
_fileSystemHandlerMock.Verify(fs => fs.WriteMappingFile(It.IsAny<string>(), It.IsAny<string>()), Times.Once);
_fileSystemHandlerMock.Verify(fs => fs.GetMappingFolder(), Times.Exactly(2));
_fileSystemHandlerMock.Verify(fs => fs.FolderExists(mappingFolder), Times.Exactly(2));
_fileSystemHandlerMock.Verify(fs => fs.WriteMappingFile(It.IsAny<string>(), It.IsAny<string>()), Times.Exactly(2));
_fileSystemHandlerMock.VerifyNoOtherCalls();
}
@@ -159,8 +199,8 @@ public class MappingBuilderTests
// Verify
_fileSystemHandlerMock.Verify(fs => fs.GetMappingFolder(), Times.Never);
_fileSystemHandlerMock.Verify(fs => fs.FolderExists(path), Times.Once);
_fileSystemHandlerMock.Verify(fs => fs.WriteMappingFile(It.IsAny<string>(), It.IsAny<string>()), Times.Once);
_fileSystemHandlerMock.Verify(fs => fs.FolderExists(path), Times.Exactly(2));
_fileSystemHandlerMock.Verify(fs => fs.WriteMappingFile(It.IsAny<string>(), It.IsAny<string>()), Times.Exactly(2));
_fileSystemHandlerMock.VerifyNoOtherCalls();
}
}

View File

@@ -499,4 +499,24 @@ public class WireMockServerAdminTests
Check.That(response.StatusCode).Equals(HttpStatusCode.OK);
Check.That(await response.Content.ReadAsStringAsync().ConfigureAwait(false)).Equals($"{{\"Status\":\"Mappings deleted. Affected GUIDs: [{guid1}, {guid2}]\"}}");
}
[Fact]
public async Task WireMockServer_Admin_()
{
// given
var server = WireMockServer.Start();
server.CreateClient();
// when
await new HttpClient().GetAsync("http://localhost:" + server.Ports[0] + "/foo").ConfigureAwait(false);
// then
Check.That(server.LogEntries).HasSize(1);
var requestLogged = server.LogEntries.First();
Check.That(requestLogged.RequestMessage.Method).IsEqualTo("GET");
Check.That(requestLogged.RequestMessage.BodyData).IsNull();
server.Stop();
}
}

View File

@@ -967,44 +967,4 @@ public class WireMockServerProxyTests
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);
}
}