Search in sources :

Example 16 with BlockingHttpClient

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));
    }
}
Also used : BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) CreatePojoRequest(io.servicetalk.examples.http.serialization.json.CreatePojoRequest) REQ_SERIALIZER(io.servicetalk.examples.http.serialization.json.SerializerUtils.REQ_SERIALIZER) HttpResponse(io.servicetalk.http.api.HttpResponse) RESP_SERIALIZER(io.servicetalk.examples.http.serialization.json.SerializerUtils.RESP_SERIALIZER) HttpClients(io.servicetalk.http.netty.HttpClients) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) CreatePojoRequest(io.servicetalk.examples.http.serialization.json.CreatePojoRequest) HttpResponse(io.servicetalk.http.api.HttpResponse)

Example 17 with BlockingHttpClient

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()));
    }
}
Also used : BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) HttpProtocolConfigs.h2Default(io.servicetalk.http.netty.HttpProtocolConfigs.h2Default) HttpSerializers.textSerializerUtf8(io.servicetalk.http.api.HttpSerializers.textSerializerUtf8) HttpResponse(io.servicetalk.http.api.HttpResponse) HttpClients(io.servicetalk.http.netty.HttpClients) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) HttpResponse(io.servicetalk.http.api.HttpResponse)

Example 18 with BlockingHttpClient

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());
    }
}
Also used : SocketOption(java.net.SocketOption) ServerContext(io.servicetalk.transport.api.ServerContext) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) HttpServerBuilder(io.servicetalk.http.api.HttpServerBuilder) DefaultTestCerts(io.servicetalk.test.resources.DefaultTestCerts) ClientSslConfigBuilder(io.servicetalk.transport.api.ClientSslConfigBuilder) ServerSslConfigBuilder(io.servicetalk.transport.api.ServerSslConfigBuilder) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 19 with BlockingHttpClient

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));
    }
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Matchers.hasToString(org.hamcrest.Matchers.hasToString) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) TestPublisher(io.servicetalk.concurrent.api.TestPublisher) HttpRequestMethod(io.servicetalk.http.api.HttpRequestMethod) Publisher(io.servicetalk.concurrent.api.Publisher) ClientGroup(io.servicetalk.client.api.ClientGroup) DefaultPartitionAttributesBuilder(io.servicetalk.client.api.internal.partition.DefaultPartitionAttributesBuilder) Function(java.util.function.Function) AfterAll(org.junit.jupiter.api.AfterAll) AVAILABLE(io.servicetalk.client.api.ServiceDiscovererEvent.Status.AVAILABLE) HttpExecutionStrategies.defaultStrategy(io.servicetalk.http.api.HttpExecutionStrategies.defaultStrategy) StreamingHttpClient(io.servicetalk.http.api.StreamingHttpClient) BeforeAll(org.junit.jupiter.api.BeforeAll) HttpSerializers.textSerializerUtf8(io.servicetalk.http.api.HttpSerializers.textSerializerUtf8) Objects.requireNonNull(java.util.Objects.requireNonNull) HttpClient(io.servicetalk.http.api.HttpClient) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) AddressUtils.serverHostAndPort(io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort) InetAddress.getLoopbackAddress(java.net.InetAddress.getLoopbackAddress) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) StreamingHttpRequestFactory(io.servicetalk.http.api.StreamingHttpRequestFactory) AddressUtils.localAddress(io.servicetalk.transport.netty.internal.AddressUtils.localAddress) ServerContext(io.servicetalk.transport.api.ServerContext) PartitionAttributesBuilder(io.servicetalk.client.api.partition.PartitionAttributesBuilder) Single(io.servicetalk.concurrent.api.Single) HttpResponse(io.servicetalk.http.api.HttpResponse) ServiceDiscoverer(io.servicetalk.client.api.ServiceDiscoverer) AsyncCloseables.newCompositeCloseable(io.servicetalk.concurrent.api.AsyncCloseables.newCompositeCloseable) Mockito.when(org.mockito.Mockito.when) OK(io.servicetalk.http.api.HttpResponseStatus.OK) InetSocketAddress(java.net.InetSocketAddress) TestSubscription(io.servicetalk.concurrent.api.TestSubscription) StandardCharsets(java.nio.charset.StandardCharsets) PartitionedServiceDiscovererEvent(io.servicetalk.client.api.partition.PartitionedServiceDiscovererEvent) DefaultHttpHeadersFactory(io.servicetalk.http.api.DefaultHttpHeadersFactory) HttpRequestMetaData(io.servicetalk.http.api.HttpRequestMetaData) Test(org.junit.jupiter.api.Test) List(java.util.List) PartitionAttributes(io.servicetalk.client.api.partition.PartitionAttributes) MatcherAssert(org.hamcrest.MatcherAssert) ExecutionContext(io.servicetalk.transport.api.ExecutionContext) Matchers.is(org.hamcrest.Matchers.is) Collections(java.util.Collections) HTTP_1_1(io.servicetalk.http.api.HttpProtocolVersion.HTTP_1_1) DefaultStreamingHttpRequestResponseFactory(io.servicetalk.http.api.DefaultStreamingHttpRequestResponseFactory) HostAndPort(io.servicetalk.transport.api.HostAndPort) Mockito.mock(org.mockito.Mockito.mock) TestSubscription(io.servicetalk.concurrent.api.TestSubscription) HttpRequestMetaData(io.servicetalk.http.api.HttpRequestMetaData) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) InetSocketAddress(java.net.InetSocketAddress) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) HttpResponse(io.servicetalk.http.api.HttpResponse) DefaultPartitionAttributesBuilder(io.servicetalk.client.api.internal.partition.DefaultPartitionAttributesBuilder) DefaultPartitionAttributesBuilder(io.servicetalk.client.api.internal.partition.DefaultPartitionAttributesBuilder) PartitionAttributesBuilder(io.servicetalk.client.api.partition.PartitionAttributesBuilder) Test(org.junit.jupiter.api.Test)

