use of org.springframework.http.ResponseCookie in project spring-framework by spring-projects.
the class ReactorServerHttpResponse method applyCookies.
@Override
protected void applyCookies() {
for (String name : getCookies().keySet()) {
for (ResponseCookie httpCookie : getCookies().get(name)) {
Cookie cookie = new DefaultCookie(name, httpCookie.getValue());
if (!httpCookie.getMaxAge().isNegative()) {
cookie.setMaxAge(httpCookie.getMaxAge().getSeconds());
}
httpCookie.getDomain().ifPresent(cookie::setDomain);
httpCookie.getPath().ifPresent(cookie::setPath);
cookie.setSecure(httpCookie.isSecure());
cookie.setHttpOnly(httpCookie.isHttpOnly());
this.response.addCookie(cookie);
}
}
}
use of org.springframework.http.ResponseCookie in project spring-framework by spring-projects.
the class RxNettyServerHttpResponse method applyCookies.
@Override
protected void applyCookies() {
for (String name : getCookies().keySet()) {
for (ResponseCookie httpCookie : getCookies().get(name)) {
Cookie cookie = new DefaultCookie(name, httpCookie.getValue());
if (!httpCookie.getMaxAge().isNegative()) {
cookie.setMaxAge(httpCookie.getMaxAge().getSeconds());
}
httpCookie.getDomain().ifPresent(cookie::setDomain);
httpCookie.getPath().ifPresent(cookie::setPath);
cookie.setSecure(httpCookie.isSecure());
cookie.setHttpOnly(httpCookie.isHttpOnly());
this.response.addCookie(cookie);
}
}
}
use of org.springframework.http.ResponseCookie in project connectors-workspace-one by vmware.
the class MockClientHttpConnector method connect.
@Override
// Method is long but we can delete when Spring supports mocking WebClient https://jira.spring.io/browse/SPR-15286
@SuppressWarnings("PMD")
public Mono<ClientHttpResponse> connect(HttpMethod method, URI uri, Function<? super ClientHttpRequest, Mono<Void>> requestCallback) {
try {
MockClientHttpRequest clientHttpRequest = new MockClientHttpRequest(method, uri);
requestCallback.apply(clientHttpRequest).block();
InputStream body = clientHttpRequest.getBody().reduce((b1, b2) -> b2.write(b1)).map(DataBuffer::asInputStream).block();
org.springframework.mock.http.client.MockClientHttpRequest request = new org.springframework.mock.http.client.MockClientHttpRequest();
request.setMethod(method);
request.setURI(uri);
clientHttpRequest.getHeaders().entrySet().stream().filter(entry -> !entry.getKey().equals(COOKIE)).forEach(entry -> request.getHeaders().addAll(entry.getKey(), entry.getValue()));
String requestCookies = clientHttpRequest.getCookies().values().stream().flatMap(Collection::stream).map(cookie -> cookie.getName() + "=" + cookie.getValue()).collect(Collectors.joining("; "));
request.getHeaders().add(HttpHeaders.COOKIE, requestCookies);
if (body != null) {
IOUtils.copy(body, request.getBody());
}
org.springframework.http.client.ClientHttpResponse response = requestHandler.handle(request);
MockClientHttpResponse mockClientHttpResponse = new MockClientHttpResponse(response.getStatusCode());
mockClientHttpResponse.getHeaders().addAll(response.getHeaders());
List<String> responseCookies = Optional.ofNullable(response.getHeaders().get(SET_COOKIE)).orElse(Collections.emptyList());
responseCookies.forEach(cookie -> {
ResponseCookie responseCookie = toResponseCookie(cookie);
mockClientHttpResponse.getCookies().add(responseCookie.getName(), responseCookie);
});
DataBuffer bodyBuffer = new DefaultDataBufferFactory().allocateBuffer();
IOUtils.copy(response.getBody(), bodyBuffer.asOutputStream());
mockClientHttpResponse.setBody(Mono.just(bodyBuffer));
return Mono.just(mockClientHttpResponse);
} catch (IOException e) {
throw new AssertionError(e);
}
}
use of org.springframework.http.ResponseCookie in project spring-security by spring-projects.
the class CookieServerCsrfTokenRepositoryTests method saveTokenWhenSecureFlagFalseThenNotSecure.
@Test
public void saveTokenWhenSecureFlagFalseThenNotSecure() {
MockServerWebExchange exchange = MockServerWebExchange.from(this.request);
this.csrfTokenRepository.setSecure(false);
this.csrfTokenRepository.saveToken(exchange, createToken()).block();
ResponseCookie cookie = exchange.getResponse().getCookies().getFirst(this.expectedCookieName);
assertThat(cookie).isNotNull();
assertThat(cookie.isSecure()).isFalse();
}
use of org.springframework.http.ResponseCookie in project spring-security by spring-projects.
the class CookieServerCsrfTokenRepositoryTests method saveAndAssertExpectedValues.
private void saveAndAssertExpectedValues(CsrfToken token) {
if (token == null) {
this.expectedMaxAge = Duration.ofSeconds(0);
this.expectedCookieValue = "";
}
MockServerWebExchange exchange = MockServerWebExchange.from(this.request);
this.csrfTokenRepository.saveToken(exchange, token).block();
ResponseCookie cookie = exchange.getResponse().getCookies().getFirst(this.expectedCookieName);
assertThat(cookie).isNotNull();
assertThat(cookie.getMaxAge()).isEqualTo(this.expectedMaxAge);
assertThat(cookie.getDomain()).isEqualTo(this.expectedDomain);
assertThat(cookie.getPath()).isEqualTo(this.expectedPath);
assertThat(cookie.isSecure()).isEqualTo(this.expectedSecure);
assertThat(cookie.isHttpOnly()).isEqualTo(this.expectedHttpOnly);
assertThat(cookie.getName()).isEqualTo(this.expectedCookieName);
assertThat(cookie.getValue()).isEqualTo(this.expectedCookieValue);
}
Aggregations