Skip to content

Commit 14b062f

Browse files
authored
feat: support built-in WireMock matchers on properties and list entries (#109)
- adding `property` configuration for request matching - adding `list` configuration for request matching - supporting WireMock's built-in matchers for request matching - cleaned template helper tests to be more elaborate - cleaned up state request matcher tests to be more elaborate <!-- Please describe your pull request here. --> ## References #64 <!-- References to relevant GitHub issues and pull requests, esp. upstream and downstream changes --> ## Submitter checklist - [ ] Recommended: Join [WireMock Slack](https://slack.wiremock.org/) to get any help in `#help-contributing` or a project-specific channel like `#wiremock-java` - [ ] The PR request is well described and justified, including the body and the references - [ ] The PR title represents the desired changelog entry - [ ] The repository's code style is followed (see the contributing guide) - [ ] Test coverage that demonstrates that the change works as expected - [ ] For new features, there's necessary documentation in this pull request or in a subsequent PR to [wiremock.org](https://github.com/wiremock/wiremock.org) <!-- Put an `x` into the [ ] to show you have filled the information. The template comes from https://github.com/wiremock/.github/blob/main/.github/pull_request_template.md You can override it by creating .github/pull_request_template.md in your own repository -->
1 parent cd6a954 commit 14b062f

File tree

5 files changed

+852
-293
lines changed

5 files changed

+852
-293
lines changed

README.md

Lines changed: 102 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ the `GET` won't have any knowledge of the previous post.
172172
|------------------------------------|--------------------|
173173
| `0.0.3`+ | `3.0.0-beta-11`+ |
174174
| `0.0.6`+ | `3.0.0-beta-14`+ |
175-
| `0.1.0`+ | `3.0.0`+ |
175+
| `0.1.0`+ | `3.0.0`+ |
176176

177177
## Installation
178178

@@ -308,7 +308,7 @@ wiremock/wiremock:3.3.0 \
308308
The state is recorded in `serveEventListeners` of a stub. The following functionalities are provided:
309309

310310
- `state` : stores a state in a context. Storing the state multiple times can be used to selectively overwrite existing properties.
311-
- to delete a selective property, set it to `null` (as string).
311+
- to delete a selective property, set it to `null` (as string).
312312
- `list` : stores a state in a list. Can be used to prepend/append new states to an existing list. List elements cannot be modified (only read/deleted).
313313

314314
`state` and `list` can be used in the same `ServeEventListener` (would count as two updates). Adding multiple `recordState` `ServeEventListener` is supported.
@@ -702,8 +702,8 @@ to avoid memory leaks).
702702
The default expiration is 60 minutes. The default value can be overwritten (`0` = default = 60 minutes):
703703

704704
```java
705-
int expiration=1024;
706-
var store=new CaffeineStore(expiration);
705+
int expiration = 1024;
706+
var store = new CaffeineStore(expiration);
707707
```
708708

