Skip to content

Commit

Permalink
Improve condition docs
Browse files Browse the repository at this point in the history
+semver: feature
  • Loading branch information
natenho committed Jun 11, 2023
1 parent f11e981 commit 2048f51
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
27 changes: 24 additions & 3 deletions website/docs/reference/request/condition-attribute.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,31 @@ If omitted, empty or null, defaults to ```true```.
```json
{
"request": {
"condition": "<#= DateTime.Now.Second % 2 == 0 #>"
"method": "GET",
"route": "customers/{id}/accounts/{account_id}",
"condition": "<#= DateTime.Now.Second % 2 == 0 #>"
},
"response": {
"status": "OK"
"body": "Match!"
}
}
```
```

In the example above, the mock will be returned for `GET customers/1234/accounts/123ABC`, leveraging `condition` [scripting](/docs/scripting) to ensure that the mock matches only if the current time has an even second.

The condition can also be used to match based on query parameters:

```json
{
"request": {
"method": "GET",
"route": "any/{myVar}",
"condition": "<#= Request.Query["foo"]?.ToString() == "bar" #>"
},
"response": {
"body": "Hello!"
}
}
```

The mock will be returned for `GET any/xxx?foo=bar`
4 changes: 2 additions & 2 deletions website/docs/reference/request/method-attribute.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ If omitted, defaults to ```GET```.
```json
{
"request": {
"method": "GET"
"method": "GET"
},
"response": {
"status": "OK"
"status": "OK"
}
}
```
4 changes: 2 additions & 2 deletions website/docs/reference/request/route-attribute.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ If omitted, empty or null, defaults to base route (/).
```json
{
"request": {
"route": "customers/{id}/accounts/{account_id}"
"route": "customers/{id}/accounts/{account_id}"
},
"response": {
"status": "OK"
"status": "OK"
}
}
```
10 changes: 8 additions & 2 deletions website/docs/request-matching/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ When a request is received, Mockaco follows a specific process:

Mockaco searches for files in alphabetical order.
1. It compares the request against the criteria specified in the request object of each file.
2. The first match that meets the criteria is selected.
2. The first match that meets the criteria is selected. In case of ambiguity, Mockaco will prioritize mocks that have a condition.
3. If no matching Mock file is found, Mockaco returns a default response of HTTP 501 (Not Implemented). Additionally, it provides a list of possible file parsing errors.

This process ensures that Mockaco handles incoming requests and provides appropriate responses based on the available mock files. In case of any errors, the default response serves as a helpful indicator for troubleshooting.
This process ensures that Mockaco handles incoming requests and provides appropriate responses based on the available mock files. In case of any errors, the default response serves as a helpful indicator for troubleshooting.

## Criteria

The request matching is based on the `request` object defined in the mock template.

Please refer to [request object reference](/docs/reference/request) for further details on how to use each criteria.

0 comments on commit 2048f51

Please sign in to comment.