#![cfg(any(feature = "response", feature = "response_std"))]
use bytes::Bytes;
use product_os_http_body::BodyBytes;
#[allow(deprecated)]
use product_os_request::{ProductOSResponse, StatusCode};
#[test]
fn test_response_new() {
let body = BodyBytes::new(Bytes::from("test response"));
let url = String::from("http://example.com");
let response = ProductOSResponse::from_body(body, url.clone());
assert_eq!(response.response_url(), &url);
}
#[test]
fn test_response_status_code() {
let body = BodyBytes::new(Bytes::from("OK"));
let response = ProductOSResponse::from_body(body, String::from("http://example.com"));
let status_code = response.status_code();
assert_eq!(status_code, 200);
}
#[test]
fn test_response_status() {
let body = BodyBytes::new(Bytes::from("OK"));
let response = ProductOSResponse::from_body(body, String::from("http://example.com"));
let status = response.try_status();
assert_eq!(status, Some(StatusCode::OK));
}
#[test]
fn test_response_url() {
let body = BodyBytes::new(Bytes::from("content"));
let url = String::from("https://api.example.com/v1/users");
let response = ProductOSResponse::from_body(body, url.clone());
assert_eq!(response.response_url(), url);
}
#[test]
fn test_response_get_headers() {
let body = BodyBytes::new(Bytes::from("test"));
let response = ProductOSResponse::from_body(body, String::from("http://example.com"));
let headers = response.try_get_headers();
assert!(headers.is_some());
let headers = headers.unwrap();
assert_eq!(headers.len(), 0);
}
#[test]
fn test_response_into_body() {
let body_content = Bytes::from("test body content");
let body = BodyBytes::new(body_content);
let response = ProductOSResponse::from_body(body, String::from("http://example.com"));
let extracted_body = response.into_body();
assert!(extracted_body.is_some());
}
#[test]
fn test_response_parts() {
let body = BodyBytes::new(Bytes::from("content"));
let response = ProductOSResponse::from_body(body, String::from("http://example.com"));
let result = response.try_parts();
assert!(result.is_some());
let (parts, _body) = result.unwrap();
assert_eq!(parts.status, StatusCode::OK);
}
#[test]
fn test_response_empty_body() {
let body = BodyBytes::new(Bytes::new());
let response = ProductOSResponse::from_body(body, String::from("http://example.com"));
assert_eq!(response.status_code(), 200);
}
#[test]
fn test_response_with_different_urls() {
let urls = vec![
"http://example.com",
"https://example.com/path",
"http://example.com:8080",
"https://api.example.com/v2/resource",
];
for url in urls {
let body = BodyBytes::new(Bytes::from("test"));
let response = ProductOSResponse::from_body(body, String::from(url));
assert_eq!(response.response_url(), url);
}
}
#[test]
fn test_response_from_response_preserves_status() {
use product_os_http::Response;
let http_response = Response::builder()
.status(404)
.body(BodyBytes::new(Bytes::from("not found")))
.unwrap();
let response =
ProductOSResponse::from_response(http_response, String::from("http://example.com"));
assert_eq!(response.status_code(), 404);
}
#[test]
fn test_response_from_parts_preserves_headers() {
use product_os_http::Response;
let http_response = Response::builder()
.status(200)
.header("x-custom", "test-value")
.body(BodyBytes::new(Bytes::from("ok")))
.unwrap();
let (parts, body) = http_response.into_parts();
let response = ProductOSResponse::from_parts(parts, body, String::from("http://example.com"));
let headers = response.try_get_headers().unwrap();
assert_eq!(headers.get("x-custom"), Some(&String::from("test-value")));
}
#[test]
fn test_response_status_code_after_consume_returns_zero() {
let body = BodyBytes::new(Bytes::from("test"));
let response = ProductOSResponse::from_body(body, String::from("http://example.com")); let _ = response.into_body(); let body2 = BodyBytes::new(Bytes::from("test2"));
let response2 = ProductOSResponse::from_body(body2, String::from("http://example.com"));
assert_eq!(response2.status_code(), 200);
}