mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-24 01:08:28 +02:00
Update Handlebars Transformer logic (ReplaceNodeOptions) (#1036)
* Update Handlebars Transformer logic (ReplaceNodeOptions) * okeee * EvaluateAndKeep = Evaluate * fix? * linux * _ * tt * xxx * fx * x * fix test
This commit is contained in:
@@ -7,6 +7,7 @@ using WireMock.Handlers;
|
||||
using WireMock.Models;
|
||||
using WireMock.ResponseBuilders;
|
||||
using WireMock.Settings;
|
||||
using WireMock.Types;
|
||||
using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests.ResponseBuilders;
|
||||
@@ -28,8 +29,10 @@ public class ResponseWithHandlebarsDateTimeTests
|
||||
_settings.FileSystemHandler = filesystemHandlerMock.Object;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Response_WithBodyAsJson_ProvideResponseAsync_Handlebars_DateTime()
|
||||
[Theory]
|
||||
[InlineData(ReplaceNodeOptions.EvaluateAndTryToConvert, JTokenType.Integer)]
|
||||
[InlineData(ReplaceNodeOptions.Evaluate, JTokenType.String)]
|
||||
public async Task Response_WithBodyAsJson_ProvideResponseAsync_Handlebars_DateTimeYear(ReplaceNodeOptions options, JTokenType expected)
|
||||
{
|
||||
// Assign
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost"), "GET", ClientIp);
|
||||
@@ -39,14 +42,61 @@ public class ResponseWithHandlebarsDateTimeTests
|
||||
{
|
||||
DateTimeYear = "{{ DateTime.UtcNow \"yyyy\" }}"
|
||||
})
|
||||
.WithTransformer();
|
||||
.WithTransformer(options);
|
||||
|
||||
// Act
|
||||
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, request, _settings).ConfigureAwait(false);
|
||||
|
||||
// Assert
|
||||
var j = JObject.FromObject(response.Message.BodyData!.BodyAsJson!);
|
||||
j["DateTimeYear"]!.Value<string>().Should().Be(DateTime.Now.Year.ToString());
|
||||
var jObject = JObject.FromObject(response.Message.BodyData!.BodyAsJson!);
|
||||
jObject["DateTimeYear"]!.Type.Should().Be(expected);
|
||||
jObject["DateTimeYear"]!.Value<string>().Should().Be(DateTime.Now.Year.ToString());
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(ReplaceNodeOptions.EvaluateAndTryToConvert, JTokenType.Date)]
|
||||
[InlineData(ReplaceNodeOptions.Evaluate, JTokenType.String)]
|
||||
public async Task Response_WithBodyAsJson_ProvideResponseAsync_Handlebars_DateTime(ReplaceNodeOptions options, JTokenType expected)
|
||||
{
|
||||
// Assign
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost"), "GET", ClientIp);
|
||||
|
||||
var responseBuilder = Response.Create()
|
||||
.WithBodyAsJson(new
|
||||
{
|
||||
DateTime = "{{ DateTime.UtcNow }}"
|
||||
})
|
||||
.WithTransformer(options);
|
||||
|
||||
// Act
|
||||
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, request, _settings).ConfigureAwait(false);
|
||||
|
||||
// Assert
|
||||
var jObject = JObject.FromObject(response.Message.BodyData!.BodyAsJson!);
|
||||
jObject["DateTime"]!.Type.Should().Be(expected);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(ReplaceNodeOptions.EvaluateAndTryToConvert, JTokenType.Integer)]
|
||||
[InlineData(ReplaceNodeOptions.Evaluate, JTokenType.String)]
|
||||
public async Task Response_WithBodyAsJson_ProvideResponseAsync_Handlebars_DateTimeWithStringFormat(ReplaceNodeOptions options, JTokenType expected)
|
||||
{
|
||||
// Assign
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost"), "GET", ClientIp);
|
||||
|
||||
var responseBuilder = Response.Create()
|
||||
.WithBodyAsJson(new
|
||||
{
|
||||
StringFormatDateTime = "{{ String.Format (DateTime.UtcNow) \"yyMMddhhmmss\" }}"
|
||||
})
|
||||
.WithTransformer(options);
|
||||
|
||||
// Act
|
||||
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, request, _settings).ConfigureAwait(false);
|
||||
|
||||
// Assert
|
||||
var jObject = JObject.FromObject(response.Message.BodyData!.BodyAsJson!);
|
||||
jObject["StringFormatDateTime"]!.Type.Should().Be(expected);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -65,4 +115,39 @@ public class ResponseWithHandlebarsDateTimeTests
|
||||
// Assert
|
||||
response.Message.BodyData!.BodyAsString.Should().Contain($"DateTimeYear = \"{DateTime.Now.Year}\"");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(ReplaceNodeOptions.EvaluateAndTryToConvert)]
|
||||
[InlineData(ReplaceNodeOptions.Evaluate)]
|
||||
public async Task Response_WithBodyAsJson_ProvideResponseAsync_Handlebars_WithStringFormatAsString(ReplaceNodeOptions options)
|
||||
{
|
||||
// Assign
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost"), "GET", ClientIp);
|
||||
|
||||
var responseBuilder = Response.Create()
|
||||
.WithBodyAsJson(new
|
||||
{
|
||||
FormatAsString1 = "{{ String.FormatAsString (DateTime.UtcNow) \"yyMMddhhmmss\" }}",
|
||||
FormatAsString2 = "{{ String.FormatAsString (DateTime.UtcNow) }}",
|
||||
FormatAsString3 = "{{ String.FormatAsString 42 \"X\" }}",
|
||||
FormatAsString4 = "{{ String.FormatAsString 42 }}"
|
||||
})
|
||||
.WithTransformer(options);
|
||||
|
||||
// Act
|
||||
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, request, _settings).ConfigureAwait(false);
|
||||
|
||||
// Assert
|
||||
var jObject = JObject.FromObject(response.Message.BodyData!.BodyAsJson!);
|
||||
jObject["FormatAsString1"]!.Type.Should().Be(JTokenType.String);
|
||||
jObject["FormatAsString2"]!.Type.Should().Be(JTokenType.String);
|
||||
|
||||
var formatAsString3 = jObject["FormatAsString3"]!;
|
||||
formatAsString3.Type.Should().Be(JTokenType.String);
|
||||
formatAsString3.Value<string>().Should().Be("2A");
|
||||
|
||||
var formatAsString4 = jObject["FormatAsString4"]!;
|
||||
formatAsString4.Type.Should().Be(JTokenType.String);
|
||||
formatAsString4.Value<string>().Should().Be("42");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user