mini-http-test

Crates.iomini-http-test
lib.rsmini-http-test
version0.2.0
sourcesrc
created_at2022-12-19 02:38:51.979357
updated_at2023-01-15 16:56:30.968052
descriptionA mini HTTP server for testing HTTP clients
homepage
repositoryhttps://github.com/deansheather/mini-http-test
max_upload_size
id740711
size27,041
Dean Sheather (deansheather)

documentation

README

mini-http-test

Crates.io docs.rs GitHub Workflow Status

Small library that provides a simple Hyper server wrapper for writing HTTP 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);

License

Licensed under the MIT license. See LICENSE for more details.

Commit count: 9

cargo fmt