-
-
Notifications
You must be signed in to change notification settings - Fork 213
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to generate json text field with random numeric content #884
Comments
Did you try adding puk = "\"{{Random Type=\"Integer\" Min=0 Max=2000000}}\"", |
It formats like this: HTTP/1.1 200 OK
Content-Type: application/json
Date: Sun, 12 Feb 2023 10:20:15 GMT
Server: Kestrel
Transfer-Encoding: chunked
{
"puk": "\"98039567\"",
} |
Hello @robertmircea , did you have time to verify this? |
2 similar comments
Hello @robertmircea , did you have time to verify this? |
Hello @robertmircea , did you have time to verify this? |
Hi @StefH, when can I expect to have this feature? Thanks a lot |
@maivanquanbk |
As described in the issue #1028 , I am facing the same situation when parsing a DateTime Just adding the comment here, so I can be notified with updates about this issue. |
@robertmircea I've decided to change the logic as follows:
A preview version with this logic is : |
Hi @StefH , thanks for providing this new preview version! I believe in topic I tested the preview package and I received the timestamps as a string when applying the A question: What if we have a scenario where we need different behaviors at the "property" level? Example: We need to return the current timestamp as a string, but a random number as an integer
How can we achieve the above result? |
I understand your request, I'm working on this, but it's difficult because the return value from Handlebars is always a string. I'm working on a work-around : A unit test for this could be: [Theory]
[InlineData(ReplaceNodeOptions.EvaluateAndTryToConvert)]
[InlineData(ReplaceNodeOptions.Evaluate)]
public async Task Response_WithBodyAsJson_ProvideResponseAsync_Handlebars_DateTimeWithStringFormatAsString(ReplaceNodeOptions options)
{
// Assign
var request = new RequestMessage(new UrlDetails("http://localhost"), "GET", ClientIp);
var responseBuilder = Response.Create()
.WithBodyAsJson(new
{
FormatAsString = "{{ String.FormatAsString (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["FormatAsString"]!.Type.Should().Be(JTokenType.String);
} However I need to think some more on this. Maybe also a another method like |
Thanks for your quick reply, @StefH ! |
You can test preview 1.5.43-ci-18089 |
Hi @StefH ! Is there anything else I should test? Am I missing any new scenario that was implemented in this preview release? |
I checked the Handlebars repo, and I noticed that the TestServer configserver
.Given(Request
.Create()
.WithPath(new ExactMatcher("/some-path"))
.UsingPost())
.RespondWith(Response
.Create()
.WithHeader("Content-Type", "application/json")
.WithBodyAsJson(new
{
timestamp_number = "{{ DateTime.UtcNow \"yyMMddhhmmss\" }}",
timestamp_string = "{{ String.FormatAsString (DateTime.UtcNow) \"yyMMddhhmmss\" }}"
})
.WithTransformer()); Response
QuestionFor a scenario where we don't have to specify a format (like a simple .RespondWith(Response
.Create()
.WithHeader("Content-Type", "application/json")
.WithBodyAsJson(new
{
somedata = new
{
frompath = "{{ String.FormatAsString (request.PathSegments.[3]) '' }}"
}
})
.WithTransformer()); |
I think you can just omit the format. No need for passing an empty string. Can you try that? |
I'd tried that. It's not possible. TestServer configsomedata = new
{
frompath = "{{ String.FormatAsString (request.PathSegments.[3]) }}"
} Response
|
@felipetofoli I've update Handlebars.Net.Helpers (Handlebars-Net/Handlebars.Net.Helpers#87) and I'll release a new NuGet and build a new preview for WireMock.Net Stay tuned.... |
@StefH |
Can you try preview 1.5.43-ci-18158 ? |
@StefH I tried the preview package: TestsEvaluateAndTryToConvert (or default:
|
FormatAsObject does indeed not work as expected. I'll merge the PR and create a new official NuGet. |
Thanks, @StefH ! |
I am trying to generate a json string field with random integers inside using response templating. Unfortunately, I cannot obtain the expected reply:
produces a numeric field instead of a string:
instead of
How can I generate a random integer with min/max constraints formatted as a string?
Alternatively, I've tried
"{{Random Type=\"TextRegex\" Pattern=\"[0-9]{10}\"}}"
and the result is the same: numeric json field instead of string. It seems that the library is converting automatically any numeric string to numeric field value.The text was updated successfully, but these errors were encountered: