mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-24 01:08:28 +02:00
Updated Request Matching (markdown)
@@ -167,215 +167,6 @@ server
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## JSON (JsonMatcher)
|
|
||||||
Checks if a JSON object (or JSON as string) is DeepEqual.
|
|
||||||
|
|
||||||
#### C# option 1
|
|
||||||
```csharp
|
|
||||||
var server = WireMockServer.Start();
|
|
||||||
server
|
|
||||||
.Given(Request
|
|
||||||
.Create()
|
|
||||||
.WithPath("/jsonmatcher1")
|
|
||||||
.WithBody(new JsonMatcher("{ \"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 matced regarding any case.
|
|
||||||
```csharp
|
|
||||||
var server = WireMockServer.Start();
|
|
||||||
server
|
|
||||||
.Given(Request
|
|
||||||
.Create()
|
|
||||||
.WithPath("/jsonmatcher3")
|
|
||||||
.WithBody(new JsonMatcher("{ \"x\": 42, \"s\": \"s\" }"), true)
|
|
||||||
.UsingPost())
|
|
||||||
.WithGuid("debaf408-3b23-4c04-9d18-ef1c020e79f2")
|
|
||||||
.RespondWith(Response.Create().WithBody(@"{ ""result"": ""jsonmatcher3 ok"" }"));
|
|
||||||
```
|
|
||||||
|
|
||||||
#### JSON Mapping option 3
|
|
||||||
``` js
|
|
||||||
{
|
|
||||||
"Guid": "debaf408-3b23-4c04-9d18-ef1c020e79f2",
|
|
||||||
"Request": {
|
|
||||||
"Path": {
|
|
||||||
"Matchers": [
|
|
||||||
{
|
|
||||||
"Name": "WildcardMatcher",
|
|
||||||
"Pattern": "/jsonmatcher1"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"Methods": [
|
|
||||||
"post"
|
|
||||||
],
|
|
||||||
"Body": {
|
|
||||||
"Matcher": {
|
|
||||||
"Name": "JsonMatcher",
|
|
||||||
"IgnoreCase": true,
|
|
||||||
"Pattern": "{ \"x\": 42, \"s\": \"s\" }"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"Response": {
|
|
||||||
"StatusCode": 200,
|
|
||||||
"Body": "{ \"result\": \"jsonmatcher3 ok\" }",
|
|
||||||
"UseTransformer": false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
```
|
|
||||||
// matching
|
|
||||||
{ "X": 42, "s": "S" }
|
|
||||||
```
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### JSON Path (JsonPathMatcher)
|
|
||||||
Deems a match if the attribute value is valid JSON and matches the JSON Path expression supplied.
|
|
||||||
A JSON body will be considered to match a path expression if the expression returns either a non-null single value (string, integer etc.), or a non-empty object or array.
|
|
||||||
|
|
||||||
#### C#
|
|
||||||
```csharp
|
|
||||||
var server = WireMockServer.Start();
|
|
||||||
server
|
|
||||||
.Given(
|
|
||||||
Request.Create().WithPath("/some/thing").UsingGet()
|
|
||||||
.WithBody(new JsonPathMatcher("$.things[?(@.name == 'RequiredThing')]"));
|
|
||||||
)
|
|
||||||
.RespondWith(Response.Create().WithBody("Hello"));
|
|
||||||
```
|
|
||||||
|
|
||||||
#### JSON Mapping
|
|
||||||
``` js
|
|
||||||
{
|
|
||||||
"Guid": "e4a600b8-9d6f-453f-90c6-3db2b0885ddb",
|
|
||||||
"Request": {
|
|
||||||
"Path": {
|
|
||||||
"Matchers": [
|
|
||||||
{
|
|
||||||
"Name": "WildcardMatcher",
|
|
||||||
"Pattern": "/jsonpath",
|
|
||||||
"IgnoreCase": false
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"Methods": [
|
|
||||||
"put"
|
|
||||||
],
|
|
||||||
"Body": {
|
|
||||||
"Matcher": {
|
|
||||||
"Name": "JsonPathMatcher",
|
|
||||||
"Pattern": "$.things[?(@.name == 'RequiredThing')]"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"Response": {
|
|
||||||
"StatusCode": 200,
|
|
||||||
"BodyDestination": "SameAsSource",
|
|
||||||
"Body": "{ \"result\": \"JsonPathMatcher !!!\"}",
|
|
||||||
"UseTransformer": false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
```
|
|
||||||
// matching
|
|
||||||
{ "things": { "name": "RequiredThing" } }
|
|
||||||
{ "things": [ { "name": "RequiredThing" }, { "name": "Wiremock" } ] }
|
|
||||||
// not matching
|
|
||||||
{ "price": 15 }
|
|
||||||
{ "things": { "name": "Wiremock" } }
|
|
||||||
```
|
|
||||||
|
|
||||||
### Jmes Path (JmesPathMatcher)
|
### Jmes Path (JmesPathMatcher)
|
||||||
The JMESPath language is described in an ABNF grammar with a complete specification.
|
The JMESPath language is described in an ABNF grammar with a complete specification.
|
||||||
A JSON body will be considered to match a path expression if the expression returns either a non-null single value (string, integer etc.), or a non-empty object or array.
|
A JSON body will be considered to match a path expression if the expression returns either a non-null single value (string, integer etc.), or a non-empty object or array.
|
||||||
|
|||||||
Reference in New Issue
Block a user