#![cfg(all(
feature = "request_std",
any(feature = "std", feature = "std_reqwest", feature = "std_hyper")
))]
use bytes::Bytes;
use product_os_http_body::BodyBytes;
use product_os_request::{Method, ProductOSClient, ProductOSRequestClient};
use std::collections::BTreeMap;
#[test]
fn test_request_add_header() {
let client = ProductOSRequestClient::new();
let mut request = client.new_request(Method::GET, "http://example.com");
request.add_header("User-Agent", "TestBot/1.0", false);
request.add_header("Authorization", "Bearer token", true);
}
#[test]
fn test_request_add_headers() {
let client = ProductOSRequestClient::new();
let mut request = client.new_request(Method::GET, "http://example.com");
let mut headers = BTreeMap::new();
headers.insert(String::from("Accept"), String::from("application/json"));
headers.insert(
String::from("Content-Type"),
String::from("application/json"),
);
request.add_headers(headers, false);
}
#[test]
fn test_request_bearer_auth() {
let client = ProductOSRequestClient::new();
let mut request = client.new_request(Method::GET, "http://example.com");
request.bearer_auth(String::from("test_token_12345"));
}
#[test]
fn test_request_add_param() {
let client = ProductOSRequestClient::new();
let mut request = client.new_request(Method::GET, "http://example.com");
request.add_param(String::from("key1"), String::from("value1"));
request.add_param(String::from("key2"), String::from("value2"));
}
#[test]
fn test_request_add_params() {
let client = ProductOSRequestClient::new();
let mut request = client.new_request(Method::GET, "http://example.com");
let mut params = BTreeMap::new();
params.insert(String::from("page"), String::from("1"));
params.insert(String::from("limit"), String::from("10"));
params.insert(String::from("sort"), String::from("desc"));
request.add_params(params);
}
#[test]
fn test_request_set_query() {
let client = ProductOSRequestClient::new();
let mut request = client.new_request(Method::GET, "http://example.com");
let mut query = BTreeMap::new();
query.insert(String::from("search"), String::from("rust"));
query.insert(String::from("filter"), String::from("crates"));
request.set_query(query);
}
#[test]
fn test_request_set_body() {
let client = ProductOSRequestClient::new();
let mut request = client.new_request(Method::POST, "http://example.com");
let body = BodyBytes::new(Bytes::from("test body content"));
request.set_body(body);
let extracted = request.into_body();
assert!(extracted.is_some(), "Body should be present after set_body");
}
#[test]
fn test_request_into_body() {
let client = ProductOSRequestClient::new();
let mut request = client.new_request(Method::POST, "http://example.com");
let body = BodyBytes::new(Bytes::from("test body"));
request.set_body(body);
let extracted = request.into_body();
assert!(extracted.is_some(), "Should extract body");
}
#[test]
fn test_request_into_body_none() {
let client = ProductOSRequestClient::new();
let request = client.new_request(Method::GET, "http://example.com");
let extracted = request.into_body();
assert!(extracted.is_none(), "GET request should have no body");
}
#[test]
fn test_request_with_all_methods() {
let client = ProductOSRequestClient::new();
let methods = vec![
Method::GET,
Method::POST,
Method::PUT,
Method::PATCH,
Method::DELETE,
Method::HEAD,
Method::OPTIONS,
Method::TRACE,
Method::CONNECT,
];
for method in methods {
let request = client.new_request(method, "http://example.com");
assert!(
request.into_body().is_none(),
"New request should have no body"
);
}
}
#[test]
fn test_request_complex_url() {
let client = ProductOSRequestClient::new();
let urls = vec![
"http://example.com",
"https://example.com",
"http://example.com:8080",
"https://example.com/path/to/resource",
"http://example.com/path?existing=param",
"https://user:pass@example.com",
"http://192.168.1.1:3000/api/v1/users",
];
for url in urls {
let _ = client.new_request(Method::GET, url);
}
}
#[test]
fn test_request_empty_headers() {
let client = ProductOSRequestClient::new();
let mut request = client.new_request(Method::GET, "http://example.com");
let empty_headers = BTreeMap::new();
request.add_headers(empty_headers, false);
}
#[test]
fn test_request_empty_params() {
let client = ProductOSRequestClient::new();
let mut request = client.new_request(Method::GET, "http://example.com");
let empty_params = BTreeMap::new();
request.add_params(empty_params);
}
#[test]
fn test_request_sensitive_headers() {
let client = ProductOSRequestClient::new();
let mut request = client.new_request(Method::GET, "http://example.com");
request.add_header("X-Api-Key", "secret_key_123", true);
request.add_header("X-Public-Info", "public_data", false);
}
#[test]
fn test_request_special_characters_in_params() {
let client = ProductOSRequestClient::new();
let mut request = client.new_request(Method::GET, "http://example.com");
request.add_param(String::from("query"), String::from("hello world"));
request.add_param(String::from("email"), String::from("test@example.com"));
request.add_param(String::from("path"), String::from("/some/path"));
}
#[test]
fn test_request_body_replaced_on_second_set() {
let client = ProductOSRequestClient::new();
let mut request = client.new_request(Method::POST, "http://example.com");
let body1 = BodyBytes::new(Bytes::from("first body"));
request.set_body(body1);
let body2 = BodyBytes::new(Bytes::from("second body"));
request.set_body(body2);
let extracted = request.into_body();
assert!(extracted.is_some(), "Should have the second body");
}
#[test]
fn test_request_query_replaced_on_set_query() {
let client = ProductOSRequestClient::new();
let mut request = client.new_request(Method::GET, "http://example.com");
request.add_param(String::from("old_key"), String::from("old_value"));
let mut new_query = BTreeMap::new();
new_query.insert(String::from("new_key"), String::from("new_value"));
request.set_query(new_query);
}
#[test]
fn test_request_bearer_auth_overwrite() {
let client = ProductOSRequestClient::new();
let mut request = client.new_request(Method::GET, "http://example.com");
request.bearer_auth(String::from("token_1"));
request.bearer_auth(String::from("token_2"));
}