A simple proxy server for serving mocked web service responses from text files. When a file matching the URL path exists, the contents of the file is returned. Otherwise the request is forwarded to the original web service.
Clone the repository and run "npm install"
Copy the config/default-example.json file and name it config/default.json. Configure the application by editing the "config/default.json" file.
Create a new text file in the "mocks-enabled" folder with a mocked web service response. The sub folders and the file name need to match the path of the web service URL. E.g. for a URL like "http://localhost/some/simple/path", the file needs to placed in a folder "mocks-enabled/some/simple" and named "path.txt".
For POST requests the name of the file is defined by the sha1 hash of the request body. It still needs to be placed in the in the sub folder matching the URL.
To enable and disable mocks, just move the mock files between the "mock-enabled" and "mock-available" folders.
To run the application:
node app
To run the test application which will always return a standard response use in the dir test:
node MockTarget.js
You can use extensions to modify mock requests or log entries. All extensions need to placed in the "extensions" folder and registered in the configuration file:
Configuration example:
...
"extensions": {
"mockRequestProcessors": [
"MyMockRequestProcessorExtension"
],
"logProcessors": [
"MyLogProcessorExtension"
]
}
The extension file needs to export an object with a version property and and a "process" function.
Extension example:
module.exports = {
version: 1,
process: function(content) {
content.logEntry.info = "some more info here"
return content.logEntry;
}
};
A straightforward UI can be accessed via the URL "DefinedURLInConfigFile/admin/ui", e.g. "http://127.0.0.1:8001/admin/ui". It offers some nice features like
- Create mocks directly from captured requests and their corresponding responses which were captured by the mock proxy.
- Manage existing mocks (activation/deactivation/edit/delete/response preview).
- Track the last x mocks which were returned by the mock proxy.
- Create mock sets to group mocks together.
- Take a look at the mock proxy logs.
The UI was ported from a Chrome plugin, which can be found here.