Fix HandlebarsContext ParseAndEvaluate method (#1213)

* Fix HandlebarsContext ParseAndEvaluate method

* test

* xxx
This commit is contained in:
Stef Heyenrath
2024-11-22 07:58:23 +01:00
committed by GitHub
parent 6f73dfe360
commit 4aaed2a6ca
4 changed files with 37 additions and 1 deletions

View File

@@ -6,6 +6,9 @@ MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{8F890C6F-9ACC-438D-928A-AD61CDA862F2}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{0BB8B634-407A-4610-A91F-11586990767A}"
ProjectSection(SolutionItems) = preProject
test\Directory.Build.props = test\Directory.Build.props
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WireMock.Net", "src\WireMock.Net\WireMock.Net.csproj", "{D3804228-91F4-4502-9595-39584E5A01AD}"
EndProject

View File

@@ -27,7 +27,9 @@ internal class HandlebarsContext : IHandlebarsContext
public object? ParseAndEvaluate(string text, object model)
{
if (Handlebars.TryEvaluate(text, model, out var result) && result is not UndefinedBindingResult)
if (text.StartsWith("{{") && text.EndsWith("}}") &&
Handlebars.TryEvaluate(text, model, out var result) &&
result is not UndefinedBindingResult)
{
return result;
}

View File

@@ -0,0 +1,8 @@
<Project>
<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)../'))" />
<ItemGroup Condition=" '$(TargetFramework)' != 'net45' and '$(TargetFramework)' != 'net452' and '$(TargetFramework)' != 'net461' ">
<AssemblyAttribute Include="System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverageAttribute" />
</ItemGroup>
</Project>

View File

@@ -130,6 +130,29 @@ public class ResponseWithTransformerTests
Check.That(response.Message.BodyData!.BodyAsString).Equals("a wiremock");
}
[Theory]
[InlineData("{{request.PathSegments.[0]}}", "a")]
[InlineData("prefix_{{request.PathSegments.[0]}}", "prefix_a")]
[InlineData("{{request.PathSegments.[0]}}_postfix", "a_postfix")]
[InlineData("prefix_{{request.PathSegments.[0]}}_postfix", "prefix_a_postfix")]
public async Task Response_ProvideResponse_Handlebars_BodyAsJson_PathSegments(string field, string expected)
{
// Assign
var urlDetails = UrlUtils.Parse(new Uri("http://localhost/wiremock/a/b"), new PathString("/wiremock"));
var request = new RequestMessage(urlDetails, "POST", ClientIp);
var responseBuilder = Response.Create()
.WithBodyAsJson(new { field })
.WithTransformer();
// Act
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, request, _settings).ConfigureAwait(false);
// Assert
var json = (JObject)response.Message.BodyData!.BodyAsJson!;
Check.That(json["field"]!.Value<string>()).Equals(expected);
}
[Theory(Skip = "Invalid token `OpenBracket`")]
[InlineData(TransformerType.Scriban)]
[InlineData(TransformerType.ScribanDotLiquid)]