use crate::{HeaderMap, Method};
pub struct Request {
pub method: Method,
pub path: String,
pub query: Option<String>,
pub headers: HeaderMap,
pub body: Vec<u8>,
}
impl Request {
pub fn new(method: Method, path: impl Into<String>) -> Self {
Self {
method,
path: path.into(),
query: None,
headers: HeaderMap::new(),
body: Vec::new(),
}
}
pub fn with_query(mut self, query: impl Into<String>) -> Self {
self.query = Some(query.into());
self
}
pub fn with_body(mut self, body: impl Into<Vec<u8>>) -> Self {
self.body = body.into();
self
}
pub fn with_header(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
self.headers.insert(name, value);
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn request_builder_sets_headers() {
let request =
Request::new(Method::Get, "/x").with_header("content-type", "application/json");
assert_eq!(
request.headers.get("Content-Type"),
Some("application/json")
);
}
}