Search in sources :

Example 61 with GetMethodWebRequest

use of com.meterware.httpunit.GetMethodWebRequest in project geode by apache.

the class TestSessionsBase method testLastAccessedTime.

/**
   * Test for issue #46 lastAccessedTime is not updated at the start of the request, but only at the
   * end.
   */
@Test
public void testLastAccessedTime() throws Exception {
    Callback c = new Callback() {

        @Override
        public void call(HttpServletRequest request, HttpServletResponse response) throws IOException {
            HttpSession session = request.getSession();
            // Hack to expose the session to our test context
            session.getServletContext().setAttribute("session", session);
            session.setAttribute("lastAccessTime", session.getLastAccessedTime());
            try {
                Thread.sleep(100);
            } catch (InterruptedException ex) {
            }
            session.setAttribute("somethingElse", 1);
            request.getSession();
            response.getWriter().write("done");
        }
    };
    servlet.getServletContext().setAttribute("callback", c);
    WebConversation wc = new WebConversation();
    WebRequest req = new GetMethodWebRequest(String.format("http://localhost:%d/test", port));
    // Execute the callback
    req.setParameter("cmd", QueryCommand.CALLBACK.name());
    req.setParameter("param", "callback");
    WebResponse response = wc.getResponse(req);
    HttpSession session = (HttpSession) servlet.getServletContext().getAttribute("session");
    Long lastAccess = (Long) session.getAttribute("lastAccessTime");
    assertTrue("Last access time not set correctly: " + lastAccess.longValue() + " not <= " + session.getLastAccessedTime(), lastAccess.longValue() <= session.getLastAccessedTime());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) WebResponse(com.meterware.httpunit.WebResponse) WebConversation(com.meterware.httpunit.WebConversation) WebRequest(com.meterware.httpunit.WebRequest) GetMethodWebRequest(com.meterware.httpunit.GetMethodWebRequest) HttpSession(javax.servlet.http.HttpSession) HttpServletResponse(javax.servlet.http.HttpServletResponse) GetMethodWebRequest(com.meterware.httpunit.GetMethodWebRequest) Test(org.junit.Test)

Example 62 with GetMethodWebRequest

use of com.meterware.httpunit.GetMethodWebRequest in project geode by apache.

the class TestSessionsBase method testSessionPersists1.

/**
   * Check that our session persists. The values we pass in as query params are used to set
   * attributes on the session.
   */
@Test
public void testSessionPersists1() throws Exception {
    String key = "value_testSessionPersists1";
    String value = "Foo";
    WebConversation wc = new WebConversation();
    WebRequest req = new GetMethodWebRequest(String.format("http://localhost:%d/test", port));
    req.setParameter("cmd", QueryCommand.SET.name());
    req.setParameter("param", key);
    req.setParameter("value", value);
    WebResponse response = wc.getResponse(req);
    String sessionId = response.getNewCookieValue("JSESSIONID");
    assertNotNull("No apparent session cookie", sessionId);
    // The request retains the cookie from the prior response...
    req.setParameter("cmd", QueryCommand.GET.name());
    req.setParameter("param", key);
    req.removeParameter("value");
    response = wc.getResponse(req);
    assertEquals(value, response.getText());
}
Also used : WebResponse(com.meterware.httpunit.WebResponse) WebConversation(com.meterware.httpunit.WebConversation) WebRequest(com.meterware.httpunit.WebRequest) GetMethodWebRequest(com.meterware.httpunit.GetMethodWebRequest) GetMethodWebRequest(com.meterware.httpunit.GetMethodWebRequest) Test(org.junit.Test)

Example 63 with GetMethodWebRequest

use of com.meterware.httpunit.GetMethodWebRequest in project geode by apache.

the class TestSessionsBase method testSessionExpirationByContainer.

/**
   * Test expiration of a session by the tomcat container, rather than gemfire expiration
   */
@Test
public void testSessionExpirationByContainer() throws Exception {
    String key = "value_testSessionExpiration1";
    String value = "Foo";
    WebConversation wc = new WebConversation();
    WebRequest req = new GetMethodWebRequest(String.format("http://localhost:%d/test", port));
    // Set an attribute
    req.setParameter("cmd", QueryCommand.SET.name());
    req.setParameter("param", key);
    req.setParameter("value", value);
    WebResponse response = wc.getResponse(req);
    // Set the session timeout of this one session.
    req.setParameter("cmd", QueryCommand.SET_MAX_INACTIVE.name());
    req.setParameter("value", "1");
    response = wc.getResponse(req);
    // Wait until the session should expire
    Thread.sleep(2000);
    // Do a request, which should cause the session to be expired
    req.setParameter("cmd", QueryCommand.GET.name());
    req.setParameter("param", key);
    response = wc.getResponse(req);
    assertEquals("", response.getText());
}
Also used : WebResponse(com.meterware.httpunit.WebResponse) WebConversation(com.meterware.httpunit.WebConversation) WebRequest(com.meterware.httpunit.WebRequest) GetMethodWebRequest(com.meterware.httpunit.GetMethodWebRequest) GetMethodWebRequest(com.meterware.httpunit.GetMethodWebRequest) Test(org.junit.Test)

