Search in sources :

Example 26 with ResponseCookie

use of org.springframework.http.ResponseCookie in project spring-framework by spring-projects.

the class HttpHandlerConnectorTests method adaptResponse.

@Test
public void adaptResponse() {
    ResponseCookie cookie = ResponseCookie.from("custom-cookie", "c0").build();
    TestHttpHandler handler = new TestHttpHandler(response -> {
        response.setStatusCode(HttpStatus.OK);
        response.getHeaders().put("custom-header", Arrays.asList("h0", "h1"));
        response.addCookie(cookie);
        return response.writeWith(Mono.just(toDataBuffer("Custom body")));
    });
    ClientHttpResponse response = new HttpHandlerConnector(handler).connect(HttpMethod.GET, URI.create("/custom-path"), ReactiveHttpOutputMessage::setComplete).block(Duration.ofSeconds(5));
    assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
    HttpHeaders headers = response.getHeaders();
    assertThat(headers.get("custom-header")).isEqualTo(Arrays.asList("h0", "h1"));
    assertThat(response.getCookies().getFirst("custom-cookie")).isEqualTo(cookie);
    assertThat(headers.get(HttpHeaders.SET_COOKIE)).isEqualTo(Collections.singletonList("custom-cookie=c0"));
    DataBuffer buffer = response.getBody().blockFirst(Duration.ZERO);
    assertThat(buffer.toString(UTF_8)).isEqualTo("Custom body");
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseCookie(org.springframework.http.ResponseCookie) ClientHttpResponse(org.springframework.http.client.reactive.ClientHttpResponse) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Example 27 with ResponseCookie

use of org.springframework.http.ResponseCookie in project spring-framework by spring-projects.

the class MockServerTests method mutateDoesNotCreateNewSession.

// SPR-15674 (in comments)
@Test
public void mutateDoesNotCreateNewSession() {
    WebTestClient client = WebTestClient.bindToWebHandler(exchange -> {
        if (exchange.getRequest().getURI().getPath().equals("/set")) {
            return exchange.getSession().doOnNext(session -> session.getAttributes().put("foo", "bar")).then();
        } else {
            return exchange.getSession().map(session -> session.getAttributeOrDefault("foo", "none")).flatMap(value -> {
                DataBuffer buffer = toDataBuffer(value);
                return exchange.getResponse().writeWith(Mono.just(buffer));
            });
        }
    }).build();
    // Set the session attribute
    EntityExchangeResult<Void> result = client.get().uri("/set").exchange().expectStatus().isOk().expectBody().isEmpty();
    ResponseCookie session = result.getResponseCookies().getFirst("SESSION");
    // Now get attribute
    client.mutate().build().get().uri("/get").cookie(session.getName(), session.getValue()).exchange().expectBody(String.class).isEqualTo("bar");
}
Also used : Arrays(java.util.Arrays) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) HttpHeaders(org.springframework.http.HttpHeaders) UTF_8(java.nio.charset.StandardCharsets.UTF_8) MediaType(org.springframework.http.MediaType) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Mono(reactor.core.publisher.Mono) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) HttpStatus(org.springframework.http.HttpStatus) ResponseCookie(org.springframework.http.ResponseCookie) ResponseCookie(org.springframework.http.ResponseCookie) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Example 28 with ResponseCookie

use of org.springframework.http.ResponseCookie in project spring-framework by spring-projects.

the class CookieAssertions method doesNotExist.

/**
 * Expect that the cookie with the given name is not present.
 */
public WebTestClient.ResponseSpec doesNotExist(String name) {
    ResponseCookie cookie = this.exchangeResult.getResponseCookies().getFirst(name);
    if (cookie != null) {
        String message = getMessage(name) + " exists with value=[" + cookie.getValue() + "]";
        this.exchangeResult.assertWithDiagnostics(() -> AssertionErrors.fail(message));
    }
    return this.responseSpec;
}
Also used : ResponseCookie(org.springframework.http.ResponseCookie)

Example 29 with ResponseCookie

use of org.springframework.http.ResponseCookie in project spring-framework by spring-projects.

the class CookieWebSessionIdResolverTests method cookieInitializer.

@Test
public void cookieInitializer() {
    this.resolver.addCookieInitializer(builder -> builder.domain("example.org"));
    this.resolver.addCookieInitializer(builder -> builder.sameSite("Strict"));
    this.resolver.addCookieInitializer(builder -> builder.secure(false));
    MockServerHttpRequest request = MockServerHttpRequest.get("https://example.org/path").build();
    MockServerWebExchange exchange = MockServerWebExchange.from(request);
    this.resolver.setSessionId(exchange, "123");
    MultiValueMap<String, ResponseCookie> cookies = exchange.getResponse().getCookies();
    assertThat(cookies.size()).isEqualTo(1);
    ResponseCookie cookie = cookies.getFirst(this.resolver.getCookieName());
    assertThat(cookie).isNotNull();
    assertThat(cookie.toString()).isEqualTo("SESSION=123; Path=/; Domain=example.org; HttpOnly; SameSite=Strict");
}
Also used : MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) ResponseCookie(org.springframework.http.ResponseCookie) Test(org.junit.jupiter.api.Test)

Example 30 with ResponseCookie

use of org.springframework.http.ResponseCookie in project spring-framework by spring-projects.

the class MockServerHttpResponseTests method cookieHeaderSet.

@Test
void cookieHeaderSet() throws Exception {
    ResponseCookie foo11 = ResponseCookie.from("foo1", "bar1").build();
    ResponseCookie foo12 = ResponseCookie.from("foo1", "bar2").build();
    ResponseCookie foo21 = ResponseCookie.from("foo2", "baz1").build();
    ResponseCookie foo22 = ResponseCookie.from("foo2", "baz2").build();
    MockServerHttpResponse response = new MockServerHttpResponse();
    response.addCookie(foo11);
    response.addCookie(foo12);
    response.addCookie(foo21);
    response.addCookie(foo22);
    response.applyCookies();
    assertThat(response.getHeaders().get(HttpHeaders.SET_COOKIE)).isEqualTo(Arrays.asList("foo1=bar1", "foo1=bar2", "foo2=baz1", "foo2=baz2"));
}
Also used : ResponseCookie(org.springframework.http.ResponseCookie) Test(org.junit.jupiter.api.Test)

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