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) {
}
}
}
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) {
}
}
}
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();
}
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) {
}
}
}
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));
}
}
Aggregations