use crate::{HeaderMap, StatusCode};
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Response {
pub status: StatusCode,
pub headers: HeaderMap,
pub body: Vec<u8>,
}
impl Response {
pub fn new(status: StatusCode) -> Self {
Self {
status,
headers: HeaderMap::new(),
body: Vec::new(),
}
}
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 response_builder_sets_headers_and_body() {
let response = Response::new(StatusCode::OK)
.with_header("content-type", "text/plain")
.with_body(b"ok".to_vec());
assert_eq!(response.status, StatusCode::OK);
assert_eq!(response.headers.get("Content-Type"), Some("text/plain"));
assert_eq!(response.body, b"ok");
}
}