use of org.springframework.http.ResponseCookie in project spring-framework by spring-projects.
the class UndertowServerHttpResponse method applyCookies.
@SuppressWarnings("deprecation")
@Override
protected void applyCookies() {
for (String name : getCookies().keySet()) {
for (ResponseCookie httpCookie : getCookies().get(name)) {
Cookie cookie = new CookieImpl(name, httpCookie.getValue());
if (!httpCookie.getMaxAge().isNegative()) {
cookie.setMaxAge((int) httpCookie.getMaxAge().getSeconds());
}
if (httpCookie.getDomain() != null) {
cookie.setDomain(httpCookie.getDomain());
}
if (httpCookie.getPath() != null) {
cookie.setPath(httpCookie.getPath());
}
cookie.setSecure(httpCookie.isSecure());
cookie.setHttpOnly(httpCookie.isHttpOnly());
cookie.setSameSiteMode(httpCookie.getSameSite());
// getResponseCookies() is deprecated in Undertow 2.2
this.exchange.getResponseCookies().putIfAbsent(name, cookie);
}
}
}
use of org.springframework.http.ResponseCookie in project spring-framework by spring-projects.
the class ServerHttpResponseTests method beforeCommitActionWithSetComplete.
@Test
void beforeCommitActionWithSetComplete() {
ResponseCookie cookie = ResponseCookie.from("ID", "123").build();
TestServerHttpResponse response = new TestServerHttpResponse();
response.beforeCommit(() -> {
response.getCookies().add(cookie.getName(), cookie);
return Mono.empty();
});
response.setComplete().block();
assertThat(response.statusCodeWritten).isTrue();
assertThat(response.headersWritten).isTrue();
assertThat(response.cookiesWritten).isTrue();
assertThat(response.body.isEmpty()).isTrue();
assertThat(response.getCookies().getFirst("ID")).isSameAs(cookie);
}
use of org.springframework.http.ResponseCookie in project spring-framework by spring-projects.
the class ServerHttpResponseTests method beforeCommitWithComplete.
@Test
void beforeCommitWithComplete() {
ResponseCookie cookie = ResponseCookie.from("ID", "123").build();
TestServerHttpResponse response = new TestServerHttpResponse();
response.beforeCommit(() -> Mono.fromRunnable(() -> response.getCookies().add(cookie.getName(), cookie)));
response.writeWith(Flux.just(wrap("a"), wrap("b"), wrap("c"))).block();
assertThat(response.statusCodeWritten).isTrue();
assertThat(response.headersWritten).isTrue();
assertThat(response.cookiesWritten).isTrue();
assertThat(response.getCookies().getFirst("ID")).isSameAs(cookie);
assertThat(response.body.size()).isEqualTo(3);
assertThat(new String(response.body.get(0).asByteBuffer().array(), StandardCharsets.UTF_8)).isEqualTo("a");
assertThat(new String(response.body.get(1).asByteBuffer().array(), StandardCharsets.UTF_8)).isEqualTo("b");
assertThat(new String(response.body.get(2).asByteBuffer().array(), StandardCharsets.UTF_8)).isEqualTo("c");
}
use of org.springframework.http.ResponseCookie 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 org.springframework.http.ResponseCookie in project spring-framework by spring-projects.
the class CookieWebSessionIdResolver method expireSession.
@Override
public void expireSession(ServerWebExchange exchange) {
ResponseCookie cookie = initSessionCookie(exchange, "", Duration.ZERO);
exchange.getResponse().getCookies().set(this.cookieName, cookie);
}
Aggregations