Search in sources :

Example 81 with Routing

use of io.helidon.webserver.Routing in project helidon by oracle.

the class ClassPathContentHandlerTest method serveFromFiles.

@Test
public void serveFromFiles() throws Exception {
    Routing routing = Routing.builder().register("/some", StaticContentSupport.create("content")).build();
    // /some/root-a.txt
    TestResponse response = TestClient.create(routing).path("/some/root-a.txt").get();
    assertThat(response.status(), is(Http.Status.OK_200));
    assertThat(filterResponse(response), is("- root A TXT"));
    assertThat(response.headers().first(Http.Header.CONTENT_TYPE), is(Optional.of(MediaType.TEXT_PLAIN.toString())));
    // /some/bar/root-a.txt
    response = TestClient.create(routing).path("/some/bar/root-b.txt").get();
    assertThat(response.status(), is(Http.Status.OK_200));
    assertThat(filterResponse(response), is("- root B TXT"));
    assertThat(response.headers().first(Http.Header.CONTENT_TYPE), is(Optional.of(MediaType.TEXT_PLAIN.toString())));
    // /some/bar/not.exist
    response = TestClient.create(routing).path("/some/bar/not.exist").get();
    assertThat(response.status(), is(Http.Status.NOT_FOUND_404));
}
Also used : Routing(io.helidon.webserver.Routing) Test(org.junit.jupiter.api.Test)

Example 82 with Routing

use of io.helidon.webserver.Routing in project helidon by oracle.

the class ClassPathContentHandlerTest method serveFromJar.

@Test
public void serveFromJar() throws Exception {
    Routing routing = Routing.builder().register("/some", StaticContentSupport.create("s-internal")).build();
    // /some/example-a.txt
    TestResponse response = TestClient.create(routing).path("/some/example-a.txt").get();
    assertThat(response.status(), is(Http.Status.OK_200));
    assertThat(filterResponse(response), is("Example A TXT"));
    assertThat(response.headers().first(Http.Header.CONTENT_TYPE), is(Optional.of(MediaType.TEXT_PLAIN.toString())));
    // /some/example-a.txt
    response = TestClient.create(routing).path("/some/a/example-a.txt").get();
    assertThat(response.status(), is(Http.Status.OK_200));
    assertThat(filterResponse(response), is("A / Example A TXT"));
    assertThat(response.headers().first(Http.Header.CONTENT_TYPE), is(Optional.of(MediaType.TEXT_PLAIN.toString())));
    // /some/a/not.exist
    response = TestClient.create(routing).path("/some/a/not.exist").get();
    assertThat(response.status(), is(Http.Status.NOT_FOUND_404));
}
Also used : Routing(io.helidon.webserver.Routing) Test(org.junit.jupiter.api.Test)

Example 83 with Routing

use of io.helidon.webserver.Routing in project helidon by oracle.

the class FileSystemContentHandlerTest method serveFile.

@Test
public void serveFile() throws Exception {
    try {
        Routing routing = Routing.builder().register("/some", StaticContentSupport.create(folder.root().toPath())).build();
        // /some/foo.txt
        TestResponse response = TestClient.create(routing).path("/some/foo.txt").get();
        assertThat(response.status(), is(Http.Status.OK_200));
        assertThat(responseToString(response), is("Foo TXT"));
        assertThat(response.headers().first(Http.Header.CONTENT_TYPE).orElse(null), is(MediaType.TEXT_PLAIN.toString()));
        // /some/css/b.css
        response = TestClient.create(routing).path("/some/css/b.css").get();
        assertThat(response.status(), is(Http.Status.OK_200));
        assertThat(responseToString(response), is("B CSS"));
        // /some/css/not.exists
        response = TestClient.create(routing).path("/some/css/not.exists").get();
        assertThat(response.status(), is(Http.Status.NOT_FOUND_404));
        // /some/css
        response = TestClient.create(routing).path("/some/css").get();
        assertThat(response.status(), is(Http.Status.MOVED_PERMANENTLY_301));
        assertThat(response.headers().first(Http.Header.LOCATION).orElse(null), is("/some/css/"));
        // /some/css/
        response = TestClient.create(routing).path("/some/css/").get();
        assertThat(response.status(), is(Http.Status.NOT_FOUND_404));
    } catch (Throwable ex) {
        ex.printStackTrace();
    }
}
Also used : Routing(io.helidon.webserver.Routing) Test(org.junit.jupiter.api.Test)

Example 84 with Routing

use of io.helidon.webserver.Routing in project helidon by oracle.

the class TestClientTest method multipleHandlers.

@Test
public void multipleHandlers() throws Exception {
    StringBuffer sb = new StringBuffer();
    Routing routing = Routing.builder().get("/foo", (req, res) -> {
        sb.append("foo-get");
        res.send();
    }).post("/foo", (req, res) -> {
        sb.append("foo-post");
        res.send();
    }).put("/foo", (req, res) -> {
        sb.append("foo-put");
        res.send();
    }).put("/foo", (req, res) -> {
        sb.append("foo-putb");
        res.send();
    }).get("/foo/bar", (req, res) -> {
        sb.append("foo/bar-get");
        res.send();
    }).build();
    TestResponse response = TestClient.create(routing).path("/foo").get();
    assertThat(sb.toString(), is("foo-get"));
    sb.setLength(0);
    response = TestClient.create(routing).path("/foo").post();
    assertThat(sb.toString(), is("foo-post"));
    sb.setLength(0);
    response = TestClient.create(routing).path("/foo").put();
    assertThat(sb.toString(), is("foo-put"));
    sb.setLength(0);
    response = TestClient.create(routing).path("/foo/bar").get();
    assertThat(sb.toString(), is("foo/bar-get"));
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) CoreMatchers.is(org.hamcrest.CoreMatchers.is) Assertions.fail(org.junit.jupiter.api.Assertions.fail) CountDownLatch(java.util.concurrent.CountDownLatch) NotFoundException(io.helidon.webserver.NotFoundException) HttpException(io.helidon.webserver.HttpException) SERVICE_UNAVAILABLE_503(io.helidon.common.http.Http.Status.SERVICE_UNAVAILABLE_503) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Http(io.helidon.common.http.Http) Routing(io.helidon.webserver.Routing) Routing(io.helidon.webserver.Routing) Test(org.junit.jupiter.api.Test)

Example 85 with Routing

use of io.helidon.webserver.Routing in project helidon by oracle.

the class StaticContentTest method setupRouting.

@BeforeAll
static void setupRouting() {
    Routing routing = Routing.builder().register("/classpath", StaticContentSupport.builder("web")).build();
    testClient = TestClient.create(routing);
}
Also used : Routing(io.helidon.webserver.Routing) BeforeAll(org.junit.jupiter.api.BeforeAll)

Aggregations

Routing (io.helidon.webserver.Routing)86 WebServer (io.helidon.webserver.WebServer)38 Config (io.helidon.config.Config)33 Test (org.junit.jupiter.api.Test)32 Http (io.helidon.common.http.Http)23 TimeUnit (java.util.concurrent.TimeUnit)21 LogConfig (io.helidon.common.LogConfig)19 MediaType (io.helidon.common.http.MediaType)19 CoreMatchers.is (org.hamcrest.CoreMatchers.is)17 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)17 SecurityContext (io.helidon.security.SecurityContext)16 HttpException (io.helidon.webserver.HttpException)15 Optional (java.util.Optional)15 CountDownLatch (java.util.concurrent.CountDownLatch)15 WebSecurity (io.helidon.security.integration.webserver.WebSecurity)13 SERVICE_UNAVAILABLE_503 (io.helidon.common.http.Http.Status.SERVICE_UNAVAILABLE_503)11 JsonpSupport (io.helidon.media.jsonp.JsonpSupport)11 Security (io.helidon.security.Security)11 StaticContentSupport (io.helidon.webserver.staticcontent.StaticContentSupport)10 TestResponse (io.helidon.webserver.testsupport.TestResponse)10