Search in sources :

Example 46 with Undertow

use of io.undertow.Undertow in project undertow by undertow-io.

the class URLDecodingHandlerTestCase method testDecodesWhenUrlDecodingIsDisabled.

@Test
public void testDecodesWhenUrlDecodingIsDisabled() throws Exception {
    // When UndertowOptions.DECODE_URL is disabled, the URLDecodingHandler should decode values.
    Undertow undertow = Undertow.builder().setServerOption(UndertowOptions.DECODE_URL, false).addHttpListener(PORT, "0.0.0.0").setHandler(new URLDecodingHandler(new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            exchange.getResponseSender().send(exchange.getRelativePath());
        }
    }, "UTF-8")).build();
    undertow.start();
    try {
        TestHttpClient client = new TestHttpClient();
        // '%253E' decodes to '%3E', which would decode to '>' if decoded twice
        try (CloseableHttpResponse response = client.execute(new HttpGet("http://localhost:" + PORT + "/%253E"))) {
            Assert.assertEquals("/%3E", getResponseString(response));
        }
    } finally {
        undertow.stop();
        // sleep 1 s to prevent BindException (Address already in use) when restarting the server
        try {
            Thread.sleep(1000);
        } catch (InterruptedException ignore) {
        }
    }
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Undertow(io.undertow.Undertow) IOException(java.io.IOException) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 47 with Undertow

use of io.undertow.Undertow in project undertow by undertow-io.

the class URLDecodingHandlerTestCase method testMultipleURLDecodingHandlers.

@Test
public void testMultipleURLDecodingHandlers() throws Exception {
    // When multiple URLDecodingHandler are present, only the first handler to consume an exchange should decode
    Undertow undertow = Undertow.builder().setServerOption(UndertowOptions.DECODE_URL, false).addHttpListener(PORT, "0.0.0.0").setHandler(new URLDecodingHandler(new URLDecodingHandler(new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            exchange.getResponseSender().send(exchange.getRelativePath());
        }
    }, "UTF-8"), "UTF-8")).build();
    undertow.start();
    try {
        TestHttpClient client = new TestHttpClient();
        // '%253E' decodes to '%3E', which would decode to '>' if decoded twice
        try (CloseableHttpResponse response = client.execute(new HttpGet("http://localhost:" + PORT + "/%253E"))) {
            Assert.assertEquals("/%3E", getResponseString(response));
        }
    } finally {
        undertow.stop();
        // sleep 1 s to prevent BindException (Address already in use) when restarting the server
        try {
            Thread.sleep(1000);
        } catch (InterruptedException ignore) {
        }
    }
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Undertow(io.undertow.Undertow) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 48 with Undertow

use of io.undertow.Undertow in project undertow by undertow-io.

the class AbstractModClusterTestBase method createNode.

static Undertow createNode(final NodeTestConfig config) {
    final Undertow.Builder builder = Undertow.builder();
    final String type = config.getType();
    switch(type) {
        case "ajp":
            builder.addAjpListener(config.getPort(), config.getHostname());
            break;
        case "http":
            builder.addHttpListener(config.getPort(), config.getHostname());
            break;
        case "https":
            builder.addHttpsListener(config.getPort(), config.getHostname(), DefaultServer.getServerSslContext());
            break;
        default:
            throw new IllegalArgumentException(type);
    }
    final SessionCookieConfig sessionConfig = new SessionCookieConfig();
    if (config.getStickySessionCookie() != null) {
        sessionConfig.setCookieName(config.getStickySessionCookie());
    }
    final PathHandler pathHandler = path(ResponseCodeHandler.HANDLE_200).addPrefixPath("/name", new StringSendHandler(config.getJvmRoute())).addPrefixPath("/session", new SessionAttachmentHandler(new SessionTestHandler(config.getJvmRoute(), sessionConfig), new InMemorySessionManager(""), sessionConfig));
    // Setup test handlers
    config.setupHandlers(pathHandler);
    builder.setSocketOption(Options.REUSE_ADDRESSES, true).setHandler(jvmRoute("JSESSIONID", config.getJvmRoute(), pathHandler));
    return builder.build();
}
Also used : SessionAttachmentHandler(io.undertow.server.session.SessionAttachmentHandler) PathHandler(io.undertow.server.handlers.PathHandler) SessionCookieConfig(io.undertow.server.session.SessionCookieConfig) Undertow(io.undertow.Undertow) InMemorySessionManager(io.undertow.server.session.InMemorySessionManager)

Example 49 with Undertow

use of io.undertow.Undertow in project undertow by undertow-io.

the class DelegatedTaskExecutorTestCase method testRejection.

@Test
public void testRejection() {
    Undertow undertow = Undertow.builder().addHttpsListener(0, null, DefaultServer.getServerSslContext()).setSslEngineDelegatedTaskExecutor(ignoredTask -> {
        throw new RejectedExecutionException();
    }).setHandler(ResponseCodeHandler.HANDLE_200).build();
    TestHttpClient client = new TestHttpClient();
    client.setSSLContext(DefaultServer.getClientSSLContext());
    undertow.start();
    try {
        int port = port(undertow);
        HttpGet request = new HttpGet("https://localhost:" + port);
        try {
            client.execute(request);
            fail("Expected an exception");
        } catch (SSLHandshakeException handshakeException) {
            // expected one of:
            // - Remote host closed connection during handshake
            // - Remote host terminated the handshake
            // This exception comes from the jvm and may change in future
            // releases so we don't verify an exact match.
            String message = handshakeException.getMessage();
            System.out.println(message);
            assertTrue("message was: " + message, message != null && (message.contains("closed") || message.contains("terminated")));
        } catch (IOException e) {
            throw new AssertionError(e);
        }
    } finally {
        undertow.stop();
        client.getConnectionManager().shutdown();
        // sleep 1 s to prevent BindException (Address already in use) when running the CI
        try {
            Thread.sleep(1000);
        } catch (InterruptedException ignore) {
        }
    }
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) IOException(java.io.IOException) Undertow(io.undertow.Undertow) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 50 with Undertow

use of io.undertow.Undertow in project undertow by undertow-io.

the class DelegatedTaskExecutorTestCase method testDelegatedTaskExecutorIsUsed.

@Test
public void testDelegatedTaskExecutorIsUsed() throws Exception {
    ExecutorService delegatedTaskExecutor = Executors.newSingleThreadExecutor();
    AtomicInteger counter = new AtomicInteger();
    Undertow undertow = Undertow.builder().addHttpsListener(0, null, DefaultServer.getServerSslContext()).setSslEngineDelegatedTaskExecutor(task -> {
        counter.getAndIncrement();
        delegatedTaskExecutor.execute(task);
    }).setHandler(ResponseCodeHandler.HANDLE_200).build();
    TestHttpClient client = new TestHttpClient();
    client.setSSLContext(DefaultServer.getClientSSLContext());
    undertow.start();
    int port = port(undertow);
    try (CloseableHttpResponse response = client.execute(new HttpGet("https://localhost:" + port))) {
        assertEquals(200, response.getStatusLine().getStatusCode());
        assertTrue("expected interactions with the delegated task executor", counter.get() > 0);
    } finally {
        undertow.stop();
        client.getConnectionManager().shutdown();
        List<Runnable> tasks = delegatedTaskExecutor.shutdownNow();
        for (Runnable task : tasks) {
            task.run();
        }
        assertTrue("ExecutorService did not shut down in time", delegatedTaskExecutor.awaitTermination(1, TimeUnit.SECONDS));
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpGet(org.apache.http.client.methods.HttpGet) ExecutorService(java.util.concurrent.ExecutorService) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Undertow(io.undertow.Undertow) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Aggregations

Undertow (io.undertow.Undertow)55 HttpHandler (io.undertow.server.HttpHandler)27 HttpServerExchange (io.undertow.server.HttpServerExchange)22 PathHandler (io.undertow.server.handlers.PathHandler)16 IOException (java.io.IOException)13 Test (org.junit.Test)11 ClassPathResourceManager (io.undertow.server.handlers.resource.ClassPathResourceManager)10 DeploymentInfo (io.undertow.servlet.api.DeploymentInfo)10 DeploymentManager (io.undertow.servlet.api.DeploymentManager)9 URI (java.net.URI)9 TestHttpClient (io.undertow.testutils.TestHttpClient)6 ServletException (javax.servlet.ServletException)6 HttpGet (org.apache.http.client.methods.HttpGet)6 LoadBalancingProxyClient (io.undertow.server.handlers.proxy.LoadBalancingProxyClient)5 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)5 XnioWorker (org.xnio.XnioWorker)5 PathResourceManager (io.undertow.server.handlers.resource.PathResourceManager)4 InMemorySessionManager (io.undertow.server.session.InMemorySessionManager)4 SessionAttachmentHandler (io.undertow.server.session.SessionAttachmentHandler)4 SessionCookieConfig (io.undertow.server.session.SessionCookieConfig)4