mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-21 08:21:53 +02:00
Updated Response Templating (markdown)
@@ -1,4 +1,3 @@
|
|||||||
## Response Templating
|
|
||||||
Response headers and bodies can optionally be rendered using [Handlebars.Net](https://github.com/rexm/Handlebars.Net) templates.
|
Response headers and bodies can optionally be rendered using [Handlebars.Net](https://github.com/rexm/Handlebars.Net) templates.
|
||||||
This enables attributes of the request to be used in generating the response e.g. to pass the value of a request ID header as a response header or render an identifier from part of the URL in the response body. To use this functionality, add `.WithTransformer()` to the response builder.
|
This enables attributes of the request to be used in generating the response e.g. to pass the value of a request ID header as a response header or render an identifier from part of the URL in the response body. To use this functionality, add `.WithTransformer()` to the response builder.
|
||||||
|
|
||||||
@@ -41,7 +40,7 @@ Mapping Json Example:
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### The request model
|
# The request model
|
||||||
The model of the request is supplied to the header and body templates. The following request attributes are available:
|
The model of the request is supplied to the header and body templates. The following request attributes are available:
|
||||||
|
|
||||||
* `request.url` - URL path and query
|
* `request.url` - URL path and query
|
||||||
@@ -63,19 +62,26 @@ The model of the request is supplied to the header and body templates. The follo
|
|||||||
* `request.body` - Request body text as string
|
* `request.body` - Request body text as string
|
||||||
* `request.bodyAsJson` - Request body as dynamic Json Object. Note that the request **must** contain the header `Content-Type` with value `application/json`!
|
* `request.bodyAsJson` - Request body as dynamic Json Object. Note that the request **must** contain the header `Content-Type` with value `application/json`!
|
||||||
|
|
||||||
### Transform the content from a referenced file
|
## Transform the content from a referenced file
|
||||||
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:
|
||||||
|
- JsonPath.SelectToken & JsonPath.SelectTokens
|
||||||
|
- Linq
|
||||||
|
- [Random]()
|
||||||
|
- Regex.Match
|
||||||
|
- XPath.SelectSingleNode & XPath.SelectNodes & XPath.Evaluate
|
||||||
|
- Xeger
|
||||||
|
|
||||||
#### Generate Random Data Support
|
## Random
|
||||||
It's possible to return random data using the `Random` Handlebars function.
|
It's possible to return random data using the `Random` Handlebars function.
|
||||||
|
|
||||||
##### Random Text
|
### Random Text
|
||||||
**Example**: to generate a random string between 8 and 20 characters, use this code in C#:
|
**Example**: to generate a random string between 8 and 20 characters, use this code in C#:
|
||||||
```csharp
|
```csharp
|
||||||
var server = WireMockServer.Start();
|
var server = WireMockServer.Start();
|
||||||
@@ -104,7 +110,7 @@ server
|
|||||||
);
|
);
|
||||||
```
|
```
|
||||||
|
|
||||||
##### Random (all supported randomizers)
|
#### 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).
|
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}"}}"`
|
- Text Regex Pattern: `"{{Xeger Pattern=\"[1-9][0-9]{3}[A-Z]{2}"}}"`
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user