Search in sources :

Example 86 with BlockingHttpClient

use of io.servicetalk.http.api.BlockingHttpClient in project servicetalk by apple.

the class HttpServerFilterOrderTest method prependOrder.

@Test
void prependOrder() throws Exception {
    StreamingHttpService filter1 = newMockService();
    StreamingHttpService filter2 = newMockService();
    ServerContext serverContext = HttpServers.forAddress(localAddress(0)).appendServiceFilter(addFilter(filter1)).appendServiceFilter(addFilter(filter2)).listenBlockingAndAwait((ctx, request, responseFactory) -> responseFactory.ok());
    BlockingHttpClient client = forSingleAddress(serverHostAndPort(serverContext)).buildBlocking();
    HttpResponse resp = client.request(client.get("/"));
    assertThat("Unexpected response.", resp.status(), is(OK));
    InOrder verifier = inOrder(filter1, filter2);
    verifier.verify(filter1).handle(any(), any(), any());
    verifier.verify(filter2).handle(any(), any(), any());
}
Also used : InOrder(org.mockito.InOrder) ServerContext(io.servicetalk.transport.api.ServerContext) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) StreamingHttpService(io.servicetalk.http.api.StreamingHttpService) HttpResponse(io.servicetalk.http.api.HttpResponse) Test(org.junit.jupiter.api.Test)

Example 87 with BlockingHttpClient

use of io.servicetalk.http.api.BlockingHttpClient in project servicetalk by apple.

the class HttpServerFilterOrderTest method conditionalNonOffload.

@Test
void conditionalNonOffload() throws Exception {
    StreamingHttpService filter0 = newMockService();
    StreamingHttpService filter1 = newMockService();
    StreamingHttpService filter2 = newMockService();
    StreamingHttpService filter3 = newMockService();
    ServerContext serverContext = HttpServers.forAddress(localAddress(0)).appendServiceFilter(addFilter(filter0)).appendNonOffloadingServiceFilter(req -> true, addFilter(filter1)).appendNonOffloadingServiceFilter(req -> false, addFilter(filter2)).appendNonOffloadingServiceFilter(req -> true, addFilter(filter3)).listenBlockingAndAwait((ctx, request, responseFactory) -> responseFactory.ok());
    BlockingHttpClient client = forSingleAddress(serverHostAndPort(serverContext)).buildBlocking();
    HttpResponse resp = client.request(client.get("/"));
    assertThat("Unexpected response.", resp.status(), is(OK));
    InOrder verifier = inOrder(filter1, filter2, filter3, filter0);
    verifier.verify(filter1).handle(any(), any(), any());
    verifier.verify(filter2, never()).handle(any(), any(), any());
    verifier.verify(filter3).handle(any(), any(), any());
    verifier.verify(filter0).handle(any(), any(), any());
}
Also used : BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) AddressUtils.localAddress(io.servicetalk.transport.netty.internal.AddressUtils.localAddress) InOrder(org.mockito.InOrder) ServerContext(io.servicetalk.transport.api.ServerContext) HttpClients.forSingleAddress(io.servicetalk.http.netty.HttpClients.forSingleAddress) HttpResponse(io.servicetalk.http.api.HttpResponse) StreamingHttpServiceFilterFactory(io.servicetalk.http.api.StreamingHttpServiceFilterFactory) Mockito.when(org.mockito.Mockito.when) OK(io.servicetalk.http.api.HttpResponseStatus.OK) Test(org.junit.jupiter.api.Test) StreamingHttpServiceFilter(io.servicetalk.http.api.StreamingHttpServiceFilter) Mockito.never(org.mockito.Mockito.never) StreamingHttpService(io.servicetalk.http.api.StreamingHttpService) ExecutionStrategyInfluencer(io.servicetalk.transport.api.ExecutionStrategyInfluencer) Mockito.inOrder(org.mockito.Mockito.inOrder) HttpExecutionStrategies(io.servicetalk.http.api.HttpExecutionStrategies) Completable.completed(io.servicetalk.concurrent.api.Completable.completed) HttpExecutionStrategy(io.servicetalk.http.api.HttpExecutionStrategy) AddressUtils.serverHostAndPort(io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort) Matchers.is(org.hamcrest.Matchers.is) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Mockito.mock(org.mockito.Mockito.mock) InOrder(org.mockito.InOrder) ServerContext(io.servicetalk.transport.api.ServerContext) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) StreamingHttpService(io.servicetalk.http.api.StreamingHttpService) HttpResponse(io.servicetalk.http.api.HttpResponse) Test(org.junit.jupiter.api.Test)

