Java Code Examples for org.springframework.mock.web.MockHttpServletResponse#getCookie()

The following examples show how to use org.springframework.mock.web.MockHttpServletResponse#getCookie() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OAuth2AuthenticationServiceTest.java    From cubeai with Apache License 2.0 6 votes vote down vote up
@Test
public void testAuthenticationCookies() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setServerName("www.test.com");
    request.addHeader("Authorization", CLIENT_AUTHORIZATION);
    Map<String, String> params = new HashMap<>();
    params.put("username", "user");
    params.put("password", "user");
    params.put("rememberMe", "true");
    MockHttpServletResponse response = new MockHttpServletResponse();
    authenticationService.authenticate(request, response, params);
    //check that cookies are set correctly
    Cookie accessTokenCookie = response.getCookie(OAuth2CookieHelper.ACCESS_TOKEN_COOKIE);
    Assert.assertEquals(ACCESS_TOKEN_VALUE, accessTokenCookie.getValue());
    Cookie refreshTokenCookie = response.getCookie(OAuth2CookieHelper.REFRESH_TOKEN_COOKIE);
    Assert.assertEquals(REFRESH_TOKEN_VALUE, OAuth2CookieHelper.getRefreshTokenValue(refreshTokenCookie));
    Assert.assertTrue(OAuth2CookieHelper.isRememberMe(refreshTokenCookie));
}
 
Example 2
Source File: OAuth2AuthenticationServiceTest.java    From cubeai with Apache License 2.0 6 votes vote down vote up
@Test
public void testAuthenticationNoRememberMe() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setServerName("www.test.com");
    Map<String, String> params = new HashMap<>();
    params.put("username", "user");
    params.put("password", "user");
    params.put("rememberMe", "false");
    MockHttpServletResponse response = new MockHttpServletResponse();
    authenticationService.authenticate(request, response, params);
    //check that cookies are set correctly
    Cookie accessTokenCookie = response.getCookie(OAuth2CookieHelper.ACCESS_TOKEN_COOKIE);
    Assert.assertEquals(ACCESS_TOKEN_VALUE, accessTokenCookie.getValue());
    Cookie refreshTokenCookie = response.getCookie(OAuth2CookieHelper.SESSION_TOKEN_COOKIE);
    Assert.assertEquals(REFRESH_TOKEN_VALUE, OAuth2CookieHelper.getRefreshTokenValue(refreshTokenCookie));
    Assert.assertFalse(OAuth2CookieHelper.isRememberMe(refreshTokenCookie));
}
 
Example 3
Source File: OAuth2AuthenticationServiceTest.java    From cubeai with Apache License 2.0 6 votes vote down vote up
@Test
public void testSessionExpired() {
    MockHttpServletRequest request = new MockHttpServletRequest(HttpMethod.GET.name(), "http://www.test.com");
    Cookie accessTokenCookie = new Cookie(OAuth2CookieHelper.ACCESS_TOKEN_COOKIE, ACCESS_TOKEN_VALUE);
    Cookie refreshTokenCookie = new Cookie(OAuth2CookieHelper.SESSION_TOKEN_COOKIE, EXPIRED_SESSION_TOKEN_VALUE);
    request.setCookies(accessTokenCookie, refreshTokenCookie);
    MockHttpServletResponse response = new MockHttpServletResponse();
    HttpServletRequest newRequest = refreshTokenFilter.refreshTokensIfExpiring(request, response);
    //cookies in response are deleted
    Cookie newAccessTokenCookie = response.getCookie(OAuth2CookieHelper.ACCESS_TOKEN_COOKIE);
    Assert.assertEquals(0, newAccessTokenCookie.getMaxAge());
    Cookie newRefreshTokenCookie = response.getCookie(OAuth2CookieHelper.REFRESH_TOKEN_COOKIE);
    Assert.assertEquals(0, newRefreshTokenCookie.getMaxAge());
    //request no longer contains cookies
    Cookie requestAccessTokenCookie = OAuth2CookieHelper.getAccessTokenCookie(newRequest);
    Assert.assertNull(requestAccessTokenCookie);
    Cookie requestRefreshTokenCookie = OAuth2CookieHelper.getRefreshTokenCookie(newRequest);
    Assert.assertNull(requestRefreshTokenCookie);
}
 
