use crate::host::{Body, Header, handler};
pub struct Response {
header: Header,
body: Body,
}
static KIND_RES: i32 = 1;
impl Response {
pub(crate) fn new() -> Self {
Self { header: Header::kind(KIND_RES), body: Body::kind(KIND_RES) }
}
pub fn status(&self) -> i32 {
handler::status_code()
}
pub fn set_status(&self, code: i32) {
handler::set_status_code(code);
}
pub fn header(&self) -> &Header {
&self.header
}
pub fn body(&self) -> &Body {
&self.body
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_body() {
let r = Response::new();
let sut = r.body().read();
assert!(!sut.is_empty());
assert!(sut.starts_with(b"<html>"));
}
#[test]
fn response_status() {
let response = Response::new();
assert_eq!(response.status(), 200);
}
#[test]
fn response_set_status() {
let response = Response::new();
response.set_status(404);
}
#[test]
fn response_header_access() {
let response = Response::new();
let header = response.header();
let _ = header.names();
}
#[test]
fn response_body_read() {
let response = Response::new();
let body = response.body();
let content = body.read();
assert!(!content.is_empty());
}
#[test]
fn response_body_write() {
let response = Response::new();
let body = response.body();
body.write(b"<html><body>Custom Response</body></html>");
}
}