Search in sources :

Example 86 with Cookie

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

the class MockWebResponseBuilderTests method buildResponseHeaders.

@Test
public void buildResponseHeaders() throws Exception {
    this.response.addHeader("Content-Type", "text/html");
    this.response.addHeader("X-Test", "value");
    Cookie cookie = new Cookie("cookieA", "valueA");
    cookie.setDomain("domain");
    cookie.setPath("/path");
    cookie.setMaxAge(1800);
    cookie.setSecure(true);
    cookie.setHttpOnly(true);
    this.response.addCookie(cookie);
    WebResponse webResponse = this.responseBuilder.build();
    List<NameValuePair> responseHeaders = webResponse.getResponseHeaders();
    assertThat(responseHeaders.size()).isEqualTo(3);
    NameValuePair header = responseHeaders.get(0);
    assertThat(header.getName()).isEqualTo("Content-Type");
    assertThat(header.getValue()).isEqualTo("text/html");
    header = responseHeaders.get(1);
    assertThat(header.getName()).isEqualTo("X-Test");
    assertThat(header.getValue()).isEqualTo("value");
    header = responseHeaders.get(2);
    assertThat(header.getName()).isEqualTo("Set-Cookie");
    assertThat(header.getValue()).startsWith("cookieA=valueA; Path=/path; Domain=domain; Max-Age=1800; Expires=").endsWith("; Secure; HttpOnly");
}
Also used : Cookie(jakarta.servlet.http.Cookie) NameValuePair(com.gargoylesoftware.htmlunit.util.NameValuePair) WebResponse(com.gargoylesoftware.htmlunit.WebResponse) Test(org.junit.jupiter.api.Test)

Example 87 with Cookie

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

the class HtmlUnitRequestBuilderTests method buildRequestCookiesSingle.

@Test
public void buildRequestCookiesSingle() {
    webRequest.setAdditionalHeader("Cookie", "name=value");
    MockHttpServletRequest actualRequest = requestBuilder.buildRequest(servletContext);
    Cookie[] cookies = actualRequest.getCookies();
    assertThat(cookies.length).isEqualTo(1);
    assertThat(cookies[0].getName()).isEqualTo("name");
    assertThat(cookies[0].getValue()).isEqualTo("value");
}
Also used : Cookie(jakarta.servlet.http.Cookie) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Test(org.junit.jupiter.api.Test)

Example 88 with Cookie

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

the class ServletCookieValueMethodArgumentResolver method resolveName.

@Override
@Nullable
protected Object resolveName(String cookieName, MethodParameter parameter, NativeWebRequest webRequest) throws Exception {
    HttpServletRequest servletRequest = webRequest.getNativeRequest(HttpServletRequest.class);
    Assert.state(servletRequest != null, "No HttpServletRequest");
    Cookie cookieValue = WebUtils.getCookie(servletRequest, cookieName);
    if (Cookie.class.isAssignableFrom(parameter.getNestedParameterType())) {
        return cookieValue;
    } else if (cookieValue != null) {
        return this.urlPathHelper.decodeRequestString(servletRequest, cookieValue.getValue());
    } else {
        return null;
    }
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) Cookie(jakarta.servlet.http.Cookie) Nullable(org.springframework.lang.Nullable)

Example 89 with Cookie

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

the class MockMvcHttpConnector method adaptResponse.

private MockClientHttpResponse adaptResponse(MvcResult mvcResult) {
    MockClientHttpResponse clientResponse = new MockMvcServerClientHttpResponse(mvcResult);
    MockHttpServletResponse servletResponse = mvcResult.getResponse();
    for (String header : servletResponse.getHeaderNames()) {
        for (String value : servletResponse.getHeaders(header)) {
            clientResponse.getHeaders().add(header, value);
        }
    }
    if (servletResponse.getForwardedUrl() != null) {
        clientResponse.getHeaders().add("Forwarded-Url", servletResponse.getForwardedUrl());
    }
    for (Cookie cookie : servletResponse.getCookies()) {
        ResponseCookie httpCookie = ResponseCookie.fromClientResponse(cookie.getName(), cookie.getValue()).maxAge(Duration.ofSeconds(cookie.getMaxAge())).domain(cookie.getDomain()).path(cookie.getPath()).secure(cookie.getSecure()).httpOnly(cookie.isHttpOnly()).build();
        clientResponse.getCookies().add(httpCookie.getName(), httpCookie);
    }
    byte[] bytes = servletResponse.getContentAsByteArray();
    DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(bytes);
    clientResponse.setBody(Mono.just(dataBuffer));
    return clientResponse;
}
Also used : HttpCookie(org.springframework.http.HttpCookie) ResponseCookie(org.springframework.http.ResponseCookie) Cookie(jakarta.servlet.http.Cookie) DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) ResponseCookie(org.springframework.http.ResponseCookie) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockClientHttpResponse(org.springframework.mock.http.client.reactive.MockClientHttpResponse)

Example 90 with Cookie

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

the class CookieGenerator method createCookie.

/**
 * Create a cookie with the given value, using the cookie descriptor
 * settings of this generator (except for "cookieMaxAge").
 * @param cookieValue the value of the cookie to crate
 * @return the cookie
 * @see #setCookieName
 * @see #setCookieDomain
 * @see #setCookiePath
 */
protected Cookie createCookie(String cookieValue) {
    Cookie cookie = new Cookie(getCookieName(), cookieValue);
    if (getCookieDomain() != null) {
        cookie.setDomain(getCookieDomain());
    }
    cookie.setPath(getCookiePath());
    return cookie;
}
Also used : Cookie(jakarta.servlet.http.Cookie)

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