mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-23 16:58:27 +02:00
Updated Response Templating (markdown)
@@ -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.
|
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`.
|
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.
|
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:
|
The following extra helpers are present in WireMock.Net:
|
||||||
- JsonPath.SelectToken & JsonPath.SelectTokens
|
- [JsonPath.SelectToken & JsonPath.SelectTokens](#jsonpath)
|
||||||
- Linq
|
- Linq
|
||||||
- [Random](#random)
|
- [Random](#random)
|
||||||
- Regex.Match
|
- Regex.Match
|
||||||
- XPath.SelectSingleNode & XPath.SelectNodes & XPath.Evaluate
|
- [XPath.SelectSingleNode & XPath.SelectNodes & XPath.Evaluate](#xpath)
|
||||||
- Xeger
|
- Xeger
|
||||||
|
|
||||||
## Random
|
## JsonPath
|
||||||
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 support is also present (internal logic is based on Newtonsoft.Json).
|
JsonPath support is also present (internal logic is based on Newtonsoft.Json).
|
||||||
|
|
||||||
Two functions are present:
|
Two functions are present:
|
||||||
1. JsonPath.SelectToken
|
1. JsonPath.SelectToken
|
||||||
2. JsonPath.SelectTokens
|
2. JsonPath.SelectTokens
|
||||||
|
|
||||||
##### JsonPath.SelectToken
|
### JsonPath.SelectToken
|
||||||
This can be used in C# like:
|
This can be used in C# like:
|
||||||
```csharp
|
```csharp
|
||||||
var server = WireMockServer.Start();
|
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
|
XPath support is also present
|
||||||
|
|
||||||
Three functions are present:
|
Three functions are present:
|
||||||
@@ -215,7 +215,7 @@ Three functions are present:
|
|||||||
2. XPath.SelectNodes
|
2. XPath.SelectNodes
|
||||||
3. XPath.Evaluate
|
3. XPath.Evaluate
|
||||||
|
|
||||||
##### XPath.SelectSingleNode
|
### XPath.SelectSingleNode
|
||||||
This can be used in C# like:
|
This can be used in C# like:
|
||||||
```csharp
|
```csharp
|
||||||
var server = WireMockServer.Start();
|
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
|
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
|
||||||
|
|
||||||
|
###
|
||||||
Reference in New Issue
Block a user