Search in sources :

Example 76 with TestHttpClient

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

the class MultipartFormDataParserTestCase method testQuotedBoundary.

@Test
public void testQuotedBoundary() throws Exception {
    DefaultServer.setRootHandler(new BlockingHandler(createHandler()));
    TestHttpClient client = new TestHttpClient();
    try {
        HttpPost post = new HttpPost(DefaultServer.getDefaultServerURL() + "/path");
        post.setHeader(Headers.CONTENT_TYPE_STRING, "multipart/form-data; boundary=\"s58IGsuzbg6GBG1yIgUO8;n4WkVf7clWMje\"");
        StringEntity entity = new StringEntity("--s58IGsuzbg6GBG1yIgUO8;n4WkVf7clWMje\r\n" + "Content-Disposition: form-data; name=\"formValue\"\r\n" + "\r\n" + "myValue\r\n" + "--s58IGsuzbg6GBG1yIgUO8;n4WkVf7clWMje\r\n" + "Content-Disposition: form-data; name=\"file\"; filename=\"uploadfile.txt\"\r\n" + "Content-Type: application/octet-stream\r\n" + "\r\n" + "file contents\r\n" + "\r\n" + "--s58IGsuzbg6GBG1yIgUO8;n4WkVf7clWMje--\r\n");
        post.setEntity(entity);
        HttpResponse result = client.execute(post);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        HttpClientUtils.readResponse(result);
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) BlockingHandler(io.undertow.server.handlers.BlockingHandler) HttpResponse(org.apache.http.HttpResponse) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 77 with TestHttpClient

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

the class PathTestCase method testBasicPathHanding.

@Test
public void testBasicPathHanding() throws IOException {
    TestHttpClient client = new TestHttpClient();
    try {
        final PathHandler handler = new PathHandler();
        handler.addPrefixPath("a", new RemainingPathHandler("/a"));
        handler.addPrefixPath("/aa", new RemainingPathHandler("/aa"));
        handler.addExactPath("/aa", new HttpHandler() {

            @Override
            public void handleRequest(HttpServerExchange exchange) throws Exception {
                exchange.getResponseSender().send("Exact /aa match:" + exchange.getRelativePath() + ":" + exchange.getResolvedPath());
            }
        });
        handler.addPrefixPath("/aa/anotherSubPath", new RemainingPathHandler("/aa/anotherSubPath"));
        final PathHandler sub = new PathHandler();
        handler.addPrefixPath("/path", sub);
        sub.addPrefixPath("/subpath", new RemainingPathHandler("/subpath"));
        sub.addPrefixPath("/", new RemainingPathHandler("/path"));
        DefaultServer.setRootHandler(handler);
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/notamatchingpath");
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.NOT_FOUND, result.getStatusLine().getStatusCode());
        HttpClientUtils.readResponse(result);
        get = new HttpGet(DefaultServer.getDefaultServerURL() + "/");
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.NOT_FOUND, result.getStatusLine().getStatusCode());
        HttpClientUtils.readResponse(result);
        runPathTest(client, "/path", "/path", "");
        runPathTest(client, "/path/a", "/path", "/a");
        runPathTest(client, "/path/subpath", "/subpath", "");
        runPathTest(client, "/path/subpath/", "/subpath", "/");
        runPathTest(client, "/path/subpath/foo", "/subpath", "/foo");
        runPathTest(client, "/a", "/a", "");
        runPathTest(client, "/aa/anotherSubPath", "/aa/anotherSubPath", "");
        runPathTest(client, "/aa/anotherSubPath/bob", "/aa/anotherSubPath", "/bob");
        runPathTest(client, "/aa/b?a=b", "/aa", "/b", Collections.singletonMap("a", "b"));
        runPathTest(client, "/path/:bar/baz", "/path", "/:bar/baz");
        //now test the exact path match
        get = new HttpGet(DefaultServer.getDefaultServerURL() + "/aa");
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        Assert.assertEquals("Exact /aa match::/aa", HttpClientUtils.readResponse(result));
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) HttpGet(org.apache.http.client.methods.HttpGet) PathHandler(io.undertow.server.handlers.PathHandler) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 78 with TestHttpClient

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

the class AbstractLoadBalancingProxyTestCase method testConnectionTimeout.

