Skip to content

Commit

Permalink
Merge pull request #1 from Az107/feature/toml_config
Browse files Browse the repository at this point in the history
Feature/toml config
  • Loading branch information
Az107 authored Jun 12, 2024
2 parents 7611fc6 + 283c9df commit 0ad0fae
Show file tree
Hide file tree
Showing 10 changed files with 241 additions and 464 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
.DS_Store
.DS_Store
64 changes: 64 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ edition = "2021"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0.115"
serde_with = "3.7.0"
toml = "0.8.14"
hteapot = {path = "../hteapot"}
96 changes: 50 additions & 46 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

## Description

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 JSON configuration file.
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
- Rust
Expand Down Expand Up @@ -34,54 +34,58 @@ CAFETERA <port> <config_path>

The server's behavior is defined by a JSON configuration file. Below is an example of the configuration file structure:

```json
```toml
[[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 = '''
{
"get": [
{
"/health": {
"status": 200,
"body": "API is up and running"
}
},
{
"/users": {
"status": 200,
"body": [
{
"id": 1,
"name": "John Doe",
"email": "test@example.org",
"path": "{{path}}" // This will be replaced with the path of the request
}
]
}
},
{
"/users/{{name}}": {
"status": 200,
"body": {
"id": 1,
"name": "{{name}}",
"email": "test@example.org"
}

}
}
],
"post": [
{
"/users": {
"status": 201,
"body": {
"id": 2,
"name": "Jane Doe",
"email": ""
}
}
}
]
"id": "{{rand}}",
"name": "Jane Doe",
"email": ""
}
'''
```
## Usage

Expand Down
55 changes: 0 additions & 55 deletions demo_config.json

This file was deleted.

51 changes: 51 additions & 0 deletions demo_config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
[[endpoints.GET]]
path = "/health"
status = 200
body = "API is up and running"

[[endpoints.GET]]
path = "/users"
status = 200
body = '''
[
{
"id": "{{rand}}",
"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": ""
}
'''
Binary file modified src/.DS_Store
Binary file not shown.
Loading

0 comments on commit 0ad0fae

Please sign in to comment.