Mock resources
For consistent development and testing, restclients uses a set of static files instead of making http calls. Web service clients and standalone modules can provide mock resources.
Mock resource paths consist of a base path, the service name, "file" and then a filesystem safe version of the requested url.
There are two ways to define where mock resources are in the file system. Paths can be registered by calling MockDAO.register_mock_path('/full/path/to/resources/')
, and web service clients can provide paths by defining a service_mock_paths
that returns a list of full paths.
Paths are searched in the order the were added via register_mock_path, and then in the web service client paths. For example, if I have this web service client dao:
from restclients_core.dao import DAO
class DemoDAO(DAO):
def service_name(self):
return 'demo'
def service_mock_paths(self):
return ['/home/demo_user/dev/demo_dao/resources']
and an application that runs this code during startup:
from restclients_core.dao import MockDAO
MockDAO.register_mock_path('/home/demo_user/dev/my_app/resources1')
MockDAO.register_mock_path('/home/demo_user/dev/my_app/resources2')
if DemoDAO().getURL("/v1/user")
is called, the following paths will be searched until a file is found:
- /home/demo_user/dev/my_app/resources1/demo/file/v1/user
- /home/demo_user/dev/my_app/resources1/demo/file/v1/user/index.html
- /home/demo_user/dev/my_app/resources2/demo/file/v1/user
- /home/demo_user/dev/my_app/resources2/demo/file/v1/user/index.html
- /home/demo_user/dev/demo_dao/resources/demo/file/v1/user
- /home/demo_user/dev/demo_dao/resources/demo/file/v1/user/index.html
if no file is found, a 404 response is returned.
Additional control of the response object can be had by adding a .http-headers file. If, in the above example, a file was found at /home/demo_user/dev/my_app/resources2/demo/file/v1/user/index.html
, restclients would look for a file named /home/demo_user/dev/my_app/resources2/demo/file/v1/user/index.html.http-headers
. That file should be valid JSON, and can be in 1 of 2 formats.
Simple format - headers only
{
"X-Demo-Header": "Custom server header"
}
Multi-valued - headers and status
{
"headers": {
"X-Demo-Header": "Custom server header"
},
"status": 418
}