@Test
public void testConnectionTimeout() throws Exception {
    TestHttpClient client = new TestHttpClient();
    HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/timeout");
    get.addHeader("Connection", "close");
    HttpResponse result = client.execute(get);
    boolean res = Boolean.parseBoolean(HttpClientUtils.readResponse(result));
    Assert.assertEquals(false, res);
    try {
        for (int i = 0; i < 20; ++i) {
            //try and make sure that all IO threads have been used, this is not reliable however
            result = client.execute(get);
            HttpClientUtils.readResponse(result);
        }
        result = client.execute(get);
        res = Boolean.parseBoolean(HttpClientUtils.readResponse(result));
        //Assert.assertEquals(true, res); //this will fail sometime, unless we make a huge number of requests to make sure all IO threads are utilised
        Thread.sleep(IDLE_TIMEOUT + 1000);
        UndertowLogger.ROOT_LOGGER.info("Sending timed out request");
        result = client.execute(get);
        res = Boolean.parseBoolean(HttpClientUtils.readResponse(result));
        Assert.assertEquals(false, res);
    } 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 79 with TestHttpClient

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

the class AbstractLoadBalancingProxyTestCase method testLoadSharedWithServerShutdown.

@Test
public void testLoadSharedWithServerShutdown() throws Exception {
    final StringBuilder resultString = new StringBuilder();
    for (int i = 0; i < 6; ++i) {
        TestHttpClient client = new TestHttpClient();
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/name");
        HttpResponse result = client.execute(get);
        Assert.assertEquals("Test failed with i=" + i, StatusCodes.OK, result.getStatusLine().getStatusCode());
        resultString.append(HttpClientUtils.readResponse(result));
        resultString.append(' ');
        server1.stop();
        Thread.sleep(600);
        get = new HttpGet(DefaultServer.getDefaultServerURL() + "/name");
        result = client.execute(get);
        Assert.assertEquals("Test failed with i=" + i, StatusCodes.OK, result.getStatusLine().getStatusCode());
        resultString.append(HttpClientUtils.readResponse(result));
        resultString.append(' ');
        server1.start();
        server2.stop();
        Thread.sleep(600);
        get = new HttpGet(DefaultServer.getDefaultServerURL() + "/name");
        result = client.execute(get);
        Assert.assertEquals("Test failed with i=" + i, StatusCodes.OK, result.getStatusLine().getStatusCode());
        resultString.append(HttpClientUtils.readResponse(result));
        resultString.append(' ');
        server2.start();
    }
    Assert.assertTrue(resultString.toString().contains("server1"));
    Assert.assertTrue(resultString.toString().contains("server2"));
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 80 with TestHttpClient

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

the class LoadBalancerConnectionPoolingTestCase method shouldReduceConnectionPool.

@Test
public void shouldReduceConnectionPool() throws Exception {
    ExecutorService executorService = Executors.newFixedThreadPool(10);
    PoolingClientConnectionManager conman = new PoolingClientConnectionManager();
    conman.setDefaultMaxPerRoute(20);
    final TestHttpClient client = new TestHttpClient(conman);
    int requests = 20;
    final CountDownLatch latch = new CountDownLatch(requests);
    long ttlStartExpire = TTL + System.currentTimeMillis();
    try {
        for (int i = 0; i < requests; ++i) {
            executorService.submit(new Runnable() {

                @Override
                public void run() {
                    HttpGet get = new HttpGet("http://" + host + ":" + (port + 1));
                    try {
                        HttpResponse response = client.execute(get);
                        Assert.assertEquals(StatusCodes.OK, response.getStatusLine().getStatusCode());
                        HttpClientUtils.readResponse(response);
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    } finally {
                        latch.countDown();
                    }
                }
            });
        }
        if (!latch.await(2000, TimeUnit.MILLISECONDS)) {
            Assert.fail();
        }
    } finally {
        client.getConnectionManager().shutdown();
        executorService.shutdown();
    }
    if (activeConnections.size() != 1) {
        //uncommon, but we guard against it to prevent intermittent failures
        if (System.currentTimeMillis() < ttlStartExpire) {
            Assert.fail("there should still be a connection");
        }
    }
    long end = System.currentTimeMillis() + (TTL * 3);
    while (!activeConnections.isEmpty() && System.currentTimeMillis() < end) {
        Thread.sleep(100);
    }
    Assert.assertEquals(0, activeConnections.size());
}
Also used : PoolingClientConnectionManager(org.apache.http.impl.conn.PoolingClientConnectionManager) HttpGet(org.apache.http.client.methods.HttpGet) ExecutorService(java.util.concurrent.ExecutorService) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) 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