use of io.undertow.server.HttpHandler 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();
}
}
use of io.undertow.server.HttpHandler in project undertow by undertow-io.
the class PredicatedHandlersTestCase method testRewrite.
@Test
public void testRewrite() throws IOException {
DefaultServer.setRootHandler(Handlers.predicates(PredicatedHandlersParser.parse("path(/skipallrules) and true -> done\n" + "method(GET) -> set(attribute='%{o,type}', value=get) \r\n" + "regex('(.*).css') -> {rewrite['${1}.xcss'];set(attribute='%{o,chained}', value=true)} \n" + "regex('(.*).redirect$') -> redirect['${1}.redirected']\n\n\n\n\n" + "set[attribute='%{o,someHeader}', value=always]\n" + "path-template('/foo/{bar}/{f}') -> set[attribute='%{o,template}', value='${bar}']\r\n" + "path-template('/bar->foo') -> redirect(/);" + "regex('(.*).css') -> set[attribute='%{o,css}', value='true'] else set[attribute='%{o,css}', value='false']; " + "path(/restart) -> {rewrite(/foo/a/b); restart; }\r\n", getClass().getClassLoader()), new HttpHandler() {
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
exchange.getResponseSender().send(exchange.getRelativePath());
}
}));
TestHttpClient client = new TestHttpClient();
try {
HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/foo/a/b");
HttpResponse result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
String response = HttpClientUtils.readResponse(result);
Assert.assertEquals("get", result.getHeaders("type")[0].getValue());
Assert.assertEquals("always", result.getHeaders("someHeader")[0].getValue());
Assert.assertEquals("a", result.getHeaders("template")[0].getValue());
Assert.assertEquals("false", result.getHeaders("css")[0].getValue());
Assert.assertEquals("/foo/a/b", response);
get = new HttpGet(DefaultServer.getDefaultServerURL() + "/foo/a/b.css");
result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
response = HttpClientUtils.readResponse(result);
Assert.assertEquals("get", result.getHeaders("type")[0].getValue());
Assert.assertEquals("true", result.getHeaders("chained")[0].getValue());
Assert.assertEquals("/foo/a/b.xcss", response);
Assert.assertEquals("always", result.getHeaders("someHeader")[0].getValue());
Assert.assertEquals("true", result.getHeaders("css")[0].getValue());
Assert.assertEquals("a", result.getHeaders("template")[0].getValue());
get = new HttpGet(DefaultServer.getDefaultServerURL() + "/foo/a/b.redirect");
result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
response = HttpClientUtils.readResponse(result);
Assert.assertEquals("get", result.getHeaders("type")[0].getValue());
Assert.assertEquals("always", result.getHeaders("someHeader")[0].getValue());
Assert.assertEquals("a", result.getHeaders("template")[0].getValue());
Assert.assertEquals("/foo/a/b.redirected", response);
get = new HttpGet(DefaultServer.getDefaultServerURL() + "/skipallrules");
result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
response = HttpClientUtils.readResponse(result);
Assert.assertEquals(0, result.getHeaders("someHeader").length);
get = new HttpGet(DefaultServer.getDefaultServerURL() + "/restart");
result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
response = HttpClientUtils.readResponse(result);
Assert.assertEquals("get", result.getHeaders("type")[0].getValue());
Assert.assertEquals("always", result.getHeaders("someHeader")[0].getValue());
Assert.assertEquals("a", result.getHeaders("template")[0].getValue());
Assert.assertEquals("/foo/a/b", response);
} finally {
client.getConnectionManager().shutdown();
}
}
use of io.undertow.server.HttpHandler in project undertow by undertow-io.
the class FixedLengthResponseTestCase method setup.
@BeforeClass
public static void setup() {
DefaultServer.setRootHandler(new HttpHandler() {
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
if (connection == null) {
connection = exchange.getConnection();
} else if (!DefaultServer.isAjp() && !DefaultServer.isProxy() && connection != exchange.getConnection()) {
Sender sender = exchange.getResponseSender();
sender.send("Connection not persistent");
return;
}
exchange.getResponseHeaders().put(Headers.CONTENT_LENGTH, message.length() + "");
final Sender sender = exchange.getResponseSender();
sender.send(message);
}
});
}
use of io.undertow.server.HttpHandler in project undertow by undertow-io.
the class FormAuthTestCase method setRootHandler.
@Override
protected void setRootHandler(HttpHandler current) {
final PredicateHandler handler = new PredicateHandler(Predicates.path("/login"), new HttpHandler() {
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
exchange.getResponseSender().send("Login Page");
}
}, current);
super.setRootHandler(new SessionAttachmentHandler(handler, new InMemorySessionManager("test"), new SessionCookieConfig()));
}
use of io.undertow.server.HttpHandler in project undertow by undertow-io.
the class ComplexSSLTestCase method complexSSLTestCase.
@Test
public void complexSSLTestCase() throws IOException, GeneralSecurityException, URISyntaxException, InterruptedException {
final PathHandler pathHandler = new PathHandler();
Path rootPath = Paths.get(FileHandlerTestCase.class.getResource("page.html").toURI()).getParent();
final NameVirtualHostHandler virtualHostHandler = new NameVirtualHostHandler();
HttpHandler root = virtualHostHandler;
root = new SimpleErrorPageHandler(root);
root = new CanonicalPathHandler(root);
virtualHostHandler.addHost("default-host", pathHandler);
virtualHostHandler.setDefaultHandler(pathHandler);
pathHandler.addPrefixPath("/", new ResourceHandler(new PathResourceManager(rootPath, 10485760)).setDirectoryListingEnabled(true));
DefaultServer.setRootHandler(root);
DefaultServer.startSSLServer();
TestHttpClient client = new TestHttpClient();
client.setSSLContext(DefaultServer.getClientSSLContext());
try {
//get file list, this works
HttpGet getFileList = new HttpGet(DefaultServer.getDefaultServerSSLAddress());
HttpResponse resultList = client.execute(getFileList);
Assert.assertEquals(StatusCodes.OK, resultList.getStatusLine().getStatusCode());
String responseList = HttpClientUtils.readResponse(resultList);
Assert.assertTrue(responseList, responseList.contains("page.html"));
Header[] headersList = resultList.getHeaders("Content-Type");
Assert.assertEquals("text/html; charset=UTF-8", headersList[0].getValue());
//get file itself, breaks
HttpGet getFile = new HttpGet(DefaultServer.getDefaultServerSSLAddress() + "/page.html");
HttpResponse result = client.execute(getFile);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
String response = HttpClientUtils.readResponse(result);
Header[] headers = result.getHeaders("Content-Type");
Assert.assertEquals("text/html", headers[0].getValue());
Assert.assertTrue(response, response.contains("A web page"));
} finally {
client.getConnectionManager().shutdown();
DefaultServer.stopSSLServer();
}
}
Aggregations