diff --git a/Cargo.lock b/Cargo.lock index 5dbca1e..b822377 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -63,19 +63,19 @@ checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" [[package]] name = "axum" -version = "0.6.20" +version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3b829e4e32b91e643de6eafe82b1d90675f5874230191a4ffbc1b336dec4d6bf" +checksum = "810a80b128d70e6ed2bdf3fe8ed72c0ae56f5f5948d01c2753282dd92a84fce8" dependencies = [ "async-trait", "axum-core", - "bitflags", "bytes", "futures-util", - "headers", - "http 0.2.9", - "http-body 0.4.5", + "http", + "http-body", + "http-body-util", "hyper", + "hyper-util", "itoa", "matchit", "memchr", @@ -96,38 +96,41 @@ dependencies = [ [[package]] name = "axum-core" -version = "0.3.4" +version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "759fa577a247914fd3f7f76d62972792636412fbfd634cd452f6a385a74d2d2c" +checksum = "de0ddc355eab88f4955090a823715df47acf0b7660aab7a69ad5ce6301ee3b73" dependencies = [ "async-trait", "bytes", "futures-util", - "http 0.2.9", - "http-body 0.4.5", + "http", + "http-body", + "http-body-util", "mime", + "pin-project-lite", "rustversion", + "sync_wrapper", "tower-layer", "tower-service", ] [[package]] name = "axum-extra" -version = "0.8.0" +version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4ab90e7b70bea63a153137162affb6a0bce26b584c24a4c7885509783e2cf30b" +checksum = "523ae92256049a3b02d3bb4df80152386cd97ddba0c8c5077619bdc8c4b1859b" dependencies = [ "axum", "axum-core", "bytes", "cookie", "futures-util", - "http 0.2.9", - "http-body 0.4.5", + "http", + "http-body", + "http-body-util", "mime", "pin-project-lite", "serde", - "tokio", "tower", "tower-layer", "tower-service", @@ -148,18 +151,6 @@ dependencies = [ "rustc-demangle", ] -[[package]] -name = "base64" -version = "0.13.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8" - -[[package]] -name = "bitflags" -version = "1.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" - [[package]] name = "blake3" version = "1.5.0" @@ -173,15 +164,6 @@ dependencies = [ "constant_time_eq", ] -[[package]] -name = "block-buffer" -version = "0.10.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" -dependencies = [ - "generic-array", -] - [[package]] name = "bumpalo" version = "3.12.0" @@ -238,9 +220,9 @@ checksum = "f7144d30dcf0fafbce74250a3963025d8d52177934239851c917d29f1df280c2" [[package]] name = "cookie" -version = "0.17.0" +version = "0.18.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7efb37c3e1ccb1ff97164ad95ac1606e8ccd35b3fa0a7d99a304c7f4a428cc24" +checksum = "3cd91cf61412820176e137621345ee43b3f4423e589e7ae4e50d601d93e35ef8" dependencies = [ "percent-encoding", "time", @@ -253,25 +235,6 @@ version = "0.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5827cebf4670468b8772dd191856768aedcb1b0278a04f989f7766351917b9dc" -[[package]] -name = "cpufeatures" -version = "0.2.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "28d997bd5e24a5928dd43e46dc529867e207907fe0b239c3477d924f7f2ca320" -dependencies = [ - "libc", -] - -[[package]] -name = "crypto-common" -version = "0.1.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" -dependencies = [ - "generic-array", - "typenum", -] - [[package]] name = "cxx" version = "1.0.92" @@ -326,14 +289,10 @@ dependencies = [ ] [[package]] -name = "digest" -version = "0.10.6" +name = "equivalent" +version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8168378f4e5023e7218c89c891c0fd8ecdb5e5e4f18cb78f38cf245dd021e76f" -dependencies = [ - "block-buffer", - "crypto-common", -] +checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" [[package]] name = "fnv" @@ -439,16 +398,6 @@ dependencies = [ "slab", ] -[[package]] -name = "generic-array" -version = "0.14.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bff49e947297f3312447abdca79f45f4738097cc82b06e72054d2223f601f1b9" -dependencies = [ - "typenum", - "version_check", -] - [[package]] name = "getrandom" version = "0.2.8" @@ -467,29 +416,29 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6c80984affa11d98d1b88b66ac8853f143217b399d3c74116778ff8fdb4ed2e" [[package]] -name = "headers" -version = "0.3.8" +name = "h2" +version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f3e372db8e5c0d213e0cd0b9be18be2aca3d44cf2fe30a9d46a65581cd454584" +checksum = "e1d308f63daf4181410c242d34c11f928dcb3aa105852019e043c9d1f4e4368a" dependencies = [ - "base64", - "bitflags", "bytes", - "headers-core", - "http 0.2.9", - "httpdate", - "mime", - "sha1", + "fnv", + "futures-core", + "futures-sink", + "futures-util", + "http", + "indexmap", + "slab", + "tokio", + "tokio-util", + "tracing", ] [[package]] -name = "headers-core" -version = "0.2.0" +name = "hashbrown" +version = "0.14.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e7f66481bfee273957b1f20485a4ff3362987f85b2c236580d81b4eb7a326429" -dependencies = [ - "http 0.2.9", -] +checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604" [[package]] name = "hermit-abi" @@ -500,17 +449,6 @@ dependencies = [ "libc", ] -[[package]] -name = "http" -version = "0.2.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bd6effc99afb63425aff9b05836f029929e345a6148a14b7ecd5ab67af944482" -dependencies = [ - "bytes", - "fnv", - "itoa", -] - [[package]] name = "http" version = "1.0.0" @@ -524,23 +462,25 @@ dependencies = [ [[package]] name = "http-body" -version = "0.4.5" +version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d5f38f16d184e36f2408a55281cd658ecbd3ca05cce6d6510a176eca393e26d1" +checksum = "1cac85db508abc24a2e48553ba12a996e87244a0395ce011e62b37158745d643" dependencies = [ "bytes", - "http 0.2.9", - "pin-project-lite", + "http", ] [[package]] -name = "http-body" -version = "1.0.0" +name = "http-body-util" +version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1cac85db508abc24a2e48553ba12a996e87244a0395ce011e62b37158745d643" +checksum = "41cb79eb393015dadd30fc252023adb0b2400a0caee0fa2a077e6e21a551e840" dependencies = [ "bytes", - "http 1.0.0", + "futures-util", + "http", + "http-body", + "pin-project-lite", ] [[package]] @@ -557,25 +497,41 @@ checksum = "c4a1e36c821dbe04574f602848a19f742f4fb3c98d40449f11bcad18d6b17421" [[package]] name = "hyper" -version = "0.14.27" +version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ffb1cfd654a8219eaef89881fdb3bb3b1cdc5fa75ded05d6933b2b382e395468" +checksum = "403f9214f3e703236b221f1a9cd88ec8b4adfa5296de01ab96216361f4692f56" dependencies = [ "bytes", "futures-channel", - "futures-core", "futures-util", - "http 0.2.9", - "http-body 0.4.5", + "h2", + "http", + "http-body", "httparse", "httpdate", "itoa", "pin-project-lite", - "socket2 0.4.9", "tokio", +] + +[[package]] +name = "hyper-util" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ca339002caeb0d159cc6e023dff48e199f081e42fa039895c7c6f38b37f2e9d" +dependencies = [ + "bytes", + "futures-channel", + "futures-util", + "http", + "http-body", + "hyper", + "pin-project-lite", + "socket2", + "tokio", + "tower", "tower-service", "tracing", - "want", ] [[package]] @@ -602,6 +558,16 @@ dependencies = [ "cxx-build", ] +[[package]] +name = "indexmap" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d530e1a18b1cb4c484e6e34556a0d948706958449fca0cab753d649f2bce3d1f" +dependencies = [ + "equivalent", + "hashbrown", +] + [[package]] name = "itoa" version = "1.0.6" @@ -898,17 +864,6 @@ dependencies = [ "serde", ] -[[package]] -name = "sha1" -version = "0.10.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f04293dc80c3993519f2d7f6f511707ee7094fe0c6d3406feb330cdb3540eba3" -dependencies = [ - "cfg-if", - "cpufeatures", - "digest", -] - [[package]] name = "slab" version = "0.4.8" @@ -918,16 +873,6 @@ dependencies = [ "autocfg", ] -[[package]] -name = "socket2" -version = "0.4.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "64a4a911eed85daf18834cfaa86a79b7d266ff93ff5ba14005426219480ed662" -dependencies = [ - "libc", - "winapi", -] - [[package]] name = "socket2" version = "0.5.4" @@ -1031,11 +976,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4f38200e3ef7995e5ef13baec2f432a6da0aa9ac495b2c0e8f3b7eec2c92d653" dependencies = [ "backtrace", + "bytes", "libc", "mio", "num_cpus", "pin-project-lite", - "socket2 0.5.4", + "socket2", "tokio-macros", "windows-sys 0.48.0", ] @@ -1051,6 +997,20 @@ dependencies = [ "syn 2.0.29", ] +[[package]] +name = "tokio-util" +version = "0.7.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5419f34732d9eb6ee4c3578b7989078579b7f039cbbb9ca2c4da015749371e15" +dependencies = [ + "bytes", + "futures-core", + "futures-sink", + "pin-project-lite", + "tokio", + "tracing", +] + [[package]] name = "tower" version = "0.4.13" @@ -1111,12 +1071,6 @@ dependencies = [ "once_cell", ] -[[package]] -name = "try-lock" -version = "0.2.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3528ecfd12c466c6f163363caf2d02a71161dd5e1cc6ae7b34207ea2d42d81ed" - [[package]] name = "typed-session" version = "0.9.0" @@ -1141,8 +1095,8 @@ dependencies = [ "axum-extra", "chrono", "futures", - "http 0.2.9", - "http-body 1.0.0", + "http", + "http-body", "hyper", "rand", "serde", @@ -1154,12 +1108,6 @@ dependencies = [ "typed-session", ] -[[package]] -name = "typenum" -version = "1.16.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "497961ef93d974e23eb6f433eb5fe1b7930b659f06d12dec6fc44a8f554c0bba" - [[package]] name = "unicode-ident" version = "1.0.8" @@ -1178,16 +1126,6 @@ version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" -[[package]] -name = "want" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1ce8a968cb1cd110d136ff8b819a556d6fb6d919363c61534f6860c7eb172ba0" -dependencies = [ - "log", - "try-lock", -] - [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" diff --git a/Cargo.toml b/Cargo.toml index 678dbaf..2bb2f6d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,8 +21,8 @@ typed-session.workspace = true http-body = "1.0.0" tracing = "0.1.40" async-trait = "0.1.74" -axum = {version = "0.6.20", features = ["headers"]} -axum-extra = { version = "0.8.0", features = ["cookie"] } +axum = {version = "0.7.1"} +axum-extra = { version = "0.9.0", features = ["cookie"] } tokio = {version = "1.33.0", features = ["sync"]} tower = "0.4.13" futures = "0.3.29" @@ -35,6 +35,6 @@ chrono = "0.4.26" serde = {version = "1.0.190", features = ["derive"]} serde_json = "1.0.107" tokio = {version = "1.33.0", features = ["macros", "rt-multi-thread"]} -hyper = "0.14.26" -http = "0.2.9" +hyper = "1.0.1" +http = "1.0.0" typed-session = {workspace = true, features = ["memory-store"]} diff --git a/src/lib.rs b/src/lib.rs index 638173e..8e5716f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,11 +20,12 @@ //! //! ```rust,no_run //! use axum::{routing::get, Router, error_handling::HandleErrorLayer, Extension}; -//! use tower::ServiceBuilder; +//! use tower::ServiceBuilder; //! use typed_session_axum::{ //! typed_session::{MemoryStore, NoLogger}, WritableSession, SessionLayer, SessionLayerError, //! }; //! use std::fmt::Display; +//! use tokio::net::TcpListener; //! use http::StatusCode; //! //! #[tokio::main] @@ -52,10 +53,8 @@ //! .layer(Extension(store)) // provide a connection to the session database //! ); //! -//! axum::Server::bind(&"0.0.0.0:3000".parse().unwrap()) -//! .serve(app.into_make_service()) -//! .await -//! .unwrap(); +//! let listener = TcpListener::bind("0.0.0.0:3000").await.unwrap(); +//! axum::serve(listener, app).await.unwrap(); //! } //! ``` //! @@ -68,17 +67,16 @@ //! use axum::http::header::SET_COOKIE; //! use typed_session_axum::{typed_session::{MemoryStore, NoLogger}, SessionHandle, SessionLayer}; //! use http::{Request, Response}; -//! use hyper::Body; //! use rand::Rng; //! use tower::{Service, ServiceBuilder, ServiceExt}; //! -//! async fn handle(request: Request) -> Result, Infallible> { +//! async fn handle(request: Request) -> Result, Infallible> { //! let session_handle = request.extensions().get::>().unwrap(); //! let mut session = session_handle.write().await; //! // Use the session as you'd like. //! session.data_mut(); //! -//! Ok(Response::new(Body::empty())) +//! Ok(Response::new(Default::default())) //! } //! //! # #[tokio::main] @@ -90,7 +88,7 @@ //! .layer(session_layer) //! .service_fn(handle); //! -//! let mut request = Request::builder().body(Body::empty()).unwrap(); +//! let mut request = Request::builder().body(String::new()).unwrap(); //! request.extensions_mut().insert(store); // provide a connection to the session database //! //! let response = service.ready().await?.call(request).await?; diff --git a/src/session/mod.rs b/src/session/mod.rs index 41f3a35..ec92b52 100644 --- a/src/session/mod.rs +++ b/src/session/mod.rs @@ -178,12 +178,12 @@ impl> } fn build_cookie(&self, cookie_value: String, expiry: SessionExpiry) -> Cookie<'static> { - let mut cookie = Cookie::build(self.cookie_name.clone(), cookie_value) + let mut cookie = Cookie::build((self.cookie_name.clone(), cookie_value)) .http_only(true) .same_site(self.same_site_policy) .secure(self.secure) .path(self.cookie_path.clone()) - .finish(); + .build(); match expiry { SessionExpiry::DateTime(expiry) => cookie.set_expires(Some( @@ -200,7 +200,7 @@ impl> } fn build_removal_cookie(&self) -> Cookie<'static> { - let cookie = Cookie::build(self.cookie_name.clone(), "") + let cookie = Cookie::build((self.cookie_name.clone(), "")) .http_only(true) .path(self.cookie_path.clone()); @@ -209,7 +209,7 @@ impl> } else { cookie } - .finish(); + .build(); cookie.make_removal(); @@ -288,18 +288,16 @@ impl s } impl< - Inner, - ReqBody, - ResBody, + Inner: 'static, + RequestBody: Send + 'static, + ResponseBody: Send, SessionData: Default + Debug + Send + Sync + 'static, SessionStoreConnection: SessionStoreConnector + Clone + Send + Sync + 'static, - > Service> for Session + > Service> for Session where - Inner: Service, Response = Response> + Clone + Send + 'static, - ResBody: Send + 'static, - ReqBody: Send + 'static, - Inner::Future: Send + 'static, - >::Error: Send + 'static, + Inner: Service, Response = Response> + Clone + Send, + Inner::Future: Send, + >::Error: Send, { type Response = Inner::Response; type Error = SessionLayerError< @@ -312,7 +310,7 @@ where self.inner.poll_ready(cx).map_err(SessionLayerError::Inner) } - fn call(&mut self, mut request: Request) -> Self::Future { + fn call(&mut self, mut request: Request) -> Self::Future { let session_layer = self.layer.clone(); let inner = self.inner.clone(); @@ -408,19 +406,12 @@ mod tests { header::{COOKIE, SET_COOKIE}, HeaderValue, StatusCode, }; - use hyper::Body; - use serde::{Deserialize, Serialize}; use std::str::FromStr; use tower::{BoxError, Service, ServiceBuilder, ServiceExt}; use typed_session::{DefaultLogger, NoLogger}; use crate::{typed_session::MemoryStore, SessionHandle, SessionLayer}; - #[derive(Deserialize, Serialize, PartialEq, Debug)] - struct Counter { - counter: i32, - } - enum ExpectedResult { Some, None, @@ -434,7 +425,7 @@ mod tests { .layer(session_layer) .service_fn(echo_with_session_change); - let mut request = Request::get("/").body(Body::empty()).unwrap(); + let mut request = Request::get("/").body("").unwrap(); request.extensions_mut().insert(store); let res = service.ready().await.unwrap().call(request).await.unwrap(); @@ -457,7 +448,7 @@ mod tests { .layer(session_layer) .service_fn(increment); - let mut request = Request::get("/").body(Body::empty()).unwrap(); + let mut request = Request::get("/").body("").unwrap(); request.extensions_mut().insert(store.clone()); let res = service.ready().await.unwrap().call(request).await.unwrap(); @@ -465,11 +456,10 @@ mod tests { assert_eq!(res.status(), StatusCode::OK); - let json_bs = &hyper::body::to_bytes(res.into_body()).await.unwrap()[..]; - let counter: Counter = serde_json::from_slice(json_bs).unwrap(); - assert_eq!(counter, Counter { counter: 1 }); + let counter = res.into_body(); + assert_eq!(counter, 1); - let mut request = Request::get("/").body(Body::empty()).unwrap(); + let mut request = Request::get("/").body("").unwrap(); request .headers_mut() .insert(COOKIE, session_cookie.to_owned()); @@ -477,9 +467,8 @@ mod tests { let res = service.ready().await.unwrap().call(request).await.unwrap(); assert_eq!(res.status(), StatusCode::OK); - let json_bs = &hyper::body::to_bytes(res.into_body()).await.unwrap()[..]; - let counter: Counter = serde_json::from_slice(json_bs).unwrap(); - assert_eq!(counter, Counter { counter: 2 }); + let counter = res.into_body(); + assert_eq!(counter, 2); } #[tokio::test] @@ -490,7 +479,7 @@ mod tests { .layer(session_layer) .service_fn(increment); - let mut request = Request::get("/").body(Body::empty()).unwrap(); + let mut request = Request::get("/").body("").unwrap(); request.extensions_mut().insert(store.clone()); let res = service.ready().await.unwrap().call(request).await.unwrap(); @@ -504,19 +493,17 @@ mod tests { assert_eq!(res.status(), StatusCode::OK); - let json_bs = &hyper::body::to_bytes(res.into_body()).await.unwrap()[..]; - let counter: Counter = serde_json::from_slice(json_bs).unwrap(); - assert_eq!(counter, Counter { counter: 1 }); + let counter = res.into_body(); + assert_eq!(counter, 1); - let mut request = Request::get("/").body(Body::empty()).unwrap(); + let mut request = Request::get("/").body("").unwrap(); request.headers_mut().insert(COOKIE, request_cookie); request.extensions_mut().insert(store); let res = service.ready().await.unwrap().call(request).await.unwrap(); assert_eq!(res.status(), StatusCode::OK); - let json_bs = &hyper::body::to_bytes(res.into_body()).await.unwrap()[..]; - let counter: Counter = serde_json::from_slice(json_bs).unwrap(); - assert_eq!(counter, Counter { counter: 2 }); + let counter = res.into_body(); + assert_eq!(counter, 2); } #[tokio::test] @@ -527,7 +514,7 @@ mod tests { .layer(session_layer) .service_fn(increment); - let mut request = Request::get("/").body(Body::empty()).unwrap(); + let mut request = Request::get("/").body("").unwrap(); request.extensions_mut().insert(store.clone()); let res = service.ready().await.unwrap().call(request).await.unwrap(); @@ -536,20 +523,18 @@ mod tests { assert_eq!(res.status(), StatusCode::OK); - let json_bs = &hyper::body::to_bytes(res.into_body()).await.unwrap()[..]; - let counter: Counter = serde_json::from_slice(json_bs).unwrap(); - assert_eq!(counter, Counter { counter: 1 }); + let counter = res.into_body(); + assert_eq!(counter, 1); - let mut request = Request::get("/").body(Body::empty()).unwrap(); + let mut request = Request::get("/").body("").unwrap(); request.headers_mut().append(COOKIE, dummy_cookie); request.headers_mut().append(COOKIE, session_cookie); request.extensions_mut().insert(store); let res = service.ready().await.unwrap().call(request).await.unwrap(); assert_eq!(res.status(), StatusCode::OK); - let json_bs = &hyper::body::to_bytes(res.into_body()).await.unwrap()[..]; - let counter: Counter = serde_json::from_slice(json_bs).unwrap(); - assert_eq!(counter, Counter { counter: 2 }); + let counter = res.into_body(); + assert_eq!(counter, 2); } #[tokio::test] @@ -558,7 +543,7 @@ mod tests { let session_layer: SessionLayer> = SessionLayer::new(); let mut service = ServiceBuilder::new().layer(session_layer).service_fn(echo); - let mut request = Request::get("/").body(Body::empty()).unwrap(); + let mut request = Request::get("/").body("").unwrap(); request.extensions_mut().insert(store); let res = service.ready().await.unwrap().call(request).await.unwrap(); @@ -579,7 +564,7 @@ mod tests { .layer(&session_layer) .service_fn(echo_read_session); - let mut request = Request::get("/").body(Body::empty()).unwrap(); + let mut request = Request::get("/").body("").unwrap(); request.extensions_mut().insert(store.clone()); let res = service.ready().await.unwrap().call(request).await.unwrap(); @@ -606,7 +591,7 @@ mod tests { echo_read_session(req).await } }); - let mut request = Request::get("/").body(Body::empty()).unwrap(); + let mut request = Request::get("/").body("").unwrap(); request .headers_mut() .insert(COOKIE, "axum.sid=aW52YWxpZC1zZXNzaW9uLWlk".parse().unwrap()); @@ -650,7 +635,7 @@ mod tests { .layer(session_layer) .service_fn(destroy); - let mut request = Request::get("/").body(Body::empty()).unwrap(); + let mut request = Request::get("/").body("").unwrap(); request.extensions_mut().insert(store.clone()); let res = service.ready().await.unwrap().call(request).await.unwrap(); @@ -663,7 +648,7 @@ mod tests { .to_str() .unwrap() .to_string(); - let mut request = Request::get("/destroy").body(Body::empty()).unwrap(); + let mut request = Request::get("/destroy").body("").unwrap(); request .headers_mut() .insert(COOKIE, session_cookie.parse().unwrap()); @@ -677,11 +662,11 @@ mod tests { ); } - async fn echo(req: Request) -> Result, BoxError> { + async fn echo(req: Request) -> Result, BoxError> { Ok(Response::new(req.into_body())) } - async fn echo_read_session(req: Request) -> Result, BoxError> { + async fn echo_read_session(req: Request) -> Result, BoxError> { { let session_handle = req.extensions().get::>().unwrap(); let session = session_handle.write().await; @@ -690,7 +675,9 @@ mod tests { Ok(Response::new(req.into_body())) } - async fn echo_with_session_change(req: Request) -> Result, BoxError> { + async fn echo_with_session_change( + req: Request, + ) -> Result, BoxError> { { let session_handle = req.extensions().get::>().unwrap(); let mut session = session_handle.write().await; @@ -699,7 +686,7 @@ mod tests { Ok(Response::new(req.into_body())) } - async fn destroy(req: Request) -> Result, BoxError> { + async fn destroy(req: Request) -> Result, BoxError> { // Destroy the session if we received a session cookie. if req.headers().get(COOKIE).is_some() { let session_handle = req.extensions().get::>().unwrap(); @@ -717,7 +704,7 @@ mod tests { Ok(Response::new(req.into_body())) } - async fn increment(mut req: Request) -> Result, BoxError> { + async fn increment(req: Request) -> Result, BoxError> { let counter; { @@ -727,9 +714,6 @@ mod tests { counter = *session.data(); } - let body = serde_json::to_string(&Counter { counter }).unwrap(); - *req.body_mut() = Body::from(body); - - Ok(Response::new(req.into_body())) + Ok(Response::new(counter)) } } diff --git a/tests/test.rs b/tests/test.rs index 558e7b2..eb81244 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -2,12 +2,11 @@ use axum::error_handling::HandleErrorLayer; use axum::routing::get; use axum::{Extension, Router}; use axum_extra::extract::cookie::Cookie; +use futures::StreamExt; use http::header::{COOKIE, SET_COOKIE}; use http::{HeaderValue, Request, StatusCode}; -use hyper::service::Service; -use hyper::Body; use std::convert::Infallible; -use tower::{ServiceBuilder, ServiceExt}; +use tower::{Service, ServiceBuilder, ServiceExt}; use typed_session::{MemoryStore, NoLogger}; use typed_session_axum::{ReadableSession, SessionLayer, SessionLayerError, WritableSession}; @@ -62,7 +61,7 @@ async fn test_hello_world() { .oneshot( Request::builder() .uri("/hello-world") - .body(Body::empty()) + .body(String::new()) .unwrap(), ) .await @@ -70,8 +69,14 @@ async fn test_hello_world() { assert_eq!(response.status(), StatusCode::OK); - let body = hyper::body::to_bytes(response.into_body()).await.unwrap(); - assert_eq!(&body[..], b"Hello, World!"); + let body = response + .into_body() + .into_data_stream() + .next() + .await + .unwrap() + .unwrap(); + assert_eq!(body, "Hello, World!".as_bytes()); } #[tokio::test] @@ -83,7 +88,7 @@ async fn test_persistent_session() { .call( Request::builder() .uri("/delete") - .body(Body::empty()) + .body(String::new()) .unwrap(), ) .await @@ -94,18 +99,24 @@ async fn test_persistent_session() { // Calling get without a cookie should return the default value, and return no cookie. let response = app - .call(Request::builder().uri("/get").body(Body::empty()).unwrap()) + .call(Request::builder().uri("/get").body(String::new()).unwrap()) .await .unwrap(); assert_eq!(response.status(), StatusCode::OK); assert!(!response.headers().contains_key(SET_COOKIE)); - let body = hyper::body::to_bytes(response.into_body()).await.unwrap(); + let body = response + .into_body() + .into_data_stream() + .next() + .await + .unwrap() + .unwrap(); assert_eq!(&body[..], b"false"); // Calling set should return a new cookie associated with the value true. let response = app - .call(Request::builder().uri("/set").body(Body::empty()).unwrap()) + .call(Request::builder().uri("/set").body(String::new()).unwrap()) .await .unwrap(); @@ -114,18 +125,28 @@ async fn test_persistent_session() { let cookie = response.headers().get(SET_COOKIE).unwrap().clone(); let cookie = Cookie::parse_encoded(cookie.to_str().unwrap()).unwrap(); println!("{cookie:?}"); - let body = hyper::body::to_bytes(response.into_body()).await.unwrap(); - assert_eq!(&body[..], b""); + assert!(response + .into_body() + .into_data_stream() + .next() + .await + .is_none()); // Calling get without cookie should return the default value. let response = app - .call(Request::builder().uri("/get").body(Body::empty()).unwrap()) + .call(Request::builder().uri("/get").body(String::new()).unwrap()) .await .unwrap(); assert_eq!(response.status(), StatusCode::OK); assert!(!response.headers().contains_key(SET_COOKIE)); - let body = hyper::body::to_bytes(response.into_body()).await.unwrap(); + let body = response + .into_body() + .into_data_stream() + .next() + .await + .unwrap() + .unwrap(); assert_eq!(&body[..], b"false"); // Calling get with the previous cookie should remember the value. @@ -134,7 +155,7 @@ async fn test_persistent_session() { Request::builder() .uri("/get") .header(COOKIE, HeaderValue::from_str(&cookie.to_string()).unwrap()) - .body(Body::empty()) + .body(String::new()) .unwrap(), ) .await @@ -142,7 +163,13 @@ async fn test_persistent_session() { assert_eq!(response.status(), StatusCode::OK); assert!(!response.headers().contains_key(SET_COOKIE)); - let body = hyper::body::to_bytes(response.into_body()).await.unwrap(); + let body = response + .into_body() + .into_data_stream() + .next() + .await + .unwrap() + .unwrap(); assert_eq!(&body[..], b"true"); // Calling unset should return a new cookie associated with the value false. @@ -151,7 +178,7 @@ async fn test_persistent_session() { Request::builder() .uri("/unset") .header(COOKIE, HeaderValue::from_str(&cookie.to_string()).unwrap()) - .body(Body::empty()) + .body(String::new()) .unwrap(), ) .await @@ -162,8 +189,12 @@ async fn test_persistent_session() { let cookie2 = response.headers().get(SET_COOKIE).unwrap().clone(); let cookie2 = Cookie::parse_encoded(cookie2.to_str().unwrap()).unwrap(); println!("{cookie2:?}"); - let body = hyper::body::to_bytes(response.into_body()).await.unwrap(); - assert_eq!(&body[..], b""); + assert!(response + .into_body() + .into_data_stream() + .next() + .await + .is_none()); // Calling get with the old cookie should return the default value false. let response = app @@ -171,7 +202,7 @@ async fn test_persistent_session() { Request::builder() .uri("/get") .header(COOKIE, HeaderValue::from_str(&cookie.to_string()).unwrap()) - .body(Body::empty()) + .body(String::new()) .unwrap(), ) .await @@ -179,7 +210,13 @@ async fn test_persistent_session() { assert_eq!(response.status(), StatusCode::OK); assert!(!response.headers().contains_key(SET_COOKIE)); - let body = hyper::body::to_bytes(response.into_body()).await.unwrap(); + let body = response + .into_body() + .into_data_stream() + .next() + .await + .unwrap() + .unwrap(); assert_eq!(&body[..], b"false"); // Calling get with the new cookie should return the previously set value false. @@ -188,7 +225,7 @@ async fn test_persistent_session() { Request::builder() .uri("/get") .header(COOKIE, HeaderValue::from_str(&cookie2.to_string()).unwrap()) - .body(Body::empty()) + .body(String::new()) .unwrap(), ) .await @@ -196,7 +233,13 @@ async fn test_persistent_session() { assert_eq!(response.status(), StatusCode::OK); assert!(!response.headers().contains_key(SET_COOKIE)); - let body = hyper::body::to_bytes(response.into_body()).await.unwrap(); + let body = response + .into_body() + .into_data_stream() + .next() + .await + .unwrap() + .unwrap(); assert_eq!(&body[..], b"false"); // Calling set with the first cookie should return a new cookie associated with the value true. @@ -206,7 +249,7 @@ async fn test_persistent_session() { Request::builder() .uri("/set") .header(COOKIE, HeaderValue::from_str(&cookie.to_string()).unwrap()) - .body(Body::empty()) + .body(String::new()) .unwrap(), ) .await @@ -217,8 +260,12 @@ async fn test_persistent_session() { let cookie3 = response.headers().get(SET_COOKIE).unwrap().clone(); let cookie3 = Cookie::parse_encoded(cookie3.to_str().unwrap()).unwrap(); println!("{cookie3:?}"); - let body = hyper::body::to_bytes(response.into_body()).await.unwrap(); - assert_eq!(&body[..], b""); + assert!(response + .into_body() + .into_data_stream() + .next() + .await + .is_none()); // Calling get with the newest cookie should return the previously set value true. // This should not be disturbed by the fact that the value was set while passing an illegal cookie. @@ -227,7 +274,7 @@ async fn test_persistent_session() { Request::builder() .uri("/get") .header(COOKIE, HeaderValue::from_str(&cookie3.to_string()).unwrap()) - .body(Body::empty()) + .body(String::new()) .unwrap(), ) .await @@ -235,6 +282,12 @@ async fn test_persistent_session() { assert_eq!(response.status(), StatusCode::OK); assert!(!response.headers().contains_key(SET_COOKIE)); - let body = hyper::body::to_bytes(response.into_body()).await.unwrap(); + let body = response + .into_body() + .into_data_stream() + .next() + .await + .unwrap() + .unwrap(); assert_eq!(&body[..], b"true"); }