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

Document new helpers and extension lifecycle #283

Merged
merged 3 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions _docs/extending-wiremock.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ At present, the following extension interfaces are available:

The interfaces in this list ending with `V2` supercede deprecated equivalents with an older, more restrictive interface. Additionally `ServeEventListener` deprecates `PostServeAction`.

As of WireMock version `3.6.0`, the `Extension` interface has two new lifecycle methods called `start()` and `stop()`.
The `start()` method is called on each extension when the WireMock server first starts (just before the stub mappings
are loaded) and the `stop()` method is called when the server is stopped. This allows extensions to perform any
initialisation or cleanup tasks.

## Registering Extensions

Expand Down
109 changes: 109 additions & 0 deletions _docs/response-templating.md
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,30 @@ Variable assignment and number helpers are available:

{% endraw %}

## Val helper

The `val` helper can be used to access values or provide a default if the value is not present. It can also be used to
assign a value to a variable much like the `assign` helper. The main difference between `val` and `assign` is that `val`
will maintain the type of the date being assigned whereas `assign` will always assign a string.

{% raw %}

```handlebars
{{val request.query.search or='default'}} // the value of request.query.search or 'default' if it's not present
{{val request.query.search default='default'}} // the value of request.query.search or 'default' if it's not present
{{val request.query.search}} // the value of request.query.search or null if it's not present
{{val request.query.search or='default' assign='myVar'}} // assign the value of request.query.search or 'default' to myVar
{{val request.query.search assign='myVar'}} // assign the value of request.query.search to myVar


{{val (array 1 2 3) default='123'}} // [1, 2, 3]
{{val 'value for myVar' assign='myVar'}}{{myVar}} // value for myVar
{{val null or='other value for myVar' assign='myVar'}}{{myVar}} // other value for myVar
{{val 10 assign='myVar'}}{{#lt myVar 20}}Less Than{{else}}More Than{{/lt}} // Less Than
```

{% endraw %}

## XPath helpers

Additionally some helpers are available for working with JSON and XML.
Expand Down Expand Up @@ -719,6 +743,91 @@ Providing no parameters will result in an empty array.

{% endraw %}

## Array add & remove helpers
The `arrayAdd` and `arrayRemove` helpers can be used to add or remove elements from an array based on a position value
or the `start` or `end` keywords. If no position is specified, the element will be added or removed from the end of the
array.

{% raw %}

```handlebars
{{arrayAdd (array 1 'three') 2 position=1}} // [1, 2, three]
{{arrayAdd (array 1 'three') 2 position='start'}} // [2, 1, three]
{{arrayAdd (array 1 'three') 2 position='end'}} // [1, three, 2]
{{arrayAdd (array 1 'three') 2}} // [1, three, 2]

{{arrayRemove (array 1 2 'three') position=1}} // [1, three]
{{arrayRemove (array 1 2 'three') position='start'}} // [2, three]
{{arrayRemove (array 1 2 'three') position='end'}} // [1, 2]
{{arrayRemove (array 1 2 'three')}} // [1, 2]
```

{% endraw %}

## arrayJoin helper

The `arrayJoin` helper will concatenate the values passed to it with the separator specified:

{% raw %}

```handlebars
{{arrayJoin ',' (array 'One' 'Two' 'Three')}} // One,Two,Three
{{arrayJoin ' - ' 'a' 'b' 'c'}} // a - b - c
{{arrayJoin ', ' (range 1 5)}} // 1, 2, 3, 4, 5
{{arrayJoin (pickRandom ':') (array 'One' 'Two' 'Three')}} // One:Two:Three
{{arrayJoin '' (array 'W' 'i' 'r' 'e' 'M' 'o' 'c' 'k' ' ' 'R' 'o' 'c' 'k' 's')}} // WireMock Rocks
```

{% endraw %}

You can also specify a `prefix` and `suffix` to be added to the start and end of the result:

{% raw %}

```handlebars
{{arrayJoin ',' (array 'One' 'Two' 'Three') prefix='[' suffix=']'}} // [One,Two,Three]
{{arrayJoin ' * ' (array 1 2 3) prefix='(' suffix=')'}} // (1 * 2 * 3)
```

{% endraw %}

The `arrayJoin` helper can also be used as a block helper:

{% raw %}

```handlebars
{{#parseJson 'myThings'}}
[
{ "id": 1, "name": "One" },
{ "id": 2, "name": "Two" },
{ "id": 3, "name": "Three" }
]
{{/parseJson}}
[{{#arrayJoin ',' myThings as |item|}}
{
"name{{item.id}}": "{{item.name}}"
}
{{/arrayJoin}}] // [{ "name1": "One" }, { "name2": "Two" }, { "name3": "Three" }]


// or the same example with the prefix and suffix parameters
{{#parseJson 'myThings'}}
[
{ "id": 1, "name": "One" },
{ "id": 2, "name": "Two" },
{ "id": 3, "name": "Three" }
]
{{/parseJson}}
{{#arrayJoin ',' myThings prefix='[' suffix=']' as |item|}}
{
"name{{item.id}}": "{{item.name}}"
}
{{/arrayJoin}} // [{ "name1": "One" }, { "name2": "Two" }, { "name3": "Three" }]
```

{% endraw %}


## Contains helper

The `contains` helper returns a boolean value indicating whether the string or array passed as the first parameter
Expand Down
Loading