Search in sources :

Example 6 with Request

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

the class AsyncMiddleManServletTest method testUpstreamTransformationThrowsBeforeCommittingProxyRequest.

@Test
public void testUpstreamTransformationThrowsBeforeCommittingProxyRequest() throws Exception {
    startServer(new EchoHttpServlet());
    startProxy(new AsyncMiddleManServlet() {

        @Override
        protected ContentTransformer newClientRequestContentTransformer(HttpServletRequest clientRequest, Request proxyRequest) {
            return new ContentTransformer() {

                @Override
                public void transform(ByteBuffer input, boolean finished, List<ByteBuffer> output) throws IOException {
                    throw new NullPointerException("explicitly_thrown_by_test");
                }
            };
        }
    });
    startClient();
    byte[] bytes = new byte[1024];
    ContentResponse response = client.newRequest("localhost", serverConnector.getLocalPort()).content(new BytesContentProvider(bytes)).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(500, response.getStatus());
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException) IOException(java.io.IOException) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) ByteBuffer(java.nio.ByteBuffer) HttpServletRequest(javax.servlet.http.HttpServletRequest) Test(org.junit.Test)

Example 7 with Request

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

the class AbstractModifyMaxInactiveIntervalTest method testSessionExpiryAfterModifiedMaxInactiveInterval.

