Skip to content

Commit

Permalink
chore: make Status::into_http() work for any defaultable response `…
Browse files Browse the repository at this point in the history
…Body` (#1978)
  • Loading branch information
shikhar authored Oct 8, 2024
1 parent c5f2cc1 commit c3c18a4
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 10 deletions.
9 changes: 2 additions & 7 deletions tonic/src/service/interceptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,7 @@ where
.poll(cx)
.map(|result| result.map(|res| res.map(boxed))),
KindProj::Status(status) => {
let response = status
.take()
.unwrap()
.into_http()
.map(|_| B::default())
.map(boxed);
let response = status.take().unwrap().into_http();
Poll::Ready(Ok(response))
}
}
Expand Down Expand Up @@ -287,7 +282,7 @@ mod tests {
#[tokio::test]
async fn handles_intercepted_status_as_response() {
let message = "Blocked by the interceptor";
let expected = Status::permission_denied(message).into_http();
let expected = Status::permission_denied(message).into_http::<BoxBody>();

let svc = tower::service_fn(|_: http::Request<TestBody>| async {
Ok::<_, Status>(http::Response::new(TestBody))
Expand Down
6 changes: 3 additions & 3 deletions tonic/src/status.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::metadata::MetadataMap;
use crate::{body::BoxBody, metadata::GRPC_CONTENT_TYPE};
use crate::metadata::GRPC_CONTENT_TYPE;
use base64::Engine as _;
use bytes::Bytes;
use http::{
Expand Down Expand Up @@ -579,8 +579,8 @@ impl Status {
}

/// Build an `http::Response` from the given `Status`.
pub fn into_http(self) -> http::Response<BoxBody> {
let mut response = http::Response::new(BoxBody::default());
pub fn into_http<B: Default>(self) -> http::Response<B> {
let mut response = http::Response::new(B::default());
response
.headers_mut()
.insert(http::header::CONTENT_TYPE, GRPC_CONTENT_TYPE);
Expand Down

0 comments on commit c3c18a4

Please sign in to comment.