#mocking #coding #zero #seconds #mock-server #api-server #mock-api

app mocks

Get a mock REST APIs with zero coding within seconds

5 releases

0.3.7 Oct 18, 2024
0.3.6 Oct 9, 2024
0.3.5 Sep 25, 2024
0.2.2 Sep 16, 2024
0.1.1 Sep 10, 2024

#74 in Testing

Download history 279/week @ 2024-09-09 752/week @ 2024-09-16 368/week @ 2024-09-23 37/week @ 2024-09-30 168/week @ 2024-10-07 183/week @ 2024-10-14 14/week @ 2024-10-21

410 downloads per month

MIT license

55KB
1.5K SLoC

mocks

Crates.io msrv 1.65.0 License

Get a mock REST APIs with zero coding within seconds.

Install

If you're a macOS Homebrew user, then you can install mocks from homebrew-tap.

brew install mocks-rs/tap/mocks

If you're a Rust programmer, mocks can be installed with cargo.

cargo install mocks

Usage

Run a REST API server

Create a storage.json.

{
  "posts": [
    { "id": "01J7BAKH37HPG116ZRRFKHBDGB", "title": "first post", "views": 100 },
    { "id": "01J7BAKH37GE8B688PT4RC7TP4", "title": "second post", "views": 10 }
  ],
  "comments": [
    { "id": 1, "text": "a comment", "post_id": "01J7BAKH37HPG116ZRRFKHBDGB" },
    { "id": 2, "text": "another comment", "post_id": "01J7BAKH37HPG116ZRRFKHBDGB" }
  ],
  "profile": { "id": "01J7BAQE1GMD78FN3J0FJCNS8T", "name": "mocks" },
  "friends": []
}

Pass it to mocks CLI.

mocks storage.json
mocks -H 127.0.0.1 -p 8080 storage.json

Get a REST API with curl.

% curl http://localhost:3000/posts/01J7BAKH37HPG116ZRRFKHBDGB
{"id":"01J7BAKH37HPG116ZRRFKHBDGB","title":"first post","views":100}

Routes

Based on the example storage.json, you'll get the following routes:

GET     /posts
GET     /posts/:id
POST    /posts
PUT     /posts/:id
PATCH   /posts/:id
DELETE  /posts/:id

# Same for comments and friends
GET     /profile
PUT     /profile
PATCH   /profile
GET     /_hc

# Health check endpoint returns a 204 response.

Options

Run mocks --help for a list of options.

Developer mode

To help with debugging, you can enable a special feature that saves mock data to a separate file.

To do this, simply set the environment variable called MOCKS_DEBUG_OVERWRITTEN_FILE.

MOCKS_DEBUG_OVERWRITTEN_FILE=storage.debug.json cargo run -- storage.json

We recommend specifying the filename as *.debug.json. For more details, please check .gitignore file.

LICENSE

This project is licensed under the MIT license.

Dependencies

~6–14MB
~160K SLoC