Search in sources :

Example 1 with MockHttpServletResponse

use of net.sourceforge.stripes.mock.MockHttpServletResponse in project jspwiki by apache.

the class WikiSessionTest method testAuthenticationCookieDefaults.

@Test
public void testAuthenticationCookieDefaults() throws ServletException, IOException {
    MockHttpServletRequest request;
    WikiSession wikiSession;
    // Set the authentication cookie first
    MockHttpServletResponse response = new MockHttpServletResponse();
    CookieAuthenticationLoginModule.setLoginCookie(m_engine, response, "Fred Flintstone");
    Cookie[] cookies = response.getCookies();
    Assert.assertEquals(1, cookies.length);
    String uid = cookies[0].getValue();
    // Adding the magic "authentication cookie" should NOT count as authenticated in the default case
    // (because cookie authentication is OFF).
    request = m_engine.newHttpRequest();
    request.setUserPrincipal(null);
    request.setCookies(new Cookie[] { new Cookie("JSPWikiUID", uid) });
    runSecurityFilter(m_engine, request);
    wikiSession = WikiSession.getWikiSession(m_engine, request);
    Assert.assertTrue(wikiSession.isAnonymous());
    Assert.assertFalse(wikiSession.isAuthenticated());
    Assert.assertEquals("127.0.0.1", wikiSession.getUserPrincipal().getName());
    // Clear the authentication cookie
    response = new MockHttpServletResponse();
    CookieAuthenticationLoginModule.clearLoginCookie(m_engine, request, response);
}
Also used : Cookie(javax.servlet.http.Cookie) MockHttpServletRequest(net.sourceforge.stripes.mock.MockHttpServletRequest) MockHttpServletResponse(net.sourceforge.stripes.mock.MockHttpServletResponse) Test(org.junit.Test)

Example 2 with MockHttpServletResponse

use of net.sourceforge.stripes.mock.MockHttpServletResponse in project jspwiki by apache.

the class WikiSessionTest method testAuthenticationCookieWhenOn.

@Test
public void testAuthenticationCookieWhenOn() throws WikiException, ServletException, IOException {
    Properties props = TestEngine.getTestProperties();
    props.setProperty(AuthenticationManager.PROP_ALLOW_COOKIE_AUTH, "true");
    m_engine = new TestEngine(props);
    MockHttpServletRequest request;
    WikiSession wikiSession;
    // Set the authentication cookie first
    MockHttpServletResponse response = new MockHttpServletResponse();
    CookieAuthenticationLoginModule.setLoginCookie(m_engine, response, "Fred Flintstone");
    Cookie[] cookies = response.getCookies();
    Assert.assertEquals(1, cookies.length);
    String uid = cookies[0].getValue();
    // Adding the magic "authentication cookie" should count as authenticated
    request = m_engine.newHttpRequest();
    request.setUserPrincipal(null);
    request.setCookies(new Cookie[] { new Cookie("JSPWikiUID", uid) });
    runSecurityFilter(m_engine, request);
    wikiSession = WikiSession.getWikiSession(m_engine, request);
    Assert.assertFalse(wikiSession.isAnonymous());
    Assert.assertTrue(wikiSession.isAuthenticated());
    Assert.assertEquals("Fred Flintstone", wikiSession.getUserPrincipal().getName());
    // Clear the authentication cookie
    response = new MockHttpServletResponse();
    CookieAuthenticationLoginModule.clearLoginCookie(m_engine, request, response);
}
Also used : Cookie(javax.servlet.http.Cookie) MockHttpServletRequest(net.sourceforge.stripes.mock.MockHttpServletRequest) Properties(java.util.Properties) MockHttpServletResponse(net.sourceforge.stripes.mock.MockHttpServletResponse) Test(org.junit.Test)

Aggregations

Cookie (javax.servlet.http.Cookie)2 MockHttpServletRequest (net.sourceforge.stripes.mock.MockHttpServletRequest)2 MockHttpServletResponse (net.sourceforge.stripes.mock.MockHttpServletResponse)2 Test (org.junit.Test)2 Properties (java.util.Properties)1