114 lines
3.1 KiB
Rust
114 lines
3.1 KiB
Rust
use std::collections::HashMap;
|
|
|
|
use tiny_http::Request;
|
|
|
|
pub trait Url {
|
|
fn get_url_without_parameters(&self) -> String;
|
|
}
|
|
|
|
impl Url for Request {
|
|
fn get_url_without_parameters(&self) -> String {
|
|
self.url().split("?").next().unwrap().to_string()
|
|
}
|
|
}
|
|
|
|
pub trait Parameters {
|
|
fn get_post_parameters(&mut self) -> HashMap<String, String>;
|
|
fn get_get_parameters(&mut self) -> Option<HashMap<String, String>>;
|
|
}
|
|
|
|
impl Parameters for Request {
|
|
fn get_post_parameters(&mut self) -> HashMap<String, String> {
|
|
let mut content = String::new();
|
|
self.as_reader().read_to_string(&mut content).unwrap();
|
|
|
|
let mut key_value_pairs = HashMap::new();
|
|
|
|
let parts = content.split("&");
|
|
|
|
for part in parts {
|
|
let mut key_value = part.split("=");
|
|
|
|
key_value_pairs.insert(key_value.next().unwrap().to_string(), key_value.next().unwrap().to_string());
|
|
}
|
|
|
|
key_value_pairs
|
|
}
|
|
|
|
fn get_get_parameters(&mut self) -> Option<HashMap<String, String>> {
|
|
let content = self.url().split("?").nth(1);
|
|
|
|
if content.is_none() {
|
|
return None;
|
|
}
|
|
|
|
let content = content.unwrap();
|
|
|
|
let mut key_value_pairs = HashMap::new();
|
|
|
|
let parts = content.split("&");
|
|
for part in parts {
|
|
let mut key_value = part.split("=");
|
|
|
|
key_value_pairs.insert(key_value.next().unwrap().to_string(), key_value.next().unwrap().to_string());
|
|
}
|
|
|
|
Some(key_value_pairs)
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use crate::entities::request::*;
|
|
use tiny_http::{Request, TestRequest};
|
|
|
|
#[test]
|
|
fn test_get_url_without_parameters() {
|
|
let request = Request::from(TestRequest::new().with_path("/test?key=value"));
|
|
|
|
let actual = request.get_url_without_parameters();
|
|
|
|
assert_eq!(actual, "/test");
|
|
|
|
let request2 = Request::from(TestRequest::new().with_path("/test2"));
|
|
|
|
assert_eq!(request2.get_url_without_parameters(), "/test2");
|
|
}
|
|
|
|
#[test]
|
|
fn test_get_post_parameters() {
|
|
let test_request = TestRequest::new();
|
|
let test_request = test_request.with_body("a=b&c=d");
|
|
|
|
let actual = Request::from(test_request).get_post_parameters();
|
|
let mut expected = HashMap::new();
|
|
expected.insert("a".to_string(), "b".to_string());
|
|
expected.insert("c".to_string(), "d".to_string());
|
|
|
|
assert_eq!(actual, expected);
|
|
}
|
|
|
|
#[test]
|
|
fn test_get_get_parameters_with_no_get_parameters() {
|
|
let test_request = TestRequest::new();
|
|
let test_request = test_request.with_path("/test");
|
|
|
|
let actual = Request::from(test_request).get_get_parameters();
|
|
|
|
assert_eq!(actual, None);
|
|
}
|
|
|
|
#[test]
|
|
fn test_get_get_parameters() {
|
|
let test_request = TestRequest::new();
|
|
let test_request = test_request.with_path("/test?a=b&c=d");
|
|
|
|
let actual = Request::from(test_request).get_get_parameters();
|
|
let mut expected = HashMap::new();
|
|
expected.insert("a".to_string(), "b".to_string());
|
|
expected.insert("c".to_string(), "d".to_string());
|
|
|
|
assert_eq!(actual, Some(expected));
|
|
}
|
|
}
|