Skip to content

Commit bf4f945

Browse files
authored
feat: upgrade axum to 0.7 (#9)
* feat: upgrade dependencies * refactor: cargo fmt
1 parent 77f18fb commit bf4f945

6 files changed

+33
-35
lines changed

Cargo.toml

+5-4
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ path = "src/lib.rs"
1616
casbin = { version = "2.0.9", default-features = false, features = ["incremental", "cached"] }
1717
tokio = { version = "1.17.0", default-features = false, optional = true }
1818
async-std = { version = "1.10.0", default-features = false, optional = true }
19-
axum = "0.5.7"
19+
axum = "0.7.4"
2020
futures = "0.3"
2121
tower = { version = "0.4", features = ["full"] }
22-
http = "0.2.8"
23-
http-body = "0.4.5"
22+
http = "1.0.0"
23+
http-body = "1.0.0"
24+
http-body-util = "0.1.0"
2425
bytes = "1.1.0"
2526

2627
[features]
@@ -32,7 +33,7 @@ runtime-async-std = ["casbin/runtime-async-std", "async-std/std"]
3233
[dev-dependencies]
3334
tokio = { version = "1.17.0", features = [ "full" ] }
3435
async-std = { version = "1.10.0", features = [ "attributes" ] }
35-
axum-test-helper = "0.1.1"
36+
axum-test-helpers = "0.7.5"
3637

3738
[profile.release]
3839
codegen-units = 1

README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99

1010
## Install
1111

12-
Add it to `Cargo.toml`
12+
Add dependencies to `Cargo.toml`
1313

14-
```toml
15-
axum = "0.5.7"
16-
axum-casbin = "1.0.0"
17-
tokio = { version = "1.17.0", features = [ "full" ] }
14+
```bash
15+
cargo add axum
16+
cargo add axum-casbin
17+
cargo add tokio --features full
1818
```
1919

2020
## Requirement

src/middleware.rs

+12-15
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
use axum::{
2-
body::{self, BoxBody},
3-
response::Response,
4-
BoxError,
5-
};
1+
use axum::{body, response::Response, BoxError};
62
use bytes::Bytes;
73
use casbin::prelude::{TryIntoAdapter, TryIntoModel};
84
use casbin::{CachedEnforcer, CoreApi, Result as CasbinResult};
95
use futures::future::BoxFuture;
106
use http::{Request, StatusCode};
11-
use http_body::{Body as HttpBody, Full};
7+
use http_body::Body as HttpBody;
8+
use http_body_util::Full;
129
use std::{
1310
convert::Infallible,
1411
ops::{Deref, DerefMut},
@@ -93,7 +90,7 @@ where
9390
ResBody: HttpBody<Data = Bytes> + Send + 'static,
9491
ResBody::Error: Into<BoxError>,
9592
{
96-
type Response = Response<BoxBody>;
93+
type Response = Response;
9794
type Error = Infallible;
9895
// `BoxFuture` is a type alias for `Pin<Box<dyn Future + Send + 'a>>`
9996
type Future = BoxFuture<'static, Result<Self::Response, Self::Error>>;
@@ -116,7 +113,7 @@ where
116113
None => {
117114
return Ok(Response::builder()
118115
.status(StatusCode::UNAUTHORIZED)
119-
.body(body::boxed(Full::from("401 Unauthorized")))
116+
.body(body::Body::new(Full::from("401 Unauthorized")))
120117
.unwrap());
121118
}
122119
};
@@ -129,20 +126,20 @@ where
129126
match lock.enforce_mut(vec![subject, domain, path, action]) {
130127
Ok(true) => {
131128
drop(lock);
132-
Ok(inner.call(req).await?.map(body::boxed))
129+
Ok(inner.call(req).await?.map(body::Body::new))
133130
}
134131
Ok(false) => {
135132
drop(lock);
136133
Ok(Response::builder()
137134
.status(StatusCode::FORBIDDEN)
138-
.body(body::boxed(Full::from("403 Forbidden")))
135+
.body(body::Body::new(Full::from("403 Forbidden")))
139136
.unwrap())
140137
}
141138
Err(_) => {
142139
drop(lock);
143140
Ok(Response::builder()
144141
.status(StatusCode::BAD_GATEWAY)
145-
.body(body::boxed(Full::from("502 Bad Gateway")))
142+
.body(body::Body::new(Full::from("502 Bad Gateway")))
146143
.unwrap())
147144
}
148145
}
@@ -151,28 +148,28 @@ where
151148
match lock.enforce_mut(vec![subject, path, action]) {
152149
Ok(true) => {
153150
drop(lock);
154-
Ok(inner.call(req).await?.map(body::boxed))
151+
Ok(inner.call(req).await?.map(body::Body::new))
155152
}
156153
Ok(false) => {
157154
drop(lock);
158155
Ok(Response::builder()
159156
.status(StatusCode::FORBIDDEN)
160-
.body(body::boxed(Full::from("403 Forbidden")))
157+
.body(body::Body::new(Full::from("403 Forbidden")))
161158
.unwrap())
162159
}
163160
Err(_) => {
164161
drop(lock);
165162
Ok(Response::builder()
166163
.status(StatusCode::BAD_GATEWAY)
167-
.body(body::boxed(Full::from("502 Bad Gateway")))
164+
.body(body::Body::new(Full::from("502 Bad Gateway")))
168165
.unwrap())
169166
}
170167
}
171168
}
172169
} else {
173170
Ok(Response::builder()
174171
.status(StatusCode::UNAUTHORIZED)
175-
.body(body::boxed(Full::from("401 Unauthorized")))
172+
.body(body::Body::new(Full::from("401 Unauthorized")))
176173
.unwrap())
177174
}
178175
})

