mockbox-0.2.0 is not a library.
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
- Clone the repository
- Build the project:
Usage
Basic Setup
- Run the server:
- The server will start on
http://0.0.0.0:333
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 script.rn must export a handle_request function that receives a request object and returns either a response object or the string "UNHANDLED".
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. Handle the request
Return an object with status and body fields:
2. Return a simple string
Return just a string for a 200 OK response:
3. Proxy to upstream server
Return the string "UNHANDLED" 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 inscript.rnis called - Response Decision:
- If the script returns a response object or string → respond directly
- If the script returns
"UNHANDLED"→ 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