22 releases
| new 0.3.7 | Dec 6, 2025 |
|---|---|
| 0.3.6 | Dec 5, 2025 |
| 0.3.4 | Nov 27, 2025 |
| 0.2.9 | Nov 14, 2025 |
| 0.1.6 | Oct 23, 2025 |
#695 in Testing
2,212 downloads per month
Used in 28 crates
(26 directly)
4.5MB
94K
SLoC
MockForge Core
Core functionality and shared logic for the MockForge mocking framework.
This crate provides the foundational building blocks used across all MockForge protocols (HTTP, WebSocket, gRPC, GraphQL). It can be used as a library to programmatically create and manage mock servers, or to build custom mocking solutions.
Overview
MockForge Core includes:
- Routing & Validation: OpenAPI-based route registration and request validation
- Request/Response Processing: Template expansion, data generation, and transformation
- Chaos Engineering: Latency injection, failure simulation, and traffic shaping
- Proxy & Hybrid Mode: Forward requests to real backends with intelligent fallback
- Request Chaining: Multi-step request workflows with context passing
- Workspace Management: Organize and persist mock configurations
- Observability: Request logging, metrics collection, and tracing
Quick Start: Embedding MockForge
Creating a Simple HTTP Mock Server
use mockforge_core::{
OpenApiSpec, OpenApiRouteRegistry, ValidationOptions,
LatencyProfile, Config,
};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Load OpenAPI specification
let spec = OpenApiSpec::from_file("api.json").await?;
// Create route registry with validation
let registry = OpenApiRouteRegistry::new(spec, ValidationOptions::default());
// Configure core features
let config = Config {
latency_enabled: true,
failures_enabled: false,
default_latency: LatencyProfile::normal(),
..Default::default()
};
// Build your HTTP server with the registry
// (See mockforge-http crate for router building)
Ok(())
}
Request Chaining
Chain multiple requests together with shared context:
use mockforge_core::{ChainDefinition, ChainRequest, RequestChainRegistry};
# async fn example() -> Result<(), Box<dyn std::error::Error>> {
let mut registry = RequestChainRegistry::new();
// Define a chain: create user → add to group → verify membership
let chain = ChainDefinition {
name: "user_onboarding".to_string(),
steps: vec![
ChainRequest {
method: "POST".to_string(),
path: "/users".to_string(),
body: Some(r#"{"name": "{{faker.name}}"}"#.to_string()),
extract: vec![("user_id".to_string(), "$.id".to_string())],
..Default::default()
},
ChainRequest {
method: "POST".to_string(),
path: "/groups/{{user_id}}/members".to_string(),
..Default::default()
},
],
};
registry.register_chain("user_onboarding", chain)?;
# Ok(())
# }
Latency & Failure Injection
Simulate realistic network conditions and errors:
use mockforge_core::{LatencyProfile, FailureConfig, create_failure_injector};
// Configure latency simulation
let latency = LatencyProfile::slow(); // 300-800ms
// Configure failure injection
let failure_config = FailureConfig {
global_error_rate: 0.05, // 5% of requests fail
default_status_codes: vec![500, 502, 503],
..Default::default()
};
let injector = create_failure_injector(Some(failure_config));
Key Modules
OpenAPI Support
openapi: Parse and work with OpenAPI specificationsopenapi_routes: Register routes from OpenAPI specs with validationvalidation: Request/response validation against schemas
Request Processing
routing: Route matching and registrationtemplating: Template variable expansion ({{uuid}}, {{now}}, etc.)request_chaining: Multi-step request workflowsoverrides: Dynamic request/response modifications
Chaos Engineering
latency: Latency injection with configurable profilesfailure_injection: Simulate service failures and errorstraffic_shaping: Bandwidth limiting and packet loss
Proxy & Hybrid
Persistence & Import
workspace: Workspace management for organizing mocksworkspace_import: Import from Postman, Insomnia, cURL, HARrecord_replay: Record real requests and replay as fixtures
Observability
request_logger: Centralized request loggingperformance: Performance metrics and profiling
Feature Flags
This crate supports several optional features:
openapi: OpenAPI specification support (enabled by default)validation: Request/response validation (enabled by default)templating: Template expansion (enabled by default)chaos: Chaos engineering features (enabled by default)proxy: Proxy and hybrid mode (enabled by default)workspace: Workspace management (enabled by default)
Examples
See the examples directory for complete working examples.
Related Crates
mockforge-http: HTTP/REST mock servermockforge-grpc: gRPC mock servermockforge-ws: WebSocket mock servermockforge-graphql: GraphQL mock servermockforge-plugin-core: Plugin developmentmockforge-data: Synthetic data generation
Documentation
Dependencies
~42–96MB
~1.5M SLoC