Example 20 with BlockingHttpClient

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());
    }
}
Also used : SerializationException(io.servicetalk.serializer.api.SerializationException) ServerContext(io.servicetalk.transport.api.ServerContext) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) HttpResponse(io.servicetalk.http.api.HttpResponse) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

BlockingHttpClient (io.servicetalk.http.api.BlockingHttpClient)86 HttpResponse (io.servicetalk.http.api.HttpResponse)57 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)45 ServerContext (io.servicetalk.transport.api.ServerContext)43 StreamingHttpResponse (io.servicetalk.http.api.StreamingHttpResponse)27 StreamingHttpRequest (io.servicetalk.http.api.StreamingHttpRequest)26 InetSocketAddress (java.net.InetSocketAddress)26 MethodSource (org.junit.jupiter.params.provider.MethodSource)25 Test (org.junit.jupiter.api.Test)24 HttpRequest (io.servicetalk.http.api.HttpRequest)20 HttpSerializers.textSerializerUtf8 (io.servicetalk.http.api.HttpSerializers.textSerializerUtf8)20 AddressUtils.localAddress (io.servicetalk.transport.netty.internal.AddressUtils.localAddress)20 Single (io.servicetalk.concurrent.api.Single)19 OK (io.servicetalk.http.api.HttpResponseStatus.OK)17 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)16 Matchers.is (org.hamcrest.Matchers.is)16 HostAndPort (io.servicetalk.transport.api.HostAndPort)15 StreamingHttpClient (io.servicetalk.http.api.StreamingHttpClient)14 HttpExecutionStrategy (io.servicetalk.http.api.HttpExecutionStrategy)13 ReservedBlockingHttpConnection (io.servicetalk.http.api.ReservedBlockingHttpConnection)13