use of io.undertow.testutils.TestHttpClient 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();
}
}
use of io.undertow.testutils.TestHttpClient in project undertow by undertow-io.
the class LotsOfHeadersRequestTestCase method testLotsOfHeadersInRequest_Default_BadRequest.
@Test
public void testLotsOfHeadersInRequest_Default_BadRequest() throws IOException {
TestHttpClient client = new TestHttpClient();
try {
HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
// add request headers more than MAX_HEADERS
for (int i = 0; i < (getDefaultMaxHeaders() + 1); ++i) {
get.addHeader(HEADER + i, MESSAGE + i);
}
HttpResponse result = client.execute(get);
//this is not great, but the HTTP/2 impl sends a stream error which is translated to a 503. Should not be a big deal in practice
Assert.assertEquals(DefaultServer.isH2() ? StatusCodes.SERVICE_UNAVAILABLE : StatusCodes.BAD_REQUEST, result.getStatusLine().getStatusCode());
} finally {
client.getConnectionManager().shutdown();
}
}
use of io.undertow.testutils.TestHttpClient in project undertow by undertow-io.
the class LotsOfQueryParametersTestCase method testLotsOfQueryParameters_MaxParameters_BadRequest.
@Test
@AjpIgnore
public void testLotsOfQueryParameters_MaxParameters_BadRequest() throws IOException {
OptionMap existing = DefaultServer.getUndertowOptions();
TestHttpClient client = new TestHttpClient();
try {
StringBuilder qs = new StringBuilder();
// add query parameters more than specified MAX_PARAMETERS
for (int i = 0; i < (TEST_MAX_PARAMETERS + 1); ++i) {
qs.append(QUERY + i);
qs.append("=");
qs.append(URLEncoder.encode(MESSAGE + i, "UTF-8"));
qs.append("&");
}
// delete last useless '&'
qs.deleteCharAt(qs.length() - 1);
HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path?" + qs.toString());
DefaultServer.setUndertowOptions(OptionMap.create(UndertowOptions.MAX_PARAMETERS, TEST_MAX_PARAMETERS));
HttpResponse result = client.execute(get);
Assert.assertEquals(StatusCodes.BAD_REQUEST, result.getStatusLine().getStatusCode());
} finally {
DefaultServer.setUndertowOptions(existing);
client.getConnectionManager().shutdown();
}
}
use of io.undertow.testutils.TestHttpClient in project undertow by undertow-io.
the class LotsOfQueryParametersTestCase method testLotsOfQueryParameters_MaxParameters_Ok.
@Test
@AjpIgnore
public void testLotsOfQueryParameters_MaxParameters_Ok() throws IOException {
OptionMap existing = DefaultServer.getUndertowOptions();
TestHttpClient client = new TestHttpClient();
try {
StringBuilder qs = new StringBuilder();
for (int i = 0; i < TEST_MAX_PARAMETERS; ++i) {
qs.append(QUERY + i);
qs.append("=");
qs.append(URLEncoder.encode(MESSAGE + i, "UTF-8"));
qs.append("&");
}
// delete last useless '&'
qs.deleteCharAt(qs.length() - 1);
HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path?" + qs.toString());
DefaultServer.setUndertowOptions(OptionMap.create(UndertowOptions.MAX_PARAMETERS, TEST_MAX_PARAMETERS));
HttpResponse result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
for (int i = 0; i < TEST_MAX_PARAMETERS; ++i) {
Header[] header = result.getHeaders(QUERY + i);
Assert.assertEquals(MESSAGE + i, header[0].getValue());
}
} finally {
DefaultServer.setUndertowOptions(existing);
client.getConnectionManager().shutdown();
}
}
use of io.undertow.testutils.TestHttpClient in project undertow by undertow-io.
the class MetricsHandlerTestCase method testMetrics.
@Test
public void testMetrics() throws IOException, InterruptedException {
MetricsHandler metricsHandler;
CompletionLatchHandler latchHandler;
DefaultServer.setRootHandler(latchHandler = new CompletionLatchHandler(metricsHandler = new MetricsHandler(new HttpHandler() {
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
Thread.sleep(100);
exchange.getResponseSender().send("Hello");
}
})));
HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
TestHttpClient client = new TestHttpClient();
try {
HttpResponse result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
Assert.assertEquals("Hello", HttpClientUtils.readResponse(result));
latchHandler.await();
latchHandler.reset();
MetricsHandler.MetricResult metrics = metricsHandler.getMetrics();
Assert.assertEquals(1, metrics.getTotalRequests());
Assert.assertTrue(metrics.getMaxRequestTime() > 0);
Assert.assertEquals(metrics.getMinRequestTime(), metrics.getMaxRequestTime());
Assert.assertEquals(metrics.getMaxRequestTime(), metrics.getTotalRequestTime());
result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
Assert.assertEquals("Hello", HttpClientUtils.readResponse(result));
latchHandler.await();
latchHandler.reset();
metrics = metricsHandler.getMetrics();
Assert.assertEquals(2, metrics.getTotalRequests());
} finally {
client.getConnectionManager().shutdown();
}
}
Aggregations