Example usage for org.springframework.mock.web MockHttpServletResponse getCookie

List of usage examples for org.springframework.mock.web MockHttpServletResponse getCookie

Introduction

In this page you can find the example usage for org.springframework.mock.web MockHttpServletResponse getCookie.

Prototype

@Nullable
    public Cookie getCookie(String name) 

Source Link

Usage

From source file:fragment.web.AuthenticationControllerTest.java

@SuppressWarnings("unchecked")
@Test/* w  w  w  .j  ava2  s  .  com*/
public void testLoggedOutWithSuffix() throws Exception {
    asRoot();
    Tenant defaultTenant = getDefaultTenant();
    defaultTenant.setUsernameSuffix("test");
    tenantService.save(defaultTenant);

    com.vmops.model.Configuration configuration = configurationService
            .locateConfigurationByName(Names.com_citrix_cpbm_username_duplicate_allowed);
    configuration.setValue("true");
    configurationService.update(configuration);

    User user = getSystemTenant().getOwner();
    MockHttpServletResponse response = new MockHttpServletResponse();
    MockHttpServletRequest request = new MockHttpServletRequest();
    String view = controller.loggedout(user.getUuid(), map, session, response, request);
    Cookie cookie = response.getCookie("JforumSSO");
    Assert.assertEquals(cookie.getValue(), "");
    if (config.getAuthenticationService().compareToIgnoreCase("cas") == 0) {
        Assert.assertEquals("redirect:" + config.getCasLogoutUrl() + "?service="
                + URLEncoder.encode(config.getCasServiceUrl(), "UTF-8"), view);
    } else {
        Assert.assertEquals("redirect:/j_spring_security_logout", view);
    }

    List<String> suffixList = (List<String>) map.get("suffixList");
    Assert.assertEquals(1, suffixList.size());

    defaultTenant = getDefaultTenant();
    defaultTenant.setUsernameSuffix(null);
    tenantService.save(defaultTenant);

}

From source file:fragment.web.HomeControllerTest.java

@Test
public void testForum() throws Exception {

    MockHttpServletResponse mockResponse = new MockHttpServletResponse();
    controller.forum(mockResponse, map);
    Object found = map.get("forumContext");
    Assert.assertNotNull(found);/*from  ww w. j a  v a2 s .com*/
    Assert.assertNotNull(mockResponse.getCookie("JforumSSO"));

}

From source file:org.craftercms.security.processors.impl.AddSecurityCookiesProcessorTest.java

@Test
public void testAddCookiesLoggedIn() throws Exception {
    String ticket = new ObjectId().toString();
    Date lastModified = new Date();

    Profile profile = new Profile();
    profile.setLastModified(lastModified);

    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    RequestContext context = new RequestContext(request, response);
    RequestSecurityProcessor flushResponseProcessor = new RequestSecurityProcessor() {

        @Override/*ww w  . ja v a 2 s  . c  om*/
        public void processRequest(RequestContext context, RequestSecurityProcessorChain processorChain)
                throws Exception {
            context.getResponse().getOutputStream().flush();
        }

    };

    RequestSecurityProcessorChain chain = new RequestSecurityProcessorChainImpl(
            Arrays.asList(processor, flushResponseProcessor).iterator());

    Authentication auth = new DefaultAuthentication(ticket, profile);
    SecurityUtils.setAuthentication(request, auth);

    processor.processRequest(context, chain);

    Cookie ticketCookie = response.getCookie(SecurityUtils.TICKET_COOKIE_NAME);

    assertNotNull(ticketCookie);
    assertEquals(ticket, ticketCookie.getValue());

    Cookie profileLastModifiedCookie = response.getCookie(SecurityUtils.PROFILE_LAST_MODIFIED_COOKIE_NAME);

    assertNotNull(profileLastModifiedCookie);
    assertEquals(profile.getLastModified().getTime(), Long.parseLong(profileLastModifiedCookie.getValue()));
}

From source file:org.craftercms.security.processors.impl.AddSecurityCookiesProcessorTest.java

@Test
public void testAddCookiesLoggedOut() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    RequestContext context = new RequestContext(request, response);
    RequestSecurityProcessor flushResponseProcessor = new RequestSecurityProcessor() {

        @Override/*from  www. j a  v a2  s .co  m*/
        public void processRequest(RequestContext context, RequestSecurityProcessorChain processorChain)
                throws Exception {
            context.getResponse().getOutputStream().flush();
        }

    };

    Cookie ticketCookie = new Cookie(SecurityUtils.TICKET_COOKIE_NAME, new ObjectId().toString());
    Cookie profileLastModifiedCookie = new Cookie(SecurityUtils.PROFILE_LAST_MODIFIED_COOKIE_NAME,
            String.valueOf(System.currentTimeMillis()));

    request.setCookies(ticketCookie, profileLastModifiedCookie);

    RequestSecurityProcessorChain chain = new RequestSecurityProcessorChainImpl(
            Arrays.asList(processor, flushResponseProcessor).iterator());

    processor.processRequest(context, chain);

    ticketCookie = response.getCookie(SecurityUtils.TICKET_COOKIE_NAME);

    assertNotNull(ticketCookie);
    assertEquals(null, ticketCookie.getValue());
    assertEquals(0, ticketCookie.getMaxAge());

    profileLastModifiedCookie = response.getCookie(SecurityUtils.PROFILE_LAST_MODIFIED_COOKIE_NAME);

    assertNotNull(profileLastModifiedCookie);
    assertEquals(null, profileLastModifiedCookie.getValue());
    assertEquals(0, profileLastModifiedCookie.getMaxAge());
}

