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();
}
}
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());
}
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());
}
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")));
}
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;
}
Aggregations