Skip to content

Commit 4aaed2a

Browse files
authored
Fix HandlebarsContext ParseAndEvaluate method (#1213)
* Fix HandlebarsContext ParseAndEvaluate method * test * xxx
1 parent 6f73dfe commit 4aaed2a

File tree

4 files changed

+37
-1
lines changed

4 files changed

+37
-1
lines changed

WireMock.Net Solution.sln

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

src/WireMock.Net/Transformers/Handlebars/HandlebarsContext.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ public string ParseAndRender(string text, object model)
2727

2828
public object? ParseAndEvaluate(string text, object model)
2929
{
30-
if (Handlebars.TryEvaluate(text, model, out var result) && result is not UndefinedBindingResult)
30+
if (text.StartsWith("{{") && text.EndsWith("}}") &&
31+
Handlebars.TryEvaluate(text, model, out var result) &&
32+
result is not UndefinedBindingResult)
3133
{
3234
return result;
3335
}

test/Directory.Build.props

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project>
2+
<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)../'))" />
3+
4+
<ItemGroup Condition=" '$(TargetFramework)' != 'net45' and '$(TargetFramework)' != 'net452' and '$(TargetFramework)' != 'net461' ">
5+
<AssemblyAttribute Include="System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverageAttribute" />
6+
</ItemGroup>
7+
8+
</Project>

test/WireMock.Net.Tests/ResponseBuilders/ResponseWithTransformerTests.cs

+23
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,29 @@ public async Task Response_ProvideResponse_Handlebars_PathSegments()
130130
Check.That(response.Message.BodyData!.BodyAsString).Equals("a wiremock");
131131
}
132132

133+
[Theory]
134+
[InlineData("{{request.PathSegments.[0]}}", "a")]
135+
[InlineData("prefix_{{request.PathSegments.[0]}}", "prefix_a")]
136+
[InlineData("{{request.PathSegments.[0]}}_postfix", "a_postfix")]
137+
[InlineData("prefix_{{request.PathSegments.[0]}}_postfix", "prefix_a_postfix")]
138+
public async Task Response_ProvideResponse_Handlebars_BodyAsJson_PathSegments(string field, string expected)
139+
{
140+
// Assign
141+
var urlDetails = UrlUtils.Parse(new Uri("http://localhost/wiremock/a/b"), new PathString("/wiremock"));
142+
var request = new RequestMessage(urlDetails, "POST", ClientIp);
143+
144+
var responseBuilder = Response.Create()
145+
.WithBodyAsJson(new { field })
146+
.WithTransformer();
147+
148+
// Act
149+
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, request, _settings).ConfigureAwait(false);
150+
151+
// Assert
152+
var json = (JObject)response.Message.BodyData!.BodyAsJson!;
153+
Check.That(json["field"]!.Value<string>()).Equals(expected);
154+
}
155+
133156
[Theory(Skip = "Invalid token `OpenBracket`")]
134157
[InlineData(TransformerType.Scriban)]
135158
[InlineData(TransformerType.ScribanDotLiquid)]

0 commit comments

Comments
 (0)