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.
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
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:
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 ((<statuscode>, <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
// 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 ;
rng
Enables the rng API to create random values.
This is enabled by default
// choose one of the passed values at random
choose ;
// choose <count> number from the passed values at random
choose_many ;
// alias for choose_many
sample ;
// get a random value between <start> and <end> (exclusive)
range ;
// get <count> random values between <start> and <end> (exclusive)
range_many ;
// get a random char value between <start> and <end> (exclusive)
range_char ;
// get <count> random char values between <start> and <end> (exclusive)
range_char_many ;
// get a random value between <start> and <end> (inclusive)
range_inclusive ;
// get <count> random values between <start> and <end> (inclusive)
range_inclusive_many // get a random char value between <start> and <end> (inclusive)
range_char_inclusive ;
// get <count> random char values between <start> and <end> (inclusive)
range_char_inclusive_many // get a random alpha numeric characters
alpha_numeric ;
// get <count> random alpha numeric numbers
alpha_numeric_many ;
// get a random bool
bool // get a random u8 value
u8 ;
// get a random u16 value
u16 ;
// get a random u32 value
u32 ;
// get a random u64 value
u64 ;
// get a random u128 value
u128 ;
// get a random i8 value
i8 ;
// get a random i16 value
i16 ;
// get a random i32 value
i32 ;
// get a random i64 value
i64 ;
// get a random i128 value
i128 ;
// get a random f32 value
f32 ;
// get a random f64 value
f64 ;
// get <count> random bool values
bool_many ;
// get <count> random u8 values
u8_manycount: usize ;
// get <count> random u16 values
u16_many ;
// get <count> random u32 values
u32_many ;
// get <count> random u64 values
u64_many ;
// get <count> random u128 values
u128_many ;
// get <count> random i8 values
i8_many ;
// get <count> random i16 values
i16_many ;
// get <count> random i32 values
i32_many ;
// get <count> random i64 values
i64_many ;
// get <count> random i128 values
i128_many ;
// get <count> random f32 values
f32_many ;
// get <count> random f64 values
f64_many ;
spec
Enables the spec API to build descriptions for generating random data.
This is not enabled by default
// 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 ;