Search in sources :

Example 6 with HttpCookie

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

the class CookieValueMethodArgumentResolver method resolveNamedValue.

@Override
protected Optional<Object> resolveNamedValue(String name, MethodParameter parameter, ServerWebExchange exchange) {
    HttpCookie cookie = exchange.getRequest().getCookies().getFirst(name);
    Class<?> paramType = parameter.getNestedParameterType();
    if (HttpCookie.class.isAssignableFrom(paramType)) {
        return Optional.ofNullable(cookie);
    } else if (cookie != null) {
        return Optional.ofNullable(cookie.getValue());
    } else {
        return Optional.empty();
    }
}
Also used : HttpCookie(org.springframework.http.HttpCookie)

Example 7 with HttpCookie

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

the class CookieValueMethodArgumentResolverTests method resolveCookieStringArgument.

@Test
public void resolveCookieStringArgument() {
    HttpCookie cookie = new HttpCookie("name", "foo");
    ServerWebExchange exchange = MockServerHttpRequest.get("/").cookie(cookie.getName(), cookie).toExchange();
    Mono<Object> mono = this.resolver.resolveArgument(this.cookieStringParameter, this.bindingContext, exchange);
    assertEquals("Invalid result", cookie.getValue(), mono.block());
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) HttpCookie(org.springframework.http.HttpCookie) Test(org.junit.Test)

Example 8 with HttpCookie

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

the class CookieValueMethodArgumentResolverTests method resolveCookieArgument.

@Test
public void resolveCookieArgument() {
    HttpCookie expected = new HttpCookie("name", "foo");
    ServerWebExchange exchange = MockServerHttpRequest.get("/").cookie(expected.getName(), expected).toExchange();
    Mono<Object> mono = this.resolver.resolveArgument(this.cookieParameter, this.bindingContext, exchange);
    assertEquals(expected, mono.block());
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) HttpCookie(org.springframework.http.HttpCookie) Test(org.junit.Test)

Example 9 with HttpCookie

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

the class CookieIntegrationTests method basicTest.

@SuppressWarnings("unchecked")
@Test
public void basicTest() throws Exception {
    URI url = new URI("http://localhost:" + port);
    String header = "SID=31d4d96e407aad42; lang=en-US";
    ResponseEntity<Void> response = new RestTemplate().exchange(RequestEntity.get(url).header("Cookie", header).build(), Void.class);
    Map<String, List<HttpCookie>> requestCookies = this.cookieHandler.requestCookies;
    assertEquals(2, requestCookies.size());
    List<HttpCookie> list = requestCookies.get("SID");
    assertEquals(1, list.size());
    assertEquals("31d4d96e407aad42", list.iterator().next().getValue());
    list = requestCookies.get("lang");
    assertEquals(1, list.size());
    assertEquals("en-US", list.iterator().next().getValue());
    List<String> headerValues = response.getHeaders().get("Set-Cookie");
    assertEquals(2, headerValues.size());
    assertThat(splitCookie(headerValues.get(0)), containsInAnyOrder(equalTo("SID=31d4d96e407aad42"), equalToIgnoringCase("Path=/"), equalToIgnoringCase("Secure"), equalToIgnoringCase("HttpOnly")));
    assertThat(splitCookie(headerValues.get(1)), containsInAnyOrder(equalTo("lang=en-US"), equalToIgnoringCase("Path=/"), equalToIgnoringCase("Domain=example.com")));
}
Also used : RestTemplate(org.springframework.web.client.RestTemplate) ArrayList(java.util.ArrayList) List(java.util.List) URI(java.net.URI) HttpCookie(org.springframework.http.HttpCookie) Test(org.junit.Test)

Example 10 with HttpCookie

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

the class ReactorServerHttpRequest method initCookies.

@Override
protected MultiValueMap<String, HttpCookie> initCookies() {
    MultiValueMap<String, HttpCookie> cookies = new LinkedMultiValueMap<>();
    for (CharSequence name : this.request.cookies().keySet()) {
        for (Cookie cookie : this.request.cookies().get(name)) {
            HttpCookie httpCookie = new HttpCookie(name.toString(), cookie.value());
            cookies.add(name.toString(), httpCookie);
        }
    }
    return cookies;
}
Also used : Cookie(io.netty.handler.codec.http.cookie.Cookie) HttpCookie(org.springframework.http.HttpCookie) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) HttpCookie(org.springframework.http.HttpCookie)

Aggregations

HttpCookie (org.springframework.http.HttpCookie)10 Test (org.junit.Test)4 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)4 URI (java.net.URI)3 Cookie (io.netty.handler.codec.http.cookie.Cookie)2 HttpMethod (org.springframework.http.HttpMethod)2 MockServerWebExchange (org.springframework.mock.http.server.reactive.test.MockServerWebExchange)2 ServerWebExchange (org.springframework.web.server.ServerWebExchange)2 Cookie (io.undertow.server.handlers.Cookie)1 UTF_8 (java.nio.charset.StandardCharsets.UTF_8)1 Duration (java.time.Duration)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Function (java.util.function.Function)1 Cookie (javax.servlet.http.Cookie)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 DataBuffer (org.springframework.core.io.buffer.DataBuffer)1 DefaultDataBufferFactory (org.springframework.core.io.buffer.DefaultDataBufferFactory)1 DataBufferTestUtils (org.springframework.core.io.buffer.support.DataBufferTestUtils)1