| Crates.io | mockforge-core |
| lib.rs | mockforge-core |
| version | 0.3.31 |
| created_at | 2025-10-16 19:46:50.308836+00 |
| updated_at | 2026-01-04 23:23:26.425582+00 |
| description | Shared logic for MockForge - routing, validation, latency, proxy |
| homepage | https://mockforge.dev |
| repository | https://github.com/SaaSy-Solutions/mockforge |
| max_upload_size | |
| id | 1886655 |
| size | 5,532,761 |
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.
MockForge Core includes:
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(())
}
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(())
# }
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));
openapi]: Parse and work with OpenAPI specificationsopenapi_routes]: Register routes from OpenAPI specs with validationvalidation]: Request/response validation against schemasrouting]: Route matching and registrationtemplating]: Template variable expansion ({{uuid}}, {{now}}, etc.)request_chaining]: Multi-step request workflowsoverrides]: Dynamic request/response modificationslatency]: Latency injection with configurable profilesfailure_injection]: Simulate service failures and errorstraffic_shaping]: Bandwidth limiting and packet lossproxy]: Forward requests to upstream servicesws_proxy]: WebSocket proxy with message transformationworkspace]: Workspace management for organizing mocksworkspace_import]: Import from Postman, Insomnia, cURL, HARrecord_replay]: Record real requests and replay as fixturesrequest_logger]: Centralized request loggingperformance]: Performance metrics and profilingThis 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)See the examples directory for complete working examples.
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