fix: base64 encode details header (#345)

This commit is contained in:
Lucio Franco
2020-05-07 15:31:26 -04:00
committed by GitHub
parent 68d2ab3339
commit e683ffef1f
7 changed files with 100 additions and 3 deletions
+2 -1
View File
@@ -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
View File
@@ -0,0 +1,21 @@
[package]
name = "integration-tests"
version = "0.1.0"
authors = ["Lucio Franco <[email protected]>"]
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
View File
@@ -0,0 +1,3 @@
fn main() {
tonic_build::compile_protos("proto/test.proto").unwrap();
}
+10
View File
@@ -0,0 +1,10 @@
syntax = "proto3";
package test;
service Test {
rpc UnaryCall(Input) returns (Output);
}
message Input {}
message Output {}
+3
View File
@@ -0,0 +1,3 @@
pub mod pb {
tonic::include_proto!("test");
}
+52
View File
@@ -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();
}
+9 -2
View File
@@ -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 {
@@ -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(),
}
}
}