Compare commits

..

7 Commits

Author SHA1 Message Date
b918dddb53 Update actions/checkout action to v4
Some checks failed
Cargo Build & Test / Tests (pull_request) Successful in 9s
Cargo Build & Test / Test publish (pull_request) Successful in 9s
Cargo Build & Test / check-cargo-version (pull_request) Failing after 6s
2024-08-20 14:00:26 +00:00
fd8f18de9a Merge pull request '[FEATURE] Add methods to get parameters' (#8) from feature/url-get-parameters into main
All checks were successful
Cargo Build & Test / Test publish (push) Successful in 10s
Reviewed-on: #8
2024-08-20 13:50:24 +00:00
8365cc9efa
Update version
All checks were successful
Cargo Build & Test / Tests (pull_request) Successful in 11s
Cargo Build & Test / Test publish (pull_request) Successful in 10s
Cargo Build & Test / check-cargo-version (pull_request) Successful in 6s
2024-08-20 15:49:22 +02:00
3667622acc
Add methods to get parameters
Some checks failed
Cargo Build & Test / Tests (pull_request) Successful in 11s
Cargo Build & Test / Test publish (pull_request) Successful in 9s
Cargo Build & Test / check-cargo-version (pull_request) Failing after 5s
2024-08-20 15:48:14 +02:00
4f0180321c Merge pull request '[FEATURE] Add unit tests' (#7) from feature/tests into main
All checks were successful
Cargo Build & Test / Test publish (push) Successful in 10s
Reviewed-on: #7
2024-08-20 13:28:06 +00:00
93d49e8a57
Update version
All checks were successful
Cargo Build & Test / Tests (pull_request) Successful in 11s
Cargo Build & Test / Test publish (pull_request) Successful in 10s
Cargo Build & Test / check-cargo-version (pull_request) Successful in 5s
2024-08-20 15:27:35 +02:00
55c023f504
Add tests to request
Some checks failed
Cargo Build & Test / Tests (pull_request) Successful in 9s
Cargo Build & Test / Test publish (pull_request) Successful in 9s
Cargo Build & Test / check-cargo-version (pull_request) Failing after 5s
2024-08-20 15:24:32 +02:00
3 changed files with 104 additions and 4 deletions

2
Cargo.lock generated

@ -22,7 +22,7 @@ checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9"
[[package]]
name = "loadstar"
version = "0.0.4"
version = "0.0.6"
dependencies = [
"tiny_http",
]

@ -2,7 +2,7 @@
name = "loadstar"
description = "A simple web framework for rust"
repository = "https://git.kjan.de/jank/loadstar"
version = "0.0.4"
version = "0.0.6"
license = "GPL-3.0-only"
edition = "2021"

@ -1,8 +1,9 @@
use std::collections::HashMap;
use tiny_http::Request;
pub trait Url {
fn get_url_without_parameters(&self) -> String;
fn get_url_without_parameters(&self) -> String;
}
impl Url for Request {
@ -11,3 +12,102 @@ impl Url for Request {
}
}
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));
}
}