mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-17 06:29:57 +02:00
Code generator improvements (#940)
* Fix quotation marks escaping in multiline string * Add support for JsonPartialMatcher and JsonPartialWildcardMatcher in mapping code generator
This commit is contained in:
@@ -107,11 +107,28 @@ internal class MappingConverter
|
|||||||
|
|
||||||
if (bodyMatcher is { Matchers: { } })
|
if (bodyMatcher is { Matchers: { } })
|
||||||
{
|
{
|
||||||
var wildcardMatcher = bodyMatcher.Matchers.OfType<WildcardMatcher>().FirstOrDefault();
|
if (bodyMatcher.Matchers.OfType<WildcardMatcher>().FirstOrDefault() is { } wildcardMatcher && wildcardMatcher.GetPatterns().Any())
|
||||||
if (wildcardMatcher is { } && wildcardMatcher.GetPatterns().Any())
|
|
||||||
{
|
{
|
||||||
sb.AppendLine($" .WithBody({GetString(wildcardMatcher)})");
|
sb.AppendLine($" .WithBody({GetString(wildcardMatcher)})");
|
||||||
}
|
}
|
||||||
|
else if (bodyMatcher.Matchers.OfType<JsonPartialMatcher>().FirstOrDefault() is { Value: { } } jsonPartialMatcher)
|
||||||
|
{
|
||||||
|
sb.AppendLine(@$" .WithBody(new JsonPartialMatcher(
|
||||||
|
value: {ToCSharpStringLiteral(jsonPartialMatcher.Value.ToString())},
|
||||||
|
ignoreCase: {ToCSharpBooleanLiteral(jsonPartialMatcher.IgnoreCase)},
|
||||||
|
throwException: {ToCSharpBooleanLiteral(jsonPartialMatcher.ThrowException)},
|
||||||
|
regex: {ToCSharpBooleanLiteral(jsonPartialMatcher.Regex)}
|
||||||
|
))");
|
||||||
|
}
|
||||||
|
else if (bodyMatcher.Matchers.OfType<JsonPartialWildcardMatcher>().FirstOrDefault() is { Value: { } } jsonPartialWildcardMatcher)
|
||||||
|
{
|
||||||
|
sb.AppendLine(@$" .WithBody(new JsonPartialWildcardMatcher(
|
||||||
|
value: {ToCSharpStringLiteral(jsonPartialWildcardMatcher.Value.ToString())},
|
||||||
|
ignoreCase: {ToCSharpBooleanLiteral(jsonPartialWildcardMatcher.IgnoreCase)},
|
||||||
|
throwException: {ToCSharpBooleanLiteral(jsonPartialWildcardMatcher.ThrowException)},
|
||||||
|
regex: {ToCSharpBooleanLiteral(jsonPartialWildcardMatcher.Regex)}
|
||||||
|
))");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sb.AppendLine(@" )");
|
sb.AppendLine(@" )");
|
||||||
|
|||||||
@@ -129,15 +129,25 @@ internal static class CSharpFormatter
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static string ToCSharpBooleanLiteral(bool value) => value ? "true" : "false";
|
||||||
|
|
||||||
public static string ToCSharpStringLiteral(string? value)
|
public static string ToCSharpStringLiteral(string? value)
|
||||||
{
|
{
|
||||||
var escapedValue = value?.Replace("\"", "\\\"") ?? string.Empty;
|
if (string.IsNullOrEmpty(value))
|
||||||
if (escapedValue.Contains("\n"))
|
|
||||||
{
|
{
|
||||||
return $"@\"{escapedValue}\"";
|
return "\"\"";
|
||||||
}
|
}
|
||||||
|
|
||||||
return $"\"{escapedValue}\"";
|
if (value.Contains("\n"))
|
||||||
|
{
|
||||||
|
var escapedValue = value?.Replace("\"", "\"\"") ?? string.Empty;
|
||||||
|
return $"@\"{escapedValue}\"";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var escapedValue = value?.Replace("\"", "\\\"") ?? string.Empty;
|
||||||
|
return $"\"{escapedValue}\"";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static string FormatPropertyName(string propertyName)
|
public static string FormatPropertyName(string propertyName)
|
||||||
|
|||||||
@@ -70,3 +70,21 @@ text
|
|||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
|
server
|
||||||
|
.Given(Request.Create()
|
||||||
|
.UsingMethod("POST")
|
||||||
|
.WithPath("/foo3")
|
||||||
|
.WithBody(new JsonPartialMatcher(
|
||||||
|
value: "{ a = 1, b = 2 }",
|
||||||
|
ignoreCase: false,
|
||||||
|
throwException: false,
|
||||||
|
regex: false
|
||||||
|
))
|
||||||
|
)
|
||||||
|
.WithGuid("4126dec8-470b-4eff-93bb-c24f83b8b1fd")
|
||||||
|
.RespondWith(Response.Create()
|
||||||
|
.WithStatusCode(200)
|
||||||
|
.WithBody(@"Line1
|
||||||
|
Some ""value"" in Line2")
|
||||||
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ using WireMock.Admin.Settings;
|
|||||||
using WireMock.Client;
|
using WireMock.Client;
|
||||||
using WireMock.Handlers;
|
using WireMock.Handlers;
|
||||||
using WireMock.Logging;
|
using WireMock.Logging;
|
||||||
|
using WireMock.Matchers;
|
||||||
using WireMock.Models;
|
using WireMock.Models;
|
||||||
using WireMock.Net.Tests.VerifyExtensions;
|
using WireMock.Net.Tests.VerifyExtensions;
|
||||||
using WireMock.RequestBuilders;
|
using WireMock.RequestBuilders;
|
||||||
@@ -719,6 +720,7 @@ public class WireMockAdminApiTests
|
|||||||
var guid1 = Guid.Parse("90356dba-b36c-469a-a17e-669cd84f1f05");
|
var guid1 = Guid.Parse("90356dba-b36c-469a-a17e-669cd84f1f05");
|
||||||
var guid2 = Guid.Parse("1b731398-4a5b-457f-a6e3-d65e541c428f");
|
var guid2 = Guid.Parse("1b731398-4a5b-457f-a6e3-d65e541c428f");
|
||||||
var guid3 = Guid.Parse("f74fd144-df53-404f-8e35-da22a640bd5f");
|
var guid3 = Guid.Parse("f74fd144-df53-404f-8e35-da22a640bd5f");
|
||||||
|
var guid4 = Guid.Parse("4126DEC8-470B-4EFF-93BB-C24F83B8B1FD");
|
||||||
var server = WireMockServer.StartWithAdminInterface();
|
var server = WireMockServer.StartWithAdminInterface();
|
||||||
|
|
||||||
server
|
server
|
||||||
@@ -769,11 +771,25 @@ 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
|
// Act
|
||||||
var api = RestClient.For<IWireMockAdminApi>(server.Url);
|
var api = RestClient.For<IWireMockAdminApi>(server.Url);
|
||||||
|
|
||||||
var mappings = await api.GetMappingsAsync().ConfigureAwait(false);
|
var mappings = await api.GetMappingsAsync().ConfigureAwait(false);
|
||||||
mappings.Should().HaveCount(3);
|
mappings.Should().HaveCount(4);
|
||||||
|
|
||||||
var code = await api.GetMappingsCodeAsync().ConfigureAwait(false);
|
var code = await api.GetMappingsCodeAsync().ConfigureAwait(false);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user