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
use std::collections::HashMap;
use crate::headers::HeaderValue;
#[derive(Debug, Clone, PartialEq)]
pub struct Request {
pub request_path: String,
pub base_path: String,
pub method: String,
pub headers: HashMap<String, Vec<HeaderValue>>,
pub body: Option<Vec<u8>>,
pub query: HashMap<String, Vec<String>>,
}
impl Default for Request {
fn default() -> Request {
Request {
request_path: "/".to_string(),
base_path: "/".to_string(),
method: "GET".to_string(),
headers: HashMap::new(),
body: None,
query: HashMap::new(),
}
}
}
impl Request {
pub fn content_type(&self) -> String {
match self
.headers
.keys()
.find(|k| k.to_uppercase() == "CONTENT-TYPE")
{
Some(header) => match self.headers.get(header).unwrap().first() {
Some(value) => value.clone().value,
None => "application/json".to_string(),
},
None => "application/json".to_string(),
}
}
pub fn is_put_or_post(&self) -> bool {
["PUT", "POST"].contains(&self.method.to_uppercase().as_str())
}
pub fn is_get_or_head(&self) -> bool {
["GET", "HEAD"].contains(&self.method.to_uppercase().as_str())
}
pub fn is_get(&self) -> bool {
self.method.to_uppercase() == "GET"
}
pub fn is_options(&self) -> bool {
self.method.to_uppercase() == "OPTIONS"
}
pub fn is_put(&self) -> bool {
self.method.to_uppercase() == "PUT"
}
pub fn is_post(&self) -> bool {
self.method.to_uppercase() == "POST"
}
pub fn is_delete(&self) -> bool {
self.method.to_uppercase() == "DELETE"
}
pub fn has_accept_header(&self) -> bool {
self.has_header("ACCEPT")
}
pub fn accept(&self) -> Vec<HeaderValue> {
self.find_header("ACCEPT")
}
pub fn has_accept_language_header(&self) -> bool {
self.has_header("ACCEPT-LANGUAGE")
}
pub fn accept_language(&self) -> Vec<HeaderValue> {
self.find_header("ACCEPT-LANGUAGE")
}
pub fn has_accept_charset_header(&self) -> bool {
self.has_header("ACCEPT-CHARSET")
}
pub fn accept_charset(&self) -> Vec<HeaderValue> {
self.find_header("ACCEPT-CHARSET")
}
pub fn has_accept_encoding_header(&self) -> bool {
self.has_header("ACCEPT-ENCODING")
}
pub fn accept_encoding(&self) -> Vec<HeaderValue> {
self.find_header("ACCEPT-ENCODING")
}
pub fn has_header(&self, header: &str) -> bool {
self.headers
.keys()
.find(|k| k.to_uppercase() == header.to_uppercase())
.is_some()
}
pub fn find_header(&self, header: &str) -> Vec<HeaderValue> {
match self
.headers
.keys()
.find(|k| k.to_uppercase() == header.to_uppercase())
{
Some(header) => self.headers.get(header).unwrap().clone(),
None => Vec::new(),
}
}
pub fn has_header_value(&self, header: &str, value: &str) -> bool {
match self
.headers
.keys()
.find(|k| k.to_uppercase() == header.to_uppercase())
{
Some(header) => match self
.headers
.get(header)
.unwrap()
.iter()
.find(|val| *val == value)
{
Some(_) => true,
None => false,
},
None => false,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::headers::*;
use expectest::prelude::*;
#[test]
fn request_does_not_have_header_test() {
let request = Request {
..Request::default()
};
expect!(request.has_header("Vary")).to(be_false());
expect!(request.has_header_value("Vary", "*")).to(be_false());
}
#[test]
fn request_with_empty_header_test() {
let request = Request {
headers: hashmap! { "HeaderA".to_string() => Vec::new() },
..Request::default()
};
expect!(request.has_header("HeaderA")).to(be_true());
expect!(request.has_header_value("HeaderA", "*")).to(be_false());
}
#[test]
fn request_with_header_single_value_test() {
let request = Request {
headers: hashmap! { "HeaderA".to_string() => vec![h!("*")] },
..Request::default()
};
expect!(request.has_header("HeaderA")).to(be_true());
expect!(request.has_header_value("HeaderA", "*")).to(be_true());
expect!(request.has_header_value("HeaderA", "other")).to(be_false());
}
#[test]
fn request_with_header_multiple_value_test() {
let request = Request {
headers: hashmap! { "HeaderA".to_string() => vec![h!("*"), h!("other")]},
..Request::default()
};
expect!(request.has_header("HeaderA")).to(be_true());
expect!(request.has_header_value("HeaderA", "*")).to(be_true());
expect!(request.has_header_value("HeaderA", "other")).to(be_true());
expect!(request.has_header_value("HeaderA", "other2")).to(be_false());
}
}