Search in sources :

Example 16 with HttpHandler

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();
    }
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) CompletionLatchHandler(io.undertow.util.CompletionLatchHandler) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 17 with HttpHandler

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();
    }
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 18 with HttpHandler

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);
        }
    });
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) Sender(io.undertow.io.Sender) HttpHandler(io.undertow.server.HttpHandler) IOException(java.io.IOException) BeforeClass(org.junit.BeforeClass)

Example 19 with HttpHandler

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()));
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) SessionAttachmentHandler(io.undertow.server.session.SessionAttachmentHandler) SessionCookieConfig(io.undertow.server.session.SessionCookieConfig) PredicateHandler(io.undertow.server.handlers.PredicateHandler) ProtocolException(org.apache.http.ProtocolException) IOException(java.io.IOException) InMemorySessionManager(io.undertow.server.session.InMemorySessionManager)

Example 20 with HttpHandler

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();
    }
}
Also used : Path(java.nio.file.Path) HttpHandler(io.undertow.server.HttpHandler) CanonicalPathHandler(io.undertow.server.handlers.CanonicalPathHandler) HttpGet(org.apache.http.client.methods.HttpGet) CanonicalPathHandler(io.undertow.server.handlers.CanonicalPathHandler) PathHandler(io.undertow.server.handlers.PathHandler) FileHandlerTestCase(io.undertow.server.handlers.file.FileHandlerTestCase) HttpResponse(org.apache.http.HttpResponse) ResourceHandler(io.undertow.server.handlers.resource.ResourceHandler) NameVirtualHostHandler(io.undertow.server.handlers.NameVirtualHostHandler) PathResourceManager(io.undertow.server.handlers.resource.PathResourceManager) TestHttpClient(io.undertow.testutils.TestHttpClient) Header(org.apache.http.Header) SimpleErrorPageHandler(io.undertow.server.handlers.error.SimpleErrorPageHandler) Test(org.junit.Test)

Aggregations

HttpHandler (io.undertow.server.HttpHandler)126 HttpServerExchange (io.undertow.server.HttpServerExchange)72 IOException (java.io.IOException)54 BeforeClass (org.junit.BeforeClass)35 Test (org.junit.Test)25 TestHttpClient (io.undertow.testutils.TestHttpClient)20 HttpResponse (org.apache.http.HttpResponse)20 HttpGet (org.apache.http.client.methods.HttpGet)19 PathHandler (io.undertow.server.handlers.PathHandler)17 HttpString (io.undertow.util.HttpString)15 DeploymentInfo (io.undertow.servlet.api.DeploymentInfo)13 Undertow (io.undertow.Undertow)12 ArrayList (java.util.ArrayList)11 HandlerWrapper (io.undertow.server.HandlerWrapper)9 InMemorySessionManager (io.undertow.server.session.InMemorySessionManager)9 SessionAttachmentHandler (io.undertow.server.session.SessionAttachmentHandler)9 Header (org.apache.http.Header)9 SessionCookieConfig (io.undertow.server.session.SessionCookieConfig)8 AuthenticationMechanism (io.undertow.security.api.AuthenticationMechanism)7 AuthenticationCallHandler (io.undertow.security.handlers.AuthenticationCallHandler)7