Search in sources :

Example 6 with StreamingHttpService

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

the class HttpPredicateRouterBuilderTest method testWhenMethodIsOneOf.

@Test
void testWhenMethodIsOneOf() {
    final StreamingHttpService service = new HttpPredicateRouterBuilder().whenMethodIsOneOf(POST, PUT).thenRouteTo(serviceA).when((ctx, req) -> true).thenRouteTo(fallbackService).buildStreaming();
    when(request.method()).thenReturn(POST);
    assertSame(responseA, service.handle(ctx, request, reqRespFactory));
    when(request.method()).thenReturn(PUT);
    assertSame(responseA, service.handle(ctx, request, reqRespFactory));
    when(request.method()).thenReturn(GET);
    assertSame(fallbackResponse, service.handle(ctx, request, reqRespFactory));
}
Also used : StreamingHttpService(io.servicetalk.http.api.StreamingHttpService) Test(org.junit.jupiter.api.Test)

Example 7 with StreamingHttpService

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

the class HttpPredicateRouterBuilderTest method testWhenPathMatches.

@Test
void testWhenPathMatches() {
    final StreamingHttpService service = new HttpPredicateRouterBuilder().whenPathMatches(".*abc").thenRouteTo(serviceA).when((ctx, req) -> true).thenRouteTo(fallbackService).buildStreaming();
    when(request.path()).thenReturn("/abc");
    assertSame(responseA, service.handle(ctx, request, reqRespFactory));
    when(request.path()).thenReturn("/defabc");
    assertSame(responseA, service.handle(ctx, request, reqRespFactory));
    when(request.path()).thenReturn("/ABC");
    assertSame(fallbackResponse, service.handle(ctx, request, reqRespFactory));
    when(request.path()).thenReturn("/abcdef");
    assertSame(fallbackResponse, service.handle(ctx, request, reqRespFactory));
}
Also used : StreamingHttpService(io.servicetalk.http.api.StreamingHttpService) Test(org.junit.jupiter.api.Test)

Example 8 with StreamingHttpService

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

the class HttpPredicateRouterBuilderTest method testWhenIsSsl.

@Test
void testWhenIsSsl() {
    final StreamingHttpService service = new HttpPredicateRouterBuilder().whenIsSsl().thenRouteTo(serviceA).when((ctx, req) -> true).thenRouteTo(fallbackService).buildStreaming();
    when(ctx.sslSession()).thenReturn(mock(SSLSession.class));
    assertSame(responseA, service.handle(ctx, request, reqRespFactory));
    when(ctx.sslSession()).thenReturn(null);
    assertSame(fallbackResponse, service.handle(ctx, request, reqRespFactory));
}
Also used : StreamingHttpService(io.servicetalk.http.api.StreamingHttpService) SSLSession(javax.net.ssl.SSLSession) Test(org.junit.jupiter.api.Test)

Example 9 with StreamingHttpService

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

the class HttpPredicateRouterBuilderTest method testWhenBiPredicate.

@Test
void testWhenBiPredicate() {
    final SocketAddress addr1 = mock(SocketAddress.class);
    final SocketAddress addr2 = mock(SocketAddress.class);
    final StreamingHttpService service = new HttpPredicateRouterBuilder().when((ctx, req) -> ctx.remoteAddress() == addr1).thenRouteTo(serviceA).when((ctx, req) -> true).thenRouteTo(fallbackService).buildStreaming();
    when(ctx.remoteAddress()).thenReturn(addr1);
    assertSame(responseA, service.handle(ctx, request, reqRespFactory));
    when(ctx.remoteAddress()).thenReturn(addr2);
    assertSame(fallbackResponse, service.handle(ctx, request, reqRespFactory));
}
Also used : StreamingHttpService(io.servicetalk.http.api.StreamingHttpService) SocketAddress(java.net.SocketAddress) Test(org.junit.jupiter.api.Test)

Example 10 with StreamingHttpService

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

the class HttpPredicateRouterBuilderTest method testWhenPredicate.

@Test
void testWhenPredicate() {
    final StreamingHttpService service = new HttpPredicateRouterBuilder().when(req -> HTTP_1_1.equals(req.version())).thenRouteTo(serviceA).when((ctx, req) -> true).thenRouteTo(fallbackService).buildStreaming();
    assertSame(responseA, service.handle(ctx, request, reqRespFactory));
    when(request.version()).thenReturn(HTTP_1_0);
    assertSame(fallbackResponse, service.handle(ctx, request, reqRespFactory));
}
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