Search in sources :

Example 36 with Cookie

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

the class CookieDeserializer method deserialize.

@Override
public Cookie deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
    ObjectMapper mapper = (ObjectMapper) jp.getCodec();
    JsonNode jsonNode = mapper.readTree(jp);
    Cookie cookie = new Cookie(readJsonNode(jsonNode, "name").asText(), readJsonNode(jsonNode, "value").asText());
    cookie.setComment(readJsonNode(jsonNode, "comment").asText());
    cookie.setDomain(readJsonNode(jsonNode, "domain").asText());
    cookie.setMaxAge(readJsonNode(jsonNode, "maxAge").asInt(-1));
    cookie.setSecure(readJsonNode(jsonNode, "secure").asBoolean());
    cookie.setVersion(readJsonNode(jsonNode, "version").asInt());
    cookie.setPath(readJsonNode(jsonNode, "path").asText());
    cookie.setHttpOnly(readJsonNode(jsonNode, "httpOnly").asBoolean());
    return cookie;
}
Also used : Cookie(jakarta.servlet.http.Cookie) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 37 with Cookie

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

the class SavedCookieTests method testGetCookie.

@Test
public void testGetCookie() {
    Cookie other = this.savedCookie.getCookie();
    assertThat(other.getComment()).isEqualTo(this.cookie.getComment());
    assertThat(other.getDomain()).isEqualTo(this.cookie.getDomain());
    assertThat(other.getMaxAge()).isEqualTo(this.cookie.getMaxAge());
    assertThat(other.getName()).isEqualTo(this.cookie.getName());
    assertThat(other.getPath()).isEqualTo(this.cookie.getPath());
    assertThat(other.getSecure()).isEqualTo(this.cookie.getSecure());
    assertThat(other.getValue()).isEqualTo(this.cookie.getValue());
    assertThat(other.getVersion()).isEqualTo(this.cookie.getVersion());
}
Also used : Cookie(jakarta.servlet.http.Cookie) Test(org.junit.jupiter.api.Test)

Example 38 with Cookie

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

the class SimpleSavedRequestTests method constructorWhenGivenSavedRequestThenCopies.

@Test
public void constructorWhenGivenSavedRequestThenCopies() {
    SavedRequest savedRequest = new SimpleSavedRequest(prepareSavedRequest());
    assertThat(savedRequest.getMethod()).isEqualTo("POST");
    List<Cookie> cookies = savedRequest.getCookies();
    assertThat(cookies).hasSize(1);
    Cookie cookie = cookies.get(0);
    assertThat(cookie.getName()).isEqualTo("cookiename");
    assertThat(cookie.getValue()).isEqualTo("cookievalue");
    Collection<String> headerNames = savedRequest.getHeaderNames();
    assertThat(headerNames).hasSize(1);
    String headerName = headerNames.iterator().next();
    assertThat(headerName).isEqualTo("headername");
    List<String> headerValues = savedRequest.getHeaderValues("headername");
    assertThat(headerValues).hasSize(1);
    String headerValue = headerValues.get(0);
    assertThat(headerValue).isEqualTo("headervalue");
    List<Locale> locales = savedRequest.getLocales();
    assertThat(locales).hasSize(1);
    Locale locale = locales.get(0);
    assertThat(locale).isEqualTo(Locale.ENGLISH);
    Map<String, String[]> parameterMap = savedRequest.getParameterMap();
    assertThat(parameterMap).hasSize(1);
    String[] values = parameterMap.get("key");
    assertThat(values).hasSize(1);
    assertThat(values[0]).isEqualTo("value");
}
Also used : Cookie(jakarta.servlet.http.Cookie) Locale(java.util.Locale) Test(org.junit.jupiter.api.Test)

Example 39 with Cookie

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

the class CookieRequestCacheTests method matchingRequestWhenRequestContainsSavedRequestCookieThenSetsAnExpiredCookieInResponse.

@Test
public void matchingRequestWhenRequestContainsSavedRequestCookieThenSetsAnExpiredCookieInResponse() {
    CookieRequestCache cookieRequestCache = new CookieRequestCache();
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setServerPort(443);
    request.setSecure(true);
    request.setScheme("https");
    request.setServerName("abc.com");
    request.setRequestURI("/destination");
    request.setQueryString("param1=a&param2=b&param3=1122");
    String redirectUrl = "https://abc.com/destination?param1=a&param2=b&param3=1122";
    request.setCookies(new Cookie(DEFAULT_COOKIE_NAME, encodeCookie(redirectUrl)));
    MockHttpServletResponse response = new MockHttpServletResponse();
    cookieRequestCache.getMatchingRequest(request, response);
    Cookie expiredCookie = response.getCookie(DEFAULT_COOKIE_NAME);
    assertThat(expiredCookie).isNotNull();
    assertThat(expiredCookie.getValue()).isEmpty();
    assertThat(expiredCookie.getMaxAge()).isZero();
}
Also used : Cookie(jakarta.servlet.http.Cookie) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 40 with Cookie

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

the class CookieRequestCacheTests method saveRequestWhenMatchesThenSavedRequestInACookieOnResponse.

@Test
public void saveRequestWhenMatchesThenSavedRequestInACookieOnResponse() {
    CookieRequestCache cookieRequestCache = new CookieRequestCache();
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setServerPort(443);
    request.setSecure(true);
    request.setScheme("https");
    request.setServerName("abc.com");
    request.setRequestURI("/destination");
    request.setQueryString("param1=a&param2=b&param3=1122");
    MockHttpServletResponse response = new MockHttpServletResponse();
    cookieRequestCache.saveRequest(request, response);
    Cookie savedCookie = response.getCookie(DEFAULT_COOKIE_NAME);
    assertThat(savedCookie).isNotNull();
    String redirectUrl = decodeCookie(savedCookie.getValue());
    assertThat(redirectUrl).isEqualTo("https://abc.com/destination?param1=a&param2=b&param3=1122");
    assertThat(savedCookie.getMaxAge()).isEqualTo(-1);
    assertThat(savedCookie.getPath()).isEqualTo("/");
    assertThat(savedCookie.isHttpOnly()).isTrue();
    assertThat(savedCookie.getSecure()).isTrue();
}
Also used : Cookie(jakarta.servlet.http.Cookie) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Aggregations

Cookie (jakarta.servlet.http.Cookie)208 Test (org.junit.jupiter.api.Test)147 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 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)13 Authentication (org.springframework.security.core.Authentication)11 Supplier (java.util.function.Supplier)10 Pattern (java.util.regex.Pattern)10 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)10 Assertions.fail (org.assertj.core.api.Assertions.fail)10 Test (org.junit.Test)10 SameSite (org.springframework.boot.web.server.Cookie.SameSite)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