Search in sources :

Example 11 with ResponseCookie

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);
        }
    }
}
Also used : Cookie(io.undertow.server.handlers.Cookie) ResponseCookie(org.springframework.http.ResponseCookie) CookieImpl(io.undertow.server.handlers.CookieImpl) ResponseCookie(org.springframework.http.ResponseCookie)

Example 12 with ResponseCookie

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

Example 13 with ResponseCookie

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

Example 14 with ResponseCookie

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;
}
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 15 with ResponseCookie

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);
}
Also used : 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