Example 4
Source File: ApiCatalogLogoutSuccessHandlerTest.java    From api-layer with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testOnLogoutSuccess() {
    MockHttpServletRequest httpServletRequest = new MockHttpServletRequest();
    MockHttpSession mockHttpSession = new MockHttpSession();
    httpServletRequest.setSession(mockHttpSession);

    MockHttpServletResponse httpServletResponse = new MockHttpServletResponse();

    AuthConfigurationProperties securityConfigurationProperties = new AuthConfigurationProperties();
    ApiCatalogLogoutSuccessHandler apiCatalogLogoutSuccessHandler = new ApiCatalogLogoutSuccessHandler(securityConfigurationProperties);

    apiCatalogLogoutSuccessHandler.onLogoutSuccess(
        httpServletRequest,
        httpServletResponse,
        new TokenAuthentication("TEST_TOKEN_STRING")
    );

    assertTrue(mockHttpSession.isInvalid());
    assertEquals(HttpStatus.OK.value(), httpServletResponse.getStatus());

    Cookie cookie = httpServletResponse.getCookie(
        securityConfigurationProperties.getCookieProperties().getCookieName());
    assertNotNull(cookie);
    assertTrue(cookie.getSecure());
    assertTrue(cookie.isHttpOnly());
}
 
Example 5
Source File: OAuth2AuthenticationServiceTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void testAuthenticationCookies() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setServerName("www.test.com");
    request.addHeader("Authorization", CLIENT_AUTHORIZATION);
    Map<String, String> params = new HashMap<>();
    params.put("username", "user");
    params.put("password", "user");
    params.put("rememberMe", "true");
    MockHttpServletResponse response = new MockHttpServletResponse();
    authenticationService.authenticate(request, response, params);
    //check that cookies are set correctly
    Cookie accessTokenCookie = response.getCookie(OAuth2CookieHelper.ACCESS_TOKEN_COOKIE);
    Assert.assertEquals(ACCESS_TOKEN_VALUE, accessTokenCookie.getValue());
    Cookie refreshTokenCookie = response.getCookie(OAuth2CookieHelper.REFRESH_TOKEN_COOKIE);
    Assert.assertEquals(REFRESH_TOKEN_VALUE, OAuth2CookieHelper.getRefreshTokenValue(refreshTokenCookie));
    Assert.assertTrue(OAuth2CookieHelper.isRememberMe(refreshTokenCookie));
}
 
Example 6
Source File: OAuth2AuthenticationServiceTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void testAuthenticationNoRememberMe() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setServerName("www.test.com");
    Map<String, String> params = new HashMap<>();
    params.put("username", "user");
    params.put("password", "user");
    params.put("rememberMe", "false");
    MockHttpServletResponse response = new MockHttpServletResponse();
    authenticationService.authenticate(request, response, params);
    //check that cookies are set correctly
    Cookie accessTokenCookie = response.getCookie(OAuth2CookieHelper.ACCESS_TOKEN_COOKIE);
    Assert.assertEquals(ACCESS_TOKEN_VALUE, accessTokenCookie.getValue());
    Cookie refreshTokenCookie = response.getCookie(OAuth2CookieHelper.SESSION_TOKEN_COOKIE);
    Assert.assertEquals(REFRESH_TOKEN_VALUE, OAuth2CookieHelper.getRefreshTokenValue(refreshTokenCookie));
    Assert.assertFalse(OAuth2CookieHelper.isRememberMe(refreshTokenCookie));
}
 
