Search in sources :

Example 6 with UndertowClient

use of io.undertow.client.UndertowClient in project undertow by undertow-io.

the class H2CUpgradeResetTestCase method testUpgradeWithReset.

/**
 * The real test that sends several POST requests with and without reset.
 * @throws Exception Some error
 */
@Test
public void testUpgradeWithReset() throws Exception {
    final UndertowClient client = UndertowClient.getInstance();
    // the client connection uses the small byte-buffer of 1024 to force the continuation frames
    final ClientConnection connection = client.connect(new URI("http://" + DefaultServer.getHostAddress() + ":" + (DefaultServer.getHostPort() + 1)), worker, new UndertowXnioSsl(worker.getXnio(), OptionMap.EMPTY, DefaultServer.getClientSSLContext()), DefaultServer.getBufferPool(), OptionMap.create(UndertowOptions.ENABLE_HTTP2, true)).get();
    try {
        // the first request triggers the upgrade to H2C and sends a RST
        sendRequest(connection, 10, true);
        // send several requests with and without reset
        sendRequest(connection, 10, false);
        sendRequest(connection, 10, true);
        sendRequest(connection, 10, false);
    } finally {
        IoUtils.safeClose(connection);
    }
}
Also used : UndertowClient(io.undertow.client.UndertowClient) ClientConnection(io.undertow.client.ClientConnection) UndertowXnioSsl(io.undertow.protocols.ssl.UndertowXnioSsl) URI(java.net.URI) Test(org.junit.Test)

Example 7 with UndertowClient

use of io.undertow.client.UndertowClient in project undertow by undertow-io.

the class HttpClientTestCase method testConnectionClose.

@Test
public void testConnectionClose() throws Exception {
    // 
    final UndertowClient client = createClient();
    final CountDownLatch latch = new CountDownLatch(1);
    final ClientConnection connection = client.connect(ADDRESS, worker, DefaultServer.getBufferPool(), OptionMap.EMPTY).get();
    try {
        ClientRequest request = new ClientRequest().setPath(MESSAGE).setMethod(Methods.GET);
        request.getRequestHeaders().put(Headers.HOST, DefaultServer.getHostAddress());
        final List<ClientResponse> responses = new CopyOnWriteArrayList<>();
        request.getRequestHeaders().add(Headers.CONNECTION, Headers.CLOSE.toString());
        connection.sendRequest(request, createClientCallback(responses, latch));
        latch.await();
        final ClientResponse response = responses.iterator().next();
        Assert.assertEquals(message, response.getAttachment(RESPONSE_BODY));
        Assert.assertEquals(false, connection.isOpen());
    } finally {
        IoUtils.safeClose(connection);
    }
}
Also used : ClientResponse(io.undertow.client.ClientResponse) UndertowClient(io.undertow.client.UndertowClient) ClientConnection(io.undertow.client.ClientConnection) CountDownLatch(java.util.concurrent.CountDownLatch) ClientRequest(io.undertow.client.ClientRequest) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Test(org.junit.Test)

Example 8 with UndertowClient

use of io.undertow.client.UndertowClient in project undertow by undertow-io.

the class HttpClientTestCase method testSsl.

@Test
public void testSsl() throws Exception {
    // 
    final UndertowClient client = createClient();
    final List<ClientResponse> responses = new CopyOnWriteArrayList<>();
    final CountDownLatch latch = new CountDownLatch(10);
    DefaultServer.startSSLServer();
    SSLContext context = DefaultServer.getClientSSLContext();
    XnioSsl ssl = new UndertowXnioSsl(DefaultServer.getWorker().getXnio(), OptionMap.EMPTY, DefaultServer.SSL_BUFFER_POOL, context);
    final ClientConnection connection = client.connect(new URI(DefaultServer.getDefaultServerSSLAddress()), worker, ssl, DefaultServer.getBufferPool(), OptionMap.EMPTY).get();
    try {
        connection.getIoThread().execute(new Runnable() {

            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    final ClientRequest request = new ClientRequest().setMethod(Methods.GET).setPath(MESSAGE);
                    request.getRequestHeaders().put(Headers.HOST, DefaultServer.getHostAddress());
                    connection.sendRequest(request, createClientCallback(responses, latch));
                }
            }
        });
        latch.await(10, TimeUnit.SECONDS);
        Assert.assertEquals(10, responses.size());
        for (final ClientResponse response : responses) {
            Assert.assertEquals(message, response.getAttachment(RESPONSE_BODY));
        }
    } finally {
        connection.getIoThread().execute(new Runnable() {

            @Override
            public void run() {
                IoUtils.safeClose(connection);
            }
        });
        DefaultServer.stopSSLServer();
    }
}
Also used : ClientResponse(io.undertow.client.ClientResponse) XnioSsl(org.xnio.ssl.XnioSsl) UndertowXnioSsl(io.undertow.protocols.ssl.UndertowXnioSsl) UndertowClient(io.undertow.client.UndertowClient) SSLContext(javax.net.ssl.SSLContext) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) ClientConnection(io.undertow.client.ClientConnection) UndertowXnioSsl(io.undertow.protocols.ssl.UndertowXnioSsl) ClientRequest(io.undertow.client.ClientRequest) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Test(org.junit.Test)

Example 9 with UndertowClient

