#![cfg(any(feature = "method", feature = "method_std"))]
use product_os_request::Method;
#[test]
fn test_method_variants_exist() {
let _ = Method::GET;
let _ = Method::POST;
let _ = Method::PUT;
let _ = Method::PATCH;
let _ = Method::DELETE;
let _ = Method::HEAD;
let _ = Method::OPTIONS;
let _ = Method::TRACE;
let _ = Method::CONNECT;
let _ = Method::ANY;
}
#[test]
fn test_method_traits() {
let method = Method::GET;
let cloned = method.clone();
assert_eq!(method, cloned);
let debug = format!("{:?}", method);
assert!(debug.contains("GET"));
assert_eq!(method, Method::GET);
assert_ne!(method, Method::POST);
}
#[test]
fn test_method_conversion_api() {
let method = Method::GET;
let http_method = method.to_http_method();
assert_eq!(http_method, product_os_http::Method::GET);
let parsed = Method::from_str("GET");
assert_eq!(parsed, Method::GET);
let from_http = Method::from_http_method(&product_os_http::Method::POST);
assert_eq!(from_http, Method::POST);
}
#[cfg(any(feature = "method", feature = "method_std"))]
#[test]
fn test_method_serde() {
use serde::{Deserialize, Serialize};
fn assert_serialize<T: Serialize>() {}
fn assert_deserialize<'de, T: Deserialize<'de>>() {}
assert_serialize::<Method>();
assert_deserialize::<Method>();
}
#[cfg(any(feature = "request", feature = "request_std"))]
mod request_api_tests {
use product_os_request::{ProductOSRequestError, Protocol, Proxy, RedirectPolicy};
#[test]
fn test_protocol_variants() {
let _ = Protocol::SOCKS5;
let _ = Protocol::HTTP;
let _ = Protocol::HTTPS;
let _ = Protocol::ALL;
}
#[test]
fn test_protocol_traits() {
let protocol = Protocol::HTTP;
let cloned = protocol.clone();
assert_eq!(protocol, cloned);
let debug = format!("{:?}", protocol);
assert_eq!(debug, "HTTP");
}
#[test]
fn test_proxy_fields() {
let proxy = Proxy {
protocol: Protocol::HTTP,
address: String::from("localhost:8080"),
};
assert_eq!(proxy.protocol, Protocol::HTTP);
assert_eq!(proxy.address, "localhost:8080");
}
#[test]
fn test_redirect_policy_variants() {
let none = RedirectPolicy::None;
let default = RedirectPolicy::Default;
let limit = RedirectPolicy::Limit(5);
assert_eq!(none, RedirectPolicy::None);
assert_eq!(default, RedirectPolicy::Default);
assert_eq!(limit, RedirectPolicy::Limit(5));
}
#[test]
fn test_error_variants() {
let err = ProductOSRequestError::Error(String::from("test"));
let display = format!("{}", err);
assert_eq!(display, "test");
let uri_err = ProductOSRequestError::InvalidUri(String::from("bad"));
let display = format!("{}", uri_err);
assert!(display.contains("Invalid URI"));
}
#[test]
fn test_error_display() {
use std::fmt::Display;
fn assert_display<T: Display>() {}
assert_display::<ProductOSRequestError>();
}
}
#[cfg(all(
feature = "request_std",
any(feature = "std", feature = "std_reqwest", feature = "std_hyper")
))]
mod request_builder_api_tests {
use product_os_request::{Method, ProductOSClient, ProductOSRequestClient};
use std::collections::BTreeMap;
#[test]
fn test_request_builder_methods() {
let client = ProductOSRequestClient::new();
let mut request = client.new_request(Method::GET, "http://example.com");
request.add_header("key", "value", false);
request.add_headers(BTreeMap::new(), false);
request.bearer_auth(String::from("token"));
request.add_param(String::from("k"), String::from("v"));
request.add_params(BTreeMap::new());
request.set_query(BTreeMap::new());
}
#[test]
fn test_request_body_methods() {
use bytes::Bytes;
use product_os_http_body::BodyBytes;
let client = ProductOSRequestClient::new();
let mut request = client.new_request(Method::POST, "http://example.com");
let body = BodyBytes::new(Bytes::new());
request.set_body(body);
let extracted = request.into_body();
assert!(extracted.is_some());
}
}
#[cfg(any(feature = "response", feature = "response_std"))]
mod response_api_tests {
use bytes::Bytes;
use product_os_http_body::BodyBytes;
use product_os_request::{ProductOSResponse, StatusCode};
#[test]
fn test_response_methods() {
let body = BodyBytes::new(Bytes::new());
let response = ProductOSResponse::from_body(body, String::from("http://example.com"));
let status = response.try_status();
assert_eq!(status, Some(StatusCode::OK));
assert_eq!(response.status_code(), 200);
let headers = response.try_get_headers();
assert!(headers.is_some());
let _ = response.response_url();
}
#[test]
fn test_response_consuming_methods() {
let body = BodyBytes::new(Bytes::new());
let response = ProductOSResponse::from_body(body, String::from("http://example.com"));
let result = response.into_body();
assert!(result.is_some());
}
#[test]
fn test_response_parts() {
let body = BodyBytes::new(Bytes::new());
let response = ProductOSResponse::from_body(body, String::from("http://example.com"));
let result = response.try_parts();
assert!(result.is_some());
}
#[test]
fn test_response_from_response() {
use product_os_http::Response;
let http_response = Response::builder()
.status(201)
.body(BodyBytes::new(Bytes::from("created")))
.unwrap();
let response =
ProductOSResponse::from_response(http_response, String::from("http://example.com"));
assert_eq!(response.status_code(), 201);
}
}
#[cfg(any(feature = "request", feature = "request_std"))]
mod requester_api_tests {
use product_os_request::{ProductOSRequester, Protocol, RedirectPolicy};
use std::collections::BTreeMap;
#[test]
fn test_requester_construction() {
let requester = ProductOSRequester::new();
assert_eq!(requester.trusted_certificates().len(), 0);
}
#[test]
fn test_requester_configuration_methods() {
let mut requester = ProductOSRequester::new();
requester.add_header("key", "value", false);
requester.set_headers(BTreeMap::new());
requester.force_secure(true);
requester.trust_all_certificates(false);
requester.trust_any_certificate_for_hostname(false);
requester.set_timeout(1000);
requester.set_connect_timeout(1000);
requester.add_trusted_certificate_der(vec![]);
let certs = requester.trusted_certificates();
assert_eq!(certs.len(), 1);
requester.set_redirect_policy(RedirectPolicy::Default);
requester.set_proxy(None);
requester.set_proxy(Some((Protocol::HTTP, String::from("localhost:8080"))));
}
#[test]
fn test_requester_clone() {
let requester = ProductOSRequester::new();
let cloned = requester.clone();
assert_eq!(
requester.trusted_certificates().len(),
cloned.trusted_certificates().len()
);
}
}
#[cfg(all(
feature = "request_std",
any(feature = "std", feature = "std_reqwest", feature = "std_hyper")
))]
mod client_api_tests {
use product_os_request::{Method, ProductOSClient, ProductOSRequestClient, ProductOSRequester};
#[test]
fn test_client_construction() {
let _ = ProductOSRequestClient::new();
}
#[test]
fn test_client_clone() {
let client = ProductOSRequestClient::new();
let _ = client.clone();
}
#[test]
fn test_client_trait_methods() {
let requester = ProductOSRequester::new();
let mut client = ProductOSRequestClient::new();
client.build(&requester);
let _ = client.new_request(Method::GET, "http://example.com");
}
}
#[test]
fn test_public_reexports() {
#[cfg(any(feature = "method", feature = "method_std"))]
{
use product_os_request::Method;
let m = Method::GET;
assert_eq!(m, Method::GET);
}
#[cfg(any(feature = "request", feature = "request_std"))]
{
use product_os_request::{
ProductOSRequestError, ProductOSRequester, Protocol, RedirectPolicy,
};
assert_eq!(Protocol::HTTP, Protocol::HTTP);
assert_eq!(RedirectPolicy::None, RedirectPolicy::None);
let err = ProductOSRequestError::Error(String::from("test"));
assert_eq!(format!("{}", err), "test");
let _ = ProductOSRequester::new();
}
#[cfg(any(feature = "response", feature = "response_std"))]
{
use product_os_request::StatusCode;
assert_eq!(StatusCode::OK, StatusCode::OK);
}
}