Skip to content

Commit d38cb90

Browse files
authored
Merge pull request #393 from ohkami-rs/v0.23.1
v0.23.1
2 parents 925df35 + 0ce5c62 commit d38cb90

File tree

29 files changed

+1149
-224
lines changed

29 files changed

+1149
-224
lines changed

Diff for: Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ exclude = [
1818
]
1919

2020
[workspace.package]
21-
version = "0.23.0"
21+
version = "0.23.1"
2222
edition = "2021"
2323
authors = ["kanarus <[email protected]>"]
2424
homepage = "https://crates.io/crates/ohkami"

Diff for: ohkami/Cargo.toml

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ features = ["rt_tokio", "nightly", "sse", "ws"]
1818

1919
[dependencies]
2020
# workspace members
21-
ohkami_lib = { version = "=0.23.0", path = "../ohkami_lib" }
22-
ohkami_macros = { version = "=0.23.0", path = "../ohkami_macros" }
23-
ohkami_openapi = { optional = true, version = "=0.23.0", path = "../ohkami_openapi" }
21+
ohkami_lib = { version = "=0.23.1", path = "../ohkami_lib" }
22+
ohkami_macros = { version = "=0.23.1", path = "../ohkami_macros" }
23+
ohkami_openapi = { optional = true, version = "=0.23.1", path = "../ohkami_openapi" }
2424

2525
# workspace dependencies
2626
byte_reader = { workspace = true }