Example 88 with BlockingHttpClient

use of io.servicetalk.http.api.BlockingHttpClient in project servicetalk by apple.

the class HttpServerFilterOrderTest method conditional.

@Test
void conditional() throws Exception {
    StreamingHttpService filter1 = newMockService();
    StreamingHttpService filter2 = newMockService();
    StreamingHttpService filter3 = newMockService();
    ServerContext serverContext = HttpServers.forAddress(localAddress(0)).appendServiceFilter(req -> true, addFilter(filter1)).appendServiceFilter(req -> false, addFilter(filter2)).appendServiceFilter(req -> true, addFilter(filter3)).listenBlockingAndAwait((ctx, request, responseFactory) -> responseFactory.ok());
    BlockingHttpClient client = forSingleAddress(serverHostAndPort(serverContext)).buildBlocking();
    HttpResponse resp = client.request(client.get("/"));
    assertThat("Unexpected response.", resp.status(), is(OK));
    InOrder verifier = inOrder(filter1, filter2, filter3);
    verifier.verify(filter1).handle(any(), any(), any());
    verifier.verify(filter2, never()).handle(any(), any(), any());
    verifier.verify(filter3).handle(any(), any(), any());
}
Also used : BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) AddressUtils.localAddress(io.servicetalk.transport.netty.internal.AddressUtils.localAddress) InOrder(org.mockito.InOrder) ServerContext(io.servicetalk.transport.api.ServerContext) HttpClients.forSingleAddress(io.servicetalk.http.netty.HttpClients.forSingleAddress) HttpResponse(io.servicetalk.http.api.HttpResponse) StreamingHttpServiceFilterFactory(io.servicetalk.http.api.StreamingHttpServiceFilterFactory) Mockito.when(org.mockito.Mockito.when) OK(io.servicetalk.http.api.HttpResponseStatus.OK) Test(org.junit.jupiter.api.Test) StreamingHttpServiceFilter(io.servicetalk.http.api.StreamingHttpServiceFilter) Mockito.never(org.mockito.Mockito.never) StreamingHttpService(io.servicetalk.http.api.StreamingHttpService) ExecutionStrategyInfluencer(io.servicetalk.transport.api.ExecutionStrategyInfluencer) Mockito.inOrder(org.mockito.Mockito.inOrder) HttpExecutionStrategies(io.servicetalk.http.api.HttpExecutionStrategies) Completable.completed(io.servicetalk.concurrent.api.Completable.completed) HttpExecutionStrategy(io.servicetalk.http.api.HttpExecutionStrategy) AddressUtils.serverHostAndPort(io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort) Matchers.is(org.hamcrest.Matchers.is) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Mockito.mock(org.mockito.Mockito.mock) InOrder(org.mockito.InOrder) ServerContext(io.servicetalk.transport.api.ServerContext) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) StreamingHttpService(io.servicetalk.http.api.StreamingHttpService) HttpResponse(io.servicetalk.http.api.HttpResponse) Test(org.junit.jupiter.api.Test)

Example 89 with BlockingHttpClient

use of io.servicetalk.http.api.BlockingHttpClient in project servicetalk by apple.

the class BlockingProtobufClient method main.

