use of io.servicetalk.http.api.BlockingHttpClient in project servicetalk by apple.
the class BlockingPojoClient method main.
public static void main(String[] args) throws Exception {
try (BlockingHttpClient client = HttpClients.forSingleAddress("localhost", 8080).buildBlocking()) {
HttpResponse resp = client.request(client.post("/pojos").payloadBody(new CreatePojoRequest("value"), REQ_SERIALIZER));
System.out.println(resp.toString((name, value) -> value));
System.out.println(resp.payloadBody(RESP_SERIALIZER));
}
}
use of io.servicetalk.http.api.BlockingHttpClient in project servicetalk by apple.
the class Http2PriorKnowledgeClient method main.
public static void main(String[] args) throws Exception {
// streaming API see helloworld examples.
try (BlockingHttpClient client = HttpClients.forSingleAddress("localhost", 8080).protocols(// Configure HTTP/2 Prior-Knowledge
h2Default()).buildBlocking()) {
HttpResponse response = client.request(client.get("/"));
System.out.println(response.toString((name, value) -> value));
System.out.println(response.payloadBody(textSerializerUtf8()));
}
}
use of io.servicetalk.http.api.BlockingHttpClient in project servicetalk by apple.
the class MutualSslTest method mutualSsl.
@ParameterizedTest
@MethodSource("params")
void mutualSsl(SslProvider serverSslProvider, SslProvider clientSslProvider, @SuppressWarnings("rawtypes") Map<SocketOption, Object> serverListenOptions, @SuppressWarnings("rawtypes") Map<SocketOption, Object> clientOptions) throws Exception {
assumeTcpFastOpen(clientOptions);
HttpServerBuilder serverBuilder = HttpServers.forAddress(localAddress(0)).sslConfig(new ServerSslConfigBuilder(DefaultTestCerts::loadServerPem, DefaultTestCerts::loadServerKey).trustManager(DefaultTestCerts::loadClientCAPem).clientAuthMode(REQUIRE).provider(serverSslProvider).build());
for (@SuppressWarnings("rawtypes") Entry<SocketOption, Object> entry : serverListenOptions.entrySet()) {
@SuppressWarnings("unchecked") SocketOption<Object> option = entry.getKey();
serverBuilder.listenSocketOption(option, entry.getValue());
}
try (ServerContext serverContext = serverBuilder.listenBlockingAndAwait((ctx, request, responseFactory) -> responseFactory.ok());
BlockingHttpClient client = newClientBuilder(serverContext, clientOptions).sslConfig(new ClientSslConfigBuilder(DefaultTestCerts::loadServerCAPem).provider(clientSslProvider).peerHost(serverPemHostname()).keyManager(DefaultTestCerts::loadClientPem, DefaultTestCerts::loadClientKey).build()).buildBlocking()) {
assertEquals(HttpResponseStatus.OK, client.request(client.get("/")).status());
}
}
use of io.servicetalk.http.api.BlockingHttpClient in project servicetalk by apple.
the class PartitionedHttpClientTest method testPartitionByTarget.
@Test
void testPartitionByTarget() throws Exception {
final Function<HttpRequestMetaData, PartitionAttributesBuilder> selector = req -> new DefaultPartitionAttributesBuilder(1).add(SRV_NAME, req.requestTarget().substring(1));
try (BlockingHttpClient clt = HttpClients.forPartitionedAddress(psd, "test-cluster", selector).initializer((pa, builder) -> builder.unresolvedAddressToHost(addr -> pa.get(SRV_NAME))).buildBlocking()) {
sdPublisher.onSubscribe(new TestSubscription());
sdPublisher.onNext(new TestPSDE(SRV_1, (InetSocketAddress) srv1.listenAddress()), new TestPSDE(SRV_2, (InetSocketAddress) srv2.listenAddress()));
final HttpResponse httpResponse1 = clt.request(clt.get("/" + SRV_2));
final HttpResponse httpResponse2 = clt.request(clt.get("/" + SRV_1));
MatcherAssert.assertThat(httpResponse1.headers().get(X_SERVER), hasToString(SRV_2));
MatcherAssert.assertThat(httpResponse2.headers().get(X_SERVER), hasToString(SRV_1));
}
}
use of io.servicetalk.http.api.BlockingHttpClient in project servicetalk by apple.
the class HttpSerializerErrorTest method blockingStreamingDeserializationHeaderMismatch.
@ParameterizedTest
@MethodSource("executors")
void blockingStreamingDeserializationHeaderMismatch(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).listenBlockingStreamingAndAwait((ctx, request, responseFactory) -> {
try {
BlockingIterable<String> reqIterable = request.payloadBody(streamingSerializer);
try (HttpPayloadWriter<String> stream = responseFactory.sendMetaData(streamingSerializer)) {
for (String reqChunk : reqIterable) {
stream.write(reqChunk);
}
}
} catch (SerializationException e) {
responseFactory.status(BAD_REQUEST);
responseFactory.sendMetaData().close();
}
});
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());
}
}
Aggregations