use of io.undertow.client.UndertowClient in project undertow by undertow-io.

the class HttpClientTestCase method testPostRequest.

@Test
public void testPostRequest() throws Exception {
    // 
    final UndertowClient client = createClient();
    final String postMessage = "This is a post request";
    final List<String> responses = new CopyOnWriteArrayList<>();
    final CountDownLatch latch = new CountDownLatch(10);
    final ClientConnection connection = client.connect(ADDRESS, worker, DefaultServer.getBufferPool(), OptionMap.EMPTY).get();
    try {
        connection.getIoThread().execute(new Runnable() {

            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    final ClientRequest request = new ClientRequest().setMethod(Methods.POST).setPath(POST);
                    request.getRequestHeaders().put(Headers.HOST, DefaultServer.getHostAddress());
                    request.getRequestHeaders().put(Headers.TRANSFER_ENCODING, "chunked");
                    connection.sendRequest(request, new ClientCallback<ClientExchange>() {

                        @Override
                        public void completed(ClientExchange result) {
                            new StringWriteChannelListener(postMessage).setup(result.getRequestChannel());
                            result.setResponseListener(new ClientCallback<ClientExchange>() {

                                @Override
                                public void completed(ClientExchange result) {
                                    new StringReadChannelListener(DefaultServer.getBufferPool()) {

                                        @Override
                                        protected void stringDone(String string) {
                                            responses.add(string);
                                            latch.countDown();
                                        }

                                        @Override
                                        protected void error(IOException e) {
                                            e.printStackTrace();
                                            latch.countDown();
                                        }
                                    }.setup(result.getResponseChannel());
                                }

                                @Override
                                public void failed(IOException e) {
                                    e.printStackTrace();
                                    latch.countDown();
                                }
                            });
                        }

                        @Override
                        public void failed(IOException e) {
                            e.printStackTrace();
                            latch.countDown();
                        }
                    });
                }
            }
        });
        latch.await(10, TimeUnit.SECONDS);
        Assert.assertEquals(10, responses.size());
        for (final String response : responses) {
            Assert.assertEquals(postMessage, response);
        }
    } finally {
        IoUtils.safeClose(connection);
    }
}
Also used : ClientExchange(io.undertow.client.ClientExchange) ClientCallback(io.undertow.client.ClientCallback) StringReadChannelListener(io.undertow.util.StringReadChannelListener) UndertowClient(io.undertow.client.UndertowClient) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) ClientConnection(io.undertow.client.ClientConnection) StringWriteChannelListener(io.undertow.util.StringWriteChannelListener) ClientRequest(io.undertow.client.ClientRequest) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Test(org.junit.Test)

Example 10 with UndertowClient

use of io.undertow.client.UndertowClient in project undertow by undertow-io.

the class AjpClientTestCase method testSimpleBasic.

@Test
public void testSimpleBasic() throws Exception {
    // 
    final UndertowClient client = createClient();
    final List<ClientResponse> responses = new CopyOnWriteArrayList<>();
    final CountDownLatch latch = new CountDownLatch(10);
    final ClientConnection connection = client.connect(ADDRESS, worker, DefaultServer.getBufferPool(), OptionMap.EMPTY).get();
    try {
        connection.getIoThread().execute(() -> {
            for (int i = 0; i < 10; i++) {
                final ClientRequest request = new ClientRequest().setMethod(Methods.GET).setPath(MESSAGE);
                request.getRequestHeaders().put(Headers.HOST, DefaultServer.getHostAddress());
                connection.sendRequest(request, createClientCallback(responses, latch));
            }
        });
        assertTrue(latch.await(10, TimeUnit.SECONDS));
        Assert.assertEquals(10, responses.size());
        for (final ClientResponse response : responses) {
            Assert.assertEquals(message, response.getAttachment(RESPONSE_BODY));
        }
    } finally {
        IoUtils.safeClose(connection);
    }
}
Also used : ClientResponse(io.undertow.client.ClientResponse) UndertowClient(io.undertow.client.UndertowClient) ClientConnection(io.undertow.client.ClientConnection) CountDownLatch(java.util.concurrent.CountDownLatch) ClientRequest(io.undertow.client.ClientRequest) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Test(org.junit.Test)

Aggregations

ClientConnection (io.undertow.client.ClientConnection)18 UndertowClient (io.undertow.client.UndertowClient)18 Test (org.junit.Test)17 ClientRequest (io.undertow.client.ClientRequest)16 CountDownLatch (java.util.concurrent.CountDownLatch)14 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)12 ClientResponse (io.undertow.client.ClientResponse)10 UndertowXnioSsl (io.undertow.protocols.ssl.UndertowXnioSsl)9 IOException (java.io.IOException)7 URI (java.net.URI)7 ClientCallback (io.undertow.client.ClientCallback)5 ClientExchange (io.undertow.client.ClientExchange)5 StringReadChannelListener (io.undertow.util.StringReadChannelListener)4 StringWriteChannelListener (io.undertow.util.StringWriteChannelListener)3 URISyntaxException (java.net.URISyntaxException)3 HttpString (io.undertow.util.HttpString)2 FutureResult (org.xnio.FutureResult)2 OptionMap (org.xnio.OptionMap)2 Undertow (io.undertow.Undertow)1 HttpHandler (io.undertow.server.HttpHandler)1