Search in sources :

Example 6 with CompletionLatchHandler

use of io.undertow.util.CompletionLatchHandler in project undertow by undertow-io.

the class JDBCLogDatabaseTestCase method testSingleLogMessageToDatabase.

@Test
public void testSingleLogMessageToDatabase() throws IOException, InterruptedException, SQLException {
    JDBCLogHandler logHandler = new JDBCLogHandler(HELLO_HANDLER, DefaultServer.getWorker(), "common", ds);
    CompletionLatchHandler latchHandler;
    DefaultServer.setRootHandler(latchHandler = new CompletionLatchHandler(logHandler));
    TestHttpClient client = new TestHttpClient();
    try {
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
        HttpResponse result = client.execute(get);
        latchHandler.await();
        logHandler.awaitWrittenForTest();
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        Assert.assertEquals("Hello", HttpClientUtils.readResponse(result));
    } finally {
        Connection conn = null;
        Statement statement = null;
        try {
            conn = ds.getConnection();
            statement = conn.createStatement();
            ResultSet resultDatabase = statement.executeQuery("SELECT * FROM PUBLIC.ACCESS;");
            resultDatabase.next();
            Assert.assertEquals(DefaultServer.getDefaultServerAddress().getAddress().getHostAddress(), resultDatabase.getString(logHandler.getRemoteHostField()));
            Assert.assertEquals("5", resultDatabase.getString(logHandler.getBytesField()));
            Assert.assertEquals("200", resultDatabase.getString(logHandler.getStatusField()));
            client.getConnectionManager().shutdown();
        } finally {
            if (statement != null) {
                statement.close();
            }
            if (conn != null) {
                conn.close();
            }
        }
    }
}
Also used : CompletionLatchHandler(io.undertow.util.CompletionLatchHandler) Statement(java.sql.Statement) HttpGet(org.apache.http.client.methods.HttpGet) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) HttpResponse(org.apache.http.HttpResponse) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 7 with CompletionLatchHandler

use of io.undertow.util.CompletionLatchHandler in project undertow by undertow-io.

the class ExtendedAccessLogFileTestCase method verifySingleLogMessageToFile.

private void verifySingleLogMessageToFile(Path logFileName, DefaultAccessLogReceiver logReceiver) throws IOException, InterruptedException {
    CompletionLatchHandler latchHandler;
    DefaultServer.setRootHandler(latchHandler = new CompletionLatchHandler(new AccessLogHandler(HELLO_HANDLER, logReceiver, PATTERN, new ExtendedAccessLogParser(ExtendedAccessLogFileTestCase.class.getClassLoader()).parse(PATTERN))));
    TestHttpClient client = new TestHttpClient();
    try {
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
        get.addHeader("test-header", "single-val");
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        Assert.assertEquals("Hello", HttpClientUtils.readResponse(result));
        latchHandler.await();
        logReceiver.awaitWrittenForTest();
        String data = new String(Files.readAllBytes(logFileName));
        String[] lines = data.split("\n");
        Assert.assertEquals("#Fields: " + PATTERN, lines[0]);
        Assert.assertEquals("#Version: 2.0", lines[1]);
        Assert.assertEquals("#Software: " + Version.getFullVersionString(), lines[2]);
        Assert.assertEquals("", lines[3]);
        Assert.assertEquals("/path 'single-val'", lines[4]);
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : CompletionLatchHandler(io.undertow.util.CompletionLatchHandler) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) TestHttpClient(io.undertow.testutils.TestHttpClient)

Example 8 with CompletionLatchHandler

use of io.undertow.util.CompletionLatchHandler in project undertow by undertow-io.

the class AccessLogFileTestCase method verifySingleLogMessageToFile.

private void verifySingleLogMessageToFile(Path logFileName, DefaultAccessLogReceiver logReceiver) throws IOException, InterruptedException {
    CompletionLatchHandler latchHandler;
    DefaultServer.setRootHandler(latchHandler = new CompletionLatchHandler(new AccessLogHandler(HELLO_HANDLER, logReceiver, "Remote address %a Code %s test-header %{i,test-header} %{i,non-existent}", AccessLogFileTestCase.class.getClassLoader())));
    TestHttpClient client = new TestHttpClient();
    try {
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
        get.addHeader("test-header", "single-val");
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        Assert.assertEquals("Hello", HttpClientUtils.readResponse(result));
        latchHandler.await();
        logReceiver.awaitWrittenForTest();
        Assert.assertEquals("Remote address " + DefaultServer.getDefaultServerAddress().getAddress().getHostAddress() + " Code 200 test-header single-val -\n", new String(Files.readAllBytes(logFileName)));
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : CompletionLatchHandler(io.undertow.util.CompletionLatchHandler) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) TestHttpClient(io.undertow.testutils.TestHttpClient)

Aggregations

TestHttpClient (io.undertow.testutils.TestHttpClient)8 CompletionLatchHandler (io.undertow.util.CompletionLatchHandler)8 HttpResponse (org.apache.http.HttpResponse)8 HttpGet (org.apache.http.client.methods.HttpGet)8 Test (org.junit.Test)6 IOException (java.io.IOException)3 Path (java.nio.file.Path)2 Connection (java.sql.Connection)2 ResultSet (java.sql.ResultSet)2 Statement (java.sql.Statement)2 ArrayList (java.util.ArrayList)2 ExecutorService (java.util.concurrent.ExecutorService)2 Future (java.util.concurrent.Future)2 HttpHandler (io.undertow.server.HttpHandler)1 HttpServerExchange (io.undertow.server.HttpServerExchange)1 MetricsHandler (io.undertow.server.handlers.MetricsHandler)1 PathHandler (io.undertow.server.handlers.PathHandler)1 DeploymentInfo (io.undertow.servlet.api.DeploymentInfo)1 DeploymentManager (io.undertow.servlet.api.DeploymentManager)1 FilterInfo (io.undertow.servlet.api.FilterInfo)1