Skip to content

Commit

Permalink
Merge pull request #19217 from michalszynkiewicz/grpc-header-test
Browse files Browse the repository at this point in the history
Add a test for gRPC header passing, remove unused filter from rest client
  • Loading branch information
michalszynkiewicz authored Aug 18, 2021
2 parents 1742e20 + 70cc7ff commit a564660
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 102 deletions.

This file was deleted.

4 changes: 2 additions & 2 deletions integration-tests/grpc-plain-text-mutiny/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy</artifactId>
<artifactId>quarkus-resteasy-jackson</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
Expand Down Expand Up @@ -63,7 +63,7 @@
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-deployment</artifactId>
<artifactId>quarkus-resteasy-jackson-deployment</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>test</scope>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,97 @@
package io.quarkus.grpc.examples.hello;

import static io.quarkus.grpc.examples.hello.IncomingInterceptor.EXTRA_BLOCKING_HEADER;
import static io.quarkus.grpc.examples.hello.IncomingInterceptor.EXTRA_HEADER;
import static io.quarkus.grpc.examples.hello.IncomingInterceptor.INTERFACE_HEADER;

import java.util.Map;

import javax.inject.Inject;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import examples.Greeter;
import examples.GreeterClient;
import examples.GreeterGrpc;
import examples.HelloReply;
import examples.HelloRequest;
import examples.MutinyGreeterGrpc;
import io.grpc.Metadata;
import io.grpc.stub.MetadataUtils;
import io.quarkus.grpc.GrpcClient;
import io.smallrye.mutiny.Uni;

