Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test/headers #10

Merged
merged 1 commit into from
Mar 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion src/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub fn get_header_value(request: &Request, value: String) -> String {


pub fn get_header_user_agent(request: &Request) -> String {
return get_header_value("User-Agent");
return get_header_value(request, "User-Agent");
}

pub fn get_header_cookies(request: &Request) -> String {
Expand All @@ -38,3 +38,45 @@ pub fn get_header_cookies(request: &Request) -> String {

return to_string(&cookies).unwrap();
}


#[cfg(test)]
mod tests {
use super::*;
use lambda_http::http::header::{HeaderMap, HeaderValue};
use lambda_http::{Body, Request};

fn mock_request(headers: Vec<(&str, &str)>) -> Request {
let mut header_map = HeaderMap::new();
for (key, value) in headers {
header_map.insert(key, HeaderValue::from_static(value));
}

Request::new(Body::Empty).with_headers(header_map)
}

#[test]
fn test_get_header_value_known() {
let request = mock_request(vec![("User-Agent", "TestAgent")]);
assert_eq!(get_header_value(&request, "User-Agent".to_string()), "TestAgent");
}

#[test]
fn test_get_header_value_unknown() {
let request = mock_request(vec![]);
assert_eq!(get_header_value(&request, "User-Agent".to_string()), "Unknown header value");
}

#[test]
fn test_get_header_user_agent() {
let request = mock_request(vec![("User-Agent", "TestAgent")]);
assert_eq!(get_header_user_agent(&request), "TestAgent");
}

#[test]
fn test_get_header_cookies() {
let request = mock_request(vec![("Cookie", "username=user; session=token")]);
assert_eq!(get_header_cookies(&request), r#"{"session":"token","username":"user"}"#);
}
}

Loading