Skip to content

πŸ§ͺ The effortless HTTP mock server for seamless API testing

License

Notifications You must be signed in to change notification settings

ararog/easyhttpmock

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

20 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

EasyHttpMock

πŸ§ͺ The effortless HTTP mock server for seamless API testing

EasyHttpMock is a powerful yet simple HTTP mock server designed specifically for testing HTTP clients. Built on top of VeTiS, it provides a clean, intuitive API for creating realistic mock endpoints that simulate real-world API behavior, making your testing workflow faster and more reliable.

✨ Why EasyHttpMock?

  • 🎯 Testing-Focused: Purpose-built for HTTP client testing scenarios
  • ⚑ Lightning Fast: Powered by VeTiS for optimal performance
  • πŸ”§ Flexible Runtime: Choose between Tokio or Smol async runtimes
  • 🌐 Full Protocol Support: HTTP/1, HTTP/2, and HTTP/3 compatibility
  • πŸ›‘οΈ Secure Testing: Built-in TLS support for HTTPS endpoint testing
  • πŸ“¦ Minimal Dependencies: Lightweight footprint for your test suite

πŸ› οΈ Quick Start

Add EasyHttpMock to your Cargo.toml:

easyhttpmock = { version = "0.1.1", features = ["tokio-rt", "http2", "tokio-rust-tls"] }

πŸ’‘ Usage Example

Here's how simple it is to create a mock HTTP server for testing:

use bytes::Bytes;
use http::StatusCode;
use http_body_util::Full;
use hyper::Response;

use easyhttpmock::{
    config::EasyHttpMockConfig,
    server::{adapters::vetis_adapter::VetisServerAdapter, PortGenerator},
    EasyHttpMock,
};

use deboa::{cert::ContentEncoding, request::DeboaRequest, Client};

use vetis::{
    server::{
        config::{SecurityConfig, ServerConfig},
    },
};

pub const CA_CERT: &[u8] = include_bytes!("../certs/ca.der");
pub const CA_CERT_PEM: &[u8] = include_bytes!("../certs/ca.crt");

pub const SERVER_CERT: &[u8] = include_bytes!("../certs/server.der");
pub const SERVER_KEY: &[u8] = include_bytes!("../certs/server.key.der");

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let tls_config = SecurityConfig::builder()
        .cert(SERVER_CERT.to_vec())
        .key(SERVER_KEY.to_vec())
        .build();

    let vetis_config = ServerConfig::builder()
        .security(tls_config)
        .with_random_port()
        .build();

    let config = EasyHttpMockConfig::<VetisServerAdapter>::builder()
        .server_config(vetis_config)
        .build();

    let mut server = EasyHttpMock::new(config);
    #[allow(unused_must_use)]
    let result = server
        .start(|_| async move {
            Ok(Response::new(Full::new(Bytes::from("Hello World"))))
        })
        .await;

    result.unwrap_or_else(|err| {
        panic!("Failed to start mock server: {}", err);
    });

    let client = Client::builder()
        .certificate(deboa::cert::Certificate::from_slice(CA_CERT, ContentEncoding::DER))
        .build();

    let request = DeboaRequest::get(server.url("/anything"))?.build()?;

    let response = client
        .execute(request)
        .await?;

    if response.status() == StatusCode::OK {
        println!("Request executed successfully");
    }

    server
        .stop()
        .await?;
    
    Ok(())
}

🎯 Perfect For

  • πŸ§ͺ Unit Testing: Mock external APIs in your test suite
  • πŸ”„ Integration Testing: Test HTTP client behavior without real services
  • πŸ“Š Load Testing: Simulate API responses under various conditions
  • πŸ› Debugging: Reproduce API issues in a controlled environment
  • πŸ“š Documentation: Create interactive API examples

βš™οΈ Supported Runtimes

  • tokio - High-performance async runtime
  • smol - Lightweight async runtime

πŸ”§ Crate Features

  • tokio-rt (default) - Tokio runtime support
  • smol-rt - Smol runtime support
  • http1 - HTTP/1 protocol support
  • http2 (default) - HTTP/2 protocol support
  • http3 - HTTP/3 protocol support
  • tokio-rust-tls (default) - TLS support for Tokio
  • smol-rust-tls - TLS support for Smol

πŸ“„ License

MIT

πŸ‘€ Author

Rogerio Pereira Araujo rogerio.araujo@gmail.com

About

πŸ§ͺ The effortless HTTP mock server for seamless API testing

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

 

Packages

No packages published

Contributors 2

  •  
  •