use of io.servicetalk.http.api.BlockingStreamingHttpClient in project servicetalk by apple.
the class BlockingStreamingHttpServiceTest method doNotWriteTheLastChunk.
@Test
void doNotWriteTheLastChunk() throws Exception {
BlockingStreamingHttpClient client = context((ctx, request, response) -> {
response.sendMetaData();
// Do not close()
});
BlockingStreamingHttpResponse response = client.request(client.get("/"));
assertResponse(response);
final BlockingIterator<Buffer> iterator = response.payloadBody().iterator();
assertThrows(TimeoutException.class, () -> iterator.hasNext(1, SECONDS));
}
use of io.servicetalk.http.api.BlockingStreamingHttpClient in project servicetalk by apple.
the class BlockingStreamingHttpServiceTest method echoServer.
private void echoServer(BlockingStreamingHttpService handler) throws Exception {
BlockingStreamingHttpClient client = context(handler);
BlockingStreamingHttpResponse response = client.request(client.post("/").payloadBody(asList("Hello\n", "World\n"), appSerializerUtf8FixLen()));
assertResponse(response, HELLO_WORLD, true);
}
use of io.servicetalk.http.api.BlockingStreamingHttpClient in project servicetalk by apple.
the class BlockingStreamingHttpServiceTest method respondWithCustomMetaData.
@Test
void respondWithCustomMetaData() throws Exception {
BlockingStreamingHttpClient client = context((ctx, request, response) -> {
response.status(ACCEPTED);
CharSequence auth = request.headers().get("X-User-Header");
if (auth != null) {
response.addHeader("X-User-Header", auth);
}
response.addHeader("X-Server-Header", "X-Server-Value");
response.sendMetaData().close();
});
BlockingStreamingHttpResponse response = client.request(client.get("/").addHeader("X-User-Header", "X-User-Value"));
assertThat(response.status(), is(ACCEPTED));
assertThat(response.version(), is(HTTP_1_1));
assertThat(response.headers().get("X-User-Header").toString(), is("X-User-Value"));
assertThat(response.headers().get("X-Server-Header").toString(), is("X-Server-Value"));
assertThat(response.toResponse().toFuture().get().payloadBody(), is(EMPTY_BUFFER));
}
use of io.servicetalk.http.api.BlockingStreamingHttpClient in project servicetalk by apple.
the class BlockingStreamingHttpServiceTest method respondWithPayloadBodyAndTrailers.
private void respondWithPayloadBodyAndTrailers(BlockingStreamingHttpService handler, boolean useDeserializer) throws Exception {
BlockingStreamingHttpClient client = context(handler);
BlockingStreamingHttpResponse response = client.request(client.get("/"));
assertResponse(response);
assertThat(response.headers().get(TRAILER).toString(), is(X_TOTAL_LENGTH));
final StringBuilder sb = new StringBuilder();
final HttpHeaders trailers;
if (useDeserializer) {
HttpMessageBodyIterator<String> msgBody = response.messageBody(appSerializerUtf8FixLen()).iterator();
while (msgBody.hasNext()) {
sb.append(msgBody.next());
}
trailers = msgBody.trailers();
} else {
HttpMessageBodyIterator<Buffer> msgBody = response.messageBody().iterator();
while (msgBody.hasNext()) {
sb.append(requireNonNull(msgBody.next()).toString(UTF_8));
}
trailers = msgBody.trailers();
}
assertThat(sb.toString(), is(HELLO_WORLD));
assertThat(trailers, notNullValue());
assertThat(trailers.get(X_TOTAL_LENGTH).toString(), is(HELLO_WORLD_LENGTH));
}
use of io.servicetalk.http.api.BlockingStreamingHttpClient in project servicetalk by apple.
the class DefaultGrpcClientCallFactory method newBlockingStreamingCall.
@Override
public <Req, Resp> BlockingStreamingClientCall<Req, Resp> newBlockingStreamingCall(final MethodDescriptor<Req, Resp> methodDescriptor, final BufferDecoderGroup decompressors) {
GrpcStreamingSerializer<Req> serializerIdentity = streamingSerializer(methodDescriptor);
GrpcStreamingDeserializer<Resp> deserializerIdentity = streamingDeserializer(methodDescriptor);
List<GrpcStreamingDeserializer<Resp>> deserializers = streamingDeserializers(methodDescriptor, decompressors.decoders());
CharSequence acceptedEncoding = decompressors.advertisedMessageEncoding();
CharSequence requestContentType = grpcContentType(methodDescriptor.requestDescriptor().serializerDescriptor().contentType());
CharSequence responseContentType = grpcContentType(methodDescriptor.responseDescriptor().serializerDescriptor().contentType());
final BlockingStreamingHttpClient client = streamingHttpClient.asBlockingStreamingClient();
return (metadata, request) -> {
Duration timeout = timeoutForRequest(metadata.timeout());
GrpcStreamingSerializer<Req> serializer = streamingSerializer(methodDescriptor, serializerIdentity, metadata.requestCompressor());
String mdPath = methodDescriptor.httpPath();
BlockingStreamingHttpRequest httpRequest = client.post(UNKNOWN_PATH.equals(mdPath) ? metadata.path() : mdPath);
initRequest(httpRequest, requestContentType, serializer.messageEncoding(), acceptedEncoding, timeout);
httpRequest.payloadBody(serializer.serialize(request, streamingHttpClient.executionContext().bufferAllocator()));
try {
assignStrategy(httpRequest, metadata);
final BlockingStreamingHttpResponse response = client.request(httpRequest);
return validateResponseAndGetPayload(response.toStreamingResponse(), responseContentType, client.executionContext().bufferAllocator(), readGrpcMessageEncodingRaw(response.headers(), deserializerIdentity, deserializers, GrpcStreamingDeserializer::messageEncoding)).toIterable();
} catch (Throwable cause) {
throw toGrpcException(cause);
}
};
}
Aggregations