#![doc(
html_favicon_url = "https://raw.githubusercontent.com/plabayo/rama/main/docs/img/rama_logo.svg"
)]
#![doc(
html_logo_url = "https://raw.githubusercontent.com/plabayo/rama/main/docs/img/rama_logo.svg"
)]
#![cfg_attr(docsrs, feature(doc_cfg))]
use rama_core::extensions::Extension;
use rama_http_types::{
HeaderMap, Method, Request, Response, StatusCode, Version, request, response,
};
use rama_net::uri::Uri;
mod into_hyperium;
mod into_rama;
#[doc(inline)]
pub use into_hyperium::{HyperiumBody, HyperiumBodyError, TryIntoHyperiumHttp};
#[doc(inline)]
pub use into_rama::{RamaBody, RamaBodyError, TryIntoRamaHttp};
mod sealed {
pub trait Sealed {}
}
macro_rules! impl_sealed {
($($ty:ty),* $(,)?) => {
$( impl crate::sealed::Sealed for $ty {} )*
};
}
impl_sealed!(
Method,
StatusCode,
Version,
Uri,
HeaderMap,
request::Parts,
response::Parts,
http::Method,
http::StatusCode,
http::Version,
http::Uri,
http::HeaderMap,
http::request::Parts,
http::response::Parts,
);
impl<T> sealed::Sealed for Request<T> {}
impl<T> sealed::Sealed for Response<T> {}
impl<T> sealed::Sealed for http::Request<T> {}
impl<T> sealed::Sealed for http::Response<T> {}
impl sealed::Sealed for &http::HeaderMap {}
#[derive(Clone, Debug, Extension)]
pub(crate) struct HyperExtensions(pub(crate) http::Extensions);
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn request_round_trip() {
let hyper = http::Request::builder()
.method("POST")
.uri("https://example.com:8443/path?q=1")
.header("x-test", "v")
.body(())
.unwrap();
let rama: Request<()> = hyper.try_into_rama_http().unwrap();
assert_eq!(rama.method().as_str(), "POST");
assert_eq!(rama.uri().to_string(), "https://example.com:8443/path?q=1");
assert_eq!(rama.headers().get("x-test").unwrap().as_bytes(), b"v");
let back: http::Request<()> = rama.try_into_hyperium_http().unwrap();
assert_eq!(back.method(), "POST");
assert_eq!(back.uri(), "https://example.com:8443/path?q=1");
assert_eq!(back.headers().get("x-test").unwrap(), "v");
}
#[test]
fn request_round_trip_preserves_rama_extension() {
use rama_core::extensions::ExtensionsRef as _;
#[derive(Debug, Clone, PartialEq, Eq, Extension)]
struct Label(u32);
let rama = Request::builder().uri("http://x/").body(()).unwrap();
rama.extensions().insert(Label(7));
let hyper: http::Request<()> = rama.try_into_hyperium_http().unwrap();
let back: Request<()> = hyper.try_into_rama_http().unwrap();
assert_eq!(*back.extensions().get_ref::<Label>().unwrap(), Label(7));
}
#[test]
fn response_round_trip() {
let hyper = http::Response::builder()
.status(404)
.header("x-y", "z")
.body(())
.unwrap();
let rama: Response<()> = hyper.try_into_rama_http().unwrap();
assert_eq!(rama.status().as_u16(), 404);
let back: http::Response<()> = rama.try_into_hyperium_http().unwrap();
assert_eq!(back.status(), 404);
assert_eq!(back.headers().get("x-y").unwrap(), "z");
}
#[test]
fn http_uri_authority_form_preserves_host() {
let mut parts = http::uri::Parts::default();
parts.authority = Some("example.com:443".parse().unwrap());
let hyper_uri = http::Uri::from_parts(parts).unwrap();
assert!(hyper_uri.scheme().is_none() && hyper_uri.authority().is_some());
let rama_uri: Uri = hyper_uri.try_into_rama_http().unwrap();
assert_eq!(
rama_uri.host(),
Some(rama_net::address::Host::EXAMPLE_NAME.view())
);
assert_eq!(rama_uri.port_u16(), Some(443));
}
}