neco_server_core/
request.rs1use crate::{HeaderMap, Method};
2
3pub struct Request {
5 pub method: Method,
7 pub path: String,
9 pub query: Option<String>,
11 pub headers: HeaderMap,
13 pub body: Vec<u8>,
15}
16
17impl Request {
18 pub fn new(method: Method, path: impl Into<String>) -> Self {
20 Self {
21 method,
22 path: path.into(),
23 query: None,
24 headers: HeaderMap::new(),
25 body: Vec::new(),
26 }
27 }
28
29 pub fn with_query(mut self, query: impl Into<String>) -> Self {
31 self.query = Some(query.into());
32 self
33 }
34
35 pub fn with_body(mut self, body: impl Into<Vec<u8>>) -> Self {
37 self.body = body.into();
38 self
39 }
40
41 pub fn with_header(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
43 self.headers.insert(name, value);
44 self
45 }
46}
47
48#[cfg(test)]
49mod tests {
50 use super::*;
51
52 #[test]
53 fn request_builder_sets_headers() {
54 let request =
55 Request::new(Method::Get, "/x").with_header("content-type", "application/json");
56
57 assert_eq!(
58 request.headers.get("Content-Type"),
59 Some("application/json")
60 );
61 }
62}