Search in sources :

Example 86 with Request

use of org.eclipse.jetty.client.api.Request in project jetty.project by eclipse.

the class HttpClientTimeoutTest method testTimeoutIsCancelledOnSuccessWithExplicitConnection.

@Slow
@Test
public void testTimeoutIsCancelledOnSuccessWithExplicitConnection() throws Exception {
    long timeout = 1000;
    start(new TimeoutHandler(timeout));
    final CountDownLatch latch = new CountDownLatch(1);
    Destination destination = client.getDestination(scheme, "localhost", connector.getLocalPort());
    FuturePromise<Connection> futureConnection = new FuturePromise<>();
    destination.newConnection(futureConnection);
    try (Connection connection = futureConnection.get(5, TimeUnit.SECONDS)) {
        Request request = client.newRequest(destination.getHost(), destination.getPort()).scheme(scheme).timeout(2 * timeout, TimeUnit.MILLISECONDS);
        connection.send(request, new Response.CompleteListener() {

            @Override
            public void onComplete(Result result) {
                Response response = result.getResponse();
                Assert.assertEquals(200, response.getStatus());
                Assert.assertFalse(result.isFailed());
                latch.countDown();
            }
        });
        Assert.assertTrue(latch.await(3 * timeout, TimeUnit.MILLISECONDS));
        TimeUnit.MILLISECONDS.sleep(2 * timeout);
        Assert.assertNull(request.getAbortCause());
    }
}
Also used : Response(org.eclipse.jetty.client.api.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) Destination(org.eclipse.jetty.client.api.Destination) FuturePromise(org.eclipse.jetty.util.FuturePromise) Connection(org.eclipse.jetty.client.api.Connection) SslConnection(org.eclipse.jetty.io.ssl.SslConnection) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) CountDownLatch(java.util.concurrent.CountDownLatch) Result(org.eclipse.jetty.client.api.Result) Test(org.junit.Test) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow)

Example 87 with Request

use of org.eclipse.jetty.client.api.Request in project jetty.project by eclipse.

the class HttpClientURITest method testPathWithQuery.

@Test
public void testPathWithQuery() throws Exception {
    String name = "a";
    String value = "1";
    final String query = name + "=" + value;
    final String path = "/path";
    start(new AbstractHandler() {

        @Override
        public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            Assert.assertEquals(path, request.getRequestURI());
            Assert.assertEquals(query, request.getQueryString());
        }
    });
    String pathQuery = path + "?" + query;
    Request request = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).timeout(5, TimeUnit.SECONDS).path(pathQuery);
    Assert.assertEquals(path, request.getPath());
    Assert.assertEquals(query, request.getQuery());
    Assert.assertTrue(request.getURI().toString().endsWith(pathQuery));
    Fields params = request.getParams();
    Assert.assertEquals(1, params.getSize());
    Assert.assertEquals(value, params.get(name).getValue());
    ContentResponse response = request.send();
    Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Fields(org.eclipse.jetty.util.Fields) Test(org.junit.Test)

Example 88 with Request

use of org.eclipse.jetty.client.api.Request in project jetty.project by eclipse.

the class HttpClientURITest method testPathWithParam.

@Test
public void testPathWithParam() throws Exception {
    String name = "a";
    String value = "1";
    final String query = name + "=" + value;
    final String path = "/path";
    String pathQuery = path + "?" + query;
    start(new AbstractHandler() {

        @Override
        public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            Assert.assertEquals(path, request.getRequestURI());
            Assert.assertEquals(query, request.getQueryString());
        }
    });
    Request request = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).timeout(5, TimeUnit.SECONDS).path(path).param(name, value);
    Assert.assertEquals(path, request.getPath());
    Assert.assertEquals(query, request.getQuery());
    Assert.assertTrue(request.getURI().toString().endsWith(pathQuery));
    Fields params = request.getParams();
    Assert.assertEquals(1, params.getSize());
    Assert.assertEquals(value, params.get(name).getValue());
    ContentResponse response = request.send();
    Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Fields(org.eclipse.jetty.util.Fields) Test(org.junit.Test)

Example 89 with Request

use of org.eclipse.jetty.client.api.Request in project jetty.project by eclipse.

the class HttpRequestAbortTest method testAbortOnContent.