@Test
public void testSessionExpiryAfterModifiedMaxInactiveInterval() throws Exception {
    int oldMaxInactive = 4;
    int newMaxInactive = 20;
    int sleep = oldMaxInactive + (int) (oldMaxInactive * 0.8);
    DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
    cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
    SessionDataStoreFactory storeFactory = createSessionDataStoreFactory();
    ((AbstractSessionDataStoreFactory) storeFactory).setGracePeriodSec(TestServer.DEFAULT_SCAVENGE_SEC);
    TestServer server = new TestServer(0, oldMaxInactive, __scavenge, cacheFactory, storeFactory);
    ServletContextHandler ctxA = server.addContext("/mod");
    ctxA.addServlet(TestModServlet.class, "/test");
    server.start();
    int port = server.getPort();
    try {
        HttpClient client = new HttpClient();
        client.start();
        try {
            // Perform a request to create a session
            ContentResponse response = client.GET("http://localhost:" + port + "/mod/test?action=create");
            assertEquals(HttpServletResponse.SC_OK, response.getStatus());
            String sessionCookie = response.getHeaders().get("Set-Cookie");
            assertTrue(sessionCookie != null);
            // Mangle the cookie, replacing Path with $Path, etc.
            sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
            //do another request to change the maxinactive interval
            Request request = client.newRequest("http://localhost:" + port + "/mod/test?action=change&val=" + newMaxInactive);
            request.header("Cookie", sessionCookie);
            response = request.send();
            assertEquals(HttpServletResponse.SC_OK, response.getStatus());
            //wait for longer than the old inactive interval
            Thread.currentThread().sleep(sleep * 1000L);
            //do another request using the cookie to ensure the session is still there
            request = client.newRequest("http://localhost:" + port + "/mod/test?action=test&val=" + newMaxInactive);
            request.header("Cookie", sessionCookie);
            response = request.send();
            assertEquals(HttpServletResponse.SC_OK, response.getStatus());
        } finally {
            client.stop();
        }
    } finally {
        server.stop();
    }
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpClient(org.eclipse.jetty.client.HttpClient) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Test(org.junit.Test)

Example 8 with Request

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

the class AbstractModifyMaxInactiveIntervalTest method testSetMaxInactiveIntervalWithNonImmortalSessionAndEviction.

@Test
public void testSetMaxInactiveIntervalWithNonImmortalSessionAndEviction() throws Exception {
    int oldMaxInactive = 10;
    int newMaxInactive = 2;
    int evict = 4;
    int sleep = evict;
    DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
    cacheFactory.setEvictionPolicy(evict);
    SessionDataStoreFactory storeFactory = createSessionDataStoreFactory();
    ((AbstractSessionDataStoreFactory) storeFactory).setGracePeriodSec(TestServer.DEFAULT_SCAVENGE_SEC);
    TestServer server = new TestServer(0, oldMaxInactive, 1, cacheFactory, storeFactory);
    ServletContextHandler ctxA = server.addContext("/mod");
    ctxA.addServlet(TestModServlet.class, "/test");
    server.start();
    int port = server.getPort();
    try {
        HttpClient client = new HttpClient();
        client.start();
        try {
            // Perform a request to create a session
            ContentResponse response = client.GET("http://localhost:" + port + "/mod/test?action=create");
            assertEquals(HttpServletResponse.SC_OK, response.getStatus());
            String sessionCookie = response.getHeaders().get("Set-Cookie");
            assertTrue(sessionCookie != null);
            // Mangle the cookie, replacing Path with $Path, etc.
            sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
            //do another request to reduce the maxinactive interval
            Request request = client.newRequest("http://localhost:" + port + "/mod/test?action=change&val=" + newMaxInactive + "&wait=" + sleep);
            request.header("Cookie", sessionCookie);
            response = request.send();
            assertEquals(HttpServletResponse.SC_OK, response.getStatus());
            //do another request using the cookie to ensure the session is still there
            request = client.newRequest("http://localhost:" + port + "/mod/test?action=test&val=" + newMaxInactive);
            request.header("Cookie", sessionCookie);
            response = request.send();
            assertEquals(HttpServletResponse.SC_OK, response.getStatus());
        } finally {
            client.stop();
        }
    } finally {
        server.stop();
    }
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpClient(org.eclipse.jetty.client.HttpClient) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Test(org.junit.Test)

Example 9 with Request

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

the class AbstractModifyMaxInactiveIntervalTest method testSetMaxInactiveIntervalWithImmortalSessionAndEviction.

@Test
public void testSetMaxInactiveIntervalWithImmortalSessionAndEviction() throws Exception {
    int oldMaxInactive = -1;
    //2min
    int newMaxInactive = 120;
    int evict = 2;
    int sleep = evict;
    DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
    cacheFactory.setEvictionPolicy(evict);
    SessionDataStoreFactory storeFactory = createSessionDataStoreFactory();
    ((AbstractSessionDataStoreFactory) storeFactory).setGracePeriodSec(TestServer.DEFAULT_SCAVENGE_SEC);
    TestServer server = new TestServer(0, oldMaxInactive, 1, cacheFactory, storeFactory);
    ServletContextHandler ctxA = server.addContext("/mod");
    ctxA.addServlet(TestModServlet.class, "/test");
    server.start();
    int port = server.getPort();
    try {
        HttpClient client = new HttpClient();
        client.start();
        try {
            // Perform a request to create a session
            ContentResponse response = client.GET("http://localhost:" + port + "/mod/test?action=create");
            assertEquals(HttpServletResponse.SC_OK, response.getStatus());
            String sessionCookie = response.getHeaders().get("Set-Cookie");
            assertTrue(sessionCookie != null);
            // Mangle the cookie, replacing Path with $Path, etc.
            sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
            //do another request to reduce the maxinactive interval
            Request request = client.newRequest("http://localhost:" + port + "/mod/test?action=change&val=" + newMaxInactive + "&wait=" + sleep);
            request.header("Cookie", sessionCookie);
            response = request.send();
            assertEquals(HttpServletResponse.SC_OK, response.getStatus());
            //do another request using the cookie to ensure the session is still there
            request = client.newRequest("http://localhost:" + port + "/mod/test?action=test&val=" + newMaxInactive);
            request.header("Cookie", sessionCookie);
            response = request.send();
            assertEquals(HttpServletResponse.SC_OK, response.getStatus());
        } finally {
            client.stop();
        }
    } finally {
        server.stop();
    }
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpClient(org.eclipse.jetty.client.HttpClient) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Test(org.junit.Test)

Example 10 with Request

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

the class AbstractProxySerializationTest method testProxySerialization.

@Test
public void testProxySerialization() throws Exception {
    String contextPath = "";
    String servletMapping = "/server";
    int scavengePeriod = 10;
    DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
    cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
    SessionDataStoreFactory storeFactory = createSessionDataStoreFactory();
    ((AbstractSessionDataStoreFactory) storeFactory).setGracePeriodSec(scavengePeriod);
    TestServer server = new TestServer(0, 20, scavengePeriod, cacheFactory, storeFactory);
    ServletContextHandler context = server.addContext(contextPath);
    InputStream is = this.getClass().getClassLoader().getResourceAsStream("proxy-serialization.jar");
    File testDir = MavenTestingUtils.getTargetTestingDir("proxy-serialization");
    testDir.mkdirs();
    File extractedJar = new File(testDir, "proxy-serialization.jar");
    extractedJar.createNewFile();
    IO.copy(is, new FileOutputStream(extractedJar));
    URLClassLoader loader = new URLClassLoader(new URL[] { extractedJar.toURI().toURL() }, Thread.currentThread().getContextClassLoader());
    context.setClassLoader(loader);
    context.addServlet("TestServlet", servletMapping);
    customizeContext(context);
    try {
        server.start();
        int port = server.getPort();
        HttpClient client = new HttpClient();
        client.start();
        try {
            ContentResponse response = client.GET("http://localhost:" + port + contextPath + servletMapping + "?action=create");
            assertEquals(HttpServletResponse.SC_OK, response.getStatus());
            String sessionCookie = response.getHeaders().get("Set-Cookie");
            assertTrue(sessionCookie != null);
            // Mangle the cookie, replacing Path with $Path, etc.
            sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
            //stop the context to be sure the sesssion will be passivated
            context.stop();
            //restart the context
            context.start();
            // Make another request using the session id from before
            Request request = client.newRequest("http://localhost:" + port + contextPath + servletMapping + "?action=test");
            request.header("Cookie", sessionCookie);
            response = request.send();
            assertEquals(HttpServletResponse.SC_OK, response.getStatus());
        } finally {
            client.stop();
        }
    } finally {
        server.stop();
    }
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) InputStream(java.io.InputStream) Request(org.eclipse.jetty.client.api.Request) FileOutputStream(java.io.FileOutputStream) URLClassLoader(java.net.URLClassLoader) HttpClient(org.eclipse.jetty.client.HttpClient) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) File(java.io.File) Test(org.junit.Test)

Aggregations

Request (org.eclipse.jetty.client.api.Request)259 Test (org.junit.Test)145 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)132 HttpServletRequest (javax.servlet.http.HttpServletRequest)123 IOException (java.io.IOException)71 HttpServletResponse (javax.servlet.http.HttpServletResponse)65 CountDownLatch (java.util.concurrent.CountDownLatch)58 HttpClient (org.eclipse.jetty.client.HttpClient)58 ServletException (javax.servlet.ServletException)55 Response (org.eclipse.jetty.client.api.Response)45 InputStream (java.io.InputStream)39 Result (org.eclipse.jetty.client.api.Result)38 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)35 URI (java.net.URI)34 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)30 ExecutionException (java.util.concurrent.ExecutionException)28 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