Skip to content

Commit

Permalink
Fix HandlebarsContext ParseAndEvaluate method (#1213)
Browse files Browse the repository at this point in the history
* Fix HandlebarsContext ParseAndEvaluate method

* test

* xxx
  • Loading branch information
StefH authored Nov 22, 2024
1 parent 6f73dfe commit 4aaed2a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
3 changes: 3 additions & 0 deletions WireMock.Net Solution.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ public string ParseAndRender(string text, object model)

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;
}
Expand Down
8 changes: 8 additions & 0 deletions test/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,29 @@ public async Task Response_ProvideResponse_Handlebars_PathSegments()
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)]
Expand Down

0 comments on commit 4aaed2a

Please sign in to comment.