Search in sources :

Example 91 with TestHttpClient

use of io.undertow.testutils.TestHttpClient in project undertow by undertow-io.

the class SenderTestCase method testAsyncSender.

@Test
public void testAsyncSender() throws IOException {
    StringBuilder sb = new StringBuilder(SENDS);
    for (int i = 0; i < SENDS; ++i) {
        sb.append("a");
    }
    HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/lots?blocking=false");
    TestHttpClient client = new TestHttpClient();
    try {
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        Assert.assertEquals(sb.toString(), HttpClientUtils.readResponse(result));
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 92 with TestHttpClient

use of io.undertow.testutils.TestHttpClient in project undertow by undertow-io.

the class SetAttributeTestCase method testRewrite.

@Test
public void testRewrite() throws IOException {
    DefaultServer.setRootHandler(rewrite("regex['/somePath/(.*)']", "/otherPath/$1", getClass().getClassLoader(), path().addPrefixPath("/otherPath", new InfoHandler()).addPrefixPath("/relative", rewrite("path-template['/foo/{bar}/{woz}']", "/foo?bar=${bar}&woz=${woz}", getClass().getClassLoader(), new InfoHandler()))));
    TestHttpClient client = new TestHttpClient();
    try {
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/relative/foo/a/b");
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        String response = HttpClientUtils.readResponse(result);
        Assert.assertEquals("URI: /relative/foo?bar=a&woz=b relative: /foo QS:?bar=a&woz=b bar: a woz: b", response);
        get = new HttpGet(DefaultServer.getDefaultServerURL() + "/somePath/foo/a/b");
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        response = HttpClientUtils.readResponse(result);
        Assert.assertEquals("URI: /otherPath/foo/a/b relative: /foo/a/b QS:", response);
        get = new HttpGet(DefaultServer.getDefaultServerURL() + "/somePath/foo?a=b");
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        response = HttpClientUtils.readResponse(result);
        Assert.assertEquals("URI: /otherPath/foo relative: /foo QS:a=b a: b", response);
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 93 with TestHttpClient

use of io.undertow.testutils.TestHttpClient in project undertow by undertow-io.

the class SimpleNonBlockingServerTestCase method sendHttpOneZeroRequest.

@Test
public void sendHttpOneZeroRequest() throws IOException {
    TestHttpClient client = new TestHttpClient();
    try {
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
        get.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_0);
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        Header[] header = result.getHeaders("MyHeader");
        Assert.assertEquals("MyValue", header[0].getValue());
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : Header(org.apache.http.Header) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 94 with TestHttpClient

use of io.undertow.testutils.TestHttpClient in project undertow by undertow-io.

the class AccessLogFileTestCase method testLogLotsOfThreads.

@Test
public void testLogLotsOfThreads() throws IOException, InterruptedException, ExecutionException {
    Path directory = logDirectory;
    Path logFileName = directory.resolve("server2.log");
    DefaultAccessLogReceiver logReceiver = new DefaultAccessLogReceiver(DefaultServer.getWorker(), directory, "server2.");
    CompletionLatchHandler latchHandler;
    DefaultServer.setRootHandler(latchHandler = new CompletionLatchHandler(NUM_REQUESTS * NUM_THREADS, new AccessLogHandler(HELLO_HANDLER, logReceiver, "REQ %{i,test-header}", AccessLogFileTestCase.class.getClassLoader())));
    ExecutorService executor = Executors.newFixedThreadPool(NUM_THREADS);
    try {
        final List<Future<?>> futures = new ArrayList<>();
        for (int i = 0; i < NUM_THREADS; ++i) {
            final int threadNo = i;
            futures.add(executor.submit(new Runnable() {

                @Override
                public void run() {
                    TestHttpClient client = new TestHttpClient();
                    try {
                        for (int i = 0; i < NUM_REQUESTS; ++i) {
                            HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
                            get.addHeader("test-header", "thread-" + threadNo + "-request-" + i);
                            HttpResponse result = client.execute(get);
                            Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
                            final String response = HttpClientUtils.readResponse(result);
                            Assert.assertEquals("Hello", response);
                        }
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    } finally {
                        client.getConnectionManager().shutdown();
                    }
                }
            }));
        }
        for (Future<?> future : futures) {
            future.get();
        }
    } finally {
        executor.shutdown();
    }
    latchHandler.await();
    logReceiver.awaitWrittenForTest();
    String completeLog = new String(Files.readAllBytes(logFileName));
    for (int i = 0; i < NUM_THREADS; ++i) {
        for (int j = 0; j < NUM_REQUESTS; ++j) {
            Assert.assertTrue(completeLog.contains("REQ thread-" + i + "-request-" + j));
        }
    }
}
Also used : Path(java.nio.file.Path) CompletionLatchHandler(io.undertow.util.CompletionLatchHandler) HttpGet(org.apache.http.client.methods.HttpGet) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) TestHttpClient(io.undertow.testutils.TestHttpClient) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) Test(org.junit.Test)

Example 95 with TestHttpClient

use of io.undertow.testutils.TestHttpClient in project undertow by undertow-io.

the class AccessLogFileTestCase method testForcedLogRotation.

@Test
public void testForcedLogRotation() throws IOException, InterruptedException {
    Path logFileName = logDirectory.resolve("server.log");
    DefaultAccessLogReceiver logReceiver = new DefaultAccessLogReceiver(DefaultServer.getWorker(), logDirectory, "server.");
    CompletionLatchHandler latchHandler;
    DefaultServer.setRootHandler(latchHandler = new CompletionLatchHandler(new AccessLogHandler(HELLO_HANDLER, logReceiver, "Remote address %a Code %s test-header %{i,test-header}", AccessLogFileTestCase.class.getClassLoader())));
    TestHttpClient client = new TestHttpClient();
    try {
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
        get.addHeader("test-header", "v1");
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        Assert.assertEquals("Hello", HttpClientUtils.readResponse(result));
        latchHandler.await();
        latchHandler.reset();
        logReceiver.awaitWrittenForTest();
        Assert.assertEquals("Remote address " + DefaultServer.getDefaultServerAddress().getAddress().getHostAddress() + " Code 200 test-header v1\n", new String(Files.readAllBytes(logFileName)));
        logReceiver.rotate();
        logReceiver.awaitWrittenForTest();
        Assert.assertFalse(Files.exists(logFileName));
        Path firstLogRotate = logDirectory.resolve("server." + new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + ".log");
        Assert.assertEquals("Remote address " + DefaultServer.getDefaultServerAddress().getAddress().getHostAddress() + " Code 200 test-header v1\n", new String(Files.readAllBytes(firstLogRotate)));
        get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
        get.addHeader("test-header", "v2");
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        Assert.assertEquals("Hello", HttpClientUtils.readResponse(result));
        latchHandler.await();
        latchHandler.reset();
        logReceiver.awaitWrittenForTest();
        Assert.assertEquals("Remote address " + DefaultServer.getDefaultServerAddress().getAddress().getHostAddress() + " Code 200 test-header v2\n", new String(Files.readAllBytes(logFileName)));
        logReceiver.rotate();
        logReceiver.awaitWrittenForTest();
        Assert.assertFalse(Files.exists(logFileName));
        Path secondLogRotate = logDirectory.resolve("server." + new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + "-1.log");
        Assert.assertEquals("Remote address " + DefaultServer.getDefaultServerAddress().getAddress().getHostAddress() + " Code 200 test-header v2\n", new String(Files.readAllBytes(secondLogRotate)));
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : Path(java.nio.file.Path) CompletionLatchHandler(io.undertow.util.CompletionLatchHandler) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Aggregations

TestHttpClient (io.undertow.testutils.TestHttpClient)302 HttpResponse (org.apache.http.HttpResponse)290 Test (org.junit.Test)269 HttpGet (org.apache.http.client.methods.HttpGet)239 Header (org.apache.http.Header)66 HttpPost (org.apache.http.client.methods.HttpPost)54 IOException (java.io.IOException)38 StringEntity (org.apache.http.entity.StringEntity)30 Path (java.nio.file.Path)29 PathHandler (io.undertow.server.handlers.PathHandler)28 HttpHandler (io.undertow.server.HttpHandler)20 CanonicalPathHandler (io.undertow.server.handlers.CanonicalPathHandler)20 PathResourceManager (io.undertow.server.handlers.resource.PathResourceManager)20 ResourceHandler (io.undertow.server.handlers.resource.ResourceHandler)20 ArrayList (java.util.ArrayList)20 HttpServerExchange (io.undertow.server.HttpServerExchange)19 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)16 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)14 NameValuePair (org.apache.http.NameValuePair)13 DigestWWWAuthenticateToken (io.undertow.security.impl.DigestWWWAuthenticateToken)11