Search in sources :

Example 11 with TestResponse

use of io.helidon.webserver.testsupport.TestResponse in project helidon by oracle.

the class PrometheusSupportTest method doubleCall.

@Test
public void doubleCall() throws Exception {
    TestResponse response = doTestRequest(null);
    assertThat(response.headers().first(Http.Header.CONTENT_TYPE).orElse(null), StringStartsWith.startsWith("text/plain"));
    String body = response.asString().get(5, TimeUnit.SECONDS);
    assertThat(body, containsString("alpha{method=\"bar\",} 6.0"));
    assertThat(body, not(containsString("alpha{method=\"baz\"")));
    alpha.labels("baz").inc();
    response = doTestRequest(null);
    body = response.asString().get(5, TimeUnit.SECONDS);
    assertThat(body, containsString("alpha{method=\"baz\",} 1.0"));
}
Also used : TestResponse(io.helidon.webserver.testsupport.TestResponse) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.jupiter.api.Test)

Example 12 with TestResponse

use of io.helidon.webserver.testsupport.TestResponse in project helidon by oracle.

the class PrometheusSupportTest method simpleCall.

@Test
public void simpleCall() throws Exception {
    TestResponse response = doTestRequest(null);
    assertThat(response.headers().first(Http.Header.CONTENT_TYPE).orElse(null), StringStartsWith.startsWith("text/plain"));
    String body = response.asString().get(5, TimeUnit.SECONDS);
    assertThat(body, containsString("# HELP beta"));
    assertThat(body, containsString("# TYPE beta counter"));
    assertThat(body, containsString("beta 3.0"));
    assertThat(body, containsString("# TYPE alpha counter"));
    assertThat(body, containsString("# HELP alpha Alpha help with \\\\ and \\n."));
    assertThat(body, containsString("alpha{method=\"bar\",} 6.0"));
    assertThat(body, containsString("alpha{method=\"\\\"foo\\\" \\\\ \\n\",} 5.0"));
}
Also used : TestResponse(io.helidon.webserver.testsupport.TestResponse) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.jupiter.api.Test)

Example 13 with TestResponse

use of io.helidon.webserver.testsupport.TestResponse in project helidon by oracle.

the class CommentServiceTest method testRouting.

@Test
public void testRouting() throws Exception {
    Routing routing = Routing.builder().register(new CommentService()).build();
    TestResponse response = TestClient.create(routing).path("one").get();
    assertEquals(Http.Status.OK_200, response.status());
    // Add first comment
    response = TestClient.create(routing).path("one").post(MediaPublisher.create(MediaType.TEXT_PLAIN, "aaa"));
    assertEquals(Http.Status.OK_200, response.status());
    response = TestClient.create(routing).path("one").get();
    assertEquals(Http.Status.OK_200, response.status());
    byte[] data = response.asBytes().toCompletableFuture().get();
    assertEquals("anonymous: aaa\n", new String(data, StandardCharsets.UTF_8));
    // Add second comment
    response = TestClient.create(routing).path("one").post(MediaPublisher.create(MediaType.TEXT_PLAIN, "bbb"));
    assertEquals(Http.Status.OK_200, response.status());
    response = TestClient.create(routing).path("one").get();
    assertEquals(Http.Status.OK_200, response.status());
    data = response.asBytes().toCompletableFuture().get();
    assertEquals("anonymous: aaa\nanonymous: bbb\n", new String(data, StandardCharsets.UTF_8));
}
Also used : TestResponse(io.helidon.webserver.testsupport.TestResponse) Routing(io.helidon.webserver.Routing) Test(org.junit.jupiter.api.Test)

Example 14 with TestResponse

use of io.helidon.webserver.testsupport.TestResponse in project helidon by oracle.

the class MainTest method testShutDown.

@Test
public void testShutDown() throws Exception {
    TestResponse response = TestClient.create(Main.createRouting()).path("/mgmt/shutdown").post();
    assertEquals(Http.Status.OK_200, response.status());
    CountDownLatch latch = new CountDownLatch(1);
    WebServer webServer = response.webServer();
    webServer.whenShutdown().thenRun(latch::countDown);
    assertTrue(latch.await(10, TimeUnit.SECONDS));
}
Also used : TestResponse(io.helidon.webserver.testsupport.TestResponse) WebServer(io.helidon.webserver.WebServer) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 15 with TestResponse

use of io.helidon.webserver.testsupport.TestResponse in project helidon by oracle.

the class UserFilterTest method filter.

@Test
public void filter() throws Exception {
    AtomicReference<User> userReference = new AtomicReference<>();
    Routing routing = Routing.builder().any(new UserFilter()).any((req, res) -> {
        userReference.set(req.context().get(User.class).orElse(null));
        res.send();
    }).build();
    TestResponse response = TestClient.create(routing).path("/").get();
    assertEquals(User.ANONYMOUS, userReference.get());
    response = TestClient.create(routing).path("/").header("Cookie", "Unauthenticated-User-Alias=Foo").get();
    assertEquals("Foo", userReference.get().getAlias());
}
Also used : Test(org.junit.jupiter.api.Test) TestResponse(io.helidon.webserver.testsupport.TestResponse) TestClient(io.helidon.webserver.testsupport.TestClient) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Routing(io.helidon.webserver.Routing) AtomicReference(java.util.concurrent.atomic.AtomicReference) TestResponse(io.helidon.webserver.testsupport.TestResponse) Routing(io.helidon.webserver.Routing) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.jupiter.api.Test)

Aggregations

TestResponse (io.helidon.webserver.testsupport.TestResponse)26 Test (org.junit.jupiter.api.Test)25 Routing (io.helidon.webserver.Routing)10 JsonObject (jakarta.json.JsonObject)4 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)3 TestClient (io.helidon.webserver.testsupport.TestClient)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 GenericType (io.helidon.common.GenericType)1 Http (io.helidon.common.http.Http)1 MediaType (io.helidon.common.http.MediaType)1 Handler (io.helidon.webserver.Handler)1 WebServer (io.helidon.webserver.WebServer)1 MediaPublisher (io.helidon.webserver.testsupport.MediaPublisher)1 TestRequest (io.helidon.webserver.testsupport.TestRequest)1 List (java.util.List)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 TimeUnit (java.util.concurrent.TimeUnit)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 CoreMatchers.is (org.hamcrest.CoreMatchers.is)1 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)1