public static void main(String[] args) throws Exception {
    try (BlockingHttpClient client = HttpClients.forSingleAddress("localhost", 8080).buildBlocking()) {
        HttpResponse resp = client.request(client.post("/protobuf").payloadBody(RequestMessage.newBuilder().setMessage("value").build(), 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) REQ_SERIALIZER(io.servicetalk.examples.http.serialization.protobuf.SerializerUtils.REQ_SERIALIZER) HttpResponse(io.servicetalk.http.api.HttpResponse) RESP_SERIALIZER(io.servicetalk.examples.http.serialization.protobuf.SerializerUtils.RESP_SERIALIZER) RequestMessage(io.servicetalk.examples.http.serialization.protobuf.ExampleProtos.RequestMessage) HttpClients(io.servicetalk.http.netty.HttpClients) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) HttpResponse(io.servicetalk.http.api.HttpResponse)

Example 90 with BlockingHttpClient

use of io.servicetalk.http.api.BlockingHttpClient in project servicetalk by apple.

the class OpenTracingClient method main.

public static void main(String[] args) throws Exception {
    // Publishing to Zipkin is optional, but demonstrated for completeness.
    try (ZipkinPublisher zipkinPublisher = new ZipkinPublisher.Builder("servicetalk-test-client", LoggingReporter.CONSOLE).build();
        Tracer tracer = new DefaultInMemoryTracer.Builder(SCOPE_MANAGER).addListener(zipkinPublisher).build();
        BlockingHttpClient client = HttpClients.forSingleAddress("localhost", 8080).appendClientFilter(new TracingHttpRequesterFilter(tracer, "servicetalk-test-client")).buildBlocking()) {
        HttpResponse response = client.request(client.get("/1"));
        System.out.println(response.toString((name, value) -> value));
        HttpResponse response2 = client.request(client.get("/2"));
        System.out.println(response2.toString((name, value) -> value));
    }
}
Also used : BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) Tracer(io.opentracing.Tracer) TracingHttpRequesterFilter(io.servicetalk.opentracing.http.TracingHttpRequesterFilter) ZipkinPublisher(io.servicetalk.opentracing.zipkin.publisher.ZipkinPublisher) HttpResponse(io.servicetalk.http.api.HttpResponse) DefaultInMemoryTracer(io.servicetalk.opentracing.inmemory.DefaultInMemoryTracer) SCOPE_MANAGER(io.servicetalk.opentracing.asynccontext.AsyncContextInMemoryScopeManager.SCOPE_MANAGER) HttpClients(io.servicetalk.http.netty.HttpClients) LoggingReporter(io.servicetalk.opentracing.zipkin.publisher.reporter.LoggingReporter) TracingHttpRequesterFilter(io.servicetalk.opentracing.http.TracingHttpRequesterFilter) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) Tracer(io.opentracing.Tracer) DefaultInMemoryTracer(io.servicetalk.opentracing.inmemory.DefaultInMemoryTracer) HttpResponse(io.servicetalk.http.api.HttpResponse) ZipkinPublisher(io.servicetalk.opentracing.zipkin.publisher.ZipkinPublisher)

Aggregations

BlockingHttpClient (io.servicetalk.http.api.BlockingHttpClient)100 HttpResponse (io.servicetalk.http.api.HttpResponse)69 ServerContext (io.servicetalk.transport.api.ServerContext)54 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)49 Test (org.junit.jupiter.api.Test)41 InetSocketAddress (java.net.InetSocketAddress)32 StreamingHttpResponse (io.servicetalk.http.api.StreamingHttpResponse)30 StreamingHttpRequest (io.servicetalk.http.api.StreamingHttpRequest)28 MethodSource (org.junit.jupiter.params.provider.MethodSource)28 AddressUtils.serverHostAndPort (io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort)26 HttpRequest (io.servicetalk.http.api.HttpRequest)25 AddressUtils.localAddress (io.servicetalk.transport.netty.internal.AddressUtils.localAddress)25 Single (io.servicetalk.concurrent.api.Single)21 OK (io.servicetalk.http.api.HttpResponseStatus.OK)21 HttpSerializers.textSerializerUtf8 (io.servicetalk.http.api.HttpSerializers.textSerializerUtf8)21 HostAndPort (io.servicetalk.transport.api.HostAndPort)21 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)21 Matchers.is (org.hamcrest.Matchers.is)20 Nullable (javax.annotation.Nullable)16 Single.succeeded (io.servicetalk.concurrent.api.Single.succeeded)15