Search in sources :

Example 31 with ResponseCookie

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

the class CookieWebSessionIdResolver method setSessionId.

@Override
public void setSessionId(ServerWebExchange exchange, String id) {
    Assert.notNull(id, "'id' is required");
    ResponseCookie cookie = initSessionCookie(exchange, id, getCookieMaxAge());
    exchange.getResponse().getCookies().set(this.cookieName, cookie);
}
Also used : ResponseCookie(org.springframework.http.ResponseCookie)

Example 32 with ResponseCookie

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

the class DefaultClientResponseTests method cookies.

@Test
public void cookies() {
    ResponseCookie cookie = ResponseCookie.from("foo", "bar").build();
    MultiValueMap<String, ResponseCookie> cookies = new LinkedMultiValueMap<>();
    cookies.add("foo", cookie);
    given(mockResponse.getCookies()).willReturn(cookies);
    assertThat(defaultClientResponse.cookies()).isSameAs(cookies);
}
Also used : LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) ResponseCookie(org.springframework.http.ResponseCookie) Test(org.junit.jupiter.api.Test)

Example 33 with ResponseCookie

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

the class DefaultServerResponseBuilderTests method build.

@Test
public void build() {
    ResponseCookie cookie = ResponseCookie.from("name", "value").build();
    Mono<ServerResponse> result = ServerResponse.status(HttpStatus.CREATED).header("MyKey", "MyValue").cookie(cookie).build();
    MockServerHttpRequest request = MockServerHttpRequest.get("https://example.com").build();
    MockServerWebExchange exchange = MockServerWebExchange.from(request);
    result.flatMap(res -> res.writeTo(exchange, EMPTY_CONTEXT)).block();
    MockServerHttpResponse response = exchange.getResponse();
    assertThat(response.getStatusCode()).isEqualTo(HttpStatus.CREATED);
    assertThat(response.getHeaders().getFirst("MyKey")).isEqualTo("MyValue");
    assertThat(response.getCookies().getFirst("name").getValue()).isEqualTo("value");
    StepVerifier.create(response.getBody()).expectComplete().verify();
}
Also used : StepVerifier(reactor.test.StepVerifier) ZonedDateTime(java.time.ZonedDateTime) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) CacheControl(org.springframework.http.CacheControl) URI(java.net.URI) ResponseCookie(org.springframework.http.ResponseCookie) HttpHeaders(org.springframework.http.HttpHeaders) MediaType(org.springframework.http.MediaType) HttpMethod(org.springframework.http.HttpMethod) Set(java.util.Set) MultiValueMap(org.springframework.util.MultiValueMap) Mono(reactor.core.publisher.Mono) Test(org.junit.jupiter.api.Test) HttpStatus(org.springframework.http.HttpStatus) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) List(java.util.List) ChronoUnit(java.time.temporal.ChronoUnit) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) DateTimeFormatter(java.time.format.DateTimeFormatter) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) ViewResolver(org.springframework.web.reactive.result.view.ViewResolver) Collections(java.util.Collections) HttpMessageWriter(org.springframework.http.codec.HttpMessageWriter) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) ResponseCookie(org.springframework.http.ResponseCookie) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) Test(org.junit.jupiter.api.Test)

Example 34 with ResponseCookie

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

the class DefaultServerResponseBuilderTests method from.

@Test
public void from() {
    ResponseCookie cookie = ResponseCookie.from("foo", "bar").build();
    ServerResponse other = ServerResponse.ok().header("foo", "bar").cookie(cookie).hint("foo", "bar").build().block();
    Mono<ServerResponse> result = ServerResponse.from(other).build();
    StepVerifier.create(result).expectNextMatches(response -> HttpStatus.OK.equals(response.statusCode()) && "bar".equals(response.headers().getFirst("foo")) && cookie.equals(response.cookies().getFirst("foo"))).expectComplete().verify();
}
Also used : ResponseCookie(org.springframework.http.ResponseCookie) Test(org.junit.jupiter.api.Test)

Example 35 with ResponseCookie

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

the class RouterFunctionsTests method toHttpHandlerHandlerReturnResponseStatusExceptionInResponseWriteTo.

@Test
public void toHttpHandlerHandlerReturnResponseStatusExceptionInResponseWriteTo() {
    HandlerFunction<ServerResponse> handlerFunction = // Mono.<ServerResponse> is required for compilation in Eclipse
    request -> Mono.just(new ServerResponse() {

        @Override
        public HttpStatus statusCode() {
            return HttpStatus.OK;
        }

        @Override
        public int rawStatusCode() {
            return 200;
        }

        @Override
        public HttpHeaders headers() {
            return new HttpHeaders();
        }

        @Override
        public MultiValueMap<String, ResponseCookie> cookies() {
            return new LinkedMultiValueMap<>();
        }

        @Override
        public Mono<Void> writeTo(ServerWebExchange exchange, Context context) {
            return Mono.error(new ResponseStatusException(HttpStatus.NOT_FOUND, "Not found"));
        }
    });
    RouterFunction<ServerResponse> routerFunction = RouterFunctions.route(RequestPredicates.all(), handlerFunction);
    HttpHandler result = RouterFunctions.toHttpHandler(routerFunction);
    assertThat(result).isNotNull();
    MockServerHttpRequest httpRequest = MockServerHttpRequest.get("https://localhost").build();
    MockServerHttpResponse httpResponse = new MockServerHttpResponse();
    result.handle(httpRequest, httpResponse).block();
    assertThat(httpResponse.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
}
Also used : StepVerifier(reactor.test.StepVerifier) ResponseStatusException(org.springframework.web.server.ResponseStatusException) HttpHeaders(org.springframework.http.HttpHeaders) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MultiValueMap(org.springframework.util.MultiValueMap) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) Mono(reactor.core.publisher.Mono) DataBuffer(org.springframework.core.io.buffer.DataBuffer) StandardCharsets(java.nio.charset.StandardCharsets) ServerWebExchange(org.springframework.web.server.ServerWebExchange) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) HttpStatus(org.springframework.http.HttpStatus) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) HttpHandler(org.springframework.http.server.reactive.HttpHandler) WebFilter(org.springframework.web.server.WebFilter) BDDMockito.given(org.mockito.BDDMockito.given) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) Optional(java.util.Optional) Collections(java.util.Collections) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) ResponseCookie(org.springframework.http.ResponseCookie) Mockito.mock(org.mockito.Mockito.mock) HttpHeaders(org.springframework.http.HttpHeaders) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) ServerWebExchange(org.springframework.web.server.ServerWebExchange) HttpHandler(org.springframework.http.server.reactive.HttpHandler) HttpStatus(org.springframework.http.HttpStatus) Mono(reactor.core.publisher.Mono) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) ResponseStatusException(org.springframework.web.server.ResponseStatusException) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) 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