Search in sources :

Example 1 with MockHttpServletRequest

use of com.mockrunner.mock.web.MockHttpServletRequest in project geode by apache.

the class CommonTests method testIsRequestedSessionIdFromCookie.

@Test
public void testIsRequestedSessionIdFromCookie() {
    MockHttpServletRequest mRequest = getWebMockObjectFactory().getMockRequest();
    Cookie c = new Cookie("JSESSIONID", "1-GF");
    mRequest.addCookie(c);
    doFilter();
    HttpServletRequest request = (HttpServletRequest) getFilteredRequest();
    request.getSession();
    assertTrue(request.isRequestedSessionIdFromCookie());
}
Also used : Cookie(javax.servlet.http.Cookie) MockHttpServletRequest(com.mockrunner.mock.web.MockHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(com.mockrunner.mock.web.MockHttpServletRequest) Test(org.junit.Test)

Example 2 with MockHttpServletRequest

use of com.mockrunner.mock.web.MockHttpServletRequest in project geode by apache.

the class CommonTests method testGetLastAccessedTime2.

/**
   * Test that repeated accesses update the last accessed time
   */
@Test
public void testGetLastAccessedTime2() throws Exception {
    // Setup
    CallbackServlet s = (CallbackServlet) getServlet();
    s.setCallback(new Callback() {

        @Override
        public void call(HttpServletRequest request, HttpServletResponse response) {
            request.getSession();
        }
    });
    doFilter();
    HttpServletRequest request = (HttpServletRequest) getFilteredRequest();
    long lastAccess = request.getSession().getLastAccessedTime();
    assertTrue("Session should have a non-zero last access time", lastAccess > 0);
    MockHttpServletResponse response = getWebMockObjectFactory().getMockResponse();
    Cookie cookie = (Cookie) response.getCookies().get(0);
    MockHttpServletRequest mRequest = getWebMockObjectFactory().createMockRequest();
    mRequest.setRequestURL("/test/foo/bar");
    mRequest.setContextPath(CONTEXT_PATH);
    mRequest.addCookie(cookie);
    getWebMockObjectFactory().addRequestWrapper(mRequest);
    Thread.sleep(50);
    doFilter();
    assertTrue("Last access time should be changing", request.getSession().getLastAccessedTime() > lastAccess);
}
Also used : MockHttpServletRequest(com.mockrunner.mock.web.MockHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) Cookie(javax.servlet.http.Cookie) MockHttpServletRequest(com.mockrunner.mock.web.MockHttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) MockHttpServletResponse(com.mockrunner.mock.web.MockHttpServletResponse) MockHttpServletResponse(com.mockrunner.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 3 with MockHttpServletRequest

use of com.mockrunner.mock.web.MockHttpServletRequest in project geode by apache.

the class CommonTests method testIsNew2.

/**
   * Subsequent calls should not return true
   */
@Test
public void testIsNew2() throws Exception {
    // Setup
    CallbackServlet s = (CallbackServlet) getServlet();
    s.setCallback(new Callback() {

        @Override
        public void call(HttpServletRequest request, HttpServletResponse response) {
            request.getSession();
        }
    });
    doFilter();
    HttpServletRequest request = (HttpServletRequest) getFilteredRequest();
    request.getSession();
    MockHttpServletResponse response = getWebMockObjectFactory().getMockResponse();
    Cookie cookie = (Cookie) response.getCookies().get(0);
    MockHttpServletRequest mRequest = getWebMockObjectFactory().createMockRequest();
    mRequest.setRequestURL("/test/foo/bar");
    mRequest.setContextPath(CONTEXT_PATH);
    mRequest.addCookie(cookie);
    getWebMockObjectFactory().addRequestWrapper(mRequest);
    doFilter();
    request = (HttpServletRequest) getFilteredRequest();
    HttpSession s1 = request.getSession();
    assertFalse("Subsequent isNew() calls should be false", request.getSession().isNew());
}
Also used : MockHttpServletRequest(com.mockrunner.mock.web.MockHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) Cookie(javax.servlet.http.Cookie) MockHttpServletRequest(com.mockrunner.mock.web.MockHttpServletRequest) HttpSession(javax.servlet.http.HttpSession) MockHttpSession(com.mockrunner.mock.web.MockHttpSession) HttpServletResponse(javax.servlet.http.HttpServletResponse) MockHttpServletResponse(com.mockrunner.mock.web.MockHttpServletResponse) MockHttpServletResponse(com.mockrunner.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 4 with MockHttpServletRequest

use of com.mockrunner.mock.web.MockHttpServletRequest in project geode by apache.

the class CommonTests method testIsRequestedSessionIdFromURL.

@Test
public void testIsRequestedSessionIdFromURL() {
    MockHttpServletRequest mRequest = getWebMockObjectFactory().getMockRequest();
    mRequest.setRequestURL("/foo/bar;jsessionid=1");
    doFilter();
    HttpServletRequest request = (HttpServletRequest) getFilteredRequest();
    request.getSession();
    assertFalse("Session ID should not be from cookie", request.isRequestedSessionIdFromCookie());
    assertTrue("Session ID should be from URL", request.isRequestedSessionIdFromURL());
}
Also used : MockHttpServletRequest(com.mockrunner.mock.web.MockHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(com.mockrunner.mock.web.MockHttpServletRequest) Test(org.junit.Test)

Example 5 with MockHttpServletRequest

use of com.mockrunner.mock.web.MockHttpServletRequest in project cayenne by apache.

the class HessianServiceTest method testGetSession.

@Test
public void testGetSession() throws Exception {
    Map<String, String> map = new HashMap<>();
    map.put(Constants.SERVER_ROP_EVENT_BRIDGE_FACTORY_PROPERTY, MockEventBridgeFactory.class.getName());
    ObjectContextFactory factory = new ObjectContextFactory() {

        public ObjectContext createContext(DataChannel parent) {
            return null;
        }

        public ObjectContext createContext() {
            return null;
        }
    };
    HessianService service = new HessianService(factory, map);
    MockHttpServletRequest request = new MockHttpServletRequest();
    HttpSession session = new MockHttpSession();
    request.setSession(session);
    // for some reason need to call this to get session activated in the
    // mock request
    request.getSession();
    try {
        ServiceContext.begin(request, null, null, null);
        assertSame(session, service.getSession(false));
    } finally {
        ServiceContext.end();
    }
}
Also used : DataChannel(org.apache.cayenne.DataChannel) HashMap(java.util.HashMap) MockHttpServletRequest(com.mockrunner.mock.web.MockHttpServletRequest) HttpSession(javax.servlet.http.HttpSession) MockHttpSession(com.mockrunner.mock.web.MockHttpSession) MockHttpSession(com.mockrunner.mock.web.MockHttpSession) ObjectContextFactory(org.apache.cayenne.configuration.ObjectContextFactory) MockEventBridgeFactory(org.apache.cayenne.event.MockEventBridgeFactory) Test(org.junit.Test)

Aggregations

MockHttpServletRequest (com.mockrunner.mock.web.MockHttpServletRequest)7 Test (org.junit.Test)7 MockHttpServletResponse (com.mockrunner.mock.web.MockHttpServletResponse)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 MockHttpSession (com.mockrunner.mock.web.MockHttpSession)3 Cookie (javax.servlet.http.Cookie)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 HttpSession (javax.servlet.http.HttpSession)2 DataChannel (org.apache.cayenne.DataChannel)2 ObjectContextFactory (org.apache.cayenne.configuration.ObjectContextFactory)2 MockFilterChain (com.mockrunner.mock.web.MockFilterChain)1 MockFilterConfig (com.mockrunner.mock.web.MockFilterConfig)1 MockServletContext (com.mockrunner.mock.web.MockServletContext)1 HashMap (java.util.HashMap)1 BaseContext (org.apache.cayenne.BaseContext)1 MockDataChannel (org.apache.cayenne.MockDataChannel)1 ObjectContext (org.apache.cayenne.ObjectContext)1 CayenneRuntime (org.apache.cayenne.configuration.CayenneRuntime)1 DIBootstrap (org.apache.cayenne.di.DIBootstrap)1 Injector (org.apache.cayenne.di.Injector)1