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
storage
Enables the storage API to persist data between requests.
This is enabled by default
Storage 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 storage
clear ;
// get keys of all stored values
keys ;
Storage Example
=> ,
spec
Enables the spec API to build descriptions for generating random data.
This is enabled by default
Spec API
// creates a spec that evaluates to the passed value
just ;
// creates a spec that evaluates to a random boolean
bool ;
// creates a spec that evaluates to a random u128 between <min> and <max> (exclusive)
uint ;
// creates a spec that evaluates to a random i128 between <min> and <max> (exclusive)
int ;
// creates a spec that evaluates to a random f64 between <min> and <max> (exclusive)
float ;
// creates a spec that evaluates to a string of random alpha numeric characters that is <len> long
alphanumeric ;
// creates a spec that evaluates to a string of random characters between <min> and <max> (exclusive)
string ;
// creates a spec that evaluates to a random value from the passed vec
one_of ;
// creates a spec that evaluates to a weighted random value from the passed vec
weighted ;
// creates a spec that evaluates to a vec of length <len>, filled with values defined by <item>
array ;
// creates a spec that evaluates to a an object
object ;
// creates a spec that has a 0.0 < p < 1.0 chance to evaluate to an optional value defined by <item>
optional ;
// creates a spec that takes all items in a vec and evaluates them to values, according to their spec
tuple ;
// evaluates a given spec
generate ;
Spec Example
use spec as s;
match path.parts