Diff for: ohkami/src/fang/bound.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@ mod dispatch {
2626
pub trait SendOnNativeFuture<T>: Future<Output = T> + SendOnNative {}
2727
impl<T, F: Future<Output = T> + SendOnNative> SendOnNativeFuture<T> for F {}
2828

29-
pub trait FPCBound: FangProcCaller + SendSyncOnNative {}
29+
pub(crate) trait FPCBound: FangProcCaller + SendSyncOnNative {}
3030
impl<T: FangProcCaller + SendSyncOnNative> FPCBound for T {}

Diff for: ohkami/src/fang/builtin/basicauth.rs

+64-61
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::prelude::*;
2-
use ::base64::engine::{Engine as _, general_purpose::STANDARD as BASE64};
2+
use crate::fang::SendSyncOnNative;
33

44
#[cfg(feature="openapi")]
55
use crate::openapi;
@@ -46,15 +46,15 @@ use crate::openapi;
4646
#[derive(Clone)]
4747
pub struct BasicAuth<S>
4848
where
49-
S: AsRef<str> + Clone + Send + Sync + 'static
49+
S: AsRef<str> + Clone + SendSyncOnNative + 'static
5050
{
5151
pub username: S,
5252
pub password: S
5353
}
5454

5555
impl<S> BasicAuth<S>
5656
where
57-
S: AsRef<str> + Clone + Send + Sync + 'static
57+
S: AsRef<str> + Clone + SendSyncOnNative + 'static
5858
{
5959
#[inline]
6060
fn matches(&self,
@@ -75,20 +75,14 @@ const _: () = {
7575

7676
#[inline]
7777
fn basic_credential_of(req: &Request) -> Result<String, Response> {
78-
let credential_base64 = req.headers
79-
.Authorization().ok_or_else(unauthorized)?
80-
.strip_prefix("Basic ").ok_or_else(unauthorized)?;
81-
82-
let credential = String::from_utf8(
83-
BASE64.decode(credential_base64).map_err(|_| unauthorized())?
84-
).map_err(|_| unauthorized())?;
85-
86-
Ok(credential)
78+
(|| crate::util::base64_decode_utf8(
79+
req.headers.Authorization()?.strip_prefix("Basic ")?
80+
).ok())().ok_or_else(unauthorized)
8781
}
8882

8983
impl<S> FangAction for BasicAuth<S>
9084
where
91-
S: AsRef<str> + Clone + Send + Sync + 'static
85+
S: AsRef<str> + Clone + SendSyncOnNative + 'static
9286
{
9387
#[inline]
9488
async fn fore<'a>(&'a self, req: &'a mut Request) -> Result<(), Response> {
@@ -111,7 +105,7 @@ const _: () = {
111105

112106
impl<S, const N: usize> FangAction for [BasicAuth<S>; N]
113107
where
114-
S: AsRef<str> + Clone + Send + Sync + 'static
108+
S: AsRef<str> + Clone + SendSyncOnNative + 'static
115109
{
116110
#[inline]
117111
async fn fore<'a>(&'a self, req: &'a mut Request) -> Result<(), Response> {
@@ -137,51 +131,60 @@ const _: () = {
137131

138132

139133
#[cfg(test)]
140-
#[cfg(feature="__rt_native__")]
141-
#[test] fn test_basicauth() {
142-
use super::*;
143-
use crate::prelude::*;
144-
use crate::testing::*;
145-
146-
let t = Ohkami::new((
147-
"/hello".GET(|| async {"Hello!"}),
148-
"/private".By(Ohkami::new((
149-
BasicAuth {
150-
username: "ohkami",
151-
password: "password"
152-
},
153-
"/".GET(|| async {"Hello, private!"})
154-
)))
155-
)).test();
156-
157-
crate::__rt__::testing::block_on(async {
158-
{
159-
let req = TestRequest::GET("/hello");
160-
let res = t.oneshot(req).await;
161-
assert_eq!(res.status().code(), 200);
162-
assert_eq!(res.text(), Some("Hello!"));
163-
}
164-
{
165-
let req = TestRequest::GET("/private");
166-
let res = t.oneshot(req).await;
167-
assert_eq!(res.status().code(), 401);
168-
}
169-
{
170-
let req = TestRequest::GET("/private")
171-
.header("Authorization", format!(
172-
"Basic {}", BASE64.encode("ohkami:password")
173-
));
174-
let res = t.oneshot(req).await;
175-
assert_eq!(res.status().code(), 200);
176-
assert_eq!(res.text(), Some("Hello, private!"));
177-
}
178-
{
179-
let req = TestRequest::GET("/private")
180-
.header("Authorization", format!(
181-
"Basic {}", BASE64.encode("ohkami:wrong")
182-
));
183-
let res = t.oneshot(req).await;
184-
assert_eq!(res.status().code(), 401);
185-
}
186-
});
134+
mod test {
135+
#[test] fn test_basicauth_fang_bound() {
136+
use crate::fang::{Fang, BoxedFPC};
137+
fn assert_fang<T: Fang<BoxedFPC>>() {}
138+
139+
assert_fang::<super::BasicAuth<&'static str>>();
140+
assert_fang::<super::BasicAuth<String>>();
141+
}
142+
143+
#[cfg(feature="__rt_native__")]
144+
#[test] fn test_basicauth() {
145+
use super::*;
146+
use crate::testing::*;
147+
148+
let t = Ohkami::new((
149+
"/hello".GET(|| async {"Hello!"}),
150+
"/private".By(Ohkami::new((
151+
BasicAuth {
152+
username: "ohkami",
153+
password: "password"
154+
},
155+
"/".GET(|| async {"Hello, private!"})
156+
)))
157+
)).test();
158+
159+
crate::__rt__::testing::block_on(async {
160+
{
161+
let req = TestRequest::GET("/hello");
162+
let res = t.oneshot(req).await;
163+
assert_eq!(res.status().code(), 200);
164+
assert_eq!(res.text(), Some("Hello!"));
165+
}
166+
{
167+
let req = TestRequest::GET("/private");
168+
let res = t.oneshot(req).await;
169+
assert_eq!(res.status().code(), 401);
170+
}
171+
{
172+
let req = TestRequest::GET("/private")
173+
.header("Authorization", format!(
174+
"Basic {}", crate::util::base64_encode("ohkami:password")
175+
));
176+
let res = t.oneshot(req).await;
177+
assert_eq!(res.status().code(), 200);
178+
assert_eq!(res.text(), Some("Hello, private!"));
179+
}
180+
{
181+
let req = TestRequest::GET("/private")
182+
.header("Authorization", format!(
183+
"Basic {}", crate::util::base64_encode("ohkami:wrong")
184+
));
185+
let res = t.oneshot(req).await;
186+
assert_eq!(res.status().code(), 401);
187+
}
188+
});
189+
}
187190
}

Diff for: ohkami/src/fang/builtin/context.rs

+19-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use crate::{Request, Response, FromRequest, fang::FangAction};
1+
use crate::{Request, Response, FromRequest};
2+
use crate::fang::{FangAction, SendSyncOnNative};
23

34
/// # Request Context
45
///
@@ -28,19 +29,19 @@ use crate::{Request, Response, FromRequest, fang::FangAction};
2829
/// }
2930
/// ```
3031
#[derive(Clone)]
31-
pub struct Context<'req, T: Send + Sync + 'static>(pub &'req T);
32+
pub struct Context<'req, T: SendSyncOnNative + 'static>(pub &'req T);
3233

33-
impl<T: Send + Sync + 'static> Context<'static, T>
34+
impl<T: SendSyncOnNative + 'static> Context<'static, T>
3435
where
3536
T: Clone
3637
{
3738
pub fn new(data: T) -> impl FangAction {
3839
return ContextAction(data);
3940

4041
#[derive(Clone)]
41-
struct ContextAction<T: Clone + Send + Sync + 'static>(T);
42+
struct ContextAction<T: Clone + SendSyncOnNative + 'static>(T);
4243

43-
impl<T: Clone + Send + Sync + 'static> FangAction for ContextAction<T> {
44+
impl<T: Clone + SendSyncOnNative + 'static> FangAction for ContextAction<T> {
4445
#[inline]
4546
async fn fore<'a>(&'a self, req: &'a mut Request) -> Result<(), Response> {
4647
req.context.set(self.0.clone());
@@ -50,7 +51,7 @@ where
5051
}
5152
}
5253

53-
impl<'req, T: Send + Sync + 'static> FromRequest<'req> for Context<'req, T> {
54+
impl<'req, T: SendSyncOnNative + 'static> FromRequest<'req> for Context<'req, T> {
5455
type Error = std::convert::Infallible;
5556

5657
#[inline]
@@ -69,3 +70,15 @@ impl<'req, T: Send + Sync + 'static> FromRequest<'req> for Context<'req, T> {
6970
}
7071
}
7172
}
73+
74+
75+
#[cfg(test)]
76+
mod test {
77+
#[test]
78+
fn context_fang_bount() {
79+
use crate::fang::{Fang, BoxedFPC};
80+
fn assert_fang<T: Fang<BoxedFPC>>(_: T) {}
81+
82+
assert_fang(super::Context::new(String::new()));
83+
}
84+
}

Diff for: ohkami/src/fang/builtin/cors.rs

+16-4
Original file line numberDiff line numberDiff line change
@@ -172,14 +172,21 @@ impl<Inner: FangProc> FangProc for CORSProc<Inner> {
172172

173173

174174
#[cfg(debug_assertions)]
175-
#[cfg(all(feature="__rt_native__", feature="DEBUG"))]
176175
#[cfg(test)]
177176
mod test {
178-
use crate::prelude::*;
179-
use crate::testing::*;
180-
use super::CORS;
177+
#[test] fn cors_fang_bound() {
178+
use crate::fang::{Fang, BoxedFPC};
179+
fn assert_fang<T: Fang<BoxedFPC>>() {}
181180

181+
assert_fang::<super::CORS>();
182+
}
183+
184+
#[cfg(all(feature="__rt_native__", feature="DEBUG"))]
182185
#[test] fn options_request() {
186+
use crate::prelude::*;
187+
use crate::testing::*;
188+
use super::CORS;
189+
183190
crate::__rt__::testing::block_on(async {
184191
let t = Ohkami::new(
185192
"/hello".POST(|| async {"Hello!"})
@@ -221,7 +228,12 @@ mod test {
221228
});
222229
}
223230

231+
#[cfg(all(feature="__rt_native__", feature="DEBUG"))]
224232
#[test] fn cors_headers() {
233+
use crate::prelude::*;
234+
use crate::testing::*;
235+
use super::CORS;
236+
225237
crate::__rt__::testing::block_on(async {
226238
let t = Ohkami::new((CORS::new("https://example.example"),
227239
"/".GET(|| async {"Hello!"})

Diff for: ohkami/src/fang/builtin/enamel.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
///
66
/// ## What it does
77
///
8-
/// By default, adds to response headers :
8+
/// By default, sets to response headers :
99
///
1010
/// - `Cross-Origin-Embedder-Policy` to `require-corp`
1111
/// - `Cross-Origin-Resource-Policy` to `same-origin`
@@ -487,6 +487,14 @@ pub mod src {
487487
}
488488
}
489489

490+
#[cfg(test)]
491+
#[test]
492+
fn enamel_fang_bound() {
493+
use crate::fang::{Fang, BoxedFPC};
494+
fn assert_fang<T: Fang<BoxedFPC>>() {}
495+
496+
assert_fang::<Enamel>();
497+
}
490498

491499
#[cfg(test)]
492500
#[cfg(feature="__rt_native__")]

0 commit comments

Comments
 (0)