intrepid_core/extract/
http_get.rsuse crate::{Context, Frame, HttpFrameMeta};
use super::{Extractor, MessageFrameError, MessageMetaJson, MetaMismatchError};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HttpGet;
impl<State> Extractor<State> for HttpGet {
type Error = MessageFrameError;
fn extract(frame: Frame, context: &Context<State>) -> Result<Self, Self::Error>
where
Self: Sized,
{
let MessageMetaJson(http_meta) = MessageMetaJson::<HttpFrameMeta>::extract(frame, context)?;
if http_meta.method != "GET" {
return Err(MetaMismatchError(http_meta.method))?;
}
Ok(Self)
}
}
#[tokio::test]
async fn extracting_different_methods() -> Result<(), Box<dyn std::error::Error>> {
use crate::HttpRequestFrame;
use axum::http::Request;
let request = HttpRequestFrame::from(Request::get("/test").body(axum::body::Body::default())?);
let context = Context::<()>::default();
let extraction = HttpGet::extract(request.into_frame().await, &context);
assert!(extraction.is_ok());
let failure_cases = [
"POST", "PUT", "DELETE", "HEAD", "OPTIONS", "CONNECT", "PATCH", "TRACE",
];
for method in failure_cases {
let request = HttpRequestFrame::from(
Request::builder()
.method(method)
.uri("/test")
.body(axum::body::Body::default())?,
);
let context = Context::<()>::default();
let extraction = HttpGet::extract(request.into_frame().await, &context);
assert!(matches!(
extraction.unwrap_err(),
MessageFrameError::MetaMismatch(MetaMismatchError(_))
));
}
Ok(())
}
#[test]
fn failure() -> Result<(), MessageFrameError> {
use crate::{Error, ExtractorError};
let context = Context::<()>::default();
let result = HttpGet::extract_from_frame_and_state(Frame::default(), &context);
assert!(matches!(
result.unwrap_err(),
Error::ExtractorError(ExtractorError::MessageFrameError(
MessageFrameError::WrongFrame(_)
))
),);
Ok(())
}