mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-21 17:10:26 +01:00
Updated Stubbing and Request Matching (markdown)
@@ -51,11 +51,68 @@ WireMock supports matching of requests to stubs and verification queries using t
|
||||
* Cookies
|
||||
* Request Body
|
||||
|
||||
### JSON (JsonMatcher)
|
||||
Checks if a JSON object is DeepEqual.
|
||||
|
||||
### JSON Path
|
||||
#### C#
|
||||
```csharp
|
||||
var server = FluentMockServer.Start();
|
||||
server
|
||||
.Given(Request
|
||||
.Create()
|
||||
.WithPath("/jsonmatcher")
|
||||
.WithBody(new JsonMatcher("{ \"x\": 42, \"s\": \"s\" }"))
|
||||
.UsingPost())
|
||||
.WithGuid("debaf408-3b23-4c04-9d18-ef1c020e79f2")
|
||||
.RespondWith(Response.Create().WithBody(@"{ ""result"": ""jsonbodytest"" }"));
|
||||
```
|
||||
|
||||
#### JSON Mapping
|
||||
``` js
|
||||
{
|
||||
"Guid": "debaf408-3b23-4c04-9d18-ef1c020e79f2",
|
||||
"Request": {
|
||||
"Path": {
|
||||
"Matchers": [
|
||||
{
|
||||
"Name": "WildcardMatcher",
|
||||
"Pattern": "/jsonbodytest",
|
||||
"IgnoreCase": false
|
||||
}
|
||||
]
|
||||
},
|
||||
"Methods": [
|
||||
"post"
|
||||
],
|
||||
"Body": {
|
||||
"Matcher": {
|
||||
"Name": "JsonMatcher",
|
||||
"Pattern": "{ \"x\": 42, \"s\": \"s\" }"
|
||||
}
|
||||
}
|
||||
},
|
||||
"Response": {
|
||||
"StatusCode": 200,
|
||||
"Body": "{ \"result\": \"jsonbodytest\" }",
|
||||
"UseTransformer": false
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
```
|
||||
// matching
|
||||
{ "x": 1, "s": "s" }
|
||||
|
||||
// not matching
|
||||
{ "x": 2, "s": "x" }
|
||||
```
|
||||
|
||||
|
||||
### 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 = FluentMockServer.Start();
|
||||
server
|
||||
@@ -66,6 +123,39 @@ server
|
||||
.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" } }
|
||||
@@ -80,14 +170,49 @@ Deems a match if the attribute value is valid XML and matches the XPath expressi
|
||||
An XML document will be considered to match if any elements are returned by the XPath evaluation.
|
||||
WireMock delegates to [XPath2.Net](https://github.com/StefH/XPath2.Net), therefore it support up to XPath version 2.0.
|
||||
|
||||
#### C#
|
||||
```csharp
|
||||
var server = FluentMockServer.Start();
|
||||
server
|
||||
.Given(
|
||||
Request.Create().WithPath("/some/thing").UsingGet()
|
||||
.WithBody(new XPathMatcher("/todo-list[count(todo-item) = 3]"));
|
||||
)
|
||||
.RespondWith(Response.WithBody("Hello"));
|
||||
.Given(Request.Create()
|
||||
.WithPath("/xpath").UsingPost()
|
||||
.WithBody(new XPathMatcher("/todo-list[count(todo-item) = 3]"))
|
||||
)
|
||||
.RespondWith(Response.Create().WithBody("XPathMatcher!"));
|
||||
```
|
||||
|
||||
#### JSON Mapping
|
||||
``` js
|
||||
{
|
||||
"Guid": "abc5848e-cedd-42ad-8f58-4ba6df01180f",
|
||||
"Priority": 0,
|
||||
"Request": {
|
||||
"Path": {
|
||||
"Matchers": [
|
||||
{
|
||||
"Name": "WildcardMatcher",
|
||||
"Pattern": "/xpath",
|
||||
"IgnoreCase": false
|
||||
}
|
||||
]
|
||||
},
|
||||
"Methods": [
|
||||
"post"
|
||||
],
|
||||
"Body": {
|
||||
"Matcher": {
|
||||
"Name": "XPathMatcher",
|
||||
"Pattern": "/todo-list[count(todo-item) = 3]"
|
||||
}
|
||||
}
|
||||
},
|
||||
"Response": {
|
||||
"StatusCode": 200,
|
||||
"BodyDestination": "SameAsSource",
|
||||
"Body": "XPathMatcher!",
|
||||
"UseTransformer": false
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Will match xml below:
|
||||
@@ -106,10 +231,10 @@ The RegexMatcher can be used to match using a regular expression.
|
||||
var server = FluentMockServer.Start();
|
||||
server
|
||||
.Given(
|
||||
Request.Create().WithPath("/reg").UsingGet()
|
||||
Request.Create().WithPath("/reg").UsingPost()
|
||||
.WithBody(new RegexMatcher("H.*o"));
|
||||
)
|
||||
.RespondWith(Response.WithBody("Hello matched with RegexMatcher"));
|
||||
.RespondWith(Response.Create().WithBody("Hello matched with RegexMatcher"));
|
||||
```
|
||||
|
||||
```
|
||||
@@ -131,7 +256,7 @@ server
|
||||
Request.Create().WithPath("/reg").UsingGet()
|
||||
.WithBody(new SimMetricsMatcher("The cat walks in the street."));
|
||||
)
|
||||
.RespondWith(Response.WithBody("Matched with SimMetricsMatcher"));
|
||||
.RespondWith(Response.Create().WithBody("Matched with SimMetricsMatcher"));
|
||||
```
|
||||
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user