Search in sources :

Example 1 with RoutingChecker

use of io.helidon.webserver.RoutingTest.RoutingChecker in project helidon by oracle.

the class RequestPredicateTest method containsExactCookie.

@Test
public void containsExactCookie() {
    final RoutingChecker checker = new RoutingChecker();
    Routing routing = Routing.builder().get("/equals", RequestPredicate.create().containsCookie("my-cookie", "abc").thenApply((req, resp) -> {
        checker.handlerInvoked("cookieIsEqual");
    }).otherwise((req, res) -> {
        checker.handlerInvoked("cookieIsNotEqual");
    })).build();
    assertThrows(NullPointerException.class, () -> {
        RequestPredicate.create().containsCookie("my-cookie", (String) null);
    });
    assertThrows(NullPointerException.class, () -> {
        RequestPredicate.create().containsCookie(null, "abc");
    });
    routing.route(mockRequest("/equals", Map.of("cookie", List.of("my-cookie=abc"))), mockResponse());
    assertThat(checker.handlersInvoked(), is("cookieIsEqual"));
    checker.reset();
    routing.route(mockRequest("/equals", Map.of("cookie", List.of("my-cookie=def"))), mockResponse());
    assertThat(checker.handlersInvoked(), is("cookieIsNotEqual"));
}
Also used : Test(org.junit.jupiter.api.Test) CoreMatchers.is(org.hamcrest.CoreMatchers.is) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Mockito(org.mockito.Mockito) RoutingTest.mockResponse(io.helidon.webserver.RoutingTest.mockResponse) List(java.util.List) Predicate(java.util.function.Predicate) Map(java.util.Map) RoutingChecker(io.helidon.webserver.RoutingTest.RoutingChecker) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Http(io.helidon.common.http.Http) MediaType(io.helidon.common.http.MediaType) RoutingChecker(io.helidon.webserver.RoutingTest.RoutingChecker) Test(org.junit.jupiter.api.Test)

Example 2 with RoutingChecker

use of io.helidon.webserver.RoutingTest.RoutingChecker in project helidon by oracle.

the class RequestPredicateTest method multipleConditions.

@Test
public void multipleConditions() {
    final RoutingChecker checker = new RoutingChecker();
    Routing routing = Routing.builder().any("/multiple", RequestPredicate.create().accepts(MediaType.TEXT_PLAIN).hasContentType(MediaType.TEXT_PLAIN).containsQueryParameter("my-param").containsCookie("my-cookie").isOfMethod(Http.Method.GET).thenApply((req, resp) -> {
        checker.handlerInvoked("hasAllConditions");
    }).otherwise((req, res) -> {
        checker.handlerInvoked("doesNotHaveAllConditions");
    })).build();
    routing.route(mockRequest("/multiple?my-param=abc", Map.of("Content-Type", List.of("text/plain"), "Accept", List.of("text/plain"), "Cookie", List.of("my-cookie=abc"))), mockResponse());
    assertThat(checker.handlersInvoked(), is("hasAllConditions"));
    checker.reset();
    routing.route(mockRequest("/multiple?my-param=abc", Map.of("Accept", List.of("text/plain"), "Cookie", List.of("my-cookie=abc"))), mockResponse());
    assertThat(checker.handlersInvoked(), is("doesNotHaveAllConditions"));
}
Also used : Test(org.junit.jupiter.api.Test) CoreMatchers.is(org.hamcrest.CoreMatchers.is) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Mockito(org.mockito.Mockito) RoutingTest.mockResponse(io.helidon.webserver.RoutingTest.mockResponse) List(java.util.List) Predicate(java.util.function.Predicate) Map(java.util.Map) RoutingChecker(io.helidon.webserver.RoutingTest.RoutingChecker) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Http(io.helidon.common.http.Http) MediaType(io.helidon.common.http.MediaType) RoutingChecker(io.helidon.webserver.RoutingTest.RoutingChecker) Test(org.junit.jupiter.api.Test)

Example 3 with RoutingChecker

use of io.helidon.webserver.RoutingTest.RoutingChecker in project helidon by oracle.

the class RequestPredicateTest method accepts1.

@Test
public void accepts1() {
    final RoutingChecker checker = new RoutingChecker();
    Routing routing = Routing.builder().get("/accepts1", RequestPredicate.create().accepts("text/plain", "application/json").thenApply((req, resp) -> {
        checker.handlerInvoked("acceptsMediaType");
    }).otherwise((req, res) -> {
        checker.handlerInvoked("doesNotAcceptMediaType");
    })).build();
    assertThrows(NullPointerException.class, () -> {
        RequestPredicate.create().accepts((String[]) null);
    });
    routing.route(mockRequest("/accepts1", Map.of("Accept", List.of("application/json"))), mockResponse());
    assertThat(checker.handlersInvoked(), is("acceptsMediaType"));
    checker.reset();
    routing.route(mockRequest("/accepts1", Map.of("Accept", List.of("text/plain"))), mockResponse());
    assertThat(checker.handlersInvoked(), is("acceptsMediaType"));
    checker.reset();
    routing.route(mockRequest("/accepts1", Map.of("Accept", List.of("text/plain", "application/xml"))), mockResponse());
    assertThat(checker.handlersInvoked(), is("acceptsMediaType"));
    checker.reset();
    routing.route(mockRequest("/accepts1", Map.of("Accept", List.of())), mockResponse());
    assertThat(checker.handlersInvoked(), is("acceptsMediaType"));
    checker.reset();
    routing.route(mockRequest("/accepts1", Map.of()), mockResponse());
    assertThat(checker.handlersInvoked(), is("acceptsMediaType"));
    checker.reset();
    routing.route(mockRequest("/accepts1", Map.of("Accept", List.of("application/xml"))), mockResponse());
    assertThat(checker.handlersInvoked(), is("doesNotAcceptMediaType"));
}
Also used : Test(org.junit.jupiter.api.Test) CoreMatchers.is(org.hamcrest.CoreMatchers.is) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Mockito(org.mockito.Mockito) RoutingTest.mockResponse(io.helidon.webserver.RoutingTest.mockResponse) List(java.util.List) Predicate(java.util.function.Predicate) Map(java.util.Map) RoutingChecker(io.helidon.webserver.RoutingTest.RoutingChecker) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Http(io.helidon.common.http.Http) MediaType(io.helidon.common.http.MediaType) RoutingChecker(io.helidon.webserver.RoutingTest.RoutingChecker) Test(org.junit.jupiter.api.Test)

