use of io.servicetalk.http.api.BlockingHttpClient in project servicetalk by apple.
the class HttpSerializerErrorTest method serializationMapThrowsPropagatesToClient.
@ParameterizedTest
@MethodSource("executors")
void serializationMapThrowsPropagatesToClient(HttpTestExecutionStrategy serverStrategy) throws Exception {
serverExecutionStrategy = serverStrategy.executorSupplier.get();
TypeReference<Map<String, Object>> mapType = new TypeReference<Map<String, Object>>() {
};
HttpStreamingSerializerDeserializer<Map<String, Object>> streamingSerializer = jsonStreamingSerializer(JACKSON.streamingSerializerDeserializer(mapType));
HttpSerializerDeserializer<Map<String, Object>> serializer = HttpSerializers.jsonSerializer(JACKSON.serializerDeserializer(mapType));
try (ServerContext srv = HttpServers.forAddress(localAddress(0)).executionStrategy(serverExecutionStrategy).listenAndAwait((ctx, request, responseFactory) -> responseFactory.ok().toStreamingResponse().payloadBody(request.toStreamingRequest().payloadBody(streamingSerializer).map(result -> {
throw DELIBERATE_EXCEPTION;
}), streamingSerializer).toResponse());
BlockingHttpClient clt = HttpClients.forSingleAddress(serverHostAndPort(srv)).buildBlocking()) {
HttpResponse resp = clt.request(clt.post("/foo").payloadBody(emptyMap(), serializer));
assertEquals(INTERNAL_SERVER_ERROR, resp.status());
}
}
use of io.servicetalk.http.api.BlockingHttpClient in project servicetalk by apple.
the class HttpSerializerErrorTest method streamingDeserializationHeaderMismatch.
@ParameterizedTest
@MethodSource("executors")
void streamingDeserializationHeaderMismatch(HttpTestExecutionStrategy serverStrategy) throws Exception {
serverExecutionStrategy = serverStrategy.executorSupplier.get();
HttpStreamingSerializerDeserializer<String> streamingSerializer = jsonStreamingSerializer(JACKSON.streamingSerializerDeserializer(String.class));
try (ServerContext srv = HttpServers.forAddress(localAddress(0)).executionStrategy(serverExecutionStrategy).listenStreamingAndAwait((ctx, request, responseFactory) -> {
try {
return succeeded(responseFactory.ok().payloadBody(request.payloadBody(streamingSerializer), streamingSerializer));
} catch (SerializationException e) {
return succeeded(responseFactory.badRequest());
}
});
BlockingHttpClient clt = HttpClients.forSingleAddress(serverHostAndPort(srv)).buildBlocking()) {
HttpResponse resp = clt.request(clt.post("/foo").payloadBody(clt.executionContext().bufferAllocator().fromAscii("hello")));
assertEquals(BAD_REQUEST, resp.status());
}
}
use of io.servicetalk.http.api.BlockingHttpClient in project servicetalk by apple.
the class HttpSerializationErrorTest method serializationMapThrowsPropagatesToClient.
@ParameterizedTest
@MethodSource("executors")
void serializationMapThrowsPropagatesToClient(HttpTestExecutionStrategy serverStrategy) throws Exception {
serverExecutionStrategy = serverStrategy.executorSupplier.get();
TypeReference<Map<String, Object>> mapType = new TypeReference<Map<String, Object>>() {
};
HttpSerializerDeserializer<Map<String, Object>> httpSerializer = jsonSerializer(JACKSON.serializerDeserializer(mapType));
HttpStreamingSerializerDeserializer<Map<String, Object>> httpStreamingSerializer = jsonStreamingSerializer(JACKSON.streamingSerializerDeserializer(mapType));
try (ServerContext srv = HttpServers.forAddress(localAddress(0)).executionStrategy(serverExecutionStrategy).listenAndAwait((ctx, request, responseFactory) -> responseFactory.ok().toStreamingResponse().payloadBody(request.toStreamingRequest().payloadBody(httpStreamingSerializer).map(result -> {
throw DELIBERATE_EXCEPTION;
}), httpStreamingSerializer).toResponse());
BlockingHttpClient clt = HttpClients.forSingleAddress(serverHostAndPort(srv)).buildBlocking()) {
HttpResponse resp = clt.request(clt.post("/foo").payloadBody(emptyMap(), httpSerializer));
assertEquals(INTERNAL_SERVER_ERROR, resp.status());
}
}
use of io.servicetalk.http.api.BlockingHttpClient in project servicetalk by apple.
the class MalformedDataAfterHttpMessageTest method afterResponse.
@Test
void afterResponse() throws Exception {
ServerSocketChannel server = nettyServer(RESPONSE_MSG);
try (BlockingHttpClient client = stClient(server.localAddress())) {
HttpRequest request = client.get("/1");
ReservedBlockingHttpConnection connection = client.reserveConnection(request);
CountDownLatch connectionClosedLatch = new CountDownLatch(1);
connection.connectionContext().onClose().whenFinally(connectionClosedLatch::countDown).subscribe();
validateClientResponse(connection.request(request));
// Verify that the next request fails and connection gets closed:
// The exception generation is currently racy. A write maybe triggered while the channel is not active
// which will lead to ClosedChannelException.
assertThat(assertThrows(Exception.class, () -> connection.request(connection.get("/2"))), anyOf(instanceOf(DecoderException.class), instanceOf(ClosedChannelException.class)));
connectionClosedLatch.await();
} finally {
server.close().sync();
}
}
use of io.servicetalk.http.api.BlockingHttpClient in project servicetalk by apple.
the class MalformedDataAfterHttpMessageTest method afterResponseNextClientRequestSucceeds.
@ParameterizedTest
@ValueSource(booleans = { true, false })
void afterResponseNextClientRequestSucceeds(boolean doOffloading) throws Exception {
Queue<ConnectionContext> contextQueue = new LinkedBlockingQueue<>();
ServerSocketChannel server = nettyServer(RESPONSE_MSG);
try (BlockingHttpClient client = stClientBuilder(server.localAddress()).executionStrategy(doOffloading ? defaultStrategy() : offloadNever()).appendClientFilter(new RetryingHttpRequesterFilter.Builder().retryOther((req, cause) -> ofConstantBackoffFullJitter(ofNanos(1), MAX_VALUE)).build()).appendConnectionFilter(connection -> new StreamingHttpConnectionFilter(connection) {
@Override
public Single<StreamingHttpResponse> request(final StreamingHttpRequest request) {
contextQueue.add(connectionContext());
return super.request(request);
}
}).buildBlocking()) {
validateClientResponse(client.request(client.get("/1")));
validateClientResponse(client.request(client.get("/2")));
ConnectionContext ctx1 = contextQueue.poll();
assertThat(ctx1, not(nullValue()));
// RetryingHttpRequesterFilter or AutoRetry may re-issue the request if a failure is seen locally. Verify
// the last connection (used for second request) is different from the first.
ConnectionContext ctx2 = null;
ConnectionContext tmp;
while ((tmp = contextQueue.poll()) != null) {
ctx2 = tmp;
}
assertThat(ctx2, not(nullValue()));
assertThat(ctx1, not(equalTo(ctx2)));
assertThat(contextQueue, empty());
} finally {
server.close().sync();
}
}
Aggregations