Example 7
Source File: OAuth2AuthenticationServiceTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void testSessionExpired() {
    MockHttpServletRequest request = new MockHttpServletRequest(HttpMethod.GET.name(), "http://www.test.com");
    Cookie accessTokenCookie = new Cookie(OAuth2CookieHelper.ACCESS_TOKEN_COOKIE, ACCESS_TOKEN_VALUE);
    Cookie refreshTokenCookie = new Cookie(OAuth2CookieHelper.SESSION_TOKEN_COOKIE, EXPIRED_SESSION_TOKEN_VALUE);
    request.setCookies(accessTokenCookie, refreshTokenCookie);
    MockHttpServletResponse response = new MockHttpServletResponse();
    HttpServletRequest newRequest = refreshTokenFilter.refreshTokensIfExpiring(request, response);
    //cookies in response are deleted
    Cookie newAccessTokenCookie = response.getCookie(OAuth2CookieHelper.ACCESS_TOKEN_COOKIE);
    Assert.assertEquals(0, newAccessTokenCookie.getMaxAge());
    Cookie newRefreshTokenCookie = response.getCookie(OAuth2CookieHelper.REFRESH_TOKEN_COOKIE);
    Assert.assertEquals(0, newRefreshTokenCookie.getMaxAge());
    //request no longer contains cookies
    Cookie requestAccessTokenCookie = OAuth2CookieHelper.getAccessTokenCookie(newRequest);
    Assert.assertNull(requestAccessTokenCookie);
    Cookie requestRefreshTokenCookie = OAuth2CookieHelper.getRefreshTokenCookie(newRequest);
    Assert.assertNull(requestRefreshTokenCookie);
}
 
Example 8
Source File: OAuth2AuthenticationServiceTest.java    From cubeai with Apache License 2.0 5 votes vote down vote up
@Test
public void testRefreshGrant() {
    MockHttpServletRequest request = createMockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    HttpServletRequest newRequest = refreshTokenFilter.refreshTokensIfExpiring(request, response);
    Cookie newAccessTokenCookie = response.getCookie(OAuth2CookieHelper.ACCESS_TOKEN_COOKIE);
    Assert.assertEquals(NEW_ACCESS_TOKEN_VALUE, newAccessTokenCookie.getValue());
    Cookie newRefreshTokenCookie = response.getCookie(OAuth2CookieHelper.REFRESH_TOKEN_COOKIE);
    Assert.assertEquals(NEW_REFRESH_TOKEN_VALUE, newRefreshTokenCookie.getValue());
    Cookie requestAccessTokenCookie = OAuth2CookieHelper.getAccessTokenCookie(newRequest);
    Assert.assertEquals(NEW_ACCESS_TOKEN_VALUE, requestAccessTokenCookie.getValue());
}
 
Example 9
Source File: OAuth2AuthenticationServiceTest.java    From cubeai with Apache License 2.0 5 votes vote down vote up
@Test
public void testLogout() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    Cookie accessTokenCookie = new Cookie(OAuth2CookieHelper.ACCESS_TOKEN_COOKIE, ACCESS_TOKEN_VALUE);
    Cookie refreshTokenCookie = new Cookie(OAuth2CookieHelper.REFRESH_TOKEN_COOKIE, REFRESH_TOKEN_VALUE);
    request.setCookies(accessTokenCookie, refreshTokenCookie);
    MockHttpServletResponse response = new MockHttpServletResponse();
    authenticationService.logout(request, response);
    Cookie newAccessTokenCookie = response.getCookie(OAuth2CookieHelper.ACCESS_TOKEN_COOKIE);
    Assert.assertEquals(0, newAccessTokenCookie.getMaxAge());
    Cookie newRefreshTokenCookie = response.getCookie(OAuth2CookieHelper.REFRESH_TOKEN_COOKIE);
    Assert.assertEquals(0, newRefreshTokenCookie.getMaxAge());
}
 
Example 10
Source File: CookieRetrievingCookieGeneratorTests.java    From springboot-shiro-cas-mybatis with MIT License 5 votes vote down vote up
@Test
public void verifyCookieAddWithRememberMe() {
    final MockHttpServletRequest request = new MockHttpServletRequest();
    request.addParameter(RememberMeCredential.REQUEST_PARAMETER_REMEMBER_ME, "true");
    final MockHttpServletResponse response = new MockHttpServletResponse();

    this.generator.addCookie(request, response, "test");
    request.setCookies(response.getCookies());

    final Cookie c = response.getCookie("test");
    assertEquals(100, c.getMaxAge());
    assertEquals("test", this.generator.retrieveCookieValue(request));
}
 
