Skip to content

Instantly share code, notes, and snippets.

@msrd0

msrd0/main.rs

Created Apr 15, 2021
Embed
What would you like to do?
example for testing handlers using State::with_new
use gotham::{
handler::HandlerError,
hyper::{Body, Response, StatusCode},
router::builder::*,
state::State,
};
async fn foo_handler(_state: &mut State) -> Result<Response<Body>, HandlerError> {
let mut res = Response::new(Body::empty());
*res.status_mut() = StatusCode::NO_CONTENT;
Ok(res)
}
fn main() {
gotham::start(
"127.0.0.1:8080",
build_simple_router(|route| route.get("/foo").to_async_borrowing(foo_handler)),
);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_foo_handler() {
State::with_new(|state| {
// don't use futures_executor::block_on outside of test functions
let res = futures_executor::block_on(foo_handler(state)).unwrap();
assert_eq!(res.status(), StatusCode::NO_CONTENT);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment