Skip to content

Commit 18a3b1b

Browse files
committed
Fix MappingConverter
1 parent a4c7f34 commit 18a3b1b

8 files changed

+246
-76
lines changed

src/WireMock.Net/Serialization/MappingConverter.cs

+11-15
Original file line numberDiff line numberDiff line change
@@ -143,25 +143,21 @@ public string ToCSharpCode(IMapping mapping, MappingConverterSettings? settings
143143

144144
if (requestMessageBodyMatcher is { Matchers: { } })
145145
{
146-
if (requestMessageBodyMatcher.Matchers.OfType<WildcardMatcher>().FirstOrDefault() is { } wildcardMatcher && wildcardMatcher.GetPatterns().Any())
146+
var firstMatcher = requestMessageBodyMatcher.Matchers.FirstOrDefault();
147+
148+
if (firstMatcher is WildcardMatcher wildcardMatcher && wildcardMatcher.GetPatterns().Any())
147149
{
148150
sb.AppendLine($" .WithBody({GetString(wildcardMatcher)})");
149151
}
150-
else if (requestMessageBodyMatcher.Matchers.OfType<JsonPartialMatcher>().FirstOrDefault() is { Value: { } } jsonPartialMatcher)
151-
{
152-
sb.AppendLine(@$" .WithBody(new JsonPartialMatcher(
153-
value: {ToCSharpStringLiteral(jsonPartialMatcher.Value.ToString())},
154-
ignoreCase: {ToCSharpBooleanLiteral(jsonPartialMatcher.IgnoreCase)},
155-
regex: {ToCSharpBooleanLiteral(jsonPartialMatcher.Regex)}
156-
))");
157-
}
158-
else if (requestMessageBodyMatcher.Matchers.OfType<JsonPartialWildcardMatcher>().FirstOrDefault() is { Value: { } } jsonPartialWildcardMatcher)
152+
153+
if (firstMatcher is JsonMatcher jsonMatcher)
159154
{
160-
sb.AppendLine(@$" .WithBody(new JsonPartialWildcardMatcher(
161-
value: {ToCSharpStringLiteral(jsonPartialWildcardMatcher.Value.ToString())},
162-
ignoreCase: {ToCSharpBooleanLiteral(jsonPartialWildcardMatcher.IgnoreCase)},
163-
regex: {ToCSharpBooleanLiteral(jsonPartialWildcardMatcher.Regex)}
164-
))");
155+
var matcherType = jsonMatcher.GetType().Name;
156+
sb.AppendLine($" .WithBody(new {matcherType}(");
157+
sb.AppendLine($" value: {ConvertToAnonymousObjectDefinition(jsonMatcher.Value, 3)},");
158+
sb.AppendLine($" ignoreCase: {ToCSharpBooleanLiteral(jsonMatcher.IgnoreCase)},");
159+
sb.AppendLine($" regex: {ToCSharpBooleanLiteral(jsonMatcher.Regex)}");
160+
sb.AppendLine(@" ))");
165161
}
166162
}
167163

src/WireMock.Net/Server/WireMockServer.Admin.cs

+2-4
Original file line numberDiff line numberDiff line change
@@ -344,15 +344,13 @@ private IResponseMessage MappingCodeGet(IRequestMessage requestMessage)
344344

345345
private static MappingConverterType GetMappingConverterType(IRequestMessage requestMessage)
346346
{
347-
var mappingConverterType = MappingConverterType.Server;
348-
349347
if (requestMessage.QueryIgnoreCase?.TryGetValue(nameof(MappingConverterType), out var values) == true &&
350348
Enum.TryParse(values.FirstOrDefault(), true, out MappingConverterType parsed))
351349
{
352-
mappingConverterType = parsed;
350+
return parsed;
353351
}
354352

355-
return mappingConverterType;
353+
return MappingConverterType.Server;
356354
}
357355

358356
private IMapping? FindMappingByGuid(IRequestMessage requestMessage)

src/WireMock.Net/Util/CSharpFormatter.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ namespace WireMock.Util;
1010

