This project creates a custom WireMock server for mocking RESTful services. It’s designed to run as a standalone service and can be configured via a simple properties file. Ideal for testing and developing against mock APIs.
Note: Currently, the master branch contains a very simple version of the Custom WireMock.
- Request Mapping: Supports GET, POST, PUT, DELETE requests with customizable response mappings.
- Configurable: Define all behavior via the
config.propertiesfile. - Flexible URL Matching: Use regex to match URLs and request bodies.
- Docker Support: Run seamlessly in Docker with minimal setup.
- Docker: Required for running the server in a containerized environment.
- Java: Required if running locally from an IDE.
- Configuration Files: Ensure the
config.propertiesand response files are correctly placed.
-
Place
config.propertiesand__filesinsrc/main/resources/. -
Run the
mainfunction with the argumentlocal:java -jar application.jar local
-
Directory Structure: Organize your project as follows:
├── docker-compose.yml └── resources ├── __files │ ├── addEmployee.json │ ├── deleteEmployee.json │ ├── helloWorld.json │ └── updateEmployee.json └── config.properties -
Docker Compose File: Create a
docker-compose.ymlfile:version: "3.8" services: echoserve: image: docker.io/jaracogmbh/echoserve:1.0.0 platform: linux/amd64 volumes: - ./resources:/data:ro ports: - "8089:8089"
-
Running the Server: From the directory containing
docker-compose.yml, run:docker-compose up
Each request configuration in config.properties must follow a specific format:
request1.requestType=GET
request1.statusCode=200
request1.url=/helloWorld
request1.contentType=application/json
request1.response=helloWorld.json- Request Configuration:
- Prefix: Each configuration starts with
requestfollowed by a unique identifier. - Properties:
requestType: The HTTP method (GET, POST, PUT, DELETE).statusCode: The HTTP status code to return.url: The URL path to map.contentType: The response content type.response: The file containing the response body.requestBody: (For POST and PUT) The expected request body.
- Prefix: Each configuration starts with
Utilize WireMock's urlMatching functionality for regex-based matching:
request1.requestType=GET
request1.statusCode=200
request1.url=/getEmployee\\?([a-z]*)=([0-9]*)
request1.contentType=application/json
request1.response=employee.json
request2.requestType=POST
request2.statusCode=200
request2.requestBody=.*
request2.url=/addEmployee
request2.contentType=application/json
request2.response=addEmployee.json- Examples:
- The first request matches the URL
/getEmployee?id=1. - The second request matches any non-empty POST body.
- The first request matches the URL
- Response files should be placed in a
__filesdirectory. - By default, this directory is located at
src/main/resources/. - The location can be customized in the configuration file.
The server's behavior can be configured via config.properties with the following properties:
- port: The port on which the server runs.
- hostname: The hostname for the server.
- fileLocation: The location of the response files and configuration file.
- The
__filesdirectory is located atsrc/test/resources/by default. - When running the server from an IDE, the default location is
src/main/resources/. - When using Docker Compose, set this to
/datainside the container.
- The
-
Local: Run the server with the command:
java -jar application.jar local -
Docker: Run the server using Docker Compose:
docker-compose up
This example demonstrates how to set up your configuration file and response file for the Custom WireMock Project.
Place this file in the /resources directory. It defines the configuration for a GET request to the /helloWorld endpoint, returning a JSON response.
fileLocation=/data
hostname=localhost
port=8089
request1.requestType=GET
request1.statusCode=200
request1.url=/helloWorld
request1.contentType=application/json
request1.response=helloWorld.jsonThis file contains the JSON response for the configured endpoint. It should be placed in the /resources/__files directory.
{
"message": "Hello World"
}Ensure the following file structure is in place:
.
└── resources
├── config.properties
└── __files
└── helloWorld.json
config.propertiesshould be located in the/resourcesdirectory.helloWorld.jsonshould be located in the/resources/__filesdirectory.
{
"message": "Hello World"
}- No Config Directory Given: Ensure the
config.propertiesfile is correctly placed and accessible. - File Permissions: Verify that the files have the correct permissions for the Docker container.