Protocol Support
orb-mockhttp supports all major HTTP versions, allowing you to test protocol-specific behavior.
Available Protocols
Section titled “Available Protocols”use orb_mockhttp::HttpProtocol;
// Available protocolsHttpProtocol::Http1 // HTTP/1.1HttpProtocol::Http2 // HTTP/2HttpProtocol::Http3 // HTTP/3 (QUIC)
// Get all protocolslet all = HttpProtocol::all(); // [Http1, Http2, Http3]Default Behavior
Section titled “Default Behavior”Without TLS
Section titled “Without TLS”let server = TestServerBuilder::new().build();
// Only HTTP/1.1 is availableassert!(server.supports_protocol(HttpProtocol::Http1));assert!(!server.supports_protocol(HttpProtocol::Http2));assert!(!server.supports_protocol(HttpProtocol::Http3));With TLS
Section titled “With TLS”let server = TestServerBuilder::new() .with_tls() .build();
// All protocols available on the same portassert!(server.supports_protocol(HttpProtocol::Http1));assert!(server.supports_protocol(HttpProtocol::Http2));assert!(server.supports_protocol(HttpProtocol::Http3));Selecting Specific Protocols
Section titled “Selecting Specific Protocols”Use with_protocols() to limit available protocols:
// HTTP/2 onlylet server = TestServerBuilder::new() .with_tls() .with_protocols(&[HttpProtocol::Http2]) .build();
// HTTP/1.1 and HTTP/2 (no HTTP/3)let server = TestServerBuilder::new() .with_tls() .with_protocols(&[HttpProtocol::Http1, HttpProtocol::Http2]) .build();
// HTTP/3 onlylet server = TestServerBuilder::new() .with_tls() .with_protocols(&[HttpProtocol::Http3]) .build();Protocol Detection in Handlers
Section titled “Protocol Detection in Handlers”Access the protocol in request handlers:
server.on_request_fn("/version", |req| { let protocol = match req.protocol() { HttpProtocol::Http1 => "HTTP/1.1", HttpProtocol::Http2 => "HTTP/2", HttpProtocol::Http3 => "HTTP/3", };
ResponseBuilder::new() .text(format!("Protocol: {}", protocol)) .build()});Or check the HTTP version:
server.on_request_fn("/version", |req| { let version = req.version(); // http::Version
ResponseBuilder::new() .text(format!("Version: {:?}", version)) .build()});Same Port for All Protocols
Section titled “Same Port for All Protocols”When TLS is enabled, all protocols share the same port number:
let server = TestServerBuilder::new() .with_tls() .build();
let port = server.port();
// HTTP/1.1 and HTTP/2 use TCP on this port// HTTP/3 uses QUIC (UDP) on this port
// Same URL works for all protocolslet url = server.url("/api");// The client's protocol choice determines which is usedTesting Protocol-Specific Behavior
Section titled “Testing Protocol-Specific Behavior”HTTP/2 Features
Section titled “HTTP/2 Features”#[tokio::test]async fn test_http2_multiplexing() { let server = TestServerBuilder::new() .with_tls() .with_protocols(&[HttpProtocol::Http2]) .build();
server.on_request("/slow") .delay(Duration::from_millis(100)) .respond_with(200, "slow");
server.on_request("/fast") .respond_with(200, "fast");
// Multiple concurrent requests over single connection let client = create_http2_client(&server);
let (slow, fast) = tokio::join!( client.get(server.url("/slow")), client.get(server.url("/fast")) );
// fast should complete before slow}HTTP/3 Specific Tests
Section titled “HTTP/3 Specific Tests”#[tokio::test]async fn test_http3() { let server = TestServerBuilder::new() .with_tls() .with_protocols(&[HttpProtocol::Http3]) .build();
server.on_request("/test") .respond_with(200, "OK");
// Use an HTTP/3 client let client = create_http3_client(&server);
let response = client.get(server.url("/test")).await.unwrap(); assert_eq!(response.status(), 200);}Protocol Negotiation
Section titled “Protocol Negotiation”#[tokio::test]async fn test_protocol_negotiation() { let server = TestServerBuilder::new() .with_tls() .build();
server.on_request_fn("/check", |req| { ResponseBuilder::new() .json(&json!({ "protocol": format!("{:?}", req.protocol()) })) .build() });
// HTTP/2 client let h2_client = create_http2_client(&server); let resp = h2_client.get(server.url("/check")).await.unwrap(); assert!(resp.text().await.unwrap().contains("Http2"));
// HTTP/1.1 client let h1_client = create_http1_client(&server); let resp = h1_client.get(server.url("/check")).await.unwrap(); assert!(resp.text().await.unwrap().contains("Http1"));}Protocol Requirements Summary
Section titled “Protocol Requirements Summary”| Protocol | TLS Required | Transport | Port |
|---|---|---|---|
| HTTP/1.1 | No | TCP | Any |
| HTTP/2 | Yes | TCP | Shared |
| HTTP/3 | Yes | QUIC (UDP) | Shared |
Complete Example
Section titled “Complete Example”use orb_mockhttp::{TestServerBuilder, HttpProtocol, ResponseBuilder};use serde_json::json;
#[tokio::test]async fn test_multi_protocol() { // Server supporting all protocols let server = TestServerBuilder::new() .with_tls() .build();
// Handler that reports which protocol was used server.on_request_fn("/api", |req| { ResponseBuilder::new() .json(&json!({ "protocol": match req.protocol() { HttpProtocol::Http1 => "HTTP/1.1", HttpProtocol::Http2 => "HTTP/2", HttpProtocol::Http3 => "HTTP/3", }, "method": req.method().as_str(), "path": req.path(), })) .build() });
// Check protocol support println!("Supported protocols:"); for protocol in server.protocols() { println!(" - {:?}", protocol); }
// Test with different clients...}