use of io.servicetalk.encoding.api.BufferEncoder in project servicetalk by apple.
the class ProtocolCompatibilityTest method testGrpcErrorStreaming.
private static void testGrpcErrorStreaming(final CompatClient client, final TestServerContext server, final boolean withStatus, @Nullable final String compression, final GrpcStatusCode expectCode, @Nullable final String expectMessage) throws Exception {
try {
BufferEncoder encoder = serviceTalkCompression(compression);
GrpcClientMetadata metadata = encoder == null ? DefaultGrpcClientMetadata.INSTANCE : new DefaultGrpcClientMetadata(encoder);
final Publisher<CompatResponse> streamingResponse = client.bidirectionalStreamingCall(metadata, Publisher.from(CompatRequest.newBuilder().setId(3).build(), CompatRequest.newBuilder().setId(4).build(), CompatRequest.newBuilder().setId(5).build()));
validateGrpcErrorInResponse(streamingResponse.toFuture(), withStatus, expectCode, expectMessage);
} finally {
closeAll(client, server);
}
}
use of io.servicetalk.encoding.api.BufferEncoder in project servicetalk by apple.
the class ProtocolCompatibilityTest method testBlockingRequestResponse.
private static void testBlockingRequestResponse(final BlockingCompatClient client, final TestServerContext server, final boolean streaming, @Nullable final String compression) throws Exception {
try {
final BufferEncoder compressor = serviceTalkCompression(compression);
final GrpcClientMetadata metadata = compressor == null ? DefaultGrpcClientMetadata.INSTANCE : new DefaultGrpcClientMetadata(compressor);
if (!streaming) {
final CompatResponse response1 = client.scalarCall(metadata, CompatRequest.newBuilder().setId(1).build());
assertEquals(1000001, response1.getSize());
} else {
// clientStreamingCall returns the "sum"
final CompatResponse response2 = client.clientStreamingCall(metadata, asList(CompatRequest.newBuilder().setId(1).build(), CompatRequest.newBuilder().setId(2).build(), CompatRequest.newBuilder().setId(3).build()));
assertEquals(1000006, response2.getSize());
// serverStreamingCall returns a stream from 0 to N-1
final BlockingIterable<CompatResponse> response3 = client.serverStreamingCall(metadata, CompatRequest.newBuilder().setId(3).build());
final List<CompatResponse> response3List = new ArrayList<>();
response3.forEach(response3List::add);
assertEquals(3, response3List.size());
assertEquals(1000000, response3List.get(0).getSize());
assertEquals(1000001, response3List.get(1).getSize());
assertEquals(1000002, response3List.get(2).getSize());
// bidirectionalStreamingCall basically echos also
final BlockingIterable<CompatResponse> response4 = client.bidirectionalStreamingCall(metadata, asList(CompatRequest.newBuilder().setId(3).build(), CompatRequest.newBuilder().setId(4).build(), CompatRequest.newBuilder().setId(5).build()));
final List<CompatResponse> response4List = new ArrayList<>();
response4.forEach(response4List::add);
assertEquals(3, response4List.size());
assertEquals(1000003, response4List.get(0).getSize());
assertEquals(1000004, response4List.get(1).getSize());
assertEquals(1000005, response4List.get(2).getSize());
}
} finally {
closeAll(client, server);
}
}
use of io.servicetalk.encoding.api.BufferEncoder in project servicetalk by apple.
the class ProtocolCompatibilityTest method testRequestResponse.
private static void testRequestResponse(final CompatClient client, final TestServerContext server, final boolean streaming, @Nullable final String compression) throws Exception {
try {
final BufferEncoder compressor = serviceTalkCompression(compression);
final GrpcClientMetadata metadata = compressor == null ? DefaultGrpcClientMetadata.INSTANCE : new DefaultGrpcClientMetadata(compressor);
if (!streaming) {
// scalarCall basically echos
final Single<CompatResponse> response1 = client.scalarCall(metadata, CompatRequest.newBuilder().setId(1).build());
assertEquals(1000001, response1.toFuture().get().getSize());
} else {
// clientStreamingCall returns the "sum"
final Single<CompatResponse> response2 = client.clientStreamingCall(metadata, Publisher.from(CompatRequest.newBuilder().setId(1).build(), CompatRequest.newBuilder().setId(2).build(), CompatRequest.newBuilder().setId(3).build()));
CompatResponse r = response2.toFuture().get();
assertEquals(1000006, r.getSize());
// serverStreamingCall returns a stream from 0 to N-1
final Publisher<CompatResponse> response3 = client.serverStreamingCall(metadata, CompatRequest.newBuilder().setId(3).build());
final List<CompatResponse> response3List = new ArrayList<>(response3.toFuture().get());
assertEquals(3, response3List.size());
assertEquals(1000000, response3List.get(0).getSize());
assertEquals(1000001, response3List.get(1).getSize());
assertEquals(1000002, response3List.get(2).getSize());
// bidirectionalStreamingCall basically echos also
final Publisher<CompatResponse> response4 = client.bidirectionalStreamingCall(metadata, Publisher.from(CompatRequest.newBuilder().setId(3).build(), CompatRequest.newBuilder().setId(4).build(), CompatRequest.newBuilder().setId(5).build()));
final List<CompatResponse> response4List = new ArrayList<>(response4.toFuture().get());
assertEquals(3, response4List.size());
assertEquals(1000003, response4List.get(0).getSize());
assertEquals(1000004, response4List.get(1).getSize());
assertEquals(1000005, response4List.get(2).getSize());
}
} finally {
closeAll(client, server);
}
}
use of io.servicetalk.encoding.api.BufferEncoder in project servicetalk by apple.
the class ContentEncodingHttpServiceFilter method create.
@Override
public StreamingHttpServiceFilter create(final StreamingHttpService service) {
return new StreamingHttpServiceFilter(service) {
@Override
public Single<StreamingHttpResponse> handle(final HttpServiceContext ctx, final StreamingHttpRequest request, final StreamingHttpResponseFactory responseFactory) {
return Single.defer(() -> {
final StreamingHttpRequest requestDecompressed;
Iterator<? extends CharSequence> contentEncodingItr = request.headers().valuesIterator(CONTENT_ENCODING);
final boolean hasContentEncoding = contentEncodingItr.hasNext();
if (hasContentEncoding) {
BufferDecoder decoder = matchAndRemoveEncoding(decompressors.decoders(), BufferDecoder::encodingName, contentEncodingItr, request.headers());
if (decoder == null) {
return succeeded(responseFactory.unsupportedMediaType()).shareContextOnSubscribe();
}
requestDecompressed = request.transformPayloadBody(pub -> decoder.streamingDecoder().deserialize(pub, ctx.executionContext().bufferAllocator()));
} else {
requestDecompressed = request;
}
return super.handle(ctx, requestDecompressed, responseFactory).map(response -> {
final CharSequence reqAcceptEncoding;
if (isPassThrough(request.method(), response) || (reqAcceptEncoding = request.headers().get(ACCEPT_ENCODING)) == null) {
return response;
}
BufferEncoder encoder = negotiateAcceptedEncodingRaw(reqAcceptEncoding, compressors, BufferEncoder::encodingName);
if (encoder == null || identityEncoder().equals(encoder)) {
return response;
}
addContentEncoding(response.headers(), encoder.encodingName());
return response.transformPayloadBody(bufPub -> encoder.streamingEncoder().serialize(bufPub, ctx.executionContext().bufferAllocator()));
}).shareContextOnSubscribe();
});
}
};
}
use of io.servicetalk.encoding.api.BufferEncoder in project servicetalk by apple.
the class ProtocolCompatibilityTest method testGrpcErrorScalar.
private static void testGrpcErrorScalar(final CompatClient client, final TestServerContext server, final boolean withStatus, @Nullable final String compression, final GrpcStatusCode expectCode, @Nullable final String expectMessage) throws Exception {
try {
BufferEncoder encoder = serviceTalkCompression(compression);
GrpcClientMetadata metadata = encoder == null ? DefaultGrpcClientMetadata.INSTANCE : new DefaultGrpcClientMetadata(encoder);
final Single<CompatResponse> scalarResponse = client.scalarCall(metadata, CompatRequest.newBuilder().setId(1).build());
validateGrpcErrorInResponse(scalarResponse.toFuture(), withStatus, expectCode, expectMessage);
} finally {
closeAll(client, server);
}
}
Aggregations