mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-17 14:40:00 +02:00
Fix HandlebarsContext ParseAndEvaluate method (#1213)
* Fix HandlebarsContext ParseAndEvaluate method * test * xxx
This commit is contained in:
@@ -6,6 +6,9 @@ MinimumVisualStudioVersion = 10.0.40219.1
|
|||||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{8F890C6F-9ACC-438D-928A-AD61CDA862F2}"
|
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{8F890C6F-9ACC-438D-928A-AD61CDA862F2}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{0BB8B634-407A-4610-A91F-11586990767A}"
|
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
|
EndProject
|
||||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WireMock.Net", "src\WireMock.Net\WireMock.Net.csproj", "{D3804228-91F4-4502-9595-39584E5A01AD}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WireMock.Net", "src\WireMock.Net\WireMock.Net.csproj", "{D3804228-91F4-4502-9595-39584E5A01AD}"
|
||||||
EndProject
|
EndProject
|
||||||
|
|||||||
@@ -27,7 +27,9 @@ internal class HandlebarsContext : IHandlebarsContext
|
|||||||
|
|
||||||
public object? ParseAndEvaluate(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;
|
return result;
|
||||||
}
|
}
|
||||||
|
|||||||
8
test/Directory.Build.props
Normal file
8
test/Directory.Build.props
Normal 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>
|
||||||
@@ -130,6 +130,29 @@ public class ResponseWithTransformerTests
|
|||||||
Check.That(response.Message.BodyData!.BodyAsString).Equals("a wiremock");
|
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`")]
|
[Theory(Skip = "Invalid token `OpenBracket`")]
|
||||||
[InlineData(TransformerType.Scriban)]
|
[InlineData(TransformerType.Scriban)]
|
||||||
[InlineData(TransformerType.ScribanDotLiquid)]
|
[InlineData(TransformerType.ScribanDotLiquid)]
|
||||||
|
|||||||
Reference in New Issue
Block a user