From db3b0c139a93787f2db79c8dbfe67c8698d7f5bf Mon Sep 17 00:00:00 2001 From: tmulle Date: Thu, 10 Oct 2024 14:46:07 -0400 Subject: [PATCH 1/2] 43811 - Updating docs to use MetaData.Key --- .../asciidoc/grpc-service-implementation.adoc | 30 +++++++++++++++---- 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/docs/src/main/asciidoc/grpc-service-implementation.adoc b/docs/src/main/asciidoc/grpc-service-implementation.adoc index ab736562e20ba..a64b62c201776 100644 --- a/docs/src/main/asciidoc/grpc-service-implementation.adoc +++ b/docs/src/main/asciidoc/grpc-service-implementation.adoc @@ -521,29 +521,47 @@ quarkus.http.auth.basic=true <1> ---- package org.acme.grpc.auth; -import static org.assertj.core.api.Assertions.assertThat; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; +import com.rajant.connector.proto.Greeter; +import com.rajant.connector.proto.HelloRequest; import io.grpc.Metadata; import io.quarkus.grpc.GrpcClient; import io.quarkus.grpc.GrpcClientUtils; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +import io.quarkus.test.junit.QuarkusTest; import org.junit.jupiter.api.Test; -public class HelloServiceTest implements Greeter { +@QuarkusTest +public class HelloServiceTest { @GrpcClient Greeter greeterClient; @Test - void shouldReturnHello() { + void shouldReturnHello() throws ExecutionException, InterruptedException, TimeoutException { Metadata headers = new Metadata(); - headers.put("Authorization", "Basic am9objpqb2hu"); + + // Set the headers - Basic auth for testing + Metadata.Key key = Metadata.Key.of("Authorization", Metadata.ASCII_STRING_MARSHALLER); + headers.put(key, "Basic YWxpY2U6YWxpY2U="); // alice:alice with "admin" role var client = GrpcClientUtils.attachHeaders(greeterClient, headers); + // Call the client CompletableFuture message = new CompletableFuture<>(); client.sayHello(HelloRequest.newBuilder().setName("Quarkus").build()) .subscribe().with(reply -> message.complete(reply.getMessage())); - assertThat(message.get(5, TimeUnit.SECONDS)).isEqualTo("Hello Quarkus"); + + // Get the values + String theValue = message.get(5, TimeUnit.SECONDS); + + // Assert + assertThat(theValue, is("Hello Quarkus")); } } ---- @@ -596,7 +614,7 @@ import io.quarkus.security.identity.request.UsernamePasswordAuthenticationReques @Singleton public class CustomGrpcSecurityMechanism implements GrpcSecurityMechanism { - private static final String AUTHORIZATION = "Authorization"; + private static final MetaData.Key AUTHORIZATION = Metadata.Key.of("Authorization", Metadata.ASCII_STRING_MARSHALLER); @Override public boolean handles(Metadata metadata) { From f0acc0d591a56239d645cf3ed700ddf28354bc21 Mon Sep 17 00:00:00 2001 From: tmulle Date: Fri, 11 Oct 2024 08:10:47 -0400 Subject: [PATCH 2/2] Fixing package name --- docs/src/main/asciidoc/grpc-service-implementation.adoc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/src/main/asciidoc/grpc-service-implementation.adoc b/docs/src/main/asciidoc/grpc-service-implementation.adoc index a64b62c201776..6fde2ec96908a 100644 --- a/docs/src/main/asciidoc/grpc-service-implementation.adoc +++ b/docs/src/main/asciidoc/grpc-service-implementation.adoc @@ -524,8 +524,8 @@ package org.acme.grpc.auth; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; -import com.rajant.connector.proto.Greeter; -import com.rajant.connector.proto.HelloRequest; +import org.acme.proto.Greeter; +import org.acme.proto.HelloRequest; import io.grpc.Metadata; import io.quarkus.grpc.GrpcClient; import io.quarkus.grpc.GrpcClientUtils;