From 9360c59f2a26594f59a9bf67e6001488c039dbcb Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Wed, 15 Apr 2020 19:52:48 +0200 Subject: [PATCH] Updated Response Templating (markdown) --- Response-Templating.md | 128 +++++++++++++++++++++-------------------- 1 file changed, 65 insertions(+), 63 deletions(-) diff --git a/Response-Templating.md b/Response-Templating.md index e2291d1..0d33234 100644 --- a/Response-Templating.md +++ b/Response-Templating.md @@ -66,80 +66,26 @@ The model of the request is supplied to the header and body templates. The follo By default, only the response (headers, statuscode, body) are transformed when the `.WithTransformer()` or `UseTransformer` are defined. In case you also want to transform the contents from a referenced file (via `BodyAsFile`), an additional parameter need to added. Like `.WithTransformer(bool)` or `UseTransformerForBodyAsFile = true`. -## Standard Handlebars helpers +# Standard Handlebars helpers All of the standard helpers (template functions) provided by the [C# Handlebars implementation](https://github.com/rexm/Handlebars.Net) are available. -## Extra Handlebars helpers +# Extra Handlebars helpers The following extra helpers are present in WireMock.Net: -- JsonPath.SelectToken & JsonPath.SelectTokens +- [JsonPath.SelectToken & JsonPath.SelectTokens](#jsonpath) - Linq - [Random](#random) - Regex.Match -- XPath.SelectSingleNode & XPath.SelectNodes & XPath.Evaluate +- [XPath.SelectSingleNode & XPath.SelectNodes & XPath.Evaluate](#xpath) - Xeger -## Random -It's possible to return random data using the `Random` Handlebars function. - -### Random Text -**Example**: to generate a random string between 8 and 20 characters, use this code in C#: -```csharp -var server = WireMockServer.Start(); -server - .Given(Request.Create().WithPath("/random").UsingGet()) - .RespondWith(Response.Create() - .WithHeader("Content-Type", "application/json") - .WithBodyAsJson( - Text = "{{Random Type=\"Text\" Min=8 Max=20}}", - ) - .WithTransformer() - ); -``` - -**Example**: to generate a random string using a Regex pattern, use this code in C#: -```csharp -var server = FluentMockServer.Start(); -server - .Given(Request.Create().WithPath("/random-regex").UsingGet()) - .RespondWith(Response.Create() - .WithHeader("Content-Type", "application/json") - .WithBodyAsJson( - Text = "{{Xeger \"[1-9][0-9]{3}[A-Z]{2}\"}", - ) - .WithTransformer() - ); -``` - -#### Random (all supported randomizers) -You can use the powerful Regular Expression string generator based on [Fare - Finite Automata and Regular Expressions](https://github.com/moodmosaic/Fare). -- Text Regex Pattern: `"{{Xeger Pattern=\"[1-9][0-9]{3}[A-Z]{2}"}}"` - -Besides a random text string, it's also possible to generate this random data: -- Integer: `"{{Random Type=\"Integer\" Min=100 Max=999}}"` -- Guid: `"{{Random Type=\"Guid\"}}"` -- City: `"{{Random Type=\"City\"}}"` -- Country: `"{{Random Type=\"Country\"}}"` -- First Name: `"{{Random Type=\"FirstName\" Male=false Female=true}}"` -- Email Address: `"{{Random Type=\"EmailAddress\"}}"` -- Text Words: `"{{Random Type=\"TextWords\" Min=10 Max=20}}"` -- Text Regex Pattern: `"{{Random Type=\"TextRegex\" Pattern=\"[1-9][0-9]{3}[A-Z]{2}"}}"` -- Text Lorum Ipsum: `"{{Random Type=\"TextIpsum\" Paragraphs=2}}"` -- String List: `"{{Random Type=\"StringList\" Values=[\"a\", \"b\", \"c\"]}}"` -- IPv4 Address: `"{{Random Type=\"IPv4Address\"}}"` -- IPv6 Address: `"{{Random Type=\"IPv6Address\" Min = "0000:0001:0000:0000:0020:ff00:0042:8000", Max = "2001:0db8:0120:0000:0030:ff00:aa42:8329"}}"` -- MAC Address: `"{{Random Type=\"MACAddress\"}}"` -- For more details on the supported random data types, see [RandomDataGenerator.Net](https://github.com/StefH/RandomDataGenerator); - -Note: instead of using `\"` in above examples, you can also use `'`. - -#### JsonPath support +## JsonPath JsonPath support is also present (internal logic is based on Newtonsoft.Json). Two functions are present: 1. JsonPath.SelectToken 2. JsonPath.SelectTokens -##### JsonPath.SelectToken +### JsonPath.SelectToken This can be used in C# like: ```csharp var server = WireMockServer.Start(); @@ -207,7 +153,61 @@ Note that also replacing values in a Json Object and returning a the body as Jso } ``` -#### XPath support +## Random +It's possible to return random data using the `Random` Handlebars function. + +### Random Text +**Example**: to generate a random string between 8 and 20 characters, use this code in C#: +```csharp +var server = WireMockServer.Start(); +server + .Given(Request.Create().WithPath("/random").UsingGet()) + .RespondWith(Response.Create() + .WithHeader("Content-Type", "application/json") + .WithBodyAsJson( + Text = "{{Random Type=\"Text\" Min=8 Max=20}}", + ) + .WithTransformer() + ); +``` + +**Example**: to generate a random string using a Regex pattern, use this code in C#: +```csharp +var server = FluentMockServer.Start(); +server + .Given(Request.Create().WithPath("/random-regex").UsingGet()) + .RespondWith(Response.Create() + .WithHeader("Content-Type", "application/json") + .WithBodyAsJson( + Text = "{{Xeger \"[1-9][0-9]{3}[A-Z]{2}\"}", + ) + .WithTransformer() + ); +``` + +### Random (all supported randomizers) +You can use the powerful Regular Expression string generator based on [Fare - Finite Automata and Regular Expressions](https://github.com/moodmosaic/Fare). +- Text Regex Pattern: `"{{Xeger Pattern=\"[1-9][0-9]{3}[A-Z]{2}"}}"` + +Besides a random text string, it's also possible to generate this random data: +- Integer: `"{{Random Type=\"Integer\" Min=100 Max=999}}"` +- Guid: `"{{Random Type=\"Guid\"}}"` +- City: `"{{Random Type=\"City\"}}"` +- Country: `"{{Random Type=\"Country\"}}"` +- First Name: `"{{Random Type=\"FirstName\" Male=false Female=true}}"` +- Email Address: `"{{Random Type=\"EmailAddress\"}}"` +- Text Words: `"{{Random Type=\"TextWords\" Min=10 Max=20}}"` +- Text Regex Pattern: `"{{Random Type=\"TextRegex\" Pattern=\"[1-9][0-9]{3}[A-Z]{2}"}}"` +- Text Lorum Ipsum: `"{{Random Type=\"TextIpsum\" Paragraphs=2}}"` +- String List: `"{{Random Type=\"StringList\" Values=[\"a\", \"b\", \"c\"]}}"` +- IPv4 Address: `"{{Random Type=\"IPv4Address\"}}"` +- IPv6 Address: `"{{Random Type=\"IPv6Address\" Min = "0000:0001:0000:0000:0020:ff00:0042:8000", Max = "2001:0db8:0120:0000:0030:ff00:aa42:8329"}}"` +- MAC Address: `"{{Random Type=\"MACAddress\"}}"` +- For more details on the supported random data types, see [RandomDataGenerator.Net](https://github.com/StefH/RandomDataGenerator); + +Note: instead of using `\"` in above examples, you can also use `'`. + +## XPath XPath support is also present Three functions are present: @@ -215,7 +215,7 @@ Three functions are present: 2. XPath.SelectNodes 3. XPath.Evaluate -##### XPath.SelectSingleNode +### XPath.SelectSingleNode This can be used in C# like: ```csharp var server = WireMockServer.Start(); @@ -254,4 +254,6 @@ Or using the admin mapping file: } ``` -For examples on `XPath.SelectNodes` and `XPath.Evaluate`, see https://github.com/WireMock-Net/WireMock.Net/blob/master/test/WireMock.Net.Tests/ResponseBuilders/ResponseWithHandlebarsXPathTests.cs \ No newline at end of file +For examples on `XPath.SelectNodes` and `XPath.Evaluate`, see https://github.com/WireMock-Net/WireMock.Net/blob/master/test/WireMock.Net.Tests/ResponseBuilders/ResponseWithHandlebarsXPathTests.cs + +### \ No newline at end of file