Skip to content
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

[Question] Dates in response templates #568

Closed
Narcalen opened this issue Jan 25, 2021 · 12 comments
Closed

[Question] Dates in response templates #568

Narcalen opened this issue Jan 25, 2021 · 12 comments
Labels

Comments

@Narcalen
Copy link

Narcalen commented Jan 25, 2021

Is it possible to add formatted dates to response templates? e.g. I would like to do smth like this:

"Response": {
    "StatusCode": 200,
    "Headers": { "Content-Type": "application/json; charset=utf-8" },
    "BodyAsJson": {
      "data": {
            "id": "{{Random Type=\"Integer\" Min=100 Max=999999}}",
            "status": "draft",
            "createdAt": "{{now format='yyyy-MM-dd'}}",
            "updatedAt": "{{now}}",
      }
    },
    "UseTransformer": true
  }

Where now is current datetime, not data extracted from request

@StefH
Copy link
Collaborator

StefH commented Jan 25, 2021

Hello @Narcalen

This is not yet supported in WireMock.Net but this could be added to Handlebars.Net.Helpers

I will take a look if it can be added there.

@StefH
Copy link
Collaborator

StefH commented Jan 26, 2021

@Narcalen

I'm looking at the code from Handlebars.Net/Helpers and in this case, the string to use would be like:

"{{Format (Now) \"yyyy-MM-dd\"}}"

Would be work for you?

@StefH
Copy link
Collaborator

StefH commented Jan 27, 2021

Or another possible solution will be:

"{{Now \"yyyy-MM-dd\"}}"

@Narcalen
Copy link
Author

Narcalen commented Jan 27, 2021

Thank you for the response! I have tried the second option and I am getting the following response: Template references a helper that is not registered. Could not find helper 'Now'

Is it possible to register additional handlebar helpers?

@StefH
Copy link
Collaborator

StefH commented Jan 27, 2021

Sorry for the confusion I caused.

This new functionality is now present in Handlebars.Net.Helpers however not yet used by WireMock.Net

I'm working in that and I keep you informed.

@StefH
Copy link
Collaborator

StefH commented Jan 30, 2021

@Narcalen
I've added functionality to Handlebars.Net.Helpers and these are used in WireMock.Net

Please try preview version WireMock.Net.1.4.3-ci-14593 from MyGet.

@Narcalen
Copy link
Author

Narcalen commented Feb 3, 2021

@StefH
Thank you for the update
I have updated to WireMock.Net.1.4.3-ci-14593 and tried the following options:
"{{Format (Now) \"yyyy-MM-dd\"}}"
"{{Now \"yyyy-MM-dd\"}}"

For both options, I received the following error:
{ "Status": "Template references a helper that cannot be resolved. Helper 'Format'" }

Moreover, I have received the same errors for other helpers, e.g.:
{ "Status": "Template references a helper that cannot be resolved. Helper 'Random'" }

Could you please suggest if I am missing anything?

@StefH
Copy link
Collaborator

StefH commented Feb 4, 2021

Can you share your example project?

@Narcalen
Copy link
Author

Narcalen commented Feb 8, 2021

https://github.com/Narcalen/example-wiremock-net

This mapping works fine with version 1.4.3 and returns Template references a helper that cannot be resolved error with 1.4.3-ci-14626

@StefH
Copy link
Collaborator

StefH commented Feb 8, 2021

This should work:

"{{Random Type=\"Integer\" Min=100 Max=999999}} {{DateTime.Now}} {{DateTime.Now \"yyyy-MMM\"}} {{String.Format (DateTime.Now) \"MMM-dd\"}}"

Use exact version 1.4.3-ci-14635

@Narcalen
Copy link
Author

Narcalen commented Feb 9, 2021

@StefH this version works fine for me. Thanks!

@StefH StefH added feature and removed question labels Feb 9, 2021
@StefH
Copy link
Collaborator

StefH commented Feb 9, 2021

great!

You can expect a new 1.4.4 in some time

@StefH StefH closed this as completed Feb 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants