Search in sources :

Example 21 with Cookie

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

the class FirewalledResponseTests method addCookieWhenCookieValueContainsCrlfThenException.

@Test
public void addCookieWhenCookieValueContainsCrlfThenException() {
    Cookie cookie = new Cookie("foo", "foo\r\nbar");
    assertThatIllegalArgumentException().isThrownBy(() -> this.fwResponse.addCookie(cookie)).withMessageContaining(CRLF_MESSAGE);
}
Also used : Cookie(jakarta.servlet.http.Cookie) Test(org.junit.jupiter.api.Test)

Example 22 with Cookie

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

the class FirewalledResponseTests method addCookieWhenCookieNameContainsCrlfThenException.

@Test
public void addCookieWhenCookieNameContainsCrlfThenException() {
    // Constructor validates the name
    Cookie cookie = new Cookie("valid-since-constructor-validates", "bar") {

        @Override
        public String getName() {
            return "foo\r\nbar";
        }
    };
    assertThatIllegalArgumentException().isThrownBy(() -> this.fwResponse.addCookie(cookie)).withMessageContaining(CRLF_MESSAGE);
}
Also used : Cookie(jakarta.servlet.http.Cookie) Test(org.junit.jupiter.api.Test)

Example 23 with Cookie

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

the class CookieCsrfTokenRepositoryTests method saveTokenNull.

@Test
public void saveTokenNull() {
    this.request.setSecure(true);
    this.repository.saveToken(null, this.request, this.response);
    Cookie tokenCookie = this.response.getCookie(CookieCsrfTokenRepository.DEFAULT_CSRF_COOKIE_NAME);
    assertThat(tokenCookie.getMaxAge()).isZero();
    assertThat(tokenCookie.getName()).isEqualTo(CookieCsrfTokenRepository.DEFAULT_CSRF_COOKIE_NAME);
    assertThat(tokenCookie.getPath()).isEqualTo(this.request.getContextPath());
    assertThat(tokenCookie.getSecure()).isEqualTo(this.request.isSecure());
    assertThat(tokenCookie.getValue()).isEmpty();
}
Also used : Cookie(jakarta.servlet.http.Cookie) Test(org.junit.jupiter.api.Test)

Example 24 with Cookie

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

the class CookieCsrfTokenRepositoryTests method saveTokenHttpOnlyFalse.

@Test
public void saveTokenHttpOnlyFalse() {
    this.repository.setCookieHttpOnly(false);
    CsrfToken token = this.repository.generateToken(this.request);
    this.repository.saveToken(token, this.request, this.response);
    Cookie tokenCookie = this.response.getCookie(CookieCsrfTokenRepository.DEFAULT_CSRF_COOKIE_NAME);
    assertThat(tokenCookie.isHttpOnly()).isFalse();
}
Also used : Cookie(jakarta.servlet.http.Cookie) Test(org.junit.jupiter.api.Test)

Example 25 with Cookie

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

the class CookieCsrfTokenRepositoryTests method saveTokenSecureFlagTrue.

@Test
public void saveTokenSecureFlagTrue() {
    this.request.setSecure(false);
    this.repository.setSecure(Boolean.TRUE);
    CsrfToken token = this.repository.generateToken(this.request);
    this.repository.saveToken(token, this.request, this.response);
    Cookie tokenCookie = this.response.getCookie(CookieCsrfTokenRepository.DEFAULT_CSRF_COOKIE_NAME);
    assertThat(tokenCookie.getSecure()).isTrue();
}
Also used : Cookie(jakarta.servlet.http.Cookie) 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