Crates.io | cafetera |
lib.rs | cafetera |
version | 0.2.5 |
source | src |
created_at | 2024-06-12 10:04:46.664087 |
updated_at | 2024-07-16 19:11:50.164783 |
description | simple HTTP mock server |
homepage | |
repository | |
max_upload_size | |
id | 1269489 |
size | 41,918 |
This is a simple HTTP mock server, designed for mocking API endpoints for testing purposes. It allows you to define custom responses for different HTTP methods and routes through a TOML configuration file.
Requirements
To get the server running, follow these steps:
Clone the repository to your local machine. Ensure you have Rust installed. If not, install Rust using rustup. Navigate to the root directory of the project. Build the project using Cargo:
cargo build
Run the server with the following command, replacing
cargo run <port> <config_path>
OR
CAFETERA <port> <config_path>
The server's behavior is defined by a JSON configuration file. Below is an example of the configuration file structure:
[[endpoints.GET]]
path = "/health"
status = 200
body = "API is up and running"
[[endpoints.GET]]
path = "/users"
status = 200
body = '''
[
{
"id": "{{rand}}", <-- this will be replaced with a random number
"name": "{{arg.name}}",
"email": "",
"path": "{{path}}"
}
]
'''
[[endpoints.GET]]
path = "/users/{{name}}"
status = 200
body = '''
{
"id": 1,
"name": "{{name}}",
"email": ""
}
'''
[[endpoints.GET]]
path = "/users/{{name}}/{{id}}"
status = 200
body = '''
{
"id": "{{id}}",
"name": "{{name}}",
"email": ""
}
'''
[[endpoints.POST]]
path = "/users"
status = 201
body = '''
{
"id": "{{rand}}",
"name": "Jane Doe",
"email": ""
}
'''
After starting the server, it will listen for HTTP requests on the specified port. The server matches incoming requests against the paths defined in the configuration file and responds with the corresponding status code and body.
Available wildcard variables:
Contributions are welcome. Please feel free to submit pull requests or open issues to suggest improvements or add new features.