|
6 | 6 | //!
|
7 | 7 | //! ```
|
8 | 8 | //! use tower_http::auth::{AsyncRequireAuthorizationLayer, AsyncAuthorizeRequest};
|
9 |
| -//! use hyper::{Request, Response, Body, Error}; |
10 |
| -//! use http::{StatusCode, header::AUTHORIZATION}; |
11 |
| -//! use tower::{Service, ServiceExt, ServiceBuilder, service_fn}; |
| 9 | +//! use http::{Request, Response, StatusCode, header::AUTHORIZATION}; |
| 10 | +//! use tower::{Service, ServiceExt, ServiceBuilder, service_fn, BoxError}; |
12 | 11 | //! use futures_util::future::BoxFuture;
|
| 12 | +//! use bytes::Bytes; |
| 13 | +//! use http_body_util::Full; |
13 | 14 | //!
|
14 | 15 | //! #[derive(Clone, Copy)]
|
15 | 16 | //! struct MyAuth;
|
|
19 | 20 | //! B: Send + Sync + 'static,
|
20 | 21 | //! {
|
21 | 22 | //! type RequestBody = B;
|
22 |
| -//! type ResponseBody = Body; |
| 23 | +//! type ResponseBody = Full<Bytes>; |
23 | 24 | //! type Future = BoxFuture<'static, Result<Request<B>, Response<Self::ResponseBody>>>;
|
24 | 25 | //!
|
25 | 26 | //! fn authorize(&mut self, mut request: Request<B>) -> Self::Future {
|
|
33 | 34 | //! } else {
|
34 | 35 | //! let unauthorized_response = Response::builder()
|
35 | 36 | //! .status(StatusCode::UNAUTHORIZED)
|
36 |
| -//! .body(Body::empty()) |
| 37 | +//! .body(Full::<Bytes>::default()) |
37 | 38 | //! .unwrap();
|
38 | 39 | //!
|
39 | 40 | //! Err(unauthorized_response)
|
|
47 | 48 | //! # None
|
48 | 49 | //! }
|
49 | 50 | //!
|
50 |
| -//! #[derive(Debug)] |
| 51 | +//! #[derive(Debug, Clone)] |
51 | 52 | //! struct UserId(String);
|
52 | 53 | //!
|
53 |
| -//! async fn handle(request: Request<Body>) -> Result<Response<Body>, Error> { |
| 54 | +//! async fn handle(request: Request<Full<Bytes>>) -> Result<Response<Full<Bytes>>, BoxError> { |
54 | 55 | //! // Access the `UserId` that was set in `on_authorized`. If `handle` gets called the
|
55 | 56 | //! // request was authorized and `UserId` will be present.
|
56 | 57 | //! let user_id = request
|
|
60 | 61 | //!
|
61 | 62 | //! println!("request from {:?}", user_id);
|
62 | 63 | //!
|
63 |
| -//! Ok(Response::new(Body::empty())) |
| 64 | +//! Ok(Response::new(Full::default())) |
64 | 65 | //! }
|
65 | 66 | //!
|
66 | 67 | //! # #[tokio::main]
|
67 |
| -//! # async fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 68 | +//! # async fn main() -> Result<(), BoxError> { |
68 | 69 | //! let service = ServiceBuilder::new()
|
69 | 70 | //! // Authorize requests using `MyAuth`
|
70 | 71 | //! .layer(AsyncRequireAuthorizationLayer::new(MyAuth))
|
|
77 | 78 | //!
|
78 | 79 | //! ```
|
79 | 80 | //! use tower_http::auth::{AsyncRequireAuthorizationLayer, AsyncAuthorizeRequest};
|
80 |
| -//! use hyper::{Request, Response, Body, Error}; |
81 |
| -//! use http::StatusCode; |
82 |
| -//! use tower::{Service, ServiceExt, ServiceBuilder}; |
| 81 | +//! use http::{Request, Response, StatusCode}; |
| 82 | +//! use tower::{Service, ServiceExt, ServiceBuilder, BoxError}; |
83 | 83 | //! use futures_util::future::BoxFuture;
|
| 84 | +//! use http_body_util::Full; |
| 85 | +//! use bytes::Bytes; |
84 | 86 | //!
|
85 | 87 | //! async fn check_auth<B>(request: &Request<B>) -> Option<UserId> {
|
86 | 88 | //! // ...
|
|
90 | 92 | //! #[derive(Debug)]
|
91 | 93 | //! struct UserId(String);
|
92 | 94 | //!
|
93 |
| -//! async fn handle(request: Request<Body>) -> Result<Response<Body>, Error> { |
| 95 | +//! async fn handle(request: Request<Full<Bytes>>) -> Result<Response<Full<Bytes>>, BoxError> { |
94 | 96 | //! # todo!();
|
95 | 97 | //! // ...
|
96 | 98 | //! }
|
97 | 99 | //!
|
98 | 100 | //! # #[tokio::main]
|
99 |
| -//! # async fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 101 | +//! # async fn main() -> Result<(), BoxError> { |
100 | 102 | //! let service = ServiceBuilder::new()
|
101 |
| -//! .layer(AsyncRequireAuthorizationLayer::new(|request: Request<Body>| async move { |
| 103 | +//! .layer(AsyncRequireAuthorizationLayer::new(|request: Request<Full<Bytes>>| async move { |
102 | 104 | //! if let Some(user_id) = check_auth(&request).await {
|
103 | 105 | //! Ok(request)
|
104 | 106 | //! } else {
|
105 | 107 | //! let unauthorized_response = Response::builder()
|
106 | 108 | //! .status(StatusCode::UNAUTHORIZED)
|
107 |
| -//! .body(Body::empty()) |
| 109 | +//! .body(Full::<Bytes>::default()) |
108 | 110 | //! .unwrap();
|
109 | 111 | //!
|
110 | 112 | //! Err(unauthorized_response)
|
@@ -306,9 +308,9 @@ where
|
306 | 308 | mod tests {
|
307 | 309 | #[allow(unused_imports)]
|
308 | 310 | use super::*;
|
| 311 | + use crate::test_helpers::Body; |
309 | 312 | use futures_util::future::BoxFuture;
|
310 | 313 | use http::{header, StatusCode};
|
311 |
| - use hyper::Body; |
312 | 314 | use tower::{BoxError, ServiceBuilder, ServiceExt};
|
313 | 315 |
|
314 | 316 | #[derive(Clone, Copy)]
|
@@ -346,7 +348,7 @@ mod tests {
|
346 | 348 | }
|
347 | 349 | }
|
348 | 350 |
|
349 |
| - #[derive(Debug)] |
| 351 | + #[derive(Clone, Debug)] |
350 | 352 | struct UserId(String);
|
351 | 353 |
|
352 | 354 | #[tokio::test]
|
|
0 commit comments