use of io.undertow.server.HttpServerExchange in project undertow by undertow-io.
the class SameSiteCookieHandlerTestCase method testNoneUACheckerEnabledAlthoughUAHeaderNotSet.
@Test
public void testNoneUACheckerEnabledAlthoughUAHeaderNotSet() throws IOException {
DefaultServer.setRootHandler(new SameSiteCookieHandler(new HttpHandler() {
@Override
public void handleRequest(final HttpServerExchange exchange) {
exchange.setResponseCookie(new CookieImpl("foo", "bar"));
}
}, "None", "foo"));
DefaultServer.startSSLServer();
TestHttpClient client = new TestHttpClient() {
// Here we need to get client instance that does not set ANY User-Agent header by default.
@Override
protected HttpParams createHttpParams() {
HttpParams params = super.createHttpParams();
params.removeParameter(CoreProtocolPNames.USER_AGENT);
HttpConnectionParams.setSoTimeout(params, 30000);
return params;
}
};
client.setSSLContext(DefaultServer.getClientSSLContext());
try {
HttpGet get = new HttpGet(DefaultServer.getDefaultServerSSLAddress());
// Don't set any User-Agent header
HttpResponse result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
Header header = result.getFirstHeader("set-cookie");
Assert.assertEquals("foo=bar; secure; SameSite=None", header.getValue());
FileUtils.readFile(result.getEntity().getContent());
} finally {
client.getConnectionManager().shutdown();
DefaultServer.stopSSLServer();
}
}
use of io.undertow.server.HttpServerExchange in project undertow by undertow-io.
the class SameSiteCookieHandlerTestCase method testAllCookies.
@Test
public void testAllCookies() throws IOException {
DefaultServer.setRootHandler(new SameSiteCookieHandler(new HttpHandler() {
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
exchange.setResponseCookie(new CookieImpl("foo", "bar"));
exchange.setResponseCookie(new CookieImpl("baz", "qux"));
exchange.setResponseCookie(new CookieImpl("test", "test"));
}
}, "Strict"));
DefaultServer.startSSLServer();
TestHttpClient client = new TestHttpClient();
client.setSSLContext(DefaultServer.getClientSSLContext());
try {
HttpGet get = new HttpGet(DefaultServer.getDefaultServerSSLAddress());
HttpResponse result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
Header[] headerArray = result.getHeaders("set-cookie");
for (Header h : headerArray) {
if (h.getValue().contains("foo")) {
Assert.assertEquals("foo=bar; SameSite=Strict", h.getValue());
}
if (h.getValue().contains("baz")) {
Assert.assertEquals("baz=qux; SameSite=Strict", h.getValue());
}
if (h.getValue().contains("test")) {
Assert.assertEquals("test=test; SameSite=Strict", h.getValue());
}
}
FileUtils.readFile(result.getEntity().getContent());
} finally {
client.getConnectionManager().shutdown();
DefaultServer.stopSSLServer();
}
}
use of io.undertow.server.HttpServerExchange in project undertow by undertow-io.
the class SameSiteCookieHandlerTestCase method testNoneIncompatibleUA.
@Test
public void testNoneIncompatibleUA() throws IOException {
DefaultServer.setRootHandler(new SameSiteCookieHandler(new HttpHandler() {
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
exchange.setResponseCookie(new CookieImpl("foo", "bar"));
}
}, "None", "foo"));
DefaultServer.startSSLServer();
TestHttpClient client = new TestHttpClient();
client.setSSLContext(DefaultServer.getClientSSLContext());
try {
HttpGet get = new HttpGet(DefaultServer.getDefaultServerSSLAddress());
// Chrome version whic is known to be incompatible with the `SameSite=None` attribute
get.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.62 Safari/537.36");
HttpResponse result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
Header header = result.getFirstHeader("set-cookie");
Assert.assertEquals("foo=bar", header.getValue());
FileUtils.readFile(result.getEntity().getContent());
} finally {
client.getConnectionManager().shutdown();
DefaultServer.stopSSLServer();
}
}
use of io.undertow.server.HttpServerExchange in project undertow by undertow-io.
the class BlockingWriteTimeoutHandlerTestCase method testWriteTimeout.
@Test
public void testWriteTimeout() throws InterruptedException {
DefaultServer.setRootHandler(BlockingWriteTimeoutHandler.builder().nextHandler(new BlockingHandler(new HttpHandler() {
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
// 1mb
final int capacity = 1 * 1024 * 1024;
final byte[] data = new byte[capacity];
for (int i = 0; i < capacity; ++i) {
data[i] = (byte) '*';
}
try {
// Must write enough data that it's not buffered
for (int i = 0; i < 20; i++) {
exchange.getOutputStream().write(data);
}
} catch (IOException e) {
exception = e;
errorLatch.countDown();
}
}
})).writeTimeout(Duration.ofMillis(1)).build());
final TestHttpClient client = new TestHttpClient();
try {
HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL());
try {
HttpResponse result = client.execute(get);
Assert.assertFalse("The result entity is buffered", result.getEntity().isRepeatable());
InputStream content = result.getEntity().getContent();
byte[] buffer = new byte[512];
int r = 0;
while ((r = content.read(buffer)) > 0) {
Thread.sleep(200);
if (exception != null) {
Assert.assertEquals(WriteTimeoutException.class, exception.getClass());
return;
}
}
Assert.fail("Write did not time out");
} catch (IOException e) {
if (errorLatch.await(5, TimeUnit.SECONDS)) {
Assert.assertEquals(WriteTimeoutException.class, exception.getClass());
} else {
Assert.fail("Write did not time out");
}
}
} finally {
client.getConnectionManager().shutdown();
}
}
use of io.undertow.server.HttpServerExchange in project undertow by undertow-io.
the class ExceptionHandlerTestCase method testExceptionMappers.
@Test
public void testExceptionMappers() throws IOException {
HttpHandler pathHandler = Handlers.path().addExactPath("/", new HttpHandler() {
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
exchange.getResponseSender().send("expected");
}
}).addExactPath("/exceptionParent", new HttpHandler() {
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
throw new ParentException();
}
}).addExactPath("/exceptionChild", new HttpHandler() {
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
throw new ChildException();
}
}).addExactPath("/exceptionAnotherChild", new HttpHandler() {
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
throw new AnotherChildException();
}
}).addExactPath("/illegalArgumentException", new HttpHandler() {
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
throw new IllegalArgumentException();
}
});
HttpHandler exceptionHandler = Handlers.exceptionHandler(pathHandler).addExceptionHandler(ChildException.class, new HttpHandler() {
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
exchange.getResponseSender().send("child exception handled");
}
}).addExceptionHandler(ParentException.class, new HttpHandler() {
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
exchange.getResponseSender().send("parent exception handled");
}
}).addExceptionHandler(Throwable.class, new HttpHandler() {
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
exchange.getResponseSender().send("catch all throwables");
}
});
DefaultServer.setRootHandler(exceptionHandler);
TestHttpClient client = new TestHttpClient();
try {
HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/");
HttpResponse result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
Assert.assertEquals("expected", HttpClientUtils.readResponse(result));
get = new HttpGet(DefaultServer.getDefaultServerURL() + "/exceptionParent");
result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
Assert.assertEquals("parent exception handled", HttpClientUtils.readResponse(result));
get = new HttpGet(DefaultServer.getDefaultServerURL() + "/exceptionChild");
result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
Assert.assertEquals("child exception handled", HttpClientUtils.readResponse(result));
get = new HttpGet(DefaultServer.getDefaultServerURL() + "/exceptionAnotherChild");
result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
Assert.assertEquals("parent exception handled", HttpClientUtils.readResponse(result));
get = new HttpGet(DefaultServer.getDefaultServerURL() + "/illegalArgumentException");
result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
Assert.assertEquals("catch all throwables", HttpClientUtils.readResponse(result));
} finally {
client.getConnectionManager().shutdown();
}
}
Aggregations