Here's a simple mock for a REST service - a basic service1/status service which returns a JSON. The prefix for these automatic services (one per message) is /diesel/react/
which normally you configure as the URL+prefix of the service implementation.
When you click service1/status, the following mock rule is executed:
0 $mock::
service1.status
. (payload
={status:"ok",someStats:"123"}
)
Note that you can't use this to serve /myStatus - here's a quick way to do that (the prefix in this case is /diesel/rest/
- see the link):
0 $mock::
diesel.rest (path == "/myStatus"
)
. (payload
={status:"ok",someStats:"123"}
)
We can use a simple path mapping, fairly common in matching REST URLs (note the special ~path
operator):
Data not found:
0 $mock::
diesel.rest (path ~path "/account2/404/id"
)
diesel.flow.return (diesel.http.response.status:Number=404
=404, diesel.http.response.header.myHeader="mine"
="mine")
Success finding data:
0 $mock::
diesel.rest (path ~path "/account2/:acctId/id"
)
. (payload
={status:"ok",accountId:acctId}
)
$when::
diesel.rest (path ~path "/path2/*idpath"
)
. (payload
={status:"ok",accountId:idpath}
)
If you have complex paths to match, or if you want a custom path with anonymous elements that you ignore:
use a regular expression and the ~=
operator. You can call this via an url like /getAccount/54554 :
0 $mock::
diesel.rest (path ~= "/getAccount/(\d+)"
)
. (payload
={status:"ok",someStats:"456"}
)
You can also use a custom path with named elements: use named capture groups in the regular expression - these are parsed and defined as values in the context. You can call this via an url like /getAccount1/54554 :
0 $mock::
diesel.rest (path ~= "/getAccount1/(?<acctId>\d+)"
)
. (payload
={status:"ok",someStats:"67",accountId:acctId}
)
0 $mock::
diesel.rest (path ~= "/getAccount/(\d+)"
)
ctx.set (dieselx.x="asdf"
="asdf")
. (payload
={status:"ok",someStats:"456"}
)
You need to log in to post a comment!