use crate::{ffi, mem};
pub fn method() -> String {
unsafe { mem::read_packed_string(ffi::req_method()) }.unwrap_or_default()
}
pub fn url() -> String {
unsafe { mem::read_packed_string(ffi::req_url()) }.unwrap_or_default()
}
pub fn header(name: &str) -> Option<String> {
let (name_ptr, name_len) = mem::host_arg_str(name);
let result = unsafe { ffi::req_header_get(name_ptr, name_len) };
unsafe { mem::read_packed_string(result) }
}
pub fn body() -> Vec<u8> {
unsafe { mem::read_packed_bytes(ffi::req_body()) }.unwrap_or_default()
}
#[cfg(test)]
mod tests {
use super::*;
use crate::ffi::test_host;
#[test]
fn method_returns_host_value() {
test_host::reset();
test_host::with_mock(|m| m.req_method = Some("POST".into()));
assert_eq!(method(), "POST");
}
#[test]
fn method_empty_when_unset() {
test_host::reset();
assert_eq!(method(), "");
}
#[test]
fn url_returns_host_value() {
test_host::reset();
test_host::with_mock(|m| m.req_url = Some("https://flaron.dev/path?q=1".into()));
assert_eq!(url(), "https://flaron.dev/path?q=1");
}
#[test]
fn header_lookup_hits_store() {
test_host::reset();
test_host::with_mock(|m| {
m.req_headers.insert("user-agent".into(), "curl/8.0".into());
});
assert_eq!(header("user-agent").as_deref(), Some("curl/8.0"));
assert!(header("missing").is_none());
}
#[test]
fn body_reads_bytes() {
test_host::reset();
test_host::with_mock(|m| m.req_body = Some(vec![1, 2, 3, 4]));
assert_eq!(body(), vec![1, 2, 3, 4]);
}
#[test]
fn body_empty_when_unset() {
test_host::reset();
assert!(body().is_empty());
}
}