From 18a3b1b41f6f9249aa4747dd5753b0be9622351b Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Wed, 8 May 2024 17:08:46 +0200 Subject: [PATCH] Fix MappingConverter --- .../Serialization/MappingConverter.cs | 26 ++--- .../Server/WireMockServer.Admin.cs | 6 +- src/WireMock.Net/Util/CSharpFormatter.cs | 10 +- ...eMockAdminApi_GetMappingsCode.verified.txt | 110 ++++++++++++++++++ .../AdminApi/WireMockAdminApiTests.cs | 89 ++++++-------- ...derTests.ToCSharpCode_Builder.verified.txt | 30 +++++ ...lderTests.ToCSharpCode_Server.verified.txt | 30 +++++ .../WireMock.Net.Tests/MappingBuilderTests.cs | 21 ++++ 8 files changed, 246 insertions(+), 76 deletions(-) create mode 100644 test/WireMock.Net.Tests/AdminApi/WireMockAdminApiTests.IWireMockAdminApi_GetMappingsCode.verified.txt create mode 100644 test/WireMock.Net.Tests/MappingBuilderTests.ToCSharpCode_Builder.verified.txt create mode 100644 test/WireMock.Net.Tests/MappingBuilderTests.ToCSharpCode_Server.verified.txt diff --git a/src/WireMock.Net/Serialization/MappingConverter.cs b/src/WireMock.Net/Serialization/MappingConverter.cs index a83eabf1c..27676b249 100644 --- a/src/WireMock.Net/Serialization/MappingConverter.cs +++ b/src/WireMock.Net/Serialization/MappingConverter.cs @@ -143,25 +143,21 @@ public string ToCSharpCode(IMapping mapping, MappingConverterSettings? settings if (requestMessageBodyMatcher is { Matchers: { } }) { - if (requestMessageBodyMatcher.Matchers.OfType().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().FirstOrDefault() is { Value: { } } jsonPartialMatcher) - { - sb.AppendLine(@$" .WithBody(new JsonPartialMatcher( - value: {ToCSharpStringLiteral(jsonPartialMatcher.Value.ToString())}, - ignoreCase: {ToCSharpBooleanLiteral(jsonPartialMatcher.IgnoreCase)}, - regex: {ToCSharpBooleanLiteral(jsonPartialMatcher.Regex)} - ))"); - } - else if (requestMessageBodyMatcher.Matchers.OfType().FirstOrDefault() is { Value: { } } jsonPartialWildcardMatcher) + + if (firstMatcher is JsonMatcher jsonMatcher) { - 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(@" ))"); } } diff --git a/src/WireMock.Net/Server/WireMockServer.Admin.cs b/src/WireMock.Net/Server/WireMockServer.Admin.cs index 01b436833..afb725182 100644 --- a/src/WireMock.Net/Server/WireMockServer.Admin.cs +++ b/src/WireMock.Net/Server/WireMockServer.Admin.cs @@ -344,15 +344,13 @@ private IResponseMessage MappingCodeGet(IRequestMessage requestMessage) 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) diff --git a/src/WireMock.Net/Util/CSharpFormatter.cs b/src/WireMock.Net/Util/CSharpFormatter.cs index 63a17a205..425c8e4dc 100644 --- a/src/WireMock.Net/Util/CSharpFormatter.cs +++ b/src/WireMock.Net/Util/CSharpFormatter.cs @@ -10,6 +10,8 @@ namespace WireMock.Util; internal static class CSharpFormatter { + private const string Null = "null"; + #region Reserved Keywords private static readonly HashSet 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) diff --git a/test/WireMock.Net.Tests/AdminApi/WireMockAdminApiTests.IWireMockAdminApi_GetMappingsCode.verified.txt b/test/WireMock.Net.Tests/AdminApi/WireMockAdminApiTests.IWireMockAdminApi_GetMappingsCode.verified.txt new file mode 100644 index 000000000..d424aca8c --- /dev/null +++ b/test/WireMock.Net.Tests/AdminApi/WireMockAdminApiTests.IWireMockAdminApi_GetMappingsCode.verified.txt @@ -0,0 +1,110 @@ +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("f74fd144-df53-404f-8e35-da22a640bd5f") + .RespondWith(Response.Create() + .WithStatusCode(200) + .WithBody("1") + ); + +server + .Given(Request.Create() + .UsingMethod("POST") + .WithPath("/foo2") + .WithParam("p2", "abc") + .WithHeader("h1", "W/\"234f2q3r\"", true) + ) + .WithGuid("4126dec8-470b-4eff-93bb-c24f83b8b1fd") + .RespondWith(Response.Create() + .WithStatusCode("201") + .WithHeader("hk", "hv") + .WithHeader("ETag", "W/\"168d8e\"") + .WithBody("2") + ); + +server + .Given(Request.Create() + .UsingMethod("DELETE") + .WithUrl("https://localhost/test") + ) + .WithGuid("c9929240-7ae8-4a5d-8ed8-0913479f6eeb") + .RespondWith(Response.Create() + .WithStatusCode(208) + .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 = "2023-05-08T11:20:19", + string_field_with_date = "2021-03-13T21:04:00Z", + multiline_text = @"This +is +multiline +text +" + }) + ); + diff --git a/test/WireMock.Net.Tests/AdminApi/WireMockAdminApiTests.cs b/test/WireMock.Net.Tests/AdminApi/WireMockAdminApiTests.cs index 30c1a12b6..dde12f4e1 100644 --- a/test/WireMock.Net.Tests/AdminApi/WireMockAdminApiTests.cs +++ b/test/WireMock.Net.Tests/AdminApi/WireMockAdminApiTests.cs @@ -491,7 +491,7 @@ public async Task IWireMockAdminApi_GetMappingAsync_WithProxy_And_ProxyUrlReplac server.Stop(); } - + [Fact] public async Task IWireMockAdminApi_GetRequestsAsync_Json() { @@ -854,10 +854,8 @@ public async Task IWireMockAdminApi_GetMappingCodeByGuidAsync() server.Stop(); } - [Theory] - [InlineData(MappingConverterType.Server)] - [InlineData(MappingConverterType.Builder)] - public async Task IWireMockAdminApi_GetMappingsCode(MappingConverterType mappingConverterType) + [Fact] + public async Task IWireMockAdminApi_GetMappingsCode() { // Arrange var guid1 = Guid.Parse("90356dba-b36c-469a-a17e-669cd84f1f05"); @@ -865,9 +863,36 @@ public async Task IWireMockAdminApi_GetMappingsCode(MappingConverterType mapping 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 guid6 = Guid.Parse("397f64ea-b36c-496a-9a32-b96988194724"); 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() @@ -875,7 +900,7 @@ public async Task IWireMockAdminApi_GetMappingsCode(MappingConverterType mapping .WithParam("p1", "xyz") .UsingGet() ) - .WithGuid(guid1) + .WithGuid(guid3) .RespondWith( Response.Create() .WithStatusCode(200) @@ -890,7 +915,7 @@ public async Task IWireMockAdminApi_GetMappingsCode(MappingConverterType mapping .WithHeader("h1", "W/\"234f2q3r\"") .UsingPost() ) - .WithGuid(guid2) + .WithGuid(guid4) .RespondWith( Response.Create() .WithStatusCode("201") @@ -899,33 +924,6 @@ public async Task IWireMockAdminApi_GetMappingsCode(MappingConverterType mapping .WithBody("2") ); - server - .Given( - Request.Create() - .WithPath("/users/post1") - .UsingPost() - .WithBodyAsJson(new - { - Request = "Hello?" - }) - ) - .WithGuid(guid3) - .RespondWith(Response.Create()); - - server - .Given( - Request.Create() - .WithPath("/users/post2") - .UsingPost() - .WithBody(new JsonMatcher(new - { - city = "Amsterdam", - country = "The Netherlands" - })) - ) - .WithGuid(guid4) - .RespondWith(Response.Create()); - server .Given( Request.Create() @@ -960,30 +958,17 @@ public async Task IWireMockAdminApi_GetMappingsCode(MappingConverterType mapping is multiline text -" }) - ); - - server - .Given( - Request.Create() - .WithPath("/foo3") - .WithBody(new JsonPartialMatcher(new { a = 1, b = 2 })) - .UsingPost() - ) - .WithGuid(guid6) - .RespondWith( - Response.Create() - .WithStatusCode(200) - .WithBody("Line1\r\nSome \"value\" in Line2") +" + }) ); // Act var api = RestClient.For(server.Url); var mappings = await api.GetMappingsAsync().ConfigureAwait(false); - mappings.Should().HaveCount(6); + mappings.Should().HaveCount(5); - var code = await api.GetMappingsCodeAsync(mappingConverterType).ConfigureAwait(false); + var code = await api.GetMappingsCodeAsync().ConfigureAwait(false); // Assert await Verifier.Verify(code).DontScrubDateTimes().DontScrubGuids(); diff --git a/test/WireMock.Net.Tests/MappingBuilderTests.ToCSharpCode_Builder.verified.txt b/test/WireMock.Net.Tests/MappingBuilderTests.ToCSharpCode_Builder.verified.txt new file mode 100644 index 000000000..be439d3bb --- /dev/null +++ b/test/WireMock.Net.Tests/MappingBuilderTests.ToCSharpCode_Builder.verified.txt @@ -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() + ); + diff --git a/test/WireMock.Net.Tests/MappingBuilderTests.ToCSharpCode_Server.verified.txt b/test/WireMock.Net.Tests/MappingBuilderTests.ToCSharpCode_Server.verified.txt new file mode 100644 index 000000000..6091acbd2 --- /dev/null +++ b/test/WireMock.Net.Tests/MappingBuilderTests.ToCSharpCode_Server.verified.txt @@ -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() + ); + diff --git a/test/WireMock.Net.Tests/MappingBuilderTests.cs b/test/WireMock.Net.Tests/MappingBuilderTests.cs index e5e5af60d..f07708549 100644 --- a/test/WireMock.Net.Tests/MappingBuilderTests.cs +++ b/test/WireMock.Net.Tests/MappingBuilderTests.cs @@ -13,6 +13,7 @@ using WireMock.ResponseBuilders; using WireMock.Serialization; using WireMock.Settings; +using WireMock.Types; using WireMock.Util; using Xunit; @@ -114,6 +115,26 @@ public Task ToJson() 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() {