Search in sources :

Example 16 with ResponseCookie

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);
        }
    }
}
Also used : Cookie(io.netty.handler.codec.http.cookie.Cookie) DefaultCookie(io.netty.handler.codec.http.cookie.DefaultCookie) ResponseCookie(org.springframework.http.ResponseCookie) DefaultCookie(io.netty.handler.codec.http.cookie.DefaultCookie) ResponseCookie(org.springframework.http.ResponseCookie)

Example 17 with ResponseCookie

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);
        }
    }
}
Also used : Cookie(io.netty.handler.codec.http.cookie.Cookie) DefaultCookie(io.netty.handler.codec.http.cookie.DefaultCookie) ResponseCookie(org.springframework.http.ResponseCookie) DefaultCookie(io.netty.handler.codec.http.cookie.DefaultCookie) ResponseCookie(org.springframework.http.ResponseCookie)

Example 18 with ResponseCookie

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);
    }
}
Also used : MockClientHttpResponse(org.springframework.mock.http.client.reactive.MockClientHttpResponse) java.util(java.util) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) HttpHeaders(org.springframework.http.HttpHeaders) HttpMethod(org.springframework.http.HttpMethod) Mono(reactor.core.publisher.Mono) IOException(java.io.IOException) DataBuffer(org.springframework.core.io.buffer.DataBuffer) StringUtils(org.apache.commons.lang3.StringUtils) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) COOKIE(org.springframework.http.HttpHeaders.COOKIE) ClientHttpResponse(org.springframework.http.client.reactive.ClientHttpResponse) IOUtils(org.apache.commons.io.IOUtils) SET_COOKIE(org.springframework.http.HttpHeaders.SET_COOKIE) ClientHttpRequest(org.springframework.http.client.reactive.ClientHttpRequest) URI(java.net.URI) ResponseCookie(org.springframework.http.ResponseCookie) ClientHttpConnector(org.springframework.http.client.reactive.ClientHttpConnector) MockClientHttpRequest(org.springframework.mock.http.client.reactive.MockClientHttpRequest) InputStream(java.io.InputStream) InputStream(java.io.InputStream) MockClientHttpRequest(org.springframework.mock.http.client.reactive.MockClientHttpRequest) IOException(java.io.IOException) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) ResponseCookie(org.springframework.http.ResponseCookie) MockClientHttpResponse(org.springframework.mock.http.client.reactive.MockClientHttpResponse) DataBuffer(org.springframework.core.io.buffer.DataBuffer)

Example 19 with ResponseCookie

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();
}
Also used : MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) ResponseCookie(org.springframework.http.ResponseCookie) Test(org.junit.jupiter.api.Test)

Example 20 with ResponseCookie

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);
}
Also used : MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) ResponseCookie(org.springframework.http.ResponseCookie)

Aggregations

ResponseCookie (org.springframework.http.ResponseCookie)35 Test (org.junit.jupiter.api.Test)23 MockServerWebExchange (org.springframework.mock.web.server.MockServerWebExchange)12 HttpHeaders (org.springframework.http.HttpHeaders)6 DataBuffer (org.springframework.core.io.buffer.DataBuffer)5 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)5 MockServerHttpRequest (org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest)5 MockServerWebExchange (org.springframework.web.testfixture.server.MockServerWebExchange)5 Mono (reactor.core.publisher.Mono)5 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)4 HttpStatus (org.springframework.http.HttpStatus)4 Collections (java.util.Collections)3 MultiValueMap (org.springframework.util.MultiValueMap)3 MockServerHttpResponse (org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse)3 Flux (reactor.core.publisher.Flux)3 StepVerifier (reactor.test.StepVerifier)3 Cookie (io.netty.handler.codec.http.cookie.Cookie)2 DefaultCookie (io.netty.handler.codec.http.cookie.DefaultCookie)2 URI (java.net.URI)2 StandardCharsets (java.nio.charset.StandardCharsets)2