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");
}
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");
}
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;
}
}
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;
}
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;
}
Aggregations