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));
}
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();
}
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);
}
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();
}
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");
}
Aggregations