Created
April 15, 2021 13:33
-
-
Save msrd0/5c6ecbfaf2934b90a3cbb0706486eb6f to your computer and use it in GitHub Desktop.
example for testing handlers using State::with_new
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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