ibraheemdev / astra

Rust web servers without async/await.
MIT License
186 stars 4 forks source link

Testing #10

Open ibraheemdev opened 1 year ago

ibraheemdev commented 1 year ago

Along with the overall lack of integration tests, some of the utilities Body::wrap_stream, BodyReader would benefit from unit tests.

icp1994 commented 1 year ago

I hit https://github.com/ibraheemdev/astra/commit/b38f8eac167d583e26653396491becfc65b35bfb today while trying to put together a tiny POST server. Here's the extracted repro snippet (fixed in master now), feel free to use it as test if you want:

use std::io::Read;

use astra::{Body, ConnectionInfo, Request, Response, Server};

fn main() {
    Server::bind("localhost:3000")
        .serve(handle)
        .expect("serve failed");
}

fn handle(mut req: Request, _info: ConnectionInfo) -> Response {
    if req.method().as_str() == "POST" {
        let mut resp = String::with_capacity(64);
        _ = req.body_mut().reader().take(64).read_to_string(&mut resp);
        Response::new(Body::new(resp))
    } else {
        Response::new(Body::empty())
    }
}