Search in sources :

Example 21 with MockServerWebExchange

use of org.springframework.mock.web.server.MockServerWebExchange in project spring-security by spring-projects.

the class CookieServerRequestCacheTests method removeMatchingRequestThenRedirectUriCookieExpired.

@Test
public void removeMatchingRequestThenRedirectUriCookieExpired() {
    MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/secured/").accept(MediaType.TEXT_HTML).cookie(new HttpCookie("REDIRECT_URI", "/secured/")));
    this.cache.removeMatchingRequest(exchange).block();
    MultiValueMap<String, ResponseCookie> cookies = exchange.getResponse().getCookies();
    ResponseCookie cookie = cookies.getFirst("REDIRECT_URI");
    assertThat(cookie).isNotNull();
    assertThat(cookie.toString()).isEqualTo("REDIRECT_URI=; Path=/; Max-Age=0; Expires=Thu, 01 Jan 1970 00:00:00 GMT; HttpOnly; SameSite=Lax");
}
Also used : MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) HttpCookie(org.springframework.http.HttpCookie) ResponseCookie(org.springframework.http.ResponseCookie) Test(org.junit.jupiter.api.Test)

Example 22 with MockServerWebExchange

use of org.springframework.mock.web.server.MockServerWebExchange in project spring-security by spring-projects.

the class CookieServerRequestCacheTests method saveRequestWhenGetRequestThenRequestUriInCookie.

@Test
public void saveRequestWhenGetRequestThenRequestUriInCookie() {
    MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/secured/").accept(MediaType.TEXT_HTML));
    this.cache.saveRequest(exchange).block();
    MultiValueMap<String, ResponseCookie> cookies = exchange.getResponse().getCookies();
    assertThat(cookies.size()).isEqualTo(1);
    ResponseCookie cookie = cookies.getFirst("REDIRECT_URI");
    assertThat(cookie).isNotNull();
    String encodedRedirectUrl = Base64.getEncoder().encodeToString("/secured/".getBytes());
    assertThat(cookie.toString()).isEqualTo("REDIRECT_URI=" + encodedRedirectUrl + "; Path=/; HttpOnly; SameSite=Lax");
}
Also used : MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) ResponseCookie(org.springframework.http.ResponseCookie) Test(org.junit.jupiter.api.Test)

Example 23 with MockServerWebExchange

use of org.springframework.mock.web.server.MockServerWebExchange in project spring-security by spring-projects.

the class CookieServerRequestCacheTests method saveRequestWhenPostRequestThenNoCookie.

@Test
public void saveRequestWhenPostRequestThenNoCookie() {
    MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.post("/secured/"));
    this.cache.saveRequest(exchange).block();
    MultiValueMap<String, ResponseCookie> cookies = exchange.getResponse().getCookies();
    assertThat(cookies).isEmpty();
}
Also used : MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) ResponseCookie(org.springframework.http.ResponseCookie) Test(org.junit.jupiter.api.Test)

Example 24 with MockServerWebExchange

use of org.springframework.mock.web.server.MockServerWebExchange in project spring-security by spring-projects.

the class CookieServerRequestCacheTests method saveRequestWhenGetRequestFaviconThenNoCookie.

@Test
public void saveRequestWhenGetRequestFaviconThenNoCookie() {
    MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/favicon.png").accept(MediaType.TEXT_HTML));
    this.cache.saveRequest(exchange).block();
    MultiValueMap<String, ResponseCookie> cookies = exchange.getResponse().getCookies();
    assertThat(cookies).isEmpty();
}
Also used : MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) ResponseCookie(org.springframework.http.ResponseCookie) Test(org.junit.jupiter.api.Test)

Example 25 with MockServerWebExchange

use of org.springframework.mock.web.server.MockServerWebExchange in project spring-security by spring-projects.

the class WebSessionServerRequestCacheTests method saveRequestGetRequestWhenPostAndCustomMatcherThenFound.

@Test
public void saveRequestGetRequestWhenPostAndCustomMatcherThenFound() {
    this.cache.setSaveRequestMatcher((e) -> ServerWebExchangeMatcher.MatchResult.match());
    MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.post("/secured/"));
    this.cache.saveRequest(exchange).block();
    URI saved = this.cache.getRedirectUri(exchange).block();
    assertThat(saved).isEqualTo(exchange.getRequest().getURI());
}
Also used : MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) URI(java.net.URI) Test(org.junit.jupiter.api.Test)

Aggregations

MockServerWebExchange (org.springframework.mock.web.server.MockServerWebExchange)94 Test (org.junit.jupiter.api.Test)81 MockServerHttpRequest (org.springframework.mock.http.server.reactive.MockServerHttpRequest)44 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)27 Mono (reactor.core.publisher.Mono)26 BeforeEach (org.junit.jupiter.api.BeforeEach)22 StepVerifier (reactor.test.StepVerifier)21 ServerWebExchange (org.springframework.web.server.ServerWebExchange)15 Duration (java.time.Duration)14 ErrorAttributes (org.springframework.boot.web.reactive.error.ErrorAttributes)13 HandlerMethod (org.springframework.web.method.HandlerMethod)13 Timed (io.micrometer.core.annotation.Timed)12 MockClock (io.micrometer.core.instrument.MockClock)12 Tag (io.micrometer.core.instrument.Tag)12 SimpleConfig (io.micrometer.core.instrument.simple.SimpleConfig)12 SimpleMeterRegistry (io.micrometer.core.instrument.simple.SimpleMeterRegistry)12 EOFException (java.io.EOFException)12 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)12 AutoTimer (org.springframework.boot.actuate.metrics.AutoTimer)12 ResponseCookie (org.springframework.http.ResponseCookie)12