Compare commits

..

5 Commits

Author SHA1 Message Date
Stef Heyenrath
71393204cc 1.5.26 2023-05-25 21:36:01 +02:00
Cezary Piątek
e5cc6f570c Code generator improvements (#940)
* Fix quotation marks escaping in multiline string

* Add support for JsonPartialMatcher and JsonPartialWildcardMatcher in mapping code generator
2023-05-25 20:59:13 +02:00
Stef Heyenrath
7c3a0c815d Add GetParameter method to IRequestMessage (#942) 2023-05-25 15:14:02 +02:00
Stef Heyenrath
e61f08fe48 WireMockMiddleware should use HandleRequestsSynchronously correctly (#939) 2023-05-19 21:43:14 +02:00
Stef Heyenrath
11f4c47851 Add more unitests for CSharpFormatter utils (#938)
* Add unit-tests for CSharpFormatter

* .

* t
2023-05-19 16:14:26 +02:00
13 changed files with 205 additions and 24 deletions

View File

@@ -1,3 +1,10 @@
# 1.5.26 (25 May 2023)
- [#938](https://github.com/WireMock-Net/WireMock.Net/pull/938) - Add more unitests for CSharpFormatter utils [test] contributed by [StefH](https://github.com/StefH)
- [#939](https://github.com/WireMock-Net/WireMock.Net/pull/939) - WireMockMiddleware should use HandleRequestsSynchronously correctly [bug] contributed by [StefH](https://github.com/StefH)
- [#940](https://github.com/WireMock-Net/WireMock.Net/pull/940) - Code generator improvements contributed by [cezarypiatek](https://github.com/cezarypiatek)
- [#942](https://github.com/WireMock-Net/WireMock.Net/pull/942) - Add GetParameter method to IRequestMessage [feature] contributed by [StefH](https://github.com/StefH)
- [#941](https://github.com/WireMock-Net/WireMock.Net/issues/941) - RequestMessage.GetParameter method missing from IRequestMessage interface [feature]
# 1.5.25 (13 May 2023)
- [#934](https://github.com/WireMock-Net/WireMock.Net/pull/934) - Code generator improvements [feature] contributed by [cezarypiatek](https://github.com/cezarypiatek)

View File

@@ -4,7 +4,7 @@
</PropertyGroup>
<PropertyGroup>
<VersionPrefix>1.5.25</VersionPrefix>
<VersionPrefix>1.5.26</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.25
SET version=1.5.26
GitHubReleaseNotes --output CHANGELOG.md --skip-empty-releases --exclude-labels question invalid doc duplicate --version %version% --token %GH_TOKEN%

View File

@@ -1,4 +1,8 @@
# 1.5.25 (13 May 2023)
- #934 Code generator improvements [feature]
# 1.5.26 (25 May 2023)
- #938 Add more unitests for CSharpFormatter utils [test]
- #939 WireMockMiddleware should use HandleRequestsSynchronously correctly [bug]
- #940 Code generator improvements
- #942 Add GetParameter method to IRequestMessage [feature]
- #941 RequestMessage.GetParameter method missing from IRequestMessage interface [feature]
The full release notes can be found here: https://github.com/WireMock-Net/WireMock.Net/blob/master/CHANGELOG.md

View File

@@ -143,6 +143,14 @@ public interface IRequestMessage
/// </summary>
string Origin { get; }
/// <summary>
/// Get a query parameter.
/// </summary>
/// <param name="key">The key.</param>
/// <param name="ignoreCase">Defines if the key should be matched using case-ignore.</param>
/// <returns>The query parameter value as WireMockList or null when not found.</returns>
WireMockList<string>? GetParameter(string key, bool ignoreCase = false);
#if NETSTANDARD1_3_OR_GREATER || NET461
/// <summary>
/// Gets the connection's client certificate

View File

@@ -60,7 +60,7 @@ namespace WireMock.Owin
public Task Invoke(IContext ctx)
#endif
{
if (_options.HandleRequestsSynchronously.GetValueOrDefault(true))
if (_options.HandleRequestsSynchronously.GetValueOrDefault(false))
{
lock (_lock)
{

View File

@@ -180,12 +180,7 @@ public class RequestMessage : IRequestMessage
#endif
}
/// <summary>
/// Get a query parameter.
/// </summary>
/// <param name="key">The key.</param>
/// <param name="ignoreCase">Defines if the key should be matched using case-ignore.</param>
/// <returns>The query parameter.</returns>
/// <inheritdoc />
public WireMockList<string>? GetParameter(string key, bool ignoreCase = false)
{
if (Query == null)

View File

@@ -107,11 +107,28 @@ internal class MappingConverter
if (bodyMatcher is { Matchers: { } })
{
var wildcardMatcher = bodyMatcher.Matchers.OfType<WildcardMatcher>().FirstOrDefault();
if (wildcardMatcher is { } && wildcardMatcher.GetPatterns().Any())
if (bodyMatcher.Matchers.OfType<WildcardMatcher>().FirstOrDefault() is { } wildcardMatcher && wildcardMatcher.GetPatterns().Any())
{
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(@" )");

View File

@@ -92,6 +92,7 @@ internal static class CSharpFormatter
"while"
});
#endregion
private const string Null = "null";
public static object ConvertToAnonymousObjectDefinition(object jsonBody)
@@ -104,7 +105,7 @@ internal static class CSharpFormatter
return ConvertJsonToAnonymousObjectDefinition(deserializedBody, 2);
}
private static string ConvertJsonToAnonymousObjectDefinition(JToken token, int ind = 0)
public static string ConvertJsonToAnonymousObjectDefinition(JToken token, int ind = 0)
{
return token switch
{
@@ -127,19 +128,29 @@ internal static class CSharpFormatter
_ => $"UNHANDLED_CASE: {token}"
};
}
public static string ToCSharpBooleanLiteral(bool value) => value ? "true" : "false";
public static string ToCSharpStringLiteral(string? value)
{
var escapedValue = value?.Replace("\"", "\\\"") ?? string.Empty;
if (escapedValue.Contains("\n"))
if (string.IsNullOrEmpty(value))
{
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}\"";
}
}
private static string FormatPropertyName(string propertyName)
public static string FormatPropertyName(string propertyName)
{
return CSharpReservedKeywords.Contains(propertyName) ? "@" + propertyName : propertyName;
}

View File

@@ -10,17 +10,17 @@ public class BytesEncodingUtilsTests
[Fact]
public void TryGetEncoding_UTF32()
{
var result = BytesEncodingUtils.TryGetEncoding(new byte[] { 0xff, 0xfe, 0x00, 0x00 }, out Encoding encoding);
var result = BytesEncodingUtils.TryGetEncoding(new byte[] { 0xff, 0xfe, 0x00, 0x00 }, out var encoding);
// Assert
result.Should().BeTrue();
encoding.CodePage.Should().Be(Encoding.UTF32.CodePage);
encoding?.CodePage.Should().Be(Encoding.UTF32.CodePage);
}
[Fact]
public void TryGetEncoding_Invalid()
{
var result = BytesEncodingUtils.TryGetEncoding(new byte[] { 0xff }, out Encoding encoding);
var result = BytesEncodingUtils.TryGetEncoding(new byte[] { 0xff }, out var encoding);
// Assert
result.Should().BeFalse();

View File

@@ -0,0 +1,105 @@
using FluentAssertions;
using Newtonsoft.Json.Linq;
using WireMock.Util;
using Xunit;
namespace WireMock.Net.Tests.Util;
public class CSharpFormatterTests
{
[Fact]
public void ConvertToAnonymousObjectDefinition_ShouldReturn_ValidValue_WhenJsonBodyIsValidJsonString()
{
// Arrange
var jsonBody = new { Key1 = "value1", Key2 = 42, F = 1.2 };
var expectedOutput = "new\r\n {\r\n Key1 = \"value1\",\r\n Key2 = 42,\r\n F = 1.2\r\n }";
// Act
var result = CSharpFormatter.ConvertToAnonymousObjectDefinition(jsonBody);
// Assert
result.Should().Be(expectedOutput);
}
[Fact]
public void ToCSharpStringLiteral_ShouldReturn_ValidValue_WhenStringIsNotNull()
{
// Arrange
var inputString = "test string";
var expectedOutput = "\"test string\"";
// Act
var result = CSharpFormatter.ToCSharpStringLiteral(inputString);
// Assert
result.Should().Be(expectedOutput);
}
[Fact]
public void ToCSharpStringLiteral_ShouldReturn_ValidValue_WhenStringContainsNewLineCharacters()
{
// Arrange
var inputString = "line1\nline2\nline3";
var expectedOutput = "@\"line1\nline2\nline3\"";
// Action
var result = CSharpFormatter.ToCSharpStringLiteral(inputString);
// Assert
result.Should().Be(expectedOutput);
}
[Fact]
public void FormatPropertyName_ShouldReturn_ValidPropertyName_WhenPropertyNameIsNotReserved()
{
// Arrange
var propertyName = "propertyname";
var expectedOutput = "propertyname";
// Action
var result = CSharpFormatter.FormatPropertyName(propertyName);
// Assert
result.Should().Be(expectedOutput);
}
[Fact]
public void FormatPropertyName_ShouldReturn_ValidPropertyName_WhenPropertyNameIsReserved()
{
// Arrange
var propertyName = "class";
var expectedOutput = "@class";
// Action
var result = CSharpFormatter.FormatPropertyName(propertyName);
// Assert
result.Should().Be(expectedOutput);
}
[Fact]
public void ConvertJsonToAnonymousObjectDefinition_ShouldReturn_ValidObject_WhenJsonInputIsValid()
{
// Arrange
var jObject = new JObject
(
new JProperty("Name", "John Smith"),
new JProperty("Age", 25.1f),
new JProperty("Gender", "Male"),
new JProperty("address", new JObject
(
new JProperty("Street", "123 Main St"),
new JProperty("City", "Anytown"),
new JProperty("State", "CA"),
new JProperty("Zip", "90001")
)
));
var expectedOutput = "new\r\n{\r\n Name = \"John Smith\",\r\n Age = 25.1,\r\n Gender = \"Male\",\r\n address = new\r\n {\r\n Street = \"123 Main St\",\r\n City = \"Anytown\",\r\n State = \"CA\",\r\n Zip = \"90001\"\r\n }\r\n}";
// Action
var result = CSharpFormatter.ConvertJsonToAnonymousObjectDefinition(jObject);
// Assert
result.Should().Be(expectedOutput);
}
}

View File

@@ -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")
);

View File

@@ -18,6 +18,7 @@ using WireMock.Admin.Settings;
using WireMock.Client;
using WireMock.Handlers;
using WireMock.Logging;
using WireMock.Matchers;
using WireMock.Models;
using WireMock.Net.Tests.VerifyExtensions;
using WireMock.RequestBuilders;
@@ -719,6 +720,7 @@ public class WireMockAdminApiTests
var guid1 = Guid.Parse("90356dba-b36c-469a-a17e-669cd84f1f05");
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 server = WireMockServer.StartWithAdminInterface();
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
var api = RestClient.For<IWireMockAdminApi>(server.Url);
var mappings = await api.GetMappingsAsync().ConfigureAwait(false);
mappings.Should().HaveCount(3);
mappings.Should().HaveCount(4);
var code = await api.GetMappingsCodeAsync().ConfigureAwait(false);