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