@Test
public void testAbortOnContent() throws Exception {
    try (StacklessLogging suppressor = new StacklessLogging(org.eclipse.jetty.server.HttpChannel.class)) {
        CountDownLatch serverLatch = new CountDownLatch(1);
        start(new EmptyServerHandler() {

            @Override
            public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
                try {
                    super.handle(target, baseRequest, request, response);
                    if (request.getDispatcherType() != DispatcherType.ERROR)
                        IO.copy(request.getInputStream(), response.getOutputStream());
                } finally {
                    serverLatch.countDown();
                }
            }
        });
        final Throwable cause = new Exception();
        final AtomicBoolean aborted = new AtomicBoolean();
        final CountDownLatch latch = new CountDownLatch(1);
        try {
            client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).onRequestContent((request, content) -> {
                aborted.set(request.abort(cause));
                latch.countDown();
            }).content(new ByteBufferContentProvider(ByteBuffer.wrap(new byte[] { 0 }), ByteBuffer.wrap(new byte[] { 1 })) {

                @Override
                public long getLength() {
                    return -1;
                }
            }).timeout(5, TimeUnit.SECONDS).send();
            Assert.fail();
        } catch (ExecutionException x) {
            Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
            if (aborted.get())
                Assert.assertSame(cause, x.getCause());
        }
        Assert.assertTrue(serverLatch.await(5, TimeUnit.SECONDS));
        HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP) client.getDestination(scheme, "localhost", connector.getLocalPort());
        DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
        Assert.assertEquals(0, connectionPool.getConnectionCount());
        Assert.assertEquals(0, connectionPool.getActiveConnections().size());
        Assert.assertEquals(0, connectionPool.getIdleConnections().size());
    }
}
Also used : Result(org.eclipse.jetty.client.api.Result) ServletException(javax.servlet.ServletException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) ByteBufferContentProvider(org.eclipse.jetty.client.util.ByteBufferContentProvider) HttpServletResponse(javax.servlet.http.HttpServletResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IOException(java.io.IOException) Test(org.junit.Test) Request(org.eclipse.jetty.client.api.Request) IO(org.eclipse.jetty.util.IO) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteBuffer(java.nio.ByteBuffer) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) CountDownLatch(java.util.concurrent.CountDownLatch) HttpServletRequest(javax.servlet.http.HttpServletRequest) DispatcherType(javax.servlet.DispatcherType) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) Assert(org.junit.Assert) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) ByteBufferContentProvider(org.eclipse.jetty.client.util.ByteBufferContentProvider) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 90 with Request

use of org.eclipse.jetty.client.api.Request in project jetty.project by eclipse.

the class HttpClientURITest method testPathWithQueryAndParam.

@Test
public void testPathWithQueryAndParam() throws Exception {
    String name1 = "a";
    String value1 = "1";
    String name2 = "b";
    String value2 = "2";
    final String query = name1 + "=" + value1 + "&" + name2 + "=" + value2;
    final String path = "/path";
    String pathQuery = path + "?" + query;
    start(new AbstractHandler() {

        @Override
        public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            Assert.assertEquals(path, request.getRequestURI());
            Assert.assertEquals(query, request.getQueryString());
        }
    });
    Request request = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).timeout(5, TimeUnit.SECONDS).path(path + "?" + name1 + "=" + value1).param(name2, value2);
    Assert.assertEquals(path, request.getPath());
    Assert.assertEquals(query, request.getQuery());
    Assert.assertTrue(request.getURI().toString().endsWith(pathQuery));
    Fields params = request.getParams();
    Assert.assertEquals(2, params.getSize());
    Assert.assertEquals(value1, params.get(name1).getValue());
    Assert.assertEquals(value2, params.get(name2).getValue());
    ContentResponse response = request.send();
    Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Fields(org.eclipse.jetty.util.Fields) Test(org.junit.Test)

Aggregations

Request (org.eclipse.jetty.client.api.Request)223 Test (org.junit.Test)137 HttpServletRequest (javax.servlet.http.HttpServletRequest)121 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)118 HttpServletResponse (javax.servlet.http.HttpServletResponse)63 IOException (java.io.IOException)62 CountDownLatch (java.util.concurrent.CountDownLatch)58 ServletException (javax.servlet.ServletException)53 HttpClient (org.eclipse.jetty.client.HttpClient)48 Response (org.eclipse.jetty.client.api.Response)37 Result (org.eclipse.jetty.client.api.Result)37 InputStream (java.io.InputStream)36 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)35 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)30 FutureResponseListener (org.eclipse.jetty.client.util.FutureResponseListener)28 ByteBuffer (java.nio.ByteBuffer)27 SalesforceException (org.apache.camel.component.salesforce.api.SalesforceException)26 Connection (org.eclipse.jetty.client.api.Connection)26 BytesContentProvider (org.eclipse.jetty.client.util.BytesContentProvider)25 URI (java.net.URI)24