From 2048f51b070ff4d6e5a72eacd26fa954bf8c47bb Mon Sep 17 00:00:00 2001 From: Renato Lima Date: Sun, 11 Jun 2023 16:38:18 -0300 Subject: [PATCH] Improve condition docs +semver: feature --- .../reference/request/condition-attribute.md | 27 ++++++++++++++++--- .../reference/request/method-attribute.md | 4 +-- .../docs/reference/request/route-attribute.md | 4 +-- website/docs/request-matching/index.md | 10 +++++-- 4 files changed, 36 insertions(+), 9 deletions(-) diff --git a/website/docs/reference/request/condition-attribute.md b/website/docs/reference/request/condition-attribute.md index e6f58a1..c175570 100644 --- a/website/docs/reference/request/condition-attribute.md +++ b/website/docs/reference/request/condition-attribute.md @@ -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!" } } -``` \ No newline at end of file +``` + +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` \ No newline at end of file diff --git a/website/docs/reference/request/method-attribute.md b/website/docs/reference/request/method-attribute.md index 2656f01..3380dc4 100644 --- a/website/docs/reference/request/method-attribute.md +++ b/website/docs/reference/request/method-attribute.md @@ -10,10 +10,10 @@ If omitted, defaults to ```GET```. ```json { "request": { - "method": "GET" + "method": "GET" }, "response": { - "status": "OK" + "status": "OK" } } ``` \ No newline at end of file diff --git a/website/docs/reference/request/route-attribute.md b/website/docs/reference/request/route-attribute.md index 8252efe..1e798a9 100644 --- a/website/docs/reference/request/route-attribute.md +++ b/website/docs/reference/request/route-attribute.md @@ -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" } } ``` \ No newline at end of file diff --git a/website/docs/request-matching/index.md b/website/docs/request-matching/index.md index 74c18d6..c405cd2 100644 --- a/website/docs/request-matching/index.md +++ b/website/docs/request-matching/index.md @@ -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. \ No newline at end of file +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. \ No newline at end of file