From source file:org.springframework.security.web.authentication.rememberme.TokenBasedRememberMeServicesTests.java

@Test
public void autoLoginReturnsNullIfNoCookiePresented() throws Exception {
    MockHttpServletResponse response = new MockHttpServletResponse();

    Authentication result = services.autoLogin(new MockHttpServletRequest(), response);
    assertThat(result).isNull();//from ww w. jav a 2 s . c  o m
    // No cookie set
    assertThat(response.getCookie(SPRING_SECURITY_REMEMBER_ME_COOKIE_KEY)).isNull();
}

From source file:org.springframework.security.web.authentication.rememberme.TokenBasedRememberMeServicesTests.java

@Test
public void autoLoginIgnoresUnrelatedCookie() throws Exception {
    Cookie cookie = new Cookie("unrelated_cookie", "foobar");
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setCookies(cookie);/* w w  w  . j  a v a2s .com*/
    MockHttpServletResponse response = new MockHttpServletResponse();

    Authentication result = services.autoLogin(request, response);

    assertThat(result).isNull();
    assertThat(response.getCookie(SPRING_SECURITY_REMEMBER_ME_COOKIE_KEY)).isNull();
}

From source file:org.springframework.security.web.authentication.rememberme.TokenBasedRememberMeServicesTests.java

@Test
public void autoLoginReturnsNullForExpiredCookieAndClearsCookie() throws Exception {
    Cookie cookie = new Cookie(SPRING_SECURITY_REMEMBER_ME_COOKIE_KEY, generateCorrectCookieContentForToken(
            System.currentTimeMillis() - 1000000, "someone", "password", "key"));
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setCookies(cookie);/*from   www . j av  a2s  .c o m*/

    MockHttpServletResponse response = new MockHttpServletResponse();

    assertThat(services.autoLogin(request, response)).isNull();
    Cookie returnedCookie = response.getCookie(SPRING_SECURITY_REMEMBER_ME_COOKIE_KEY);
    assertThat(returnedCookie).isNotNull();
    assertThat(returnedCookie.getMaxAge()).isZero();
}

From source file:org.springframework.security.web.authentication.rememberme.TokenBasedRememberMeServicesTests.java

@Test
public void autoLoginReturnsNullAndClearsCookieIfMissingThreeTokensInCookieValue() throws Exception {
    Cookie cookie = new Cookie(SPRING_SECURITY_REMEMBER_ME_COOKIE_KEY,
            new String(Base64.encodeBase64("x".getBytes())));
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setCookies(cookie);//w  ww.  j  a va2  s  .c o m

    MockHttpServletResponse response = new MockHttpServletResponse();
    assertThat(services.autoLogin(request, response)).isNull();

    Cookie returnedCookie = response.getCookie(SPRING_SECURITY_REMEMBER_ME_COOKIE_KEY);
    assertThat(returnedCookie).isNotNull();
    assertThat(returnedCookie.getMaxAge()).isZero();
}

From source file:org.springframework.security.web.authentication.rememberme.TokenBasedRememberMeServicesTests.java

@Test
public void autoLoginClearsNonBase64EncodedCookie() throws Exception {
    Cookie cookie = new Cookie(SPRING_SECURITY_REMEMBER_ME_COOKIE_KEY, "NOT_BASE_64_ENCODED");
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setCookies(cookie);// ww w. j  ava2  s .co  m

    MockHttpServletResponse response = new MockHttpServletResponse();
    assertThat(services.autoLogin(request, response)).isNull();

    Cookie returnedCookie = response.getCookie(SPRING_SECURITY_REMEMBER_ME_COOKIE_KEY);
    assertThat(returnedCookie).isNotNull();
    assertThat(returnedCookie.getMaxAge()).isZero();
}

From source file:org.springframework.security.web.authentication.rememberme.TokenBasedRememberMeServicesTests.java

@Test
public void autoLoginClearsCookieIfSignatureBlocksDoesNotMatchExpectedValue() throws Exception {
    udsWillReturnUser();/*from w  ww.  ja v a2s. co m*/
    Cookie cookie = new Cookie(SPRING_SECURITY_REMEMBER_ME_COOKIE_KEY, generateCorrectCookieContentForToken(
            System.currentTimeMillis() + 1000000, "someone", "password", "WRONG_KEY"));
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setCookies(cookie);

    MockHttpServletResponse response = new MockHttpServletResponse();

    assertThat(services.autoLogin(request, response)).isNull();

    Cookie returnedCookie = response.getCookie(SPRING_SECURITY_REMEMBER_ME_COOKIE_KEY);
    assertThat(returnedCookie).isNotNull();
    assertThat(returnedCookie.getMaxAge()).isZero();
}