709709
## Match a request against a context
@@ -760,6 +760,46 @@ As for other matchers, templating is supported.
760760
}
761761
```
762762

763+
### Full flexible property match
764+
765+
In case you want full flexibility into matching on a property, you can simply specify `property` and use one of WireMock's built-in matchers, allowing you to
766+
configure
767+
logical operators, regex, date matchers, absence and much more. The basic syntax:
768+
769+
```json
770+
"property": {
771+
<property-a>: <matcher-a>,
772+
<property-b>: <matcher-b>
773+
}
774+
```
775+
776+
Example:
777+
778+
```json
779+
{
780+
"request": {
781+
"method": "GET",
782+
"urlPattern": "/test/[^\/]+/[^\/]+",
783+
"customMatcher": {
784+
"name": "state-matcher",
785+
"parameters": {
786+
"property": {
787+
"myProperty": {
788+
"contains": "myValue"
789+
}
790+
}
791+
}
792+
},
793+
"response": {
794+
"status": 200
795+
}
796+
}
797+
```
798+
799+
The implementation makes use of WireMock's internal matching system and supports any implementation of `StringValuePattern`. As of WireMock 3.3, this includes
800+
`equalTo`,`equalToJson`,`matchesJsonPath`,`matchesJsonSchema`,`equalToXml`,`matchesXPath`,`contains`,`not`,`doesNotContain`,`matches`,`doesNotMatch`,`before`,
801+
`after`,`equalToDateTime`,`anything`,`absent`,`and`,`or`,`matchesPathTemplate`.
802+
For documentation on using these matchers, check the [WireMock documentation](https://wiremock.org/docs/request-matching/)
763803

764804
### Context update count match
765805

@@ -770,7 +810,8 @@ for request matching as well. The following matchers are available:
770810
- `updateCountLessThan`
771811
- `updateCountMoreThan`
772812

773-
As for other matchers, templating is supported. In case the provided value for this check is not numeric, it is handled as non-matching. No error will be reported or logged.
813+
As for other matchers, templating is supported. In case the provided value for this check is not numeric, it is handled as non-matching. No error will be
814+
reported or logged.
774815

775816
```json
776817
{
@@ -800,7 +841,8 @@ for request matching as well. The following matchers are available:
800841
- `listSizeLessThan`
801842
- `listSizeMoreThan`
802843

803-
As for other matchers, templating is supported. In case the provided value for this check is not numeric, it is handled as non-matching. No error will be reported or logged.
844+
As for other matchers, templating is supported. In case the provided value for this check is not numeric, it is handled as non-matching. No error will be
845+
reported or logged.
804846

805847
```json
806848
{
@@ -821,6 +863,57 @@ As for other matchers, templating is supported. In case the provided value for t
821863
}
822864
```
823865

866+
### Full flexible list entry property match
867+
868+
Similar to properties, you have full flexibility into matching on a property of a list entry by specifying `list` and using one of WireMock's built-in matchers
869+
The basic syntax:
870+
871+
```json
872+
"list": {
873+
<index-a>: {
874+
<property-a>: <matcher-a>,
875+
<property-b>: <matcher-b>
876+
},
877+
<index-b>: {
878+
<property-a>: <matcher-a>,
879+
<property-b>: <matcher-b>
880+
}
881+
}
882+
```
883+
884+
As index, you can use the actual index as well as `first`, `last`, `-1`.
885+
886+
Example:
887+
888+
```json
889+
{
890+
"request": {
891+
"method": "GET",
892+
"urlPattern": "/test/[^\/]+/[^\/]+",
893+
"customMatcher": {
894+
"name": "state-matcher",
895+
"parameters": {
896+
"list": {
897+
"1": {
898+
"myProperty": {
899+
"contains": "myValue"
900+
}
901+
}
902+
}
903+
}
904+
},
905+
"response": {
906+
"status": 200
907+
}
908+
}
909+
```
910+
911+
The implementation makes use of WireMock's internal matching system and supports any implementation of `StringValuePattern`. As of WireMock 3.3, this includes
912+
`equalTo`,`equalToJson`,`matchesJsonPath`,`matchesJsonSchema`,`equalToXml`,`matchesXPath`,`contains`,`not`,`doesNotContain`,`matches`,`doesNotMatch`,`before`,
913+
`after`,`equalToDateTime`,`anything`,`absent`,`and`,`or`,`matchesPathTemplate`.
914+
For documentation on using these matchers, check the [WireMock documentation](https://wiremock.org/docs/request-matching/)
915+
916+
824917
### Negative context exists match
825918

826919
```json
@@ -931,10 +1024,10 @@ Example with bodyFileName:
9311024

9321025
### Missing properties and defaults
9331026

934-
Missing Helper properties as well as unknown context properties result in using a built-in default.
1027+
Missing Helper properties as well as unknown context properties result in using a built-in default.
9351028

9361029
You can also specify a `default` for the state
937-
helper: `"clientId": "{{state context=request.pathSegments.[1] property='firstname' default='John'}}",` .
1030+
helper: `"clientId": "{{state context=request.pathSegments.[1] property='firstname' default='John'}}",` .
9381031

9391032
If unsure, you may consult the log for to see whether an error occurred.
9401033

@@ -984,7 +1077,7 @@ and setting `verbose=true` or starting WireMock standalone (or docker) with `ver
9841077

9851078
- EventListeners and Matchers report errors with WireMock-internal exceptions. Additionally, errors are logged.
9861079
In order to see them, [register a notifier](https://wiremock.org/3.x/docs/configuration/#notification-logging).
987-
- Response templating errors are printed in the actual response body.
1080+
- Response templating errors are printed in the actual response body.
9881081
- Various actions and decisions of this extensions are logged on info level, along with the context they are happening in.
9891082

9901083
# Examples

0 commit comments

Comments
 (0)