Updated Request Matching JsonPartialMatcher (markdown)

Stef Heyenrath
2020-11-17 17:30:28 +01:00
parent ecca6968e8
commit fe879c65e7

@@ -1 +1,106 @@
.
## JSON (JsonPartialMatcher)
Checks if a JSON object has a partial match.
Example:
Matcher value
`{"test":"abc"}` against input `{"test":"abc","other":"xyz"}` is matched by this JsonPartialMatcher.
#### C# option 1
```csharp
var server = WireMockServer.Start();
server
.Given(Request
.Create()
.WithPath("/jsonpartialmatcher1")
.WithBody(new JsonPartialMatcher("{ \"x\": 42, \"s\": \"s\" }"))
.UsingPost())
.WithGuid("debaf408-3b23-4c04-9d18-ef1c020e79f2")
.RespondWith(Response.Create().WithBody(@"{ ""result"": ""jsonbodytest1"" }"));
```
#### JSON Mapping option 1
``` js
{
"Guid": "debaf408-3b23-4c04-9d18-ef1c020e79f2",
"Request": {
"Path": {
"Matchers": [
{
"Name": "WildcardMatcher",
"Pattern": "/jsonmatcher1"
}
]
},
"Methods": [
"post"
],
"Body": {
"Matcher": {
"Name": "JsonMatcher",
"Pattern": "{ \"x\": 42, \"s\": \"s\" }"
}
}
},
"Response": {
"StatusCode": 200,
"Body": "{ \"result\": \"jsonbodytest\" }",
"UseTransformer": false
}
}
```
#### C# option 2
```csharp
var server = WireMockServer.Start();
server
.Given(Request
.Create()
.WithPath("/jsonmatcher2")
.WithBody(new JsonMatcher(new { x = 42, s = "s" }))
.UsingPost())
.WithGuid("debaf408-3b23-4c04-9d18-ef1c020e79f2")
.RespondWith(Response.Create().WithBody(@"{ ""result"": ""jsonbodytest2"" }"));
```
#### JSON Mapping option 2
``` js
{
"Guid": "debaf408-3b23-4c04-9d18-ef1c020e79f2",
"Request": {
"Path": {
"Matchers": [
{
"Name": "WildcardMatcher",
"Pattern": "/jsonmatcher2"
}
]
},
"Methods": [
"post"
],
"Body": {
"Matcher": {
"Name": "JsonMatcher",
"Pattern": { "x": 42, "s": "s" }
}
}
},
"Response": {
"StatusCode": 200,
"Body": "{ \"result\": \"jsonbodytest2\" }",
"UseTransformer": false
}
}
```
```
// matching
{ "x": 42, "s": "s" }
// not matching
{ "x": 42, "s": "?" }
```
#### C# option 3
It's also possible to use set `IgnoreCase` to true, this means that the PropertNames and PropertyValues will be matched regarding any case.
Same logic as the normal JsonMatcher.