Example 11
Source File: CookieRetrievingCookieGeneratorTests.java    From springboot-shiro-cas-mybatis with MIT License 5 votes vote down vote up
@Test
public void verifyCookieAddWithoutRememberMe() {
    final MockHttpServletRequest request = new MockHttpServletRequest();
    final MockHttpServletResponse response = new MockHttpServletResponse();
    this.generator.addCookie(request, response, "test");
    request.setCookies(response.getCookies());

    final Cookie c = response.getCookie("test");
    assertEquals(5, c.getMaxAge());
    assertEquals("test", this.generator.retrieveCookieValue(request));
}
 
Example 12
Source File: CookieRetrievingCookieGeneratorTests.java    From cas4.0.x-server-wechat with Apache License 2.0 5 votes vote down vote up
@Test
public void testCookieAddWithRememberMe() {
    final MockHttpServletRequest request = new MockHttpServletRequest();
    request.addParameter(RememberMeCredential.REQUEST_PARAMETER_REMEMBER_ME, "true");
    final MockHttpServletResponse response = new MockHttpServletResponse();

    this.g.addCookie(request, response, "test");

    final Cookie c = response.getCookie("test");
    assertEquals(100, c.getMaxAge());
    assertEquals("test", c.getValue());
}
 
Example 13
Source File: CookieRetrievingCookieGeneratorTests.java    From cas4.0.x-server-wechat with Apache License 2.0 5 votes vote down vote up
@Test
public void testCookieAddWithoutRememberMe() {
    final MockHttpServletRequest request = new MockHttpServletRequest();
    final MockHttpServletResponse response = new MockHttpServletResponse();

    this.g.addCookie(request, response, "test");

    final Cookie c = response.getCookie("test");
    assertEquals(5, c.getMaxAge());
    assertEquals("test", c.getValue());
}
 
Example 14
Source File: OAuth2AuthenticationServiceTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void testRefreshGrant() {
    MockHttpServletRequest request = createMockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    HttpServletRequest newRequest = refreshTokenFilter.refreshTokensIfExpiring(request, response);
    Cookie newAccessTokenCookie = response.getCookie(OAuth2CookieHelper.ACCESS_TOKEN_COOKIE);
    Assert.assertEquals(NEW_ACCESS_TOKEN_VALUE, newAccessTokenCookie.getValue());
    Cookie newRefreshTokenCookie = response.getCookie(OAuth2CookieHelper.REFRESH_TOKEN_COOKIE);
    Assert.assertEquals(NEW_REFRESH_TOKEN_VALUE, newRefreshTokenCookie.getValue());
    Cookie requestAccessTokenCookie = OAuth2CookieHelper.getAccessTokenCookie(newRequest);
    Assert.assertEquals(NEW_ACCESS_TOKEN_VALUE, requestAccessTokenCookie.getValue());
}
 
Example 15
Source File: OAuth2AuthenticationServiceTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void testLogout() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    Cookie accessTokenCookie = new Cookie(OAuth2CookieHelper.ACCESS_TOKEN_COOKIE, ACCESS_TOKEN_VALUE);
    Cookie refreshTokenCookie = new Cookie(OAuth2CookieHelper.REFRESH_TOKEN_COOKIE, REFRESH_TOKEN_VALUE);
    request.setCookies(accessTokenCookie, refreshTokenCookie);
    MockHttpServletResponse response = new MockHttpServletResponse();
    authenticationService.logout(request, response);
    Cookie newAccessTokenCookie = response.getCookie(OAuth2CookieHelper.ACCESS_TOKEN_COOKIE);
    Assert.assertEquals(0, newAccessTokenCookie.getMaxAge());
    Cookie newRefreshTokenCookie = response.getCookie(OAuth2CookieHelper.REFRESH_TOKEN_COOKIE);
    Assert.assertEquals(0, newRefreshTokenCookie.getMaxAge());
}