1111
internal static class CSharpFormatter
1212
{
13+
private const string Null = "null";
14+
1315
#region Reserved Keywords
1416
private static readonly HashSet<string> CSharpReservedKeywords = new(new[]
1517
{
@@ -92,17 +94,15 @@ internal static class CSharpFormatter
9294
"while"
9395
});
9496
#endregion
95-
96-
private const string Null = "null";
97-
98-
public static object ConvertToAnonymousObjectDefinition(object jsonBody)
97+
98+
public static object ConvertToAnonymousObjectDefinition(object jsonBody, int ind = 2)
9999
{
100100
var serializedBody = JsonConvert.SerializeObject(jsonBody);
101101
using var jsonReader = new JsonTextReader(new StringReader(serializedBody));
102102
jsonReader.DateParseHandling = DateParseHandling.None;
103103
var deserializedBody = JObject.Load(jsonReader);
104104

105-
return ConvertJsonToAnonymousObjectDefinition(deserializedBody, 2);
105+
return ConvertJsonToAnonymousObjectDefinition(deserializedBody, ind);
106106
}
107107

108108
public static string ConvertJsonToAnonymousObjectDefinition(JToken token, int ind = 0)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
var server = WireMockServer.Start();
2+
server
3+
.Given(Request.Create()
4+
.UsingMethod("POST")
5+
.WithPath("/users/post1")
6+
.WithBody(new JsonMatcher(
7+
value: new
8+
{
9+
city = "Amsterdam",
10+
country = "The Netherlands"
11+
},
12+
ignoreCase: false,
13+
regex: false
14+
))
15+
)
16+
.WithGuid("90356dba-b36c-469a-a17e-669cd84f1f05")
17+
.RespondWith(Response.Create()
18+
);
19+
20+
server
21+
.Given(Request.Create()
22+
.UsingMethod("POST")
23+
.WithPath("/users/post2")
24+
.WithBody(new JsonPartialMatcher(
25+
value: new
26+
{
27+
city = "City",
28+
country = "Country"
29+
},
30+
ignoreCase: false,
31+
regex: false
32+
))
33+
)
34+
.WithGuid("1b731398-4a5b-457f-a6e3-d65e541c428f")
35+
.RespondWith(Response.Create()
36+
.WithBody(@"Line1
37+
Some ""value"" in Line2")
38+
);
39+
40+
server
41+
.Given(Request.Create()
42+
.UsingMethod("GET")
43+
.WithPath("/foo1")
44+
.WithParam("p1", "xyz")
45+
)
46+
.WithGuid("f74fd144-df53-404f-8e35-da22a640bd5f")
47+
.RespondWith(Response.Create()
48+
.WithStatusCode(200)
49+
.WithBody("1")
50+
);
51+
52+
server
53+
.Given(Request.Create()
54+
.UsingMethod("POST")
55+
.WithPath("/foo2")
56+
.WithParam("p2", "abc")
57+
.WithHeader("h1", "W/\"234f2q3r\"", true)
58+
)
59+
.WithGuid("4126dec8-470b-4eff-93bb-c24f83b8b1fd")
60+
.RespondWith(Response.Create()
61+
.WithStatusCode("201")
62+
.WithHeader("hk", "hv")
63+
.WithHeader("ETag", "W/\"168d8e\"")
64+
.WithBody("2")
65+
);
66+
67+
server
68+
.Given(Request.Create()
69+
.UsingMethod("DELETE")
70+
.WithUrl("https://localhost/test")
71+
)
72+
.WithGuid("c9929240-7ae8-4a5d-8ed8-0913479f6eeb")
73+
.RespondWith(Response.Create()
74+
.WithStatusCode(208)
75+
.WithBodyAsJson(new
76+
{
77+
@as = 1,
78+
b = 1.2,
79+
d = true,
80+
e = false,
81+
f = new [] { 1, 2, 3, 4 },
82+
g = new
83+
{
84+
z1 = 1,
85+
z2 = 2,
86+
z3 = new [] { "a", "b", "c" },
87+
z4 = new []
88+
{
89+
new
90+
{
91+
a = 1,
92+
b = 2
93+
},
94+
new
95+
{
96+
a = 2,
97+
b = 3
98+
}
99+
}
100+
},
101+
date_field = "2023-05-08T11:20:19",
102+
string_field_with_date = "2021-03-13T21:04:00Z",
103+
multiline_text = @"This
104+
is
105+
multiline
106+
text
107+
"
108+
})
109+
);
110+

test/WireMock.Net.Tests/AdminApi/WireMockAdminApiTests.cs

+37-52
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ public async Task IWireMockAdminApi_GetMappingAsync_WithProxy_And_ProxyUrlReplac
491491

492492
server.Stop();
493493
}
494-
494+
495495
[Fact]
496496
public async Task IWireMockAdminApi_GetRequestsAsync_Json()
497497
{
@@ -854,28 +854,53 @@ public async Task IWireMockAdminApi_GetMappingCodeByGuidAsync()
854854
server.Stop();
855855
}
856856

857-
[Theory]
858-
[InlineData(MappingConverterType.Server)]
859-
[InlineData(MappingConverterType.Builder)]
860-
public async Task IWireMockAdminApi_GetMappingsCode(MappingConverterType mappingConverterType)
857+
[Fact]
858+
public async Task IWireMockAdminApi_GetMappingsCode()
861859
{
862860
// Arrange
863861
var guid1 = Guid.Parse("90356dba-b36c-469a-a17e-669cd84f1f05");
864862
var guid2 = Guid.Parse("1b731398-4a5b-457f-a6e3-d65e541c428f");
865863
var guid3 = Guid.Parse("f74fd144-df53-404f-8e35-da22a640bd5f");
866864
var guid4 = Guid.Parse("4126DEC8-470B-4EFF-93BB-C24F83B8B1FD");
867865
var guid5 = Guid.Parse("c9929240-7ae8-4a5d-8ed8-0913479f6eeb");
868-
var guid6 = Guid.Parse("397f64ea-b36c-496a-9a32-b96988194724");
869866
var server = WireMockServer.StartWithAdminInterface();
870867

868+
server
869+
.Given(
870+
Request.Create()
871+
.WithPath("/users/post1")
872+
.UsingPost()
873+
.WithBody(new JsonMatcher(new
874+
{
875+
city = "Amsterdam",
876+
country = "The Netherlands"
877+
}))
878+
)
879+
.WithGuid(guid1)
880+
.RespondWith(Response.Create());
881+
882+
server
883+
.Given(
884+
Request.Create()
885+
.WithPath("/users/post2")
886+
.UsingPost()
887+
.WithBody(new JsonPartialMatcher(new
888+
{
889+
city = "City",
890+
country = "Country"
891+
}))
892+
)
893+
.WithGuid(guid2)
894+
.RespondWith(Response.Create().WithBody("Line1\r\nSome \"value\" in Line2"));
895+
871896
server
872897
.Given(
873898
Request.Create()
874899
.WithPath("/foo1")
875900
.WithParam("p1", "xyz")
876901
.UsingGet()
877902
)
878-
.WithGuid(guid1)
903+
.WithGuid(guid3)
879904
.RespondWith(
880905
Response.Create()
881906
.WithStatusCode(200)
@@ -890,7 +915,7 @@ public async Task IWireMockAdminApi_GetMappingsCode(MappingConverterType mapping
890915
.WithHeader("h1", "W/\"234f2q3r\"")
891916
.UsingPost()
892917
)
893-
.WithGuid(guid2)
918+
.WithGuid(guid4)
894919
.RespondWith(
895920
Response.Create()
896921
.WithStatusCode("201")
@@ -899,33 +924,6 @@ public async Task IWireMockAdminApi_GetMappingsCode(MappingConverterType mapping
899924
.WithBody("2")
900925
);
901926

902-
server
903-
.Given(
904-
Request.Create()
905-
.WithPath("/users/post1")
906-
.UsingPost()
907-
.WithBodyAsJson(new
908-
{
909-
Request = "Hello?"
910-
})
911-
)
912-
.WithGuid(guid3)
913-
.RespondWith(Response.Create());
914-
915-
server
916-
.Given(
917-
Request.Create()
918-
.WithPath("/users/post2")
919-
.UsingPost()
920-
.WithBody(new JsonMatcher(new
921-
{
922-
city = "Amsterdam",
923-
country = "The Netherlands"
924-
}))
925-
)
926-
.WithGuid(guid4)
927-
.RespondWith(Response.Create());
928-
929927
server
930928
.Given(
931929
Request.Create()
@@ -960,30 +958,17 @@ public async Task IWireMockAdminApi_GetMappingsCode(MappingConverterType mapping
960958
is
961959
multiline
962960
text
963-
" })
964-
);
965-
966-
server
967-
.Given(
968-
Request.Create()
969-
.WithPath("/foo3")
970-
.WithBody(new JsonPartialMatcher(new { a = 1, b = 2 }))
971-
.UsingPost()
972-
)
973-
.WithGuid(guid6)
974-
.RespondWith(
975-
Response.Create()
976-
.WithStatusCode(200)
977-
.WithBody("Line1\r\nSome \"value\" in Line2")
961+
"
962+
})
978963
);
979964

980965
// Act
981966
var api = RestClient.For<IWireMockAdminApi>(server.Url);
982967

983968
var mappings = await api.GetMappingsAsync().ConfigureAwait(false);
984-
mappings.Should().HaveCount(6);
969+
mappings.Should().HaveCount(5);
985970

986-
var code = await api.GetMappingsCodeAsync(mappingConverterType).ConfigureAwait(false);
971+
var code = await api.GetMappingsCodeAsync().ConfigureAwait(false);
987972

988973
// Assert
989974
await Verifier.Verify(code).DontScrubDateTimes().DontScrubGuids();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
var builder = new MappingBuilder();
2+
builder
3+
.Given(Request.Create()
4+
.UsingMethod("GET")
5+
.WithPath("/foo")
6+
.WithParam("test", "it.Length < 10")
7+
)
8+
.WithGuid("41372914-1838-4c67-916b-b9aacdd096ce")
9+
.RespondWith(Response.Create()
10+
.WithBody("{ msg: \"Hello world!\"}")
11+
);
12+
13+
builder
14+
.Given(Request.Create()
15+
.UsingMethod("POST")
16+
.WithPath("/users/post2")
17+
.WithBody(new JsonMatcher(
18+
value: new
19+
{
20+
city = "Amsterdam",
21+
country = "The Netherlands"
22+
},
23+
ignoreCase: false,
24+
regex: false
25+
))
26+
)
27+
.WithGuid("98fae52e-76df-47d9-876f-2ee32e931d9b")
28+
.RespondWith(Response.Create()
29+
);
30+

0 commit comments

Comments
 (0)