@Path("/hello")
public class HelloWorldEndpoint {

@GrpcClient("hello")
GreeterGrpc.GreeterBlockingStub blockingHelloService;
GreeterGrpc.GreeterBlockingStub blockingHelloClient;

@GrpcClient("hello")
MutinyGreeterGrpc.MutinyGreeterStub mutinyHelloClient;

@GrpcClient("hello")
MutinyGreeterGrpc.MutinyGreeterStub mutinyHelloService;
Greeter interfaceHelloClient;

@Inject
IncomingInterceptor interceptor;

@GET
@Path("/blocking/{name}")
public String helloBlocking(@PathParam("name") String name) {
HelloReply reply = blockingHelloService.sayHello(HelloRequest.newBuilder().setName(name).build());
public String helloBlocking(@PathParam("name") String name, @QueryParam("headers") boolean headers) {
Metadata extraHeaders = new Metadata();
if (headers) {
extraHeaders.put(EXTRA_BLOCKING_HEADER, "my-blocking-value");
}
HelloReply reply = MetadataUtils.attachHeaders(blockingHelloClient, extraHeaders)
.sayHello(HelloRequest.newBuilder().setName(name).build());
return generateResponse(reply);

}

@GET
@Path("/mutiny/{name}")
public Uni<String> helloMutiny(@PathParam("name") String name) {
return mutinyHelloService.sayHello(HelloRequest.newBuilder().setName(name).build())
.onItem().transform((reply) -> generateResponse(reply));
public Uni<String> helloMutiny(@PathParam("name") String name, @QueryParam("headers") boolean headers) {
Metadata extraHeaders = new Metadata();
if (headers) {
extraHeaders.put(EXTRA_HEADER, "my-extra-value");
}
MutinyGreeterGrpc.MutinyGreeterStub alteredClient = MetadataUtils.attachHeaders(mutinyHelloClient, extraHeaders);
return alteredClient.sayHello(HelloRequest.newBuilder().setName(name).build())
.onItem().transform(this::generateResponse);
}

@GET
@Path("/interface/{name}")
public Uni<String> helloInterface(@PathParam("name") String name, @QueryParam("headers") boolean headers) {
Metadata extraHeaders = new Metadata();
if (headers) {
extraHeaders.put(INTERFACE_HEADER, "my-interface-value");
}

MutinyGreeterGrpc.MutinyGreeterStub stub = ((GreeterClient) interfaceHelloClient).getStub();
MutinyGreeterGrpc.MutinyGreeterStub alteredClient = MetadataUtils.attachHeaders(stub, extraHeaders);

return alteredClient.sayHello(HelloRequest.newBuilder().setName(name).build())
.onItem().transform(this::generateResponse);

}

@DELETE
public void clear() {
interceptor.clear();
}

@GET
@Path("/headers")
@Produces(MediaType.APPLICATION_JSON)
public Map<String, String> getCollectedHeaders() {
return interceptor.getCollectedHeaders();
}

public String generateResponse(HelloReply reply) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package io.quarkus.grpc.examples.hello;

import static java.util.Arrays.asList;

import java.util.HashMap;
import java.util.Map;

import javax.enterprise.context.ApplicationScoped;

import io.grpc.Metadata;
import io.grpc.ServerCall;
import io.grpc.ServerCallHandler;
import io.grpc.ServerInterceptor;

@ApplicationScoped
public class IncomingInterceptor implements ServerInterceptor {

public static final Metadata.Key<String> EXTRA_HEADER = Metadata.Key.of("my-extra-header",
Metadata.ASCII_STRING_MARSHALLER);
public static final Metadata.Key<String> INTERFACE_HEADER = Metadata.Key.of("my-interface-header",
Metadata.ASCII_STRING_MARSHALLER);
public static final Metadata.Key<String> EXTRA_BLOCKING_HEADER = Metadata.Key.of("my-blocking-header",
Metadata.ASCII_STRING_MARSHALLER);

private final Map<String, String> headerValues = new HashMap<>();

@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> serverCall, Metadata metadata,
ServerCallHandler<ReqT, RespT> serverCallHandler) {

for (Metadata.Key<String> key : asList(EXTRA_HEADER, INTERFACE_HEADER, EXTRA_BLOCKING_HEADER)) {
String header = metadata.get(key);
if (header != null) {
headerValues.put(key.name(), header);
}
}

return serverCallHandler.startCall(serverCall, metadata);
}

public void clear() {
headerValues.clear();
}

public Map<String, String> getCollectedHeaders() {
return headerValues;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package io.quarkus.grpc.examples.hello;

import static io.restassured.RestAssured.delete;
import static io.restassured.RestAssured.get;
import static io.restassured.RestAssured.given;
import static org.assertj.core.api.Assertions.assertThat;

import java.util.Map;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import io.quarkus.test.junit.QuarkusTest;
Expand All @@ -14,12 +19,55 @@ class HelloWorldEndpointTest {
public void testHelloWorldServiceUsingBlockingStub() {
String response = get("/hello/blocking/neo").asString();
assertThat(response).startsWith("Hello neo");

assertNoHeaders();
}

@Test
public void testHelloWorldServiceUsingMutinyStub() {
String response = get("/hello/mutiny/neo-mutiny").asString();
assertThat(response).startsWith("Hello neo-mutiny");
assertNoHeaders();
}

@Test
void shouldSetHeaderWithMutiny() {
String response = given().queryParam("headers", "true")
.when().get("/hello/mutiny/neo-mutiny-w-headers").asString();
assertThat(response).startsWith("Hello neo-mutiny-w-headers");
assertHasHeader("my-extra-header", "my-extra-value");
}

@Test
void shouldSetHeader() {
String response = given().queryParam("headers", "true")
.when().get("/hello/blocking/neo-w-headers").asString();
assertThat(response).startsWith("Hello neo-w-headers");
assertHasHeader("my-blocking-header", "my-blocking-value");
}

@Test
void shouldSetHeaderWithInterface() {
String response = given().queryParam("headers", "true")
.when().get("/hello/interface/i-neo-w-headers").asString();
assertThat(response).startsWith("Hello i-neo-w-headers");
assertHasHeader("my-interface-header", "my-interface-value");
}

@BeforeEach
public void setUp() {
delete("/hello").then().statusCode(204);
}

private void assertHasHeader(String key, String value) {
Map<?, ?> result = get("/hello/headers").as(Map.class);
assertThat(result).hasSize(1);
assertThat(result.get(key)).isEqualTo(value);
}

private void assertNoHeaders() {
Map<?, ?> result = get("/hello/headers").as(Map.class);
assertThat(result).hasSize(0);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ public void testHelloWorldServiceUsingBlockingStub() {
@Test
public void testHelloWorldServiceUsingMutinyStub() {
HelloReply reply = MutinyGreeterGrpc.newMutinyStub(channel)
.sayHello(HelloRequest.newBuilder().setName("neo-blocking").build())
.sayHello(HelloRequest.newBuilder().setName("neo-mutiny").build())
.await().atMost(Duration.ofSeconds(5));
assertThat(reply.getMessage()).isEqualTo("Hello neo-blocking");
assertThat(reply.getMessage()).isEqualTo("Hello neo-mutiny");
}

}

0 comments on commit a564660

Please sign in to comment.