checkpoint

This commit is contained in:
David Pedersen
2022-07-03 16:57:44 +02:00
parent a7749d2bf2
commit 5deef529ae
3 changed files with 35 additions and 23 deletions
+1
View File
@@ -3,6 +3,7 @@ use async_trait::async_trait;
use std::convert::Infallible; use std::convert::Infallible;
/// TODO(david): docs /// TODO(david): docs
// TODO(david): document how to extract this from middleware
#[derive(Clone, Copy, Debug, Default)] #[derive(Clone, Copy, Debug, Default)]
pub struct State<S>(pub S); pub struct State<S>(pub S);
-5
View File
@@ -326,9 +326,4 @@ mod tests {
.await; .await;
assert_eq!(res.status(), StatusCode::OK); assert_eq!(res.status(), StatusCode::OK);
} }
#[test]
fn extracting_state() {
todo!()
}
} }
+34 -18
View File
@@ -259,7 +259,7 @@ macro_rules! impl_service {
impl<F, Fut, Out, S, ReqBody, ResBody, $($ty,)*> Service<Request<ReqBody>> for FromFn<F, S, ($($ty,)*)> impl<F, Fut, Out, S, ReqBody, ResBody, $($ty,)*> Service<Request<ReqBody>> for FromFn<F, S, ($($ty,)*)>
where where
F: FnMut($($ty),*, Next<ReqBody>) -> Fut + Clone + Send + 'static, F: FnMut($($ty),*, Next<ReqBody>) -> Fut + Clone + Send + 'static,
$( $ty: FromRequest<ReqBody> + Send, )* $( $ty: FromRequest<(), ReqBody> + Send, )*
Fut: Future<Output = Out> + Send + 'static, Fut: Future<Output = Out> + Send + 'static,
Out: IntoResponse + 'static, Out: IntoResponse + 'static,
S: Service<Request<ReqBody>, Response = Response<ResBody>, Error = Infallible> S: Service<Request<ReqBody>, Response = Response<ResBody>, Error = Infallible>
@@ -286,7 +286,7 @@ macro_rules! impl_service {
let mut f = self.f.clone(); let mut f = self.f.clone();
let future = Box::pin(async move { let future = Box::pin(async move {
let mut parts = RequestParts::new(req); let mut parts = RequestParts::new((), req);
$( $(
let $ty = match $ty::from_request(&mut parts).await { let $ty = match $ty::from_request(&mut parts).await {
Ok(value) => value, Ok(value) => value,
@@ -370,9 +370,8 @@ impl fmt::Debug for ResponseFuture {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::{body::Empty, routing::get, Router}; use crate::{extract::State, routing::get, test_helpers::TestClient, Router};
use http::{HeaderMap, StatusCode}; use http::{HeaderMap, StatusCode};
use tower::ServiceExt;
#[tokio::test] #[tokio::test]
async fn basic() { async fn basic() {
@@ -391,22 +390,39 @@ mod tests {
.route("/", get(handle)) .route("/", get(handle))
.layer(from_fn(insert_header)); .layer(from_fn(insert_header));
let res = app let client = TestClient::new(app);
.oneshot(
Request::builder() let res = client.get("/").send().await;
.uri("/")
.body(body::boxed(Empty::new()))
.unwrap(),
)
.await
.unwrap();
assert_eq!(res.status(), StatusCode::OK); assert_eq!(res.status(), StatusCode::OK);
let body = hyper::body::to_bytes(res).await.unwrap(); assert_eq!(res.text().await, "ok");
assert_eq!(&body[..], b"ok");
} }
#[test] #[tokio::test]
fn extracting_state() { async fn extracting_state() {
todo!() async fn access_state<B>(req: Request<B>, next: Next<B>) -> impl IntoResponse {
let State(state) = req.extensions().get::<State<AppState>>().unwrap().clone();
state.value
}
async fn handle() {
panic!()
}
#[derive(Clone)]
struct AppState {
value: &'static str,
}
let app = Router::with_state(AppState { value: "foo" })
.route("/", get(handle))
.layer(from_fn(access_state));
let client = TestClient::new(app);
let res = client.get("/").send().await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.text().await, "foo");
} }
} }