Search in sources :

Example 21 with StreamingHttpService

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

the class HttpPredicateRouterBuilderCookieTest method testWhenCookieIsPresent.

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

Example 22 with StreamingHttpService

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

the class HttpPredicateRouterBuilderCookieTest method testWhenCookieValues.

@Test
void testWhenCookieValues() {
    final StreamingHttpService service = new HttpPredicateRouterBuilder().whenCookie("session").values(new AnyMatchPredicate<>(cookie1)).thenRouteTo(serviceA).when((ctx, req) -> true).thenRouteTo(fallbackService).buildStreaming();
    when(headers.getCookiesIterator("session")).then(answerIteratorOf(cookie1));
    assertSame(responseA, service.handle(ctx, request, reqRespFactory));
    when(headers.getCookiesIterator("session")).then(answerIteratorOf(cookie2, cookie1));
    assertSame(responseA, service.handle(ctx, request, reqRespFactory));
    when(headers.getCookiesIterator("session")).then(answerIteratorOf(cookie2));
    assertSame(fallbackResponse, service.handle(ctx, request, reqRespFactory));
    when(headers.getCookiesIterator("session")).thenReturn(emptyIterator());
    assertSame(fallbackResponse, service.handle(ctx, request, reqRespFactory));
}
Also used : StreamingHttpService(io.servicetalk.http.api.StreamingHttpService) Test(org.junit.jupiter.api.Test)

Example 23 with StreamingHttpService

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

the class HttpPredicateRouterBuilderHeaderTest method testWhenHeaderFirstValue.

@Test
void testWhenHeaderFirstValue() {
    final StreamingHttpService service = new HttpPredicateRouterBuilder().whenHeader("host").firstValue("localhost").thenRouteTo(serviceA).when((ctx, req) -> true).thenRouteTo(fallbackService).buildStreaming();
    when(headers.valuesIterator("host")).then(answerIteratorOf("localhost"));
    assertSame(responseA, service.handle(ctx, request, reqRespFactory));
    when(headers.valuesIterator("host")).then(answerIteratorOf("localhost", "127.0.0.1"));
    assertSame(responseA, service.handle(ctx, request, reqRespFactory));
    when(headers.valuesIterator("host")).then(answerIteratorOf("127.0.0.1", "localhost"));
    assertSame(fallbackResponse, service.handle(ctx, request, reqRespFactory));
    when(headers.valuesIterator("host")).then(answerIteratorOf("127.0.0.1"));
    assertSame(fallbackResponse, service.handle(ctx, request, reqRespFactory));
    when(headers.valuesIterator("host")).thenReturn(emptyIterator());
    assertSame(fallbackResponse, service.handle(ctx, request, reqRespFactory));
}
Also used : StreamingHttpService(io.servicetalk.http.api.StreamingHttpService) Test(org.junit.jupiter.api.Test)

Example 24 with StreamingHttpService

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

the class HttpPredicateRouterBuilderHeaderTest method testWhenHeaderFirstValueMatchesPattern.

@Test
void testWhenHeaderFirstValueMatchesPattern() {
    final StreamingHttpService service = new HttpPredicateRouterBuilder().whenHeader("host").firstValueMatches(Pattern.compile("127\\..*", CASE_INSENSITIVE)).thenRouteTo(serviceA).when((ctx, req) -> true).thenRouteTo(fallbackService).buildStreaming();
    when(headers.valuesIterator("host")).then(answerIteratorOf("127.0.0.1"));
    assertSame(responseA, service.handle(ctx, request, reqRespFactory));
    when(headers.valuesIterator("host")).then(answerIteratorOf("127.0.0.1", "localhost"));
    assertSame(responseA, service.handle(ctx, request, reqRespFactory));
    when(headers.valuesIterator("host")).then(answerIteratorOf("localhost", "127.0.0.1"));
    assertSame(fallbackResponse, service.handle(ctx, request, reqRespFactory));
    when(headers.valuesIterator("host")).then(answerIteratorOf("localhost"));
    assertSame(fallbackResponse, service.handle(ctx, request, reqRespFactory));
}
Also used : StreamingHttpService(io.servicetalk.http.api.StreamingHttpService) Test(org.junit.jupiter.api.Test)

Example 25 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)53 Test (org.junit.jupiter.api.Test)38 ServerContext (io.servicetalk.transport.api.ServerContext)11 BlockingHttpClient (io.servicetalk.http.api.BlockingHttpClient)9 StreamingHttpRequest (io.servicetalk.http.api.StreamingHttpRequest)9 StreamingHttpResponse (io.servicetalk.http.api.StreamingHttpResponse)9 HttpExecutionStrategy (io.servicetalk.http.api.HttpExecutionStrategy)8 HttpServiceContext (io.servicetalk.http.api.HttpServiceContext)7 Single (io.servicetalk.concurrent.api.Single)6 HttpResponse (io.servicetalk.http.api.HttpResponse)6 StreamingHttpResponseFactory (io.servicetalk.http.api.StreamingHttpResponseFactory)6 Single.succeeded (io.servicetalk.concurrent.api.Single.succeeded)5 BlockingStreamingHttpService (io.servicetalk.http.api.BlockingStreamingHttpService)5 HttpApiConversions.toStreamingHttpService (io.servicetalk.http.api.HttpApiConversions.toStreamingHttpService)5 StreamingHttpClient (io.servicetalk.http.api.StreamingHttpClient)5 StreamingHttpServiceFilter (io.servicetalk.http.api.StreamingHttpServiceFilter)5 InOrder (org.mockito.InOrder)5 HttpExecutionContext (io.servicetalk.http.api.HttpExecutionContext)4 HttpExecutionStrategies (io.servicetalk.http.api.HttpExecutionStrategies)4 HttpExecutionStrategies.defaultStrategy (io.servicetalk.http.api.HttpExecutionStrategies.defaultStrategy)4