Skip to content

Commit

Permalink
fix: base64 encode details header (#345)
Browse files Browse the repository at this point in the history
  • Loading branch information
LucioFranco committed May 7, 2020
1 parent 68d2ab3 commit e683ffe
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 3 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ members = [
"tests/wellknown",
"tests/extern_path/uuid",
"tests/ambiguous_methods",
"tests/extern_path/my_application"
"tests/extern_path/my_application",
"tests/integration_tests"
]
21 changes: 21 additions & 0 deletions tests/integration_tests/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "integration-tests"
version = "0.1.0"
authors = ["Lucio Franco <luciofranco14@gmail.com>"]
edition = "2018"
publish = false
license = "MIT"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
tonic = { path = "../../tonic" }
prost = "0.6"
futures-util = "0.3"
bytes = "0.5"

[dev-dependencies]
tokio = { version = "0.2", features = ["macros", "rt-core", "tcp"] }

[build-dependencies]
tonic-build = { path = "../../tonic-build" }
3 changes: 3 additions & 0 deletions tests/integration_tests/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
tonic_build::compile_protos("proto/test.proto").unwrap();
}
10 changes: 10 additions & 0 deletions tests/integration_tests/proto/test.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
syntax = "proto3";

package test;

service Test {
rpc UnaryCall(Input) returns (Output);
}

message Input {}
message Output {}
3 changes: 3 additions & 0 deletions tests/integration_tests/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod pb {
tonic::include_proto!("test");
}
52 changes: 52 additions & 0 deletions tests/integration_tests/tests/status.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use bytes::Bytes;
use futures_util::FutureExt;
use integration_tests::pb::{test_client, test_server, Input, Output};
use std::time::Duration;
use tokio::sync::oneshot;
use tonic::{transport::Server, Code, Request, Response, Status};

#[tokio::test]
async fn status_with_details() {
struct Svc;

#[tonic::async_trait]
impl test_server::Test for Svc {
async fn unary_call(&self, _: Request<Input>) -> Result<Response<Output>, Status> {
Err(Status::with_details(
Code::ResourceExhausted,
"Too many requests",
Bytes::from_static(&[1]),
))
}
}

let svc = test_server::TestServer::new(Svc);

let (tx, rx) = oneshot::channel::<()>();

let jh = tokio::spawn(async move {
Server::builder()
.add_service(svc)
.serve_with_shutdown("127.0.0.1:1337".parse().unwrap(), rx.map(drop))
.await
.unwrap();
});

tokio::time::delay_for(Duration::from_millis(100)).await;

let mut channel = test_client::TestClient::connect("http://127.0.0.1:1337")
.await
.unwrap();

let err = channel
.unary_call(Request::new(Input {}))
.await
.unwrap_err();

assert_eq!(err.message(), "Too many requests");
assert_eq!(err.details(), &[1]);

tx.send(()).unwrap();

jh.await.unwrap();
}
11 changes: 9 additions & 2 deletions tonic/src/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,9 +334,14 @@ impl Status {
.map(|cow| cow.to_string())
})
.unwrap_or_else(|| Ok(String::new()));

let details = header_map
.get(GRPC_STATUS_DETAILS_HEADER)
.map(|h| Bytes::copy_from_slice(h.as_bytes()))
.map(|h| {
base64::decode(h.as_bytes())
.expect("Invalid status header, expected base64 encoded value")
})
.map(Bytes::from)
.unwrap_or_else(Bytes::new);
match error_message {
Ok(message) => Status {
Expand Down Expand Up @@ -404,10 +409,12 @@ impl Status {

/// Create a new `Status` with the associated code, message, and binary details field.
pub fn with_details(code: Code, message: impl Into<String>, details: Bytes) -> Status {
let details = base64::encode_config(&details[..], base64::STANDARD_NO_PAD);

Status {
code,
message: message.into(),
details: details,
details: details.into(),
}
}
}
Expand Down

0 comments on commit e683ffe

Please sign in to comment.