GithubHelp home page GithubHelp logo

cafetera's Introduction

CAFETERA

Test

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 TOML configuration file.

Requirements

  • Rust

Setup

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 with your desired port number and <config_path> with the path to your configuration JSON file:

cargo run <port> <config_path>

OR

CAFETERA <port> <config_path>

Configuration

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": ""
}
'''

Usage

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:

  • {{path}}: The path of the request
  • {{query}}: The query string of the request
  • {{rand}}: A random number between 0 and 100
  • {{arg.<name>}}: The value of the query parameter with the specified name
  • {{header.<name>}}: The value of the header with the specified name
  • {{<name>}}: The value of the path parameter at the specified index

Contributions

Contributions are welcome. Please feel free to submit pull requests or open issues to suggest improvements or add new features.

cafetera's People

Contributors

az107 avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.