Updated Stubbing and Request Matching (markdown)

Stef Heyenrath
2018-07-20 20:17:08 +02:00
parent 5c46c49647
commit da581f4659

@@ -383,12 +383,91 @@ The model of the request is supplied to the header and body templates. The follo
* `request.cookies.<key>` - Value of a request cookie e.g. request.cookies.JSESSIONID
* `request.body` - Request body text (avoid for non-text bodies)
#### Handlebars helpers
### Handlebars helpers
All of the standard helpers (template functions) provided by the C# Handlebars implementation plus all of the string helpers are available e.g.
`{{capitalize request.query.search}}`
### JsonPath support
JsonPath support is also present (internal logic is based on Newtonsoft.Json).
Two functions are present:
1. JsonPath.SelectToken
2. JsonPath.SelectTokens
#### JsonPath.SelectToken
This can be used in C# like:
```csharp
var server = FluentMockServer.Start();
server
.Given(Request.Create().WithPath("/jsonpathtestToken").UsingPost())
.RespondWith(Response.Create()
.WithHeader("Content-Type", "application/json")
.WithBody("{{JsonPath.SelectToken request.body \"$.Manufacturers[?(@.Name == 'Acme Co')]\"}}")
.WithTransformer()
);
```
Or using the admin mapping file:
``` js
{
"Request": {
"Path": {
"Matchers": [
{
"Name": "WildcardMatcher",
"Pattern": "/jsonpathtestToken"
}
]
},
"Methods": [
"post"
]
},
"Response": {
"StatusCode": 200,
"BodyDestination": "SameAsSource",
"Body": "{{JsonPath.SelectToken request.body \"$.Manufacturers[?(@.Name == 'Acme Co')]\"}}",
"UseTransformer": true,
"Headers": {
"Content-Type": "application/json"
}
}
}
```
Note that also replacing values in a Json Object and returning a the body as Json is supported, to use this, use a mapping file like this:
``` js
{
"Request": {
"Path": {
"Matchers": [
{
"Name": "WildcardMatcher",
"Pattern": "/test"
}
]
},
"Methods": [
"post"
]
},
"Response": {
"StatusCode": 200,
"BodyAsJson": {
"path": "{{request.path}}",
"result": "{{JsonPath.SelectToken request.bodyAsJson \"username\"}}"
},
"UseTransformer": true,
"Headers": {
"Content-Type": "application/json"
}
}
}
```
## Stub priority
It is sometimes the case that youll want to declare two or more stub mappings that overlap, in that a given request would be a match for more than one of them.
It is sometimes the case that youll want to declare two or more stub mappings that "overlap", in that a given request would be a match for more than one of them.
One example of this might be where you want to define a catch-all stub for any URL that doesnt match any more specific cases. Adding a priority to a stub mapping facilitates this: