Skip to content

Instantly share code, notes, and snippets.

@msrd0
Created April 15, 2021 13:33
Show Gist options
  • Save msrd0/5c6ecbfaf2934b90a3cbb0706486eb6f to your computer and use it in GitHub Desktop.
Save msrd0/5c6ecbfaf2934b90a3cbb0706486eb6f to your computer and use it in GitHub Desktop.
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