Skip to content

Commit

Permalink
add test for custom headers
Browse files Browse the repository at this point in the history
Signed-off-by: Sahil Yeole <[email protected]>
  • Loading branch information
beelchester committed Sep 8, 2024
1 parent e3f2d66 commit dc16ec7
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 10 deletions.
2 changes: 1 addition & 1 deletion examples/grpc-reflection.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
schema
@server(port: 8000)
@upstream(baseURL: "http://localhost:50051", httpCache: 42, batch: {delay: 10})
@link(src: "http://localhost:50051", type: Grpc, headers: [{key: "authorization", value: "secret"}]) {
@link(src: "http://localhost:50051", type: Grpc, headers: [{key: "authorization", value: "Bearer 123"}]) {
query: Query
}

Expand Down
38 changes: 38 additions & 0 deletions src/core/proto_reader/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,4 +382,42 @@ mod grpc_fetch {

Ok(())
}

#[tokio::test]
async fn test_custom_headers_resp_list_all() -> Result<()> {
let server = start_mock_server();

let http_reflection_service_not_found = server.mock(|when, then| {
when.method(httpmock::Method::POST)
.path("/grpc.reflection.v1alpha.ServerReflection/ServerReflectionInfo")
.header("authorization", "Bearer 123");
then.status(200).body(get_fake_resp());
});

let runtime = crate::core::runtime::test::init(None);

let grpc_reflection = GrpcReflection::new(
format!("http://localhost:{}", server.port()),
Some(vec![KeyValue {
key: "authorization".to_string(),
value: "Bearer 123".to_string(),
}]),
runtime,
);

let resp = grpc_reflection.list_all_files().await?;

assert_eq!(
[
"news.NewsService".to_string(),
"grpc.reflection.v1alpha.ServerReflection".to_string()
]
.to_vec(),
resp
);

http_reflection_service_not_found.assert();

Ok(())
}
}
13 changes: 4 additions & 9 deletions tailcall-upstream-grpc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,16 +216,11 @@ fn init_tracer() -> Result<(), Error> {
Ok(())
}

/// Intercepts the request and checks if the token is valid.
fn intercept(req: Request<()>) -> Result<Request<()>, Status> {
println!("Intercepting request: {:?}", req);
if let Some(token) = req.metadata().get("authorization") {
if token != "secret" {
Err(Status::permission_denied("Unauthorized"))
} else {
Ok(req)
}
} else {
Err(Status::permission_denied("Unauthorized"))
match req.metadata().get("authorization") {
Some(token) if token == "Bearer 123" => Ok(req),
_ => Err(Status::permission_denied("Unauthorized")),

Check warning on line 223 in tailcall-upstream-grpc/src/main.rs

View check run for this annotation

Codecov / codecov/patch

tailcall-upstream-grpc/src/main.rs#L220-L223

Added lines #L220 - L223 were not covered by tests
}
}

Check warning on line 225 in tailcall-upstream-grpc/src/main.rs

View check run for this annotation

Codecov / codecov/patch

tailcall-upstream-grpc/src/main.rs#L225

Added line #L225 was not covered by tests

Expand Down

0 comments on commit dc16ec7

Please sign in to comment.