Search in sources :

Example 86 with MockServerWebExchange

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

the class HttpStatusReturningServerLogoutSuccessHandlerTests method buildFilterExchange.

private static WebFilterExchange buildFilterExchange() {
    MockServerHttpRequest request = MockServerHttpRequest.get("/").build();
    MockServerWebExchange exchange = MockServerWebExchange.from(request);
    return new WebFilterExchange(exchange, mock(WebFilterChain.class));
}
Also used : MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) WebFilterChain(org.springframework.web.server.WebFilterChain) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) WebFilterExchange(org.springframework.security.web.server.WebFilterExchange)

Example 87 with MockServerWebExchange

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

the class CookieServerRequestCacheTests method saveRequestWhenPostRequestAndCustomMatcherThenRequestUriInCookie.

@Test
public void saveRequestWhenPostRequestAndCustomMatcherThenRequestUriInCookie() {
    this.cache.setSaveRequestMatcher((e) -> ServerWebExchangeMatcher.MatchResult.match());
    MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.post("/secured/"));
    this.cache.saveRequest(exchange).block();
    MultiValueMap<String, ResponseCookie> cookies = exchange.getResponse().getCookies();
    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 88 with MockServerWebExchange

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

the class CookieServerRequestCacheTests method getRedirectUriWhenCookieValueNotEncodedThenRedirectUriIsNull.

@Test
public void getRedirectUriWhenCookieValueNotEncodedThenRedirectUriIsNull() {
    MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/secured/").accept(MediaType.TEXT_HTML).cookie(new HttpCookie("REDIRECT_URI", "/secured/")));
    URI redirectUri = this.cache.getRedirectUri(exchange).block();
    assertThat(redirectUri).isNull();
}
Also used : MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) HttpCookie(org.springframework.http.HttpCookie) URI(java.net.URI) Test(org.junit.jupiter.api.Test)

Example 89 with MockServerWebExchange

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

the class CookieServerRequestCacheTests method saveRequestWhenGetRequestWithQueryParamsThenRequestUriInCookie.

@Test
public void saveRequestWhenGetRequestWithQueryParamsThenRequestUriInCookie() {
    MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/secured/").queryParam("key", "value").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/?key=value".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 90 with MockServerWebExchange

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

the class CookieServerRequestCacheTests method getRedirectUriWhenCookieThenReturnsRedirectUriFromCookie.

@Test
public void getRedirectUriWhenCookieThenReturnsRedirectUriFromCookie() {
    String encodedRedirectUrl = Base64.getEncoder().encodeToString("/secured/".getBytes());
    MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/secured/").accept(MediaType.TEXT_HTML).cookie(new HttpCookie("REDIRECT_URI", encodedRedirectUrl)));
    URI redirectUri = this.cache.getRedirectUri(exchange).block();
    assertThat(redirectUri).isEqualTo(URI.create("/secured/"));
}
Also used : MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) HttpCookie(org.springframework.http.HttpCookie) 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