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
pub mod models;
pub mod server;
mod utils {
use std::{
collections::HashMap,
io::{Error, ErrorKind},
};
use crate::models::RequestMethod;
#[allow(dead_code)]
pub fn get_url_query(url: String) -> HashMap<String, String> {
let mut queries: HashMap<String, String> = HashMap::new();
let mut query: Vec<&str> = url.split(&['?', '&', '#'][..]).collect();
query.remove(0);
for part in query {
let name_value: Vec<&str> = part.split("=").collect();
if name_value.len() == 2 {
queries.insert(name_value[0].to_string(), name_value[1].to_string());
}
}
return queries;
}
pub fn get_request_method_by_string(string_method: String) -> Result<RequestMethod, Error> {
if string_method == "GET" {
return Ok(RequestMethod::GET);
} else if string_method == "HEAD" {
return Ok(RequestMethod::HEAD);
} else if string_method == "POST" {
return Ok(RequestMethod::POST);
} else if string_method == "PUT" {
return Ok(RequestMethod::PUT);
} else if string_method == "DELETE" {
return Ok(RequestMethod::DELETE);
} else if string_method == "CONNECT" {
return Ok(RequestMethod::CONNECT);
} else if string_method == "OPTIONS" {
return Ok(RequestMethod::OPTIONS);
} else if string_method == "TRACE" {
return Ok(RequestMethod::TRACE);
} else if string_method == "PATCH" {
return Ok(RequestMethod::PATCH);
} else {
return Err(Error::new(
ErrorKind::NotFound,
String::from("Unknown request type was provided."),
));
}
}
}
#[cfg(test)]
mod tests {
use crate::models::RequestMethod;
#[test]
fn test_getting_query() {
let query = crate::utils::get_url_query(String::from(
"https://some.url?and=some&query=params#bla=bla",
));
assert_eq!(query.len(), 3);
}
#[test]
fn test_getting_method() {
assert!(matches!(
crate::utils::get_request_method_by_string(String::from("GET")).unwrap(),
RequestMethod::GET
));
assert!(matches!(
crate::utils::get_request_method_by_string(String::from("POST")).unwrap(),
RequestMethod::POST
));
}
}