Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating GRPC Server Auth test docs to use MetaData.Key #43813

Closed
wants to merge 4 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions docs/src/main/asciidoc/grpc-service-implementation.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use a more neutral package name? Like org.acme?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops yes sorry that should be org.acme
I’ll fix it in a few mins

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

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<String> 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<String> 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"));
}
}
----
Expand Down Expand Up @@ -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<String> AUTHORIZATION = Metadata.Key.of("Authorization", Metadata.ASCII_STRING_MARSHALLER);

@Override
public boolean handles(Metadata metadata) {
Expand Down