-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from kana-rus/add_testing
Add: layer6_testing::Testing
- Loading branch information
Showing
7 changed files
with
113 additions
and
35 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use std::{pin::Pin, future::Future}; | ||
use crate::{Response, Request}; | ||
|
||
#[cfg(test)] use crate::{Ohkami, Context}; | ||
|
||
pub trait Testing { | ||
fn oneshot<'test>(&self, req: &'test mut Request) -> TestResponse<'test>; | ||
} | ||
|
||
pub struct TestResponse<'test>( | ||
Box<dyn Future<Output = Response> + 'test> | ||
); impl<'test> Future for TestResponse<'test> { | ||
type Output = Response; | ||
fn poll(self: Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> std::task::Poll<Self::Output> { | ||
unsafe {self.map_unchecked_mut(|tr| tr.0.as_mut())} | ||
.poll(cx) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
impl Testing for Ohkami { | ||
fn oneshot<'test>(&self, req: &'test mut Request) -> TestResponse<'test> { | ||
let router = { | ||
let mut router = self.routes.clone(); | ||
for (methods, fang) in &self.fangs { | ||
router = router.apply_fang(methods, fang.clone()) | ||
} | ||
router.into_radix() | ||
}; | ||
|
||
let res = async move {router.handle(Context::new(), req).await}; | ||
TestResponse(Box::new(res)) | ||
} | ||
} |
Oops, something went wrong.