Search in sources :

Example 11 with Cookie

use of jakarta.servlet.http.Cookie in project spring-security by spring-projects.

the class CookieClearingLogoutHandlerTests method invalidAge.

@Test
public void invalidAge() {
    Cookie cookie1 = new Cookie("my_cookie", null);
    cookie1.setPath("/foo");
    cookie1.setMaxAge(100);
    assertThatIllegalArgumentException().isThrownBy(() -> new CookieClearingLogoutHandler(cookie1));
}
Also used : Cookie(jakarta.servlet.http.Cookie) Test(org.junit.jupiter.api.Test)

Example 12 with Cookie

use of jakarta.servlet.http.Cookie in project spring-security by spring-projects.

the class CookieClearingLogoutHandlerTests method emptyContextRootIsConverted.

// SEC-2036
@Test
public void emptyContextRootIsConverted() {
    MockHttpServletResponse response = new MockHttpServletResponse();
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setContextPath("");
    CookieClearingLogoutHandler handler = new CookieClearingLogoutHandler("my_cookie");
    handler.logout(request, response, mock(Authentication.class));
    assertThat(response.getCookies()).hasSize(1);
    for (Cookie c : response.getCookies()) {
        assertThat(c.getPath()).isEqualTo("/");
        assertThat(c.getMaxAge()).isZero();
    }
}
Also used : Cookie(jakarta.servlet.http.Cookie) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Authentication(org.springframework.security.core.Authentication) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 13 with Cookie

use of jakarta.servlet.http.Cookie in project spring-security by spring-projects.

the class CookieClearingLogoutHandlerTests method configuredCookiesAreCleared.

@Test
public void configuredCookiesAreCleared() {
    MockHttpServletResponse response = new MockHttpServletResponse();
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setContextPath("/app");
    CookieClearingLogoutHandler handler = new CookieClearingLogoutHandler("my_cookie", "my_cookie_too");
    handler.logout(request, response, mock(Authentication.class));
    assertThat(response.getCookies()).hasSize(2);
    for (Cookie c : response.getCookies()) {
        assertThat(c.getPath()).isEqualTo("/app");
        assertThat(c.getMaxAge()).isZero();
    }
}
Also used : Cookie(jakarta.servlet.http.Cookie) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Authentication(org.springframework.security.core.Authentication) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 14 with Cookie

use of jakarta.servlet.http.Cookie in project spring-security by spring-projects.

the class CookieMixinTests method serializeCookie.

// @formatter:on
@Test
public void serializeCookie() throws JsonProcessingException, JSONException {
    Cookie cookie = new Cookie("demo", "cookie1");
    String actualString = this.mapper.writeValueAsString(cookie);
    JSONAssert.assertEquals(COOKIE_JSON, actualString, true);
}
Also used : Cookie(jakarta.servlet.http.Cookie) Test(org.junit.jupiter.api.Test)

Example 15 with Cookie

use of jakarta.servlet.http.Cookie in project spring-security by spring-projects.

the class DefaultSavedRequestMixinTests method matchRequestBuildWithConstructorAndBuilder.

// @formatter:on
@Test
public void matchRequestBuildWithConstructorAndBuilder() {
    DefaultSavedRequest request = new DefaultSavedRequest.Builder().setCookies(Collections.singletonList(new SavedCookie(new Cookie("SESSION", "123456789")))).setHeaders(Collections.singletonMap("x-auth-token", Collections.singletonList("12"))).setScheme("http").setRequestURL("http://localhost").setServerName("localhost").setRequestURI("").setLocales(Collections.singletonList(new Locale("en"))).setContextPath("").setMethod("").setServletPath("").build();
    MockHttpServletRequest mockRequest = new MockHttpServletRequest();
    mockRequest.setCookies(new Cookie("SESSION", "123456789"));
    mockRequest.addHeader("x-auth-token", "12");
    assertThat(request.doesRequestMatch(mockRequest, new PortResolverImpl())).isTrue();
}
Also used : SavedCookie(org.springframework.security.web.savedrequest.SavedCookie) Cookie(jakarta.servlet.http.Cookie) Locale(java.util.Locale) PortResolverImpl(org.springframework.security.web.PortResolverImpl) SavedCookie(org.springframework.security.web.savedrequest.SavedCookie) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) DefaultSavedRequest(org.springframework.security.web.savedrequest.DefaultSavedRequest) Test(org.junit.jupiter.api.Test)

Aggregations

Cookie (jakarta.servlet.http.Cookie)197 Test (org.junit.jupiter.api.Test)137 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)45 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)40 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)30 Locale (java.util.Locale)19 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)19 MvcResult (org.springframework.test.web.servlet.MvcResult)15 Authentication (org.springframework.security.core.Authentication)11 Test (org.junit.Test)10 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)9 MockHttpServletRequestBuilder (org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder)9 IOException (java.io.IOException)8 LocaleContext (org.springframework.context.i18n.LocaleContext)8 SimpleLocaleContext (org.springframework.context.i18n.SimpleLocaleContext)8 SimpleTimeZoneAwareLocaleContext (org.springframework.context.i18n.SimpleTimeZoneAwareLocaleContext)8 TimeZoneAwareLocaleContext (org.springframework.context.i18n.TimeZoneAwareLocaleContext)8 Map (java.util.Map)6 SavedCookie (org.springframework.security.web.savedrequest.SavedCookie)6 ModelAndView (org.springframework.web.servlet.ModelAndView)6