http_wasm_guest/host/
response.rs1use crate::host::{Body, Header, handler};
2pub struct Response {
4 pub header: Header,
6 pub body: Body,
8}
9const KIND_RES: i32 = 1;
10
11impl Response {
12 pub(crate) fn new() -> Self {
14 Self { header: Header::new(KIND_RES), body: Body::new(KIND_RES) }
15 }
16 pub fn status(&self) -> i32 {
18 handler::status_code()
19 }
20
21 pub fn set_status(&self, code: i32) {
25 handler::set_status_code(code);
26 }
27
28 #[deprecated(since = "0.11.2", note = "use the `header` field directly instead")]
30 pub fn header(&self) -> &Header {
31 &self.header
32 }
33
34 #[deprecated(since = "0.11.2", note = "use the `body` field directly instead")]
36 pub fn body(&self) -> &Body {
37 &self.body
38 }
39}
40
41#[cfg(test)]
42mod tests {
43 use super::*;
44
45 #[test]
46 fn test_body() {
47 let r = Response::new();
48 let sut = r.body.read();
49 assert!(!sut.is_empty());
50 assert!(sut.starts_with(b"<html>"));
51 }
52
53 #[test]
54 fn response_status() {
55 let response = Response::new();
56 assert_eq!(response.status(), 200);
58 }
59
60 #[test]
61 fn response_set_status() {
62 let response = Response::new();
63 response.set_status(404);
65 }
66
67 #[test]
68 fn response_header_access() {
69 let response = Response::new();
70 let header = response.header;
71 let _ = header.names_iter();
73 }
74
75 #[test]
76 fn response_body_read() {
77 let response = Response::new();
78 let body = response.body;
79 let content = body.read();
80 assert!(!content.is_empty());
82 }
83
84 #[test]
85 fn response_body_write() {
86 let response = Response::new();
87 let body = response.body;
88 body.write(b"<html><body>Custom Response</body></html>");
90 }
91}