forked from line/armeria
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for alternative protobuf content types (line#4364)
Motivation: Armeria `UnframedGrpcService` doesn't support alternative protobuf content types Modifications: - add `application/x-protobuf` and `application/x-google-protobuf` to MediaType - add isProtobuf() function - identify Protobuf contentType in UnframedGrpcService using isProtobuf() - respond with request-given protobuf content type for deframed response Result: - Closes line#4355 - Armeria `UnframedGrpcService` now supports `application/x-protobuf` and `application/x-google-protobuf` media types.
- Loading branch information
1 parent
5e95716
commit aab6852
Showing
7 changed files
with
192 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
.../test/java/com/linecorp/armeria/server/grpc/UnframedGrpcServiceResponseMediaTypeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* Copyright 2022 LINE Corporation | ||
* | ||
* LINE Corporation licenses this file to you under the Apache License, | ||
* version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package com.linecorp.armeria.server.grpc; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.util.stream.Stream; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.ArgumentsProvider; | ||
import org.junit.jupiter.params.provider.ArgumentsSource; | ||
|
||
import com.linecorp.armeria.common.AggregatedHttpResponse; | ||
import com.linecorp.armeria.common.HttpMethod; | ||
import com.linecorp.armeria.common.HttpRequest; | ||
import com.linecorp.armeria.common.HttpStatus; | ||
import com.linecorp.armeria.common.MediaType; | ||
import com.linecorp.armeria.grpc.testing.TestServiceGrpc; | ||
import com.linecorp.armeria.protobuf.EmptyProtos; | ||
import com.linecorp.armeria.server.ServiceRequestContext; | ||
import com.linecorp.armeria.testing.junit5.common.EventLoopExtension; | ||
|
||
import io.grpc.BindableService; | ||
import io.grpc.stub.StreamObserver; | ||
|
||
public class UnframedGrpcServiceResponseMediaTypeTest { | ||
|
||
@RegisterExtension | ||
static EventLoopExtension eventLoop = new EventLoopExtension(); | ||
|
||
private static class TestService extends TestServiceGrpc.TestServiceImplBase { | ||
|
||
@Override | ||
public void emptyCall(EmptyProtos.Empty request, StreamObserver<EmptyProtos.Empty> responseObserver) { | ||
responseObserver.onNext(EmptyProtos.Empty.newBuilder().build()); | ||
responseObserver.onCompleted(); | ||
} | ||
} | ||
|
||
private static final TestService testService = new TestService(); | ||
private static final int MAX_MESSAGE_BYTES = 1024; | ||
|
||
@Test | ||
void respondWithCorrespondingJsonMediaType() throws Exception { | ||
final UnframedGrpcService unframedGrpcService = buildUnframedGrpcService(testService); | ||
|
||
final HttpRequest request = HttpRequest.of(HttpMethod.POST, | ||
"/armeria.grpc.testing.TestService/EmptyCall", | ||
MediaType.JSON_UTF_8, "{}"); | ||
final ServiceRequestContext ctx = ServiceRequestContext.builder(request) | ||
.build(); | ||
|
||
final AggregatedHttpResponse res = unframedGrpcService.serve(ctx, request).aggregate().join(); | ||
assertThat(res.status()).isEqualTo(HttpStatus.OK); | ||
assertThat(res.contentType()).isEqualTo(MediaType.JSON_UTF_8); | ||
} | ||
|
||
@ParameterizedTest | ||
@ArgumentsSource(ProtobufMediaTypeProvider.class) | ||
void respondWithCorrespondingProtobufMediaType(MediaType protobufType) throws Exception { | ||
final UnframedGrpcService unframedGrpcService = buildUnframedGrpcService(testService); | ||
|
||
final HttpRequest request = HttpRequest.of(HttpMethod.POST, | ||
"/armeria.grpc.testing.TestService/EmptyCall", | ||
protobufType, | ||
EmptyProtos.Empty.getDefaultInstance().toByteArray()); | ||
final ServiceRequestContext ctx = ServiceRequestContext.builder(request) | ||
.build(); | ||
|
||
final AggregatedHttpResponse res = unframedGrpcService.serve(ctx, request).aggregate().join(); | ||
assertThat(res.status()).isEqualTo(HttpStatus.OK); | ||
assertThat(res.contentType()).isEqualTo(protobufType); | ||
} | ||
|
||
private static class ProtobufMediaTypeProvider implements ArgumentsProvider { | ||
@Override | ||
public Stream<? extends Arguments> provideArguments(ExtensionContext context) { | ||
return Stream.of(MediaType.PROTOBUF, MediaType.X_PROTOBUF, MediaType.X_GOOGLE_PROTOBUF) | ||
.map(Arguments::of); | ||
} | ||
} | ||
|
||
private static UnframedGrpcService buildUnframedGrpcService(BindableService bindableService) { | ||
return (UnframedGrpcService) GrpcService.builder() | ||
.addService(bindableService) | ||
.maxRequestMessageLength(MAX_MESSAGE_BYTES) | ||
.maxResponseMessageLength(MAX_MESSAGE_BYTES) | ||
.enableUnframedRequests(true) | ||
.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters