use std::env; use std::net::SocketAddr; use bravery::{error_400, error_500, App, EmptyState, Handler, HttpError, Request, Response}; use std::collections::HashMap; extern crate serde; extern crate serde_json; #[macro_use] extern crate serde_derive; #[derive(Serialize)] struct JsonStruct<'a> { message: &'a str, } #[derive(Serialize, Deserialize)] struct MyBody<'a> { pub message: &'a str, } #[derive(Clone)] struct TestHandler {} impl Handler for TestHandler { fn invoke(&self, req: Request) -> Result { let body: MyBody = req .body_as() .map_err(error_400("Unable to deserialize body"))?; let json = JsonStruct { message: body.message, }; let val = serde_json::to_vec(&json).map_err(error_500("Unable to serialize"))?; Ok(Response { status_code: 200, content_type: Some("application/json".to_string()), body: val, headers: HashMap::new(), }) } } fn get_app() -> App { let mut app: App = Default::default(); app.post("/", Box::new(TestHandler {})); app } fn main() -> Result<(), Box> { let addr = env::args() .nth(1) .unwrap_or_else(|| "127.0.0.1:8880".to_string()); let addr = addr.parse::()?; get_app().run(addr)?; Ok(()) } #[cfg(test)] mod tests { use super::*; #[test] fn post_body() { let app = get_app(); let body = serde_json::to_string(&MyBody { message: "my_message", }) .unwrap(); let request = app.create_request("POST", "/", "", body.as_bytes().to_vec()); let response = app.inject(request); assert_eq!(response.status_code, 200); assert_eq!( response.body, serde_json::to_vec(&JsonStruct { message: "my_message" }) .unwrap() ); } #[test] fn post_body_400() { let app = get_app(); let body = "{}"; let request = app.create_request("POST", "/", "", body.as_bytes().to_vec()); let response = app.inject(request); assert_eq!(response.status_code, 400); assert_eq!( response.body, serde_json::to_vec(&HttpError { status_code: 400, error_message: "Unable to deserialize body".to_string(), details: "missing field `message` at line 1 column 2".to_string(), }) .unwrap() ); } }