Search in sources :

Example 16 with StreamingHttpService

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

the class HttpPredicateRouterBuilderQueryTest method testWhenQueryParamIsPresent.

@Test
void testWhenQueryParamIsPresent() {
    final StreamingHttpService service = new HttpPredicateRouterBuilder().whenQueryParam("page").isPresent().thenRouteTo(serviceA).when((ctx, req) -> true).thenRouteTo(fallbackService).buildStreaming();
    when(request.queryParametersIterator("page")).then(answerIteratorOf("home"));
    assertSame(responseA, service.handle(ctx, request, reqRespFactory));
    when(request.queryParametersIterator("page")).thenReturn(emptyIterator());
    assertSame(fallbackResponse, service.handle(ctx, request, reqRespFactory));
}
Also used : StreamingHttpService(io.servicetalk.http.api.StreamingHttpService) Test(org.junit.jupiter.api.Test)

Example 17 with StreamingHttpService

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

the class HttpServerOverrideOffloadingTest method setup.

HttpClient setup(HttpExecutionStrategy serverStrategy, @Nullable HttpExecutionStrategy routeStrategy) throws Exception {
    routeService = new OffloadingTesterService(routeStrategy);
    RouteContinuation route = new HttpPredicateRouterBuilder().whenPathStartsWith("/service");
    if (null != routeStrategy) {
        route = route.executionStrategy(routeService.usingStrategy());
    }
    StreamingHttpService router = route.thenRouteTo(routeService).buildStreaming();
    server = HttpServers.forAddress(localAddress(0)).ioExecutor(EXECUTION_CONTEXT.ioExecutor()).executor(EXECUTION_CONTEXT.executor()).executionStrategy(serverStrategy).listenStreamingAndAwait(router);
    return HttpClients.forSingleAddress(serverHostAndPort(server)).build();
}
Also used : RouteContinuation(io.servicetalk.http.router.predicate.dsl.RouteContinuation) StreamingHttpService(io.servicetalk.http.api.StreamingHttpService)

Example 18 with StreamingHttpService

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

the class InOrderRouter method handle.

@Override
public Single<StreamingHttpResponse> handle(final HttpServiceContext ctx, final StreamingHttpRequest request, final StreamingHttpResponseFactory factory) {
    for (final Route pair : routes) {
        if (pair.predicate().test(ctx, request)) {
            StreamingHttpService service = pair.service();
            final HttpExecutionStrategy strategy = pair.routeStrategy();
            HttpExecutionContext useContext = ctx.executionContext();
            if (null != strategy && useContext.executionStrategy().missing(strategy).hasOffloads()) {
                // Additional offloading needed
                service = StreamingHttpServiceToOffloadedStreamingHttpService.offloadService(strategy, useContext.executor(), IoThreadFactory.IoThread::currentThreadIsIoThread, service);
            }
            return service.handle(ctx, request, factory);
        }
    }
    return fallbackService.handle(ctx, request, factory);
}
Also used : HttpExecutionContext(io.servicetalk.http.api.HttpExecutionContext) IoThreadFactory(io.servicetalk.transport.api.IoThreadFactory) StreamingHttpServiceToOffloadedStreamingHttpService(io.servicetalk.http.api.StreamingHttpServiceToOffloadedStreamingHttpService) StreamingHttpService(io.servicetalk.http.api.StreamingHttpService) HttpExecutionStrategy(io.servicetalk.http.api.HttpExecutionStrategy)

Example 19 with StreamingHttpService

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

the class HttpPredicateRouterBuilderTest method testCloseAsyncClosesAllServices.

@Test
void testCloseAsyncClosesAllServices() throws Exception {
    when(serviceA.closeAsync()).thenReturn(completableA);
    when(serviceB.closeAsync()).thenReturn(completableB);
    when(serviceC.closeAsync()).thenReturn(completableC);
    final StreamingHttpService service = new HttpPredicateRouterBuilder().whenMethod(GET).thenRouteTo(serviceA).whenMethod(POST).thenRouteTo(serviceB).when((ctx, req) -> true).thenRouteTo(serviceC).buildStreaming();
    service.closeAsync().toFuture().get();
    verify(serviceA).closeAsync();
    verify(serviceB).closeAsync();
    verify(serviceC).closeAsync();
    completableA.verifyNotCancelled();
    completableB.verifyNotCancelled();
    completableC.verifyNotCancelled();
    completableA.verifyListenCalled();
    completableB.verifyListenCalled();
    completableC.verifyListenCalled();
}
Also used : StreamingHttpService(io.servicetalk.http.api.StreamingHttpService) Test(org.junit.jupiter.api.Test)

Example 20 with StreamingHttpService

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

the class HttpPredicateRouterBuilderHeaderTest method testMultipleHeaderRoutes.

@Test
void testMultipleHeaderRoutes() {
    final StreamingHttpService service = new HttpPredicateRouterBuilder().whenHeader("host").firstValue("a.com").thenRouteTo(serviceA).whenHeader("host").firstValue("b.com").thenRouteTo(serviceB).whenHeader("host").firstValue("c.com").thenRouteTo(serviceC).whenHeader("host").firstValue("d.com").thenRouteTo(serviceD).when((ctx, req) -> true).thenRouteTo(fallbackService).buildStreaming();
    when(headers.valuesIterator("host")).then(answerIteratorOf("d.com"));
    assertSame(responseD, service.handle(ctx, request, reqRespFactory));
    verify(request, times(4)).headers();
    verify(headers, times(4)).valuesIterator("host");
}
Also used : StreamingHttpService(io.servicetalk.http.api.StreamingHttpService) Test(org.junit.jupiter.api.Test)

Aggregations

StreamingHttpService (io.servicetalk.http.api.StreamingHttpService)50 Test (org.junit.jupiter.api.Test)38 BlockingHttpClient (io.servicetalk.http.api.BlockingHttpClient)8 ServerContext (io.servicetalk.transport.api.ServerContext)8 StreamingHttpRequest (io.servicetalk.http.api.StreamingHttpRequest)7 StreamingHttpResponse (io.servicetalk.http.api.StreamingHttpResponse)7 HttpResponse (io.servicetalk.http.api.HttpResponse)6 HttpApiConversions.toStreamingHttpService (io.servicetalk.http.api.HttpApiConversions.toStreamingHttpService)5 HttpExecutionStrategy (io.servicetalk.http.api.HttpExecutionStrategy)5 HttpServiceContext (io.servicetalk.http.api.HttpServiceContext)5 StreamingHttpResponseFactory (io.servicetalk.http.api.StreamingHttpResponseFactory)5 StreamingHttpServiceFilter (io.servicetalk.http.api.StreamingHttpServiceFilter)5 InOrder (org.mockito.InOrder)5 Single (io.servicetalk.concurrent.api.Single)4 Single.succeeded (io.servicetalk.concurrent.api.Single.succeeded)4 BlockingStreamingHttpService (io.servicetalk.http.api.BlockingStreamingHttpService)4 OK (io.servicetalk.http.api.HttpResponseStatus.OK)4 StreamingHttpClient (io.servicetalk.http.api.StreamingHttpClient)4 AddressUtils.localAddress (io.servicetalk.transport.netty.internal.AddressUtils.localAddress)4 AddressUtils.serverHostAndPort (io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort)4