use hyper::http::request::Parts;
use serde_json::Value;
#[derive(Debug)]
#[non_exhaustive]
pub struct ParsedRequest {
pub url_path: String,
pub component_parts: Parts,
pub body_json: Option<Value>,
pub body_len: Option<usize>,
}
impl ParsedRequest {
pub fn new(url_path: String, component_parts: Parts) -> Self {
Self {
url_path,
component_parts,
body_json: None,
body_len: None,
}
}
pub fn with_body(mut self, body_json: Option<Value>, body_len: Option<usize>) -> Self {
self.body_json = body_json;
self.body_len = body_len;
self
}
}
#[cfg(test)]
mod tests {
use super::*;
fn parts() -> Parts {
hyper::Request::builder()
.method("GET")
.uri("/x")
.body(())
.unwrap()
.into_parts()
.0
}
#[test]
fn new_has_no_body() {
let req = ParsedRequest::new("/x".to_owned(), parts());
assert_eq!(req.url_path, "/x");
assert!(req.body_json.is_none());
assert!(req.body_len.is_none());
}
#[test]
fn with_body_attaches_json_and_len() {
let req = ParsedRequest::new("/x".to_owned(), parts())
.with_body(Some(serde_json::json!({"a": 1})), Some(9));
assert_eq!(req.body_json.unwrap()["a"], 1);
assert_eq!(req.body_len, Some(9));
}
#[test]
fn with_body_can_clear_back_to_no_body() {
let req = ParsedRequest::new("/x".to_owned(), parts())
.with_body(Some(serde_json::json!({"a": 1})), Some(9))
.with_body(None, None);
assert!(req.body_json.is_none());
assert!(req.body_len.is_none());
}
}