use serde_json::json;
use std::collections::HashMap;
pub fn ok_with_headers(headers: HashMap<&str, &str>) -> String {
json!({
"status": "OK",
"code": 200,
"description": "Request processed successfully",
"headers": headers
})
.to_string()
}
pub fn bad_request_with_headers(headers: HashMap<&str, &str>) -> String {
json!({
"status": "Bad Request",
"code": 400,
"description": "The server cannot process the request due to malformed syntax",
"headers": headers
})
.to_string()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_ok_with_headers() {
let mut headers = HashMap::new();
headers.insert("Content-Type", "application/json");
let response = ok_with_headers(headers.clone());
assert!(response.contains("\"status\":\"OK\""));
assert!(response.contains("\"code\":200"));
assert!(response.contains("\"description\":\"Request processed successfully\""));
assert!(response.contains("\"headers\":{"));
assert!(response.contains("\"Content-Type\":\"application/json\""));
}
#[test]
fn test_bad_request_with_headers() {
let mut headers = HashMap::new();
headers.insert("Content-Type", "application/json");
let response = bad_request_with_headers(headers.clone());
assert!(response.contains("\"status\":\"Bad Request\""));
assert!(response.contains("\"code\":400"));
assert!(response.contains(
"\"description\":\"The server cannot process the request due to malformed syntax\""
));
assert!(response.contains("\"headers\":{"));
assert!(response.contains("\"Content-Type\":\"application/json\""));
}
}