use of io.servicetalk.http.api.HttpResponse in project servicetalk by apple.
the class HttpProvidersTest method testSingleAddressHttpClientBuilderProviderForResolvedAddress.
@Test
void testSingleAddressHttpClientBuilderProviderForResolvedAddress() throws Exception {
try (ServerContext serverContext = HttpServers.forAddress(localAddress(0)).listenStreamingAndAwait(new TestServiceStreaming())) {
SocketAddress serverAddress = serverContext.listenAddress();
TestSingleAddressHttpClientBuilderProvider.MODIFY_FOR_ADDRESS.set(serverAddress);
try (BlockingHttpClient client = HttpClients.forResolvedAddress(serverAddress).buildBlocking()) {
assertThat(TestSingleAddressHttpClientBuilderProvider.BUILD_COUNTER.get(), is(1));
HttpResponse response = client.request(client.get(SVC_ECHO));
assertThat(response.status(), is(OK));
assertThat(TestSingleAddressHttpClientBuilderProvider.CONNECTION_COUNTER.get(), is(1));
}
}
}
use of io.servicetalk.http.api.HttpResponse in project servicetalk by apple.
the class HttpProvidersTest method testSingleAddressHttpClientBuilderProviderWithHostAndPort.
@Test
void testSingleAddressHttpClientBuilderProviderWithHostAndPort() throws Exception {
try (ServerContext serverContext = HttpServers.forAddress(localAddress(0)).listenStreamingAndAwait(new TestServiceStreaming())) {
HostAndPort serverAddress = serverHostAndPort(serverContext);
TestSingleAddressHttpClientBuilderProvider.MODIFY_FOR_ADDRESS.set(serverAddress);
try (BlockingHttpClient client = HttpClients.forSingleAddress(serverAddress).buildBlocking()) {
assertThat(TestSingleAddressHttpClientBuilderProvider.BUILD_COUNTER.get(), is(1));
HttpResponse response = client.request(client.get(SVC_ECHO));
assertThat(response.status(), is(OK));
assertThat(TestSingleAddressHttpClientBuilderProvider.CONNECTION_COUNTER.get(), is(1));
}
}
}
use of io.servicetalk.http.api.HttpResponse 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.HttpResponse in project servicetalk by apple.
the class DefaultMultiAddressUrlHttpClientBuilderTest method internalClientsInheritExecutionContext.
@Test
void internalClientsInheritExecutionContext() throws Exception {
HttpExecutionContext expectedCtx = new HttpExecutionContextBuilder().ioExecutor(CTX.ioExecutor()).executor(CTX.executor()).bufferAllocator(PREFER_DIRECT_ALLOCATOR).executionStrategy(offloadNone()).build();
AtomicReference<HttpExecutionContext> actualInternalCtx = new AtomicReference<>();
try (ServerContext serverContext = HttpServers.forAddress(localAddress(0)).executionStrategy(offloadNone()).listenStreamingAndAwait((ctx, request, responseFactory) -> succeeded(responseFactory.ok()));
BlockingHttpClient blockingHttpClient = HttpClients.forMultiAddressUrl().initializer((scheme, address, builder) -> builder.appendClientFilter(client -> {
actualInternalCtx.set(client.executionContext());
return new StreamingHttpClientFilter(client) {
};
})).ioExecutor(expectedCtx.ioExecutor()).executor(expectedCtx.executor()).bufferAllocator(expectedCtx.bufferAllocator()).executionStrategy(expectedCtx.executionStrategy()).buildBlocking()) {
// Check external client
assertExecutionContext(expectedCtx, blockingHttpClient.executionContext());
// Make a request to trigger the filter execution that extracts the execution context.
HttpResponse response = blockingHttpClient.request(blockingHttpClient.get("http://" + serverHostAndPort(serverContext)));
assertThat(response.status(), is(OK));
// Check internal client
assertExecutionContext(expectedCtx, actualInternalCtx.get());
}
}
use of io.servicetalk.http.api.HttpResponse in project servicetalk by apple.
the class ExpectContinueTest method serverRespondsWithSuccessAggregated.
@ParameterizedTest(name = "protocol={0} withCL={1}")
@MethodSource("arguments")
void serverRespondsWithSuccessAggregated(HttpProtocol protocol, boolean withCL) throws Exception {
try (HttpServerContext serverContext = startServer(protocol, (ctx, request, response) -> {
requestReceived.countDown();
returnResponse.await();
response.status(ACCEPTED);
try (HttpPayloadWriter<Buffer> writer = response.sendMetaData()) {
for (Buffer chunk : request.payloadBody()) {
writer.write(chunk);
}
}
});
StreamingHttpClient client = createClient(serverContext, protocol);
HttpConnection connection = client.reserveConnection(client.get("/")).toFuture().get().asConnection()) {
BufferAllocator allocator = connection.executionContext().bufferAllocator();
returnResponse.countDown();
HttpResponse response = connection.request(newRequest(connection, withCL, false, PAYLOAD + PAYLOAD, allocator)).toFuture().get();
assertThat(response.status(), is(ACCEPTED));
assertThat(response.payloadBody().toString(US_ASCII), equalTo(PAYLOAD + PAYLOAD));
sendFollowUpRequest(connection.asStreamingConnection(), withCL, allocator, ACCEPTED);
}
}
Aggregations