Example 4 with RoutingChecker

use of io.helidon.webserver.RoutingTest.RoutingChecker in project helidon by oracle.

the class RequestPredicateTest method containsExactHeader.

@Test
public void containsExactHeader() {
    final RoutingChecker checker = new RoutingChecker();
    Routing routing = Routing.builder().get("/equals", RequestPredicate.create().containsHeader("my-header", "abc").thenApply((req, resp) -> {
        checker.handlerInvoked("headerIsEqual");
    }).otherwise((req, res) -> {
        checker.handlerInvoked("headerIsNotEqual");
    })).build();
    assertThrows(NullPointerException.class, () -> {
        RequestPredicate.create().containsHeader("my-header", (String) null);
    });
    assertThrows(NullPointerException.class, () -> {
        RequestPredicate.create().containsHeader(null, "abc");
    });
    routing.route(mockRequest("/equals", Map.of("my-header", List.of("abc"))), mockResponse());
    assertThat(checker.handlersInvoked(), is("headerIsEqual"));
    checker.reset();
    routing.route(mockRequest("/equals", Map.of("my-header", List.of("def"))), mockResponse());
    assertThat(checker.handlersInvoked(), is("headerIsNotEqual"));
}
Also used : Test(org.junit.jupiter.api.Test) CoreMatchers.is(org.hamcrest.CoreMatchers.is) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Mockito(org.mockito.Mockito) RoutingTest.mockResponse(io.helidon.webserver.RoutingTest.mockResponse) List(java.util.List) Predicate(java.util.function.Predicate) Map(java.util.Map) RoutingChecker(io.helidon.webserver.RoutingTest.RoutingChecker) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Http(io.helidon.common.http.Http) MediaType(io.helidon.common.http.MediaType) RoutingChecker(io.helidon.webserver.RoutingTest.RoutingChecker) Test(org.junit.jupiter.api.Test)

Example 5 with RoutingChecker

use of io.helidon.webserver.RoutingTest.RoutingChecker in project helidon by oracle.

the class RequestPredicateTest method isOfMethod1.

@Test
public void isOfMethod1() {
    final RoutingChecker checker = new RoutingChecker();
    Routing routing = Routing.builder().any("/getOrPost", RequestPredicate.create().isOfMethod("GET", "POST").thenApply((req, resp) -> {
        checker.handlerInvoked("methodFound");
    }).otherwise((req, res) -> {
        checker.handlerInvoked("methodNotFound");
    })).build();
    assertThrows(NullPointerException.class, () -> {
        RequestPredicate.create().isOfMethod((String[]) null);
    });
    routing.route(mockRequest("/getOrPost"), mockResponse());
    assertThat(checker.handlersInvoked(), is("methodFound"));
    checker.reset();
    routing.route(mockRequest("/getOrPost", Http.Method.PUT), mockResponse());
    assertThat(checker.handlersInvoked(), is("methodNotFound"));
}
Also used : Test(org.junit.jupiter.api.Test) CoreMatchers.is(org.hamcrest.CoreMatchers.is) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Mockito(org.mockito.Mockito) RoutingTest.mockResponse(io.helidon.webserver.RoutingTest.mockResponse) List(java.util.List) Predicate(java.util.function.Predicate) Map(java.util.Map) RoutingChecker(io.helidon.webserver.RoutingTest.RoutingChecker) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Http(io.helidon.common.http.Http) MediaType(io.helidon.common.http.MediaType) RoutingChecker(io.helidon.webserver.RoutingTest.RoutingChecker) Test(org.junit.jupiter.api.Test)

Aggregations

Http (io.helidon.common.http.Http)19 MediaType (io.helidon.common.http.MediaType)19 RoutingChecker (io.helidon.webserver.RoutingTest.RoutingChecker)19 RoutingTest.mockResponse (io.helidon.webserver.RoutingTest.mockResponse)19 List (java.util.List)19 Map (java.util.Map)19 Predicate (java.util.function.Predicate)19 CoreMatchers.is (org.hamcrest.CoreMatchers.is)19 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)19 Assertions.assertThrows (org.junit.jupiter.api.Assertions.assertThrows)19 Test (org.junit.jupiter.api.Test)19 Mockito (org.mockito.Mockito)19