rust_web_server/test_client/
mod.rs1#[cfg(test)]
2mod tests;
3
4use crate::application::Application;
5use crate::header::Header;
6use crate::http::VERSION;
7use crate::mime_type::MimeType;
8use crate::range::Range;
9use crate::request::{METHOD, Request};
10use crate::response::{Response, STATUS_CODE_REASON_PHRASE};
11use crate::server::{Address, ConnectionInfo};
12use crate::symbol::SYMBOL;
13
14pub struct TestClient<A: Application> {
39 app: A,
40 connection: ConnectionInfo,
41}
42
43impl<A: Application> TestClient<A> {
44 pub fn new(app: A) -> Self {
47 TestClient {
48 app,
49 connection: ConnectionInfo {
50 client: Address { ip: "127.0.0.1".to_string(), port: 12345 },
51 server: Address { ip: "127.0.0.1".to_string(), port: 7878 },
52 request_size: 16000,
53 sni_hostname: None,
54 },
55 }
56 }
57
58 pub fn get(&self, path: &str) -> TestRequest<'_, A> {
60 TestRequest::new(METHOD.get.to_string(), path, self)
61 }
62
63 pub fn post(&self, path: &str) -> TestRequest<'_, A> {
65 TestRequest::new(METHOD.post.to_string(), path, self)
66 }
67
68 pub fn put(&self, path: &str) -> TestRequest<'_, A> {
70 TestRequest::new(METHOD.put.to_string(), path, self)
71 }
72
73 pub fn patch(&self, path: &str) -> TestRequest<'_, A> {
75 TestRequest::new(METHOD.patch.to_string(), path, self)
76 }
77
78 pub fn delete(&self, path: &str) -> TestRequest<'_, A> {
80 TestRequest::new(METHOD.delete.to_string(), path, self)
81 }
82
83 pub fn options(&self, path: &str) -> TestRequest<'_, A> {
85 TestRequest::new(METHOD.options.to_string(), path, self)
86 }
87}
88
89pub struct TestRequest<'a, A: Application> {
91 method: String,
92 path: String,
93 headers: Vec<Header>,
94 body: Vec<u8>,
95 client: &'a TestClient<A>,
96}
97
98impl<'a, A: Application> TestRequest<'a, A> {
99 fn new(method: String, path: &str, client: &'a TestClient<A>) -> Self {
100 TestRequest {
101 method,
102 path: path.to_string(),
103 headers: vec![],
104 body: vec![],
105 client,
106 }
107 }
108
109 pub fn header(mut self, name: &str, value: &str) -> Self {
111 self.headers.push(Header { name: name.to_string(), value: value.to_string() });
112 self
113 }
114
115 pub fn body_bytes(mut self, body: Vec<u8>) -> Self {
117 self.body = body;
118 self
119 }
120
121 pub fn body_text(mut self, text: &str) -> Self {
123 self.body = text.as_bytes().to_vec();
124 self
125 }
126
127 pub fn send(self) -> TestResponse {
129 let request = Request {
130 method: self.method,
131 request_uri: self.path,
132 http_version: VERSION.http_1_1.to_string(),
133 headers: self.headers,
134 body: self.body,
135 };
136
137 let response = self.client.app.execute(&request, &self.client.connection)
138 .unwrap_or_else(|msg| {
139 let dummy = Request {
140 method: "GET".to_string(),
141 request_uri: "/".to_string(),
142 http_version: VERSION.http_1_1.to_string(),
143 headers: vec![],
144 body: vec![],
145 };
146 let header_list = Header::get_header_list(&dummy);
147 let body = msg.into_bytes();
148 let cr = Range::get_content_range(body, MimeType::TEXT_PLAIN.to_string());
149 Response::get_response(
150 STATUS_CODE_REASON_PHRASE.n500_internal_server_error,
151 Some(header_list),
152 Some(vec![cr]),
153 )
154 });
155
156 TestResponse::from_response(response)
157 }
158}
159
160pub struct TestResponse {
162 status: i16,
163 reason: String,
164 headers: Vec<Header>,
165 body: Vec<u8>,
166}
167
168impl TestResponse {
169 fn from_response(mut r: Response) -> Self {
170 let body: Vec<u8> = r.content_range_list.iter()
171 .flat_map(|cr| cr.body.iter().copied())
172 .collect();
173
174 if r.content_range_list.len() == 1 {
177 let content_range = r.content_range_list.get(0).unwrap();
178 r.headers.push(Header {
179 name: Header::_CONTENT_TYPE.to_string(),
180 value: content_range.content_type.to_string(),
181 });
182 let content_range_header_value = [
183 Range::BYTES,
184 SYMBOL.whitespace,
185 &content_range.range.start.to_string(),
186 SYMBOL.hyphen,
187 &content_range.range.end.to_string(),
188 SYMBOL.slash,
189 &content_range.size,
190 ].join("");
191 r.headers.push(Header {
192 name: Header::_CONTENT_RANGE.to_string(),
193 value: content_range_header_value,
194 });
195 r.headers.push(Header {
196 name: Header::_CONTENT_LENGTH.to_string(),
197 value: content_range.body.len().to_string(),
198 });
199 }
200
201 TestResponse {
202 status: r.status_code,
203 reason: r.reason_phrase,
204 headers: r.headers,
205 body,
206 }
207 }
208
209 pub fn status(&self) -> i16 {
211 self.status
212 }
213
214 pub fn reason(&self) -> &str {
216 &self.reason
217 }
218
219 pub fn header(&self, name: &str) -> Option<&str> {
221 let lower = name.to_lowercase();
222 self.headers
223 .iter()
224 .find(|h| h.name.to_lowercase() == lower)
225 .map(|h| h.value.as_str())
226 }
227
228 pub fn headers(&self) -> &[Header] {
230 &self.headers
231 }
232
233 pub fn body_bytes(&self) -> &[u8] {
235 &self.body
236 }
237
238 pub fn body_text(&self) -> &str {
240 std::str::from_utf8(&self.body).expect("response body is not valid UTF-8")
241 }
242
243 pub fn is_success(&self) -> bool {
245 (200..300).contains(&self.status)
246 }
247}