-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse_builder.rs
401 lines (352 loc) · 12.7 KB
/
response_builder.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
use flate2::{write::GzEncoder, Compression};
use super::{Response, StatusCode};
use std::{default::Default, io::Write};
#[derive(Clone, Debug)]
pub struct ResponseBuilder<S> {
status_code: S,
headers: Option<Vec<(String, String)>>,
body: Option<Vec<u8>>,
set_content_length_header: bool,
}
impl ResponseBuilder<MissingStatusCode> {
pub fn new() -> ResponseBuilder<MissingStatusCode> {
ResponseBuilder {
status_code: MissingStatusCode,
headers: None,
body: None,
set_content_length_header: true,
}
}
pub fn with_status_code(self, status_code: StatusCode) -> ResponseBuilder<StatusCode> {
ResponseBuilder {
status_code,
headers: self.headers,
body: self.body,
set_content_length_header: self.set_content_length_header,
}
}
pub fn ok() -> ResponseBuilder<StatusCode> {
ResponseBuilder {
status_code: StatusCode::Ok,
..Default::default()
}
}
pub fn not_found() -> ResponseBuilder<StatusCode> {
ResponseBuilder {
status_code: StatusCode::NotFound,
..Default::default()
}
}
pub fn bad_request() -> ResponseBuilder<StatusCode> {
ResponseBuilder {
status_code: StatusCode::BadRequest,
..Default::default()
}
}
pub fn internal_server_error() -> ResponseBuilder<StatusCode> {
ResponseBuilder {
status_code: StatusCode::InternalServerError,
..Default::default()
}
}
}
impl ResponseBuilder<StatusCode> {
pub fn build(self) -> Response {
let mut headers = self.headers.unwrap_or_default();
// Check if the Content-Encoding header is set to "gzip"
let content_encoding_header = headers
.iter()
.find(|(k, v)| k == "Content-Encoding" && v == "gzip");
let encoded_body = match self.body {
Some(body) => match content_encoding_header {
// If the Content-Encoding header is set to "gzip", encode the body
Some(_) => {
let mut new_body = Vec::new();
let mut encoder = GzEncoder::new(&mut new_body, Compression::default());
encoder.write_all(&body).unwrap();
encoder.finish().unwrap();
Some(new_body)
}
// If the Content-Encoding header is not set to "gzip", return the body as is
None => Some(body),
},
// If there the body is None, return it as is
None => None,
};
// Calculate the Content-Length header value
let content_length = encoded_body.as_ref().map(|b| b.len()).unwrap_or(0);
// Set the Content-Length header if the `without_content_length_header` method was not called
match self.set_content_length_header {
false => (), // No Content-Length header for empty bodies
true => headers.push(("Content-Length".to_string(), content_length.to_string())),
}
Response {
status_code: self.status_code,
headers,
body: encoded_body,
}
}
}
impl<S> ResponseBuilder<S> {
/// # Set header(s) on the response.
///
/// You can set a single header multiple times, or multiple headers at once.
///
/// When you set a single header, pass a tuple `(header_key, header_value)` with
/// values implementing the `Into<String>` trait.
///
/// When you set multiple headers, pass a vector of tuples `Vec<(header_key, header_value)>`
/// with values implementing the `Into<String>` trait.
///
/// Do not set the `Content-Length` header manually. It is calculated automatically based on
/// the body length.
///
/// # Example
///
/// ```
/// # use http::ResponseBuilder;
/// let response = ResponseBuilder::ok()
/// .with(("Content-Type", "text/plain"))
/// .with(vec![("X-Custom-Header", "value"), ("Keep-Alive", "timeout=5, max=1000")])
/// .build();
///
/// let expected_headers = vec![
/// ("Content-Type".to_string(), "text/plain".to_string()),
/// ("X-Custom-Header".to_string(), "value".to_string()),
/// ("Keep-Alive".to_string(), "timeout=5, max=1000".to_string()),
/// ("Content-Length".to_string(), "0".to_string()),
/// ];
///
/// let response_string = "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nX-Custom-Header: value\r\nKeep-Alive: timeout=5, max=1000\r\nContent-Length: 0\r\n\r\n";
///
/// assert_eq!(response.headers.len(), expected_headers.len());
/// assert_eq!(response.headers, expected_headers);
/// assert_eq!(response.to_string(), response_string);
/// ```
pub fn with<T: Into<String>>(self, part: impl IntoResponsePart<T>) -> Self {
match part.into_response_part() {
ResponsePart::Header(key, value) => self.header(key, value),
ResponsePart::Headers(headers) => self.headers(headers),
}
}
fn header(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
let key: String = key.into();
if key.as_str() != "Content-Length" {
if let Some(ref mut headers) = self.headers {
headers.push((key, value.into()));
} else {
self.headers = Some(vec![(key, value.into())]);
}
}
self
}
fn headers(mut self, headers: Vec<(impl Into<String>, impl Into<String>)>) -> Self {
// Filter out the Content-Length header and convert headers' keys and values to owned strings
let headers = headers
.into_iter()
.map(|(key, value)| -> (String, String) { (key.into(), value.into()) })
.filter(|(key, _)| key.clone().as_str() != "Content-Length")
.collect::<Vec<_>>();
if let Some(ref mut existing_headers) = self.headers {
existing_headers.extend(headers); // Extend existing headers
} else {
self.headers = Some(headers); // Set the headers
}
self
}
// This method is used to not to set the Content-Length header on empty bodies to pass codecrafters tests
/// # Do not set the Content-Length header on the response.
///
/// This method is used to not to set the Content-Length header.
///
/// # Example
///
/// ```
/// # use http::ResponseBuilder;
/// let response = ResponseBuilder::ok()
/// .without_content_length_header()
/// .build();
///
/// assert_eq!(response.headers.len(), 0);
/// ```
pub fn without_content_length_header(mut self) -> Self {
self.set_content_length_header = false;
self
}
/// # Set the body of the response.
///
/// The body is a byte vector. To set the body, pass any value implementing the
/// `Into<Vec<u8>>` trait.
///
/// # Example
///
/// ```
/// # use http::ResponseBuilder;
/// let response = ResponseBuilder::ok()
/// .body("Hello, world!")
/// .build();
///
/// assert_eq!(response.body, Some("Hello, world!".as_bytes().to_vec()));
/// ```
pub fn body(mut self, body: impl Into<Vec<u8>>) -> Self {
self.body = Some(body.into());
self
}
}
impl Default for ResponseBuilder<StatusCode> {
fn default() -> Self {
ResponseBuilder {
status_code: StatusCode::Ok,
headers: None,
body: None,
set_content_length_header: true,
}
}
}
// Auxiliary type to represent a missing status code in the builder
#[derive(Clone, Default)]
pub struct MissingStatusCode;
// Auxiliary enum to represent a part of a response
pub enum ResponsePart<T> {
Header(T, T),
Headers(Vec<(T, T)>),
}
// Trait to convert header value(s) into a response part
pub trait IntoResponsePart<T> {
fn into_response_part(self) -> ResponsePart<T>;
}
// Implement the IntoResponsePart trait for tuples of two elements to represent a single header
impl<T: Into<String>> IntoResponsePart<T> for (T, T) {
fn into_response_part(self) -> ResponsePart<T> {
ResponsePart::Header(self.0, self.1)
}
}
// Implement the IntoResponsePart trait for vectors of tuples of two elements to represent multiple headers
impl<T: Into<String>> IntoResponsePart<T> for Vec<(T, T)> {
fn into_response_part(self) -> ResponsePart<T> {
ResponsePart::Headers(self)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_response_builder_default() {
let response = ResponseBuilder::default().build();
assert_eq!(
response.status_code,
StatusCode::Ok,
"Status code should be 200 OK"
);
assert_eq!(
response.headers,
vec![("Content-Length".to_string(), "0".to_string())],
"Headers should contain Content-Length: 0"
);
assert!(response.body.is_none(), "No body should be set");
assert_eq!(
response.to_string(),
"HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n",
"Response string should be 200 OK"
);
}
#[test]
fn test_response_builder_ok() {
let response = ResponseBuilder::ok().build();
assert_eq!(
response.status_code,
StatusCode::Ok,
"Status code should be 200 OK"
);
}
#[test]
fn test_response_builder_not_found() {
let response = ResponseBuilder::not_found().build();
assert_eq!(
response.status_code,
StatusCode::NotFound,
"Status code should be 404 Not Found"
);
}
#[test]
fn test_response_builder_bad_request() {
let response = ResponseBuilder::bad_request().build();
assert_eq!(
response.status_code,
StatusCode::BadRequest,
"Status code should be 400 Bad Request"
);
}
#[test]
fn test_response_builder_internal_server_error() {
let response = ResponseBuilder::internal_server_error().build();
assert_eq!(
response.status_code,
StatusCode::InternalServerError,
"Status code should be 500 Internal Server Error"
);
}
#[test]
fn test_without_content_length_header() {
let response = ResponseBuilder::ok()
.without_content_length_header()
.build();
dbg!(&response.headers);
assert_eq!(
response.headers,
vec![],
"No headers should be set when the `without_content_length_header` method is called"
);
let response = ResponseBuilder::ok().build();
assert_eq!(
response.headers,
vec![("Content-Length".to_string(), "0".to_string())],
"Headers should contain Content-Length: 0"
);
// Manually set Content-Length header and then call without_content_length_header
let response = ResponseBuilder::ok()
.with(("Content-Length", "0"))
.without_content_length_header()
.build();
assert_eq!(response.headers.len(), 0, "No headers should be set");
assert_eq!(response.headers, vec![], "No headers should be set");
}
#[test]
fn test_response_builder_with() {
// Set a single header
let response = ResponseBuilder::ok()
.with(("Content-Type", "text/html"))
.without_content_length_header()
.build();
let headers = vec![("Content-Type".to_string(), "text/html".to_string())];
assert_eq!(response.headers.len(), 1, "One header should be set");
assert_eq!(
response.headers, headers,
"Headers should contain Content-Type: text/html only"
);
// Set multiple headers at once
let response = ResponseBuilder::ok()
.with(vec![("Content-Type", "text/html"), ("X-Custom", "value")])
.build();
let headers = vec![
("Content-Type".to_string(), "text/html".to_string()),
("X-Custom".to_string(), "value".to_string()),
("Content-Length".to_string(), "0".to_string()),
];
assert_eq!(response.headers.len(), 3, "Three headers should be set");
assert_eq!(
response.headers, headers,
"Headers should contain Content-Type: text/html, X-Custom: value, and Content-Length: 4"
);
}
#[test]
fn test_response_builder_body() {
let body = "Hello, world!";
let response = ResponseBuilder::ok().body(body).build();
assert_eq!(
response.body.unwrap(),
body.as_bytes().to_vec(),
"Body should be set to the given value"
);
}
}