@@ -57,8 +57,8 @@ pub fn generate<T: Service>(
|
|||||||
impl<T> #service_ident<T>
|
impl<T> #service_ident<T>
|
||||||
where
|
where
|
||||||
T: tonic::client::GrpcService<tonic::body::BoxBody>,
|
T: tonic::client::GrpcService<tonic::body::BoxBody>,
|
||||||
T::ResponseBody: Body + Send + 'static,
|
|
||||||
T::Error: Into<StdError>,
|
T::Error: Into<StdError>,
|
||||||
|
T::ResponseBody: Default + Body<Data = Bytes> + Send + 'static,
|
||||||
<T::ResponseBody as Body>::Error: Into<StdError> + Send,
|
<T::ResponseBody as Body>::Error: Into<StdError> + Send,
|
||||||
{
|
{
|
||||||
pub fn new(inner: T) -> Self {
|
pub fn new(inner: T) -> Self {
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ pub type StdError = Box<dyn std::error::Error + Send + Sync + 'static>;
|
|||||||
#[cfg(feature = "compression")]
|
#[cfg(feature = "compression")]
|
||||||
pub use crate::codec::{CompressionEncoding, EnabledCompressionEncodings};
|
pub use crate::codec::{CompressionEncoding, EnabledCompressionEncodings};
|
||||||
pub use crate::service::interceptor::InterceptedService;
|
pub use crate::service::interceptor::InterceptedService;
|
||||||
|
pub use bytes::Bytes;
|
||||||
pub use http_body::Body;
|
pub use http_body::Body;
|
||||||
|
|
||||||
pub type BoxFuture<T, E> = self::Pin<Box<dyn self::Future<Output = Result<T, E>> + Send + 'static>>;
|
pub type BoxFuture<T, E> = self::Pin<Box<dyn self::Future<Output = Result<T, E>> + Send + 'static>>;
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
//! See [`Interceptor`] for more details.
|
//! See [`Interceptor`] for more details.
|
||||||
|
|
||||||
use crate::{request::SanitizeHeaders, Status};
|
use crate::{request::SanitizeHeaders, Status};
|
||||||
|
use bytes::Bytes;
|
||||||
use pin_project::pin_project;
|
use pin_project::pin_project;
|
||||||
use std::{
|
use std::{
|
||||||
fmt,
|
fmt,
|
||||||
@@ -140,9 +141,11 @@ where
|
|||||||
|
|
||||||
impl<S, F, ReqBody, ResBody> Service<http::Request<ReqBody>> for InterceptedService<S, F>
|
impl<S, F, ReqBody, ResBody> Service<http::Request<ReqBody>> for InterceptedService<S, F>
|
||||||
where
|
where
|
||||||
|
ResBody: Default + http_body::Body<Data = Bytes> + Send + 'static,
|
||||||
F: Interceptor,
|
F: Interceptor,
|
||||||
S: Service<http::Request<ReqBody>, Response = http::Response<ResBody>>,
|
S: Service<http::Request<ReqBody>, Response = http::Response<ResBody>>,
|
||||||
S::Error: Into<crate::Error>,
|
S::Error: Into<crate::Error>,
|
||||||
|
ResBody::Error: Into<crate::Error>,
|
||||||
{
|
{
|
||||||
type Response = http::Response<ResBody>;
|
type Response = http::Response<ResBody>;
|
||||||
type Error = crate::Error;
|
type Error = crate::Error;
|
||||||
@@ -215,6 +218,8 @@ impl<F, E, B> Future for ResponseFuture<F>
|
|||||||
where
|
where
|
||||||
F: Future<Output = Result<http::Response<B>, E>>,
|
F: Future<Output = Result<http::Response<B>, E>>,
|
||||||
E: Into<crate::Error>,
|
E: Into<crate::Error>,
|
||||||
|
B: Default + http_body::Body<Data = Bytes> + Send + 'static,
|
||||||
|
B::Error: Into<crate::Error>,
|
||||||
{
|
{
|
||||||
type Output = Result<http::Response<B>, crate::Error>;
|
type Output = Result<http::Response<B>, crate::Error>;
|
||||||
|
|
||||||
@@ -222,8 +227,9 @@ where
|
|||||||
match self.project().kind.project() {
|
match self.project().kind.project() {
|
||||||
KindProj::Future(future) => future.poll(cx).map_err(Into::into),
|
KindProj::Future(future) => future.poll(cx).map_err(Into::into),
|
||||||
KindProj::Error(status) => {
|
KindProj::Error(status) => {
|
||||||
let error = status.take().unwrap().into();
|
let response = status.take().unwrap().to_http().map(|_| B::default());
|
||||||
Poll::Ready(Err(error))
|
|
||||||
|
Poll::Ready(Ok(response))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -233,11 +239,38 @@ where
|
|||||||
mod tests {
|
mod tests {
|
||||||
#[allow(unused_imports)]
|
#[allow(unused_imports)]
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use http::header::HeaderMap;
|
||||||
|
use std::{
|
||||||
|
pin::Pin,
|
||||||
|
task::{Context, Poll},
|
||||||
|
};
|
||||||
use tower::ServiceExt;
|
use tower::ServiceExt;
|
||||||
|
|
||||||
|
#[derive(Debug, Default)]
|
||||||
|
struct TestBody;
|
||||||
|
|
||||||
|
impl http_body::Body for TestBody {
|
||||||
|
type Data = Bytes;
|
||||||
|
type Error = Status;
|
||||||
|
|
||||||
|
fn poll_data(
|
||||||
|
self: Pin<&mut Self>,
|
||||||
|
_cx: &mut Context<'_>,
|
||||||
|
) -> Poll<Option<Result<Self::Data, Self::Error>>> {
|
||||||
|
Poll::Ready(None)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn poll_trailers(
|
||||||
|
self: Pin<&mut Self>,
|
||||||
|
_cx: &mut Context<'_>,
|
||||||
|
) -> Poll<Result<Option<HeaderMap>, Self::Error>> {
|
||||||
|
Poll::Ready(Ok(None))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn doesnt_remove_headers() {
|
async fn doesnt_remove_headers_from_requests() {
|
||||||
let svc = tower::service_fn(|request: http::Request<hyper::Body>| async move {
|
let svc = tower::service_fn(|request: http::Request<TestBody>| async move {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
request
|
request
|
||||||
.headers()
|
.headers()
|
||||||
@@ -246,7 +279,7 @@ mod tests {
|
|||||||
"test-tonic"
|
"test-tonic"
|
||||||
);
|
);
|
||||||
|
|
||||||
Ok::<_, hyper::Error>(hyper::Response::new(hyper::Body::empty()))
|
Ok::<_, Status>(http::Response::new(TestBody))
|
||||||
});
|
});
|
||||||
|
|
||||||
let svc = InterceptedService::new(svc, |request: crate::Request<()>| {
|
let svc = InterceptedService::new(svc, |request: crate::Request<()>| {
|
||||||
@@ -257,14 +290,36 @@ mod tests {
|
|||||||
.expect("missing in interceptor"),
|
.expect("missing in interceptor"),
|
||||||
"test-tonic"
|
"test-tonic"
|
||||||
);
|
);
|
||||||
|
|
||||||
Ok(request)
|
Ok(request)
|
||||||
});
|
});
|
||||||
|
|
||||||
let request = http::Request::builder()
|
let request = http::Request::builder()
|
||||||
.header("user-agent", "test-tonic")
|
.header("user-agent", "test-tonic")
|
||||||
.body(hyper::Body::empty())
|
.body(TestBody)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
svc.oneshot(request).await.unwrap();
|
svc.oneshot(request).await.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn handles_intercepted_status_as_response() {
|
||||||
|
let message = "Blocked by the interceptor";
|
||||||
|
let expected = Status::permission_denied(message).to_http();
|
||||||
|
|
||||||
|
let svc = tower::service_fn(|_: http::Request<TestBody>| async {
|
||||||
|
Ok::<_, Status>(http::Response::new(TestBody))
|
||||||
|
});
|
||||||
|
|
||||||
|
let svc = InterceptedService::new(svc, |_: crate::Request<()>| {
|
||||||
|
Err(Status::permission_denied(message))
|
||||||
|
});
|
||||||
|
|
||||||
|
let request = http::Request::builder().body(TestBody).unwrap();
|
||||||
|
let response = svc.oneshot(request).await.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(expected.status(), response.status());
|
||||||
|
assert_eq!(expected.version(), response.version());
|
||||||
|
assert_eq!(expected.headers(), response.headers());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user