keyforkd: add tests for middleware, relax serde service trait bounds

This commit is contained in:
Ryan Heywood 2023-09-07 15:17:29 -05:00
parent 0d6753ef47
commit 705b0ad826
Signed by: ryan
GPG Key ID: 8E401478A3FBEF72
1 changed files with 46 additions and 4 deletions

View File

@ -50,10 +50,10 @@ pub enum SerdeServiceError {
impl<S, Request> Service<Vec<u8>> for SerdeService<S, Request> impl<S, Request> Service<Vec<u8>> for SerdeService<S, Request>
where where
S: Service<Request> + Send + Sync, S: Service<Request>,
Request: DeserializeOwned + Send, Request: DeserializeOwned,
<S as Service<Request>>::Error: std::error::Error + Send, <S as Service<Request>>::Response: Serialize,
<S as Service<Request>>::Response: Serialize + Send, <S as Service<Request>>::Error: std::error::Error,
<S as Service<Request>>::Future: Send + 'static, <S as Service<Request>>::Future: Send + 'static,
{ {
type Response = Vec<u8>; type Response = Vec<u8>;
@ -89,3 +89,45 @@ where
}) })
} }
} }
#[cfg(test)]
mod tests {
use super::*;
use serde::{Deserialize, Serialize};
use std::{future::Future, pin::Pin, task::Poll};
use tower::{ServiceBuilder, ServiceExt};
#[derive(Serialize, Deserialize)]
struct Test {
field: String,
}
struct App;
#[derive(Debug, thiserror::Error)]
enum Infallible {}
impl Service<Test> for App {
type Response = Test;
type Error = Infallible;
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>;
fn poll_ready(&mut self, _cx: &mut std::task::Context<'_>) -> std::task::Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn call(&mut self, req: Test) -> Self::Future {
Box::pin(async { Ok(req) })
}
}
#[tokio::test]
async fn can_serde_responses() {
let content = serialize(&Test { field: "hello world!".to_string() }).unwrap();
let mut service = ServiceBuilder::new()
.layer(SerdeLayer::<Test>::new())
.service(App);
let result = service.ready().await.unwrap().call(content.clone()).await.unwrap();
assert_eq!(result, content);
}
}