tests/test_middleware.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use axum::{response::Response, routing::get, BoxError, Router};
22
use axum_casbin::{CasbinAxumLayer, CasbinVals};
3-
use axum_test_helper::TestClient;
3+
use axum_test_helpers::TestClient;
44
use bytes::Bytes;
55
use casbin::function_map::key_match2;
66
use casbin::{CoreApi, DefaultModel, FileAdapter};
@@ -95,12 +95,12 @@ async fn test_middleware() {
9595

9696
let client = TestClient::new(app);
9797

98-
let resp_pen_1 = client.get("/pen/1").send().await;
98+
let resp_pen_1 = client.get("/pen/1").await;
9999
assert_eq!(resp_pen_1.status(), StatusCode::OK);
100100

101-
let resp_book = client.get("/book/2").send().await;
101+
let resp_book = client.get("/book/2").await;
102102
assert_eq!(resp_book.status(), StatusCode::OK);
103103

104-
let resp_pen_2 = client.get("/pen/2").send().await;
104+
let resp_pen_2 = client.get("/pen/2").await;
105105
assert_eq!(resp_pen_2.status(), StatusCode::FORBIDDEN);
106106
}

tests/test_middleware_domain.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use axum::{response::Response, routing::get, BoxError, Router};
22
use axum_casbin::{CasbinAxumLayer, CasbinVals};
3-
use axum_test_helper::TestClient;
3+
use axum_test_helpers::TestClient;
44
use bytes::Bytes;
55
use casbin::{DefaultModel, FileAdapter};
66
use futures::future::BoxFuture;
@@ -85,9 +85,9 @@ async fn test_middleware_domain() {
8585

8686
let client = TestClient::new(app);
8787

88-
let resp_pen = client.get("/pen/1").send().await;
88+
let resp_pen = client.get("/pen/1").await;
8989
assert_eq!(resp_pen.status(), StatusCode::OK);
9090

91-
let resp_book = client.get("/book/1").send().await;
91+
let resp_book = client.get("/book/1").await;
9292
assert_eq!(resp_book.status(), StatusCode::FORBIDDEN);
9393
}

tests/test_set_enforcer.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use axum::{response::Response, routing::get, BoxError, Router};
22
use axum_casbin::{CasbinAxumLayer, CasbinVals};
3-
use axum_test_helper::TestClient;
3+
use axum_test_helpers::TestClient;
44
use bytes::Bytes;
55
use casbin::function_map::key_match2;
66
use casbin::{CachedEnforcer, CoreApi, DefaultModel, FileAdapter};
@@ -103,12 +103,12 @@ async fn test_set_enforcer() {
103103

104104
let client = TestClient::new(app);
105105

106-
let resp_pen_1 = client.get("/pen/1").send().await;
106+
let resp_pen_1 = client.get("/pen/1").await;
107107
assert_eq!(resp_pen_1.status(), StatusCode::OK);
108108

109-
let resp_book = client.get("/book/2").send().await;
109+
let resp_book = client.get("/book/2").await;
110110
assert_eq!(resp_book.status(), StatusCode::OK);
111111

112-
let resp_pen_2 = client.get("/pen/2").send().await;
112+
let resp_pen_2 = client.get("/pen/2").await;
113113
assert_eq!(resp_pen_2.status(), StatusCode::FORBIDDEN);
114114
}

0 commit comments

Comments
 (0)