Search in sources :

Example 36 with HttpClient

use of org.eclipse.jetty.client.HttpClient in project jetty.project by eclipse.

the class DeleteUnloadableSessionTest method testDeleteUnloadableSession.

/**
     * Test that session data that can't be loaded results in a null Session object
     * @throws Exception
     */
@Test
public void testDeleteUnloadableSession() throws Exception {
    String contextPath = "";
    String servletMapping = "/server";
    int inactivePeriod = -1;
    int scavengePeriod = 100;
    DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
    cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
    cacheFactory.setRemoveUnloadableSessions(true);
    SessionDataStoreFactory storeFactory = new DelSessionDataStoreFactory();
    ((AbstractSessionDataStoreFactory) storeFactory).setGracePeriodSec(scavengePeriod);
    TestServer server = new TestServer(0, inactivePeriod, scavengePeriod, cacheFactory, storeFactory);
    ServletContextHandler context = server.addContext(contextPath);
    TestServlet servlet = new TestServlet();
    ServletHolder holder = new ServletHolder(servlet);
    context.addServlet(holder, servletMapping);
    try (StacklessLogging stackless = new StacklessLogging(Log.getLogger("org.eclipse.jetty.server.session"))) {
        server.start();
        int port = server.getPort();
        HttpClient client = new HttpClient();
        client.start();
        try {
            String sessionCookie = "JSESSIONID=w0rm3zxpa6h1zg1mevtv76b3te00.w0;$Path=/";
            Request request = client.newRequest("http://localhost:" + port + contextPath + servletMapping + "?action=test");
            request.header("Cookie", sessionCookie);
            ContentResponse response = request.send();
            assertEquals(HttpServletResponse.SC_OK, response.getStatus());
            assertFalse(context.getSessionHandler().getSessionCache().getSessionDataStore().exists(TestServer.extractSessionId(sessionCookie)));
        } finally {
            client.stop();
        }
    } finally {
        server.stop();
    }
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpClient(org.eclipse.jetty.client.HttpClient) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Test(org.junit.Test)

Example 37 with HttpClient

use of org.eclipse.jetty.client.HttpClient in project jetty.project by eclipse.

the class DirtyAttributeTest method testDirtyWrite.

@Test
public void testDirtyWrite() throws Exception {
    DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
    cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
    SessionDataStoreFactory storeFactory = new TestPassivatingSessionDataStoreFactory();
    ((AbstractSessionDataStoreFactory) storeFactory).setGracePeriodSec(SCAVENGE);
    TestServer server = new TestServer(0, INACTIVE, SCAVENGE, cacheFactory, storeFactory);
    ServletContextHandler ctxA = server.addContext("/mod");
    ctxA.addServlet(TestDirtyServlet.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 session attribute
            Request request = client.newRequest("http://localhost:" + port + "/mod/test?action=setA");
            request.header("Cookie", sessionCookie);
            response = request.send();
            assertEquals(HttpServletResponse.SC_OK, response.getStatus());
            A_VALUE.assertPassivatesEquals(1);
            A_VALUE.assertActivatesEquals(1);
            A_VALUE.assertBindsEquals(1);
            A_VALUE.assertUnbindsEquals(0);
            //do another request using the cookie to try changing the session attribute to the same value again              
            request = client.newRequest("http://localhost:" + port + "/mod/test?action=setA");
            request.header("Cookie", sessionCookie);
            response = request.send();
            assertEquals(HttpServletResponse.SC_OK, response.getStatus());
            A_VALUE.assertPassivatesEquals(2);
            A_VALUE.assertActivatesEquals(2);
            A_VALUE.assertBindsEquals(1);
            A_VALUE.assertUnbindsEquals(0);
            //do another request using the cookie and change to a different value             
            request = client.newRequest("http://localhost:" + port + "/mod/test?action=setB");
            request.header("Cookie", sessionCookie);
            response = request.send();
            assertEquals(HttpServletResponse.SC_OK, response.getStatus());
            B_VALUE.assertPassivatesEquals(1);
            B_VALUE.assertActivatesEquals(1);
            B_VALUE.assertBindsEquals(1);
            B_VALUE.assertUnbindsEquals(0);
            A_VALUE.assertBindsEquals(1);
            A_VALUE.assertUnbindsEquals(1);
        } 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 38 with HttpClient

use of org.eclipse.jetty.client.HttpClient in project jetty.project by eclipse.

the class IdleSessionTest method testSessionIdle.

/**
     * @throws Exception
     */
@Test
public void testSessionIdle() throws Exception {
    String contextPath = "";
    String servletMapping = "/server";
    int inactivePeriod = 20;
    int scavengePeriod = 3;
    int evictionSec = 5;
    DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
    cacheFactory.setEvictionPolicy(evictionSec);
    SessionDataStoreFactory storeFactory = new TestSessionDataStoreFactory();
    _server1 = new TestServer(0, inactivePeriod, scavengePeriod, cacheFactory, storeFactory);
    ServletHolder holder = new ServletHolder(_servlet);
    ServletContextHandler contextHandler = _server1.addContext(contextPath);
    contextHandler.addServlet(holder, servletMapping);
    _server1.start();
    int port1 = _server1.getPort();
    try (StacklessLogging stackless = new StacklessLogging(Log.getLogger("org.eclipse.jetty.server.session"))) {
        HttpClient client = new HttpClient();
        client.start();
        String url = "http://localhost:" + port1 + contextPath + servletMapping;
        //make a request to set up a session on the server
        ContentResponse response = client.GET(url + "?action=init");
        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=");
        //and wait until the session should be idled out
        pause(evictionSec * 3);
        //check that the session has been idled
        String id = TestServer.extractSessionId(sessionCookie);
        assertFalse(contextHandler.getSessionHandler().getSessionCache().contains(id));
        assertTrue(contextHandler.getSessionHandler().getSessionCache().getSessionDataStore().exists(id));
        //make another request to de-idle the session
        Request request = client.newRequest(url + "?action=test");
        request.getHeaders().add("Cookie", sessionCookie);
        ContentResponse response2 = request.send();
        assertEquals(HttpServletResponse.SC_OK, response2.getStatus());
        //check session de-idled
        assertTrue(contextHandler.getSessionHandler().getSessionCache().contains(id));
        //wait again for the session to be idled
        pause(evictionSec * 3);
        //check that it is
        assertFalse(contextHandler.getSessionHandler().getSessionCache().contains(id));
        assertTrue(contextHandler.getSessionHandler().getSessionCache().getSessionDataStore().exists(id));
        //While idle, take some action to ensure that a deidle won't work, like
        //deleting the sessions in the store
        ((TestSessionDataStore) contextHandler.getSessionHandler().getSessionCache().getSessionDataStore())._map.clear();
        //make a request
        request = client.newRequest(url + "?action=testfail");
        request.getHeaders().add("Cookie", sessionCookie);
        response2 = request.send();
        assertEquals(HttpServletResponse.SC_OK, response2.getStatus());
        //Test trying to de-idle an expired session (ie before the scavenger can get to it)
        //make a request to set up a session on the server
        response = client.GET(url + "?action=init");
        assertEquals(HttpServletResponse.SC_OK, response.getStatus());
        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=");
        id = TestServer.extractSessionId(sessionCookie);
        //and wait until the session should be idled out
        pause(evictionSec * 3);
        //stop the scavenger
        if (_server1.getHouseKeeper() != null)
            _server1.getHouseKeeper().stop();
        //check that the session is idle
        assertFalse(contextHandler.getSessionHandler().getSessionCache().contains(id));
        assertTrue(contextHandler.getSessionHandler().getSessionCache().getSessionDataStore().exists(id));
        //wait until the session should be expired
        pause(inactivePeriod + (3 * scavengePeriod));
        //make another request to de-idle the session
        request = client.newRequest(url + "?action=testfail");
        request.getHeaders().add("Cookie", sessionCookie);
        response2 = request.send();
        assertEquals(HttpServletResponse.SC_OK, response2.getStatus());
        assertFalse(contextHandler.getSessionHandler().getSessionCache().contains(id));
        assertFalse(contextHandler.getSessionHandler().getSessionCache().getSessionDataStore().exists(id));
    } finally {
        _server1.stop();
    }
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) HttpClient(org.eclipse.jetty.client.HttpClient) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Test(org.junit.Test)

Example 39 with HttpClient

use of org.eclipse.jetty.client.HttpClient in project jetty.project by eclipse.

the class DataSourceLoginServiceTest method startClient.

protected void startClient(String user, String pwd) throws Exception {
    _client = new HttpClient();
    QueuedThreadPool executor = new QueuedThreadPool();
    executor.setName(executor.getName() + "-client");
    _client.setExecutor(executor);
    AuthenticationStore authStore = _client.getAuthenticationStore();
    authStore.addAuthentication(new BasicAuthentication(_baseUri, __realm, user, pwd));
    _client.start();
}
Also used : QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) HttpClient(org.eclipse.jetty.client.HttpClient) BasicAuthentication(org.eclipse.jetty.client.util.BasicAuthentication) AuthenticationStore(org.eclipse.jetty.client.api.AuthenticationStore)

Example 40 with HttpClient

use of org.eclipse.jetty.client.HttpClient in project jetty.project by eclipse.

the class JdbcLoginServiceTest method startClient.

protected void startClient(String user, String pwd) throws Exception {
    _client = new HttpClient();
    QueuedThreadPool executor = new QueuedThreadPool();
    executor.setName(executor.getName() + "-client");
    _client.setExecutor(executor);
    AuthenticationStore authStore = _client.getAuthenticationStore();
    authStore.addAuthentication(new BasicAuthentication(_baseUri, __realm, user, pwd));
    _client.start();
}
Also used : QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) HttpClient(org.eclipse.jetty.client.HttpClient) BasicAuthentication(org.eclipse.jetty.client.util.BasicAuthentication) AuthenticationStore(org.eclipse.jetty.client.api.AuthenticationStore)

Aggregations

HttpClient (org.eclipse.jetty.client.HttpClient)172 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)98 Test (org.junit.Test)90 Request (org.eclipse.jetty.client.api.Request)51 HttpServletRequest (javax.servlet.http.HttpServletRequest)42 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)40 Test (org.testng.annotations.Test)31 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)23 QueuedThreadPool (org.eclipse.jetty.util.thread.QueuedThreadPool)18 CloudStore (com.yahoo.athenz.zts.store.CloudStore)17 HttpCertSigner (com.yahoo.athenz.zts.cert.impl.HttpCertSigner)14 HttpCertSignerFactory (com.yahoo.athenz.zts.cert.impl.HttpCertSignerFactory)14 SslContextFactory (org.eclipse.jetty.util.ssl.SslContextFactory)12 HTTP2Client (org.eclipse.jetty.http2.client.HTTP2Client)10 URI (java.net.URI)8 StacklessLogging (org.eclipse.jetty.util.log.StacklessLogging)8 CountDownLatch (java.util.concurrent.CountDownLatch)7 HttpProxy (org.eclipse.jetty.client.HttpProxy)7 Before (org.junit.Before)7 HttpServletResponse (javax.servlet.http.HttpServletResponse)6