Example 64 with GetMethodWebRequest

use of com.meterware.httpunit.GetMethodWebRequest in project geode by apache.

the class TestSessionsBase method testSanity.

/**
   * Check that the basics are working
   */
@Test
public void testSanity() throws Exception {
    WebConversation wc = new WebConversation();
    WebRequest req = new GetMethodWebRequest(String.format("http://localhost:%d/test", port));
    req.setParameter("cmd", QueryCommand.GET.name());
    req.setParameter("param", "null");
    WebResponse response = wc.getResponse(req);
    assertEquals("JSESSIONID", response.getNewCookieNames()[0]);
}
Also used : WebResponse(com.meterware.httpunit.WebResponse) WebConversation(com.meterware.httpunit.WebConversation) WebRequest(com.meterware.httpunit.WebRequest) GetMethodWebRequest(com.meterware.httpunit.GetMethodWebRequest) GetMethodWebRequest(com.meterware.httpunit.GetMethodWebRequest) Test(org.junit.Test)

Example 65 with GetMethodWebRequest

use of com.meterware.httpunit.GetMethodWebRequest in project geode by apache.

the class SessionReplicationIntegrationJUnitTest method testUserCanModifyTheirOwnCookie.

/**
   * Test that a servlet can modify cookies
   */
@Test
public void testUserCanModifyTheirOwnCookie() throws Exception {
    Callback c = new Callback() {

        @Override
        public void call(HttpServletRequest request, HttpServletResponse response) throws IOException {
            Cookie userCookie = findUserCookie(request.getCookies());
            if (userCookie == null) {
                userCookie = new Cookie("myCookie", "0");
            } else {
                userCookie = new Cookie("myCookie", Integer.toString(Integer.valueOf(userCookie.getValue()) + 1));
            }
            response.addCookie(userCookie);
            request.getSession().setAttribute("dummy", "value");
        }
    };
    tester.setAttribute("callback_1", c);
    String url = tester.createConnector(true);
    tester.start();
    WebConversation wc = new WebConversation();
    WebRequest req = new GetMethodWebRequest(url + "/test/hello");
    req.setHeaderField("Cookie", "myCookie=" + 5);
    final WebResponse webResponse = wc.getResponse(req);
    assertEquals("6", webResponse.getNewCookieValue("myCookie"));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Cookie(javax.servlet.http.Cookie) WebResponse(com.meterware.httpunit.WebResponse) WebConversation(com.meterware.httpunit.WebConversation) WebRequest(com.meterware.httpunit.WebRequest) GetMethodWebRequest(com.meterware.httpunit.GetMethodWebRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) GetMethodWebRequest(com.meterware.httpunit.GetMethodWebRequest) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Aggregations

GetMethodWebRequest (com.meterware.httpunit.GetMethodWebRequest)76 WebResponse (com.meterware.httpunit.WebResponse)73 WebRequest (com.meterware.httpunit.WebRequest)72 Test (org.junit.Test)55 WebConversation (com.meterware.httpunit.WebConversation)43 ServletUnitClient (com.meterware.servletunit.ServletUnitClient)16 TextBlock (com.meterware.httpunit.TextBlock)14 URL (java.net.URL)14 HttpServletRequest (javax.servlet.http.HttpServletRequest)14 HttpServletResponse (javax.servlet.http.HttpServletResponse)14 HttpSession (javax.servlet.http.HttpSession)9 PostMethodWebRequest (com.meterware.httpunit.PostMethodWebRequest)8 PutMethodWebRequest (com.meterware.httpunit.PutMethodWebRequest)8 WebForm (com.meterware.httpunit.WebForm)7 OperateOnDeployment (org.jboss.arquillian.container.test.api.OperateOnDeployment)5 HttpNotFoundException (com.meterware.httpunit.HttpNotFoundException)4 WebLink (com.meterware.httpunit.WebLink)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 AuthorizationRequiredException (com.meterware.httpunit.AuthorizationRequiredException)3 WebTable (com.meterware.httpunit.WebTable)3