Crate mini_http_test

source ·
Expand description

Provides a simple Hyper server wrapper that can be used for writing test servers. Vaguely inspired by Go’s httptest package.

Currently only supports HTTP/1.1 and does not support TLS. Only supports the Tokio async runtime.

Example

use std::sync::{Arc, Mutex};

use mini_http_test::{
    handle_ok,
    hyper::{body, Request, Response},
    Server,
};

let val = Arc::new(Mutex::new(1234));
let server = {
    let val = val.clone();
    Server::new(move |_: Request<body::Incoming>| async move {
        let mut val = val.lock().expect("lock poisoned");
        *val += 1;
        handle_ok(Response::new(val.to_string().into()))
    })
    .await
    .expect("create server")
};

let res = reqwest::Client::new()
    .get(server.url("/").to_string())
    .send()
    .await
    .expect("send request");

assert_eq!(res.status(), 200);
assert_eq!(*val.lock().expect("lock poisoned"), 1235);
assert_eq!(res.text().await.expect("read response"), "1235");

assert_eq!(server.req_count(), 1);

There are also more examples as tests.

Re-exports

pub use hyper;

Structs

Listens on a random port, running the given function to handle each request.

Enums

Traits

A handy extension to hyper::Request that allows for easily reading the request body as a single Bytes object.
A handler that can be used by a Server. If the handler returns an error, the error will be logged and a 500 response will be returned to the client.

Functions

Converts a value into a Result<T, Infallible> so it can be used as the return type for a Handler.