Mockbox
A flexible HTTP proxy server powered by Rune scripting. Every incoming request is first handled by a Rune script, which can either respond directly or indicate that the request should be proxied to an upstream server.
Table of Contents
Features
- Rune Scripting: Handle HTTP requests with dynamic Rune scripts
- Upstream Proxy: Automatically proxy unhandled requests to another web server
- Hot-reloadable: Modify scripts without restarting
- Full HTTP Support: Access method, path, headers, and body in scripts
Installation
Pre-Built Binaries (via cargo-binstall)
Pre-Built Binaries (manual download)
You can download pre-built binaries from the latest release.
From Source
- Clone the repository
- Build the project:
Usage
Basic Setup
- Generate the an example script (online version):
If you want to learn more about rune, check out the rune book.
- Run the server:
If no script path is passed to mockbox, it will check for a file called mockbox.rn in the current directory and if it doesn't exist, it will check in $HOME/.local/share/mockbox for a mockbox.rn.
- The server will start on
http://127.0.0.1:3333
Configuration
Configure the upstream server using the MOCKBOX_UPSTREAM environment variable:
MOCKBOX_UPSTREAM=http://localhost:8080
or by using the appropriate cli option:
Rune Script API
Your mockbox.rn must export a handle_request function that receives a request object and returns either a string, an object, a tuple ((<status_code>, <response>)).
Request Object
The request object passed to your handler contains:
method: HTTP method (e.g., "GET", "POST")path: Request path (e.g., "/api/users")body: Request body as a string
Response Options
1. Return a simple string
Return just a string for a 200 OK response:
2. Return an object
Return an object for a 200 OK response:
The object will automatically be converted to json.
3. Return status and response
Return a tuple with status and response (string or object):
4. Proxy to upstream server
Explicitly return nothing to proxy the request:
Example Scripts
Mock API Endpoints
Route-based Handling
Conditional Mocking
Error Responses
Architecture
- Request Reception: Axum receives the HTTP request
- Rune Execution: The
handle_requestfunction inmockbox.rnis called - Response Decision:
- If the script returns a string, object or response tuple → respond directly
- If the script doesn't return anything or explicitly returns
()→ proxy to upstream server
- Upstream Proxy: Forward the original request to the configured upstream URL
- Response: Return the response from either Rune or the upstream server
Use Cases
- Mock APIs: Create mock responses for testing frontend applications
- Request Interception: Log, modify, or reject requests based on conditions
- A/B Testing: Route requests to different backends based on rules
- Development Environment: Override specific endpoints while keeping others real
Testing
Test your Rune scripts by making HTTP requests:
# Test a mocked endpoint
# Test the upstream proxy
Features
cache
Enables the cache API to persist data between requests.
This is enabled by default
Cache API
// store a rune value
set ;
// load a stored value
get ;
// delete a stored value
delete ;
// check if a value exists
has ;
// clear the whole cache
clear ;
// get keys of all stored values
keys ;
Cache Example
=> ,
rugen
Enables the rugen API to build descriptions for generating random data.
This is enabled by default
Rugen Example
This is what describing data with RuGen looks like:
use *;
describe?
For more information on RuGen, please visit the project repository.