-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_method.rs
More file actions
193 lines (170 loc) · 5.77 KB
/
http_method.rs
File metadata and controls
193 lines (170 loc) · 5.77 KB
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
use std::convert::Infallible;
use std::str::FromStr;
use pyo3::Borrowed;
use pyo3::exceptions::PyValueError;
use pyo3::prelude::*;
use pyo3::types::PyString;
/// HTTP methods used in an ASGI server.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum HttpMethod {
#[default]
Get,
Post,
Put,
Delete,
Patch,
Head,
Options,
Trace,
Connect,
}
impl<'a, 'py> FromPyObject<'a, 'py> for HttpMethod {
type Error = PyErr;
fn extract(ob: Borrowed<'a, 'py, PyAny>) -> PyResult<Self> {
let method: String = ob.extract()?;
method
.to_uppercase()
.as_str()
.parse()
.map_err(PyValueError::new_err)
}
}
impl<'py> IntoPyObject<'py> for HttpMethod {
type Target = PyString;
type Output = Bound<'py, Self::Target>;
type Error = Infallible;
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
match self {
HttpMethod::Get => "GET".into_pyobject(py),
HttpMethod::Post => "POST".into_pyobject(py),
HttpMethod::Put => "PUT".into_pyobject(py),
HttpMethod::Delete => "DELETE".into_pyobject(py),
HttpMethod::Patch => "PATCH".into_pyobject(py),
HttpMethod::Head => "HEAD".into_pyobject(py),
HttpMethod::Options => "OPTIONS".into_pyobject(py),
HttpMethod::Trace => "TRACE".into_pyobject(py),
HttpMethod::Connect => "CONNECT".into_pyobject(py),
}
}
}
impl FromStr for HttpMethod {
type Err = String;
fn from_str(method: &str) -> Result<HttpMethod, Self::Err> {
match method.to_uppercase().as_str() {
"GET" => Ok(HttpMethod::Get),
"POST" => Ok(HttpMethod::Post),
"PUT" => Ok(HttpMethod::Put),
"DELETE" => Ok(HttpMethod::Delete),
"PATCH" => Ok(HttpMethod::Patch),
"HEAD" => Ok(HttpMethod::Head),
"OPTIONS" => Ok(HttpMethod::Options),
"TRACE" => Ok(HttpMethod::Trace),
"CONNECT" => Ok(HttpMethod::Connect),
_ => Err(format!("Invalid HTTP method: {method}")),
}
}
}
impl TryFrom<String> for HttpMethod {
type Error = String;
fn try_from(method: String) -> Result<HttpMethod, Self::Error> {
method.as_str().parse()
}
}
impl TryFrom<&http_handler::Method> for HttpMethod {
type Error = String;
fn try_from(method: &http_handler::Method) -> Result<HttpMethod, Self::Error> {
match *method {
http_handler::Method::GET => Ok(HttpMethod::Get),
http_handler::Method::POST => Ok(HttpMethod::Post),
http_handler::Method::PUT => Ok(HttpMethod::Put),
http_handler::Method::DELETE => Ok(HttpMethod::Delete),
http_handler::Method::PATCH => Ok(HttpMethod::Patch),
http_handler::Method::HEAD => Ok(HttpMethod::Head),
http_handler::Method::OPTIONS => Ok(HttpMethod::Options),
http_handler::Method::TRACE => Ok(HttpMethod::Trace),
http_handler::Method::CONNECT => Ok(HttpMethod::Connect),
_ => Err(format!("Invalid HTTP method: {method}")),
}
}
}
impl From<HttpMethod> for String {
fn from(method: HttpMethod) -> String {
match method {
HttpMethod::Get => "GET".to_string(),
HttpMethod::Post => "POST".to_string(),
HttpMethod::Put => "PUT".to_string(),
HttpMethod::Delete => "DELETE".to_string(),
HttpMethod::Patch => "PATCH".to_string(),
HttpMethod::Head => "HEAD".to_string(),
HttpMethod::Options => "OPTIONS".to_string(),
HttpMethod::Trace => "TRACE".to_string(),
HttpMethod::Connect => "CONNECT".to_string(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_http_method_pyobject_conversion() {
Python::attach(|py| {
let tests = vec![
(HttpMethod::Get, "GET"),
(HttpMethod::Post, "POST"),
(HttpMethod::Put, "PUT"),
(HttpMethod::Delete, "DELETE"),
(HttpMethod::Patch, "PATCH"),
(HttpMethod::Options, "OPTIONS"),
(HttpMethod::Head, "HEAD"),
];
for (http_method, method_str) in tests {
// Convert HttpMethods to PyObject
let py_value = http_method.into_pyobject(py).unwrap();
assert_eq!(py_value, method_str);
// Convert back to HttpMethods
let extracted: HttpMethod = py_value.extract().unwrap();
assert_eq!(extracted, http_method);
}
});
}
#[test]
fn test_http_method_string_conversion() {
let methods = vec![
("GET", HttpMethod::Get),
("POST", HttpMethod::Post),
("PUT", HttpMethod::Put),
("DELETE", HttpMethod::Delete),
("PATCH", HttpMethod::Patch),
("HEAD", HttpMethod::Head),
("OPTIONS", HttpMethod::Options),
("TRACE", HttpMethod::Trace),
("CONNECT", HttpMethod::Connect),
];
for (method_str, http_method) in methods {
let parsed: HttpMethod = method_str.parse().expect("should parse valid HTTP method");
assert_eq!(parsed, http_method);
assert_eq!(String::from(http_method), method_str);
}
// Test invalid method
assert!(HttpMethod::try_from("INVALID".to_string()).is_err());
}
#[test]
fn test_http_method_try_from_http_handler_method() {
let test_cases = vec![
(http_handler::Method::GET, HttpMethod::Get),
(http_handler::Method::POST, HttpMethod::Post),
(http_handler::Method::PUT, HttpMethod::Put),
(http_handler::Method::DELETE, HttpMethod::Delete),
(http_handler::Method::PATCH, HttpMethod::Patch),
(http_handler::Method::HEAD, HttpMethod::Head),
(http_handler::Method::OPTIONS, HttpMethod::Options),
(http_handler::Method::TRACE, HttpMethod::Trace),
(http_handler::Method::CONNECT, HttpMethod::Connect),
];
for (http_handler_method, expected_asgi_method) in test_cases {
let result: Result<HttpMethod, String> = (&http_handler_method).try_into();
assert!(result.is_ok(), "Failed to convert {http_handler_method}");
assert_eq!(result.unwrap(), expected_asgi_method);
}
}
}