Search in sources :

Example 16 with MockServerWebExchange

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

the class CookieServerCsrfTokenRepositoryTests method saveTokenWhenSslInfoPresentThenSecure.

@Test
public void saveTokenWhenSslInfoPresentThenSecure() {
    this.request.sslInfo(new MockSslInfo());
    MockServerWebExchange exchange = MockServerWebExchange.from(this.request);
    this.csrfTokenRepository.saveToken(exchange, createToken()).block();
    ResponseCookie cookie = exchange.getResponse().getCookies().getFirst(this.expectedCookieName);
    assertThat(cookie).isNotNull();
    assertThat(cookie.isSecure()).isTrue();
}
Also used : MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) ResponseCookie(org.springframework.http.ResponseCookie) Test(org.junit.jupiter.api.Test)

Example 17 with MockServerWebExchange

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

the class CookieServerCsrfTokenRepositoryTests method saveTokenWhenNoSubscriptionThenNotWritten.

@Test
public void saveTokenWhenNoSubscriptionThenNotWritten() {
    MockServerWebExchange exchange = MockServerWebExchange.from(this.request);
    this.csrfTokenRepository.saveToken(exchange, createToken());
    assertThat(exchange.getResponse().getCookies().getFirst(this.expectedCookieName)).isNull();
}
Also used : MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) Test(org.junit.jupiter.api.Test)

Example 18 with MockServerWebExchange

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

the class CsrfWebFilterTests method matchesRequireCsrfProtectionWhenNonStandardHTTPMethodIsUsed.

@Test
public // gh-8452
void matchesRequireCsrfProtectionWhenNonStandardHTTPMethodIsUsed() {
    MockServerWebExchange nonStandardHttpExchange = MockServerWebExchange.from(MockServerHttpRequest.method("non-standard-http-method", "/"));
    ServerWebExchangeMatcher serverWebExchangeMatcher = CsrfWebFilter.DEFAULT_CSRF_MATCHER;
    assertThat(serverWebExchangeMatcher.matches(nonStandardHttpExchange).map(MatchResult::isMatch).block()).isTrue();
}
Also used : ServerWebExchangeMatcher(org.springframework.security.web.server.util.matcher.ServerWebExchangeMatcher) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) Test(org.junit.jupiter.api.Test)

Example 19 with MockServerWebExchange

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

the class CsrfWebFilterTests method doFilterWhenSkipExchangeInvokedThenSkips.

@Test
public void doFilterWhenSkipExchangeInvokedThenSkips() {
    PublisherProbe<Void> chainResult = PublisherProbe.empty();
    given(this.chain.filter(any())).willReturn(chainResult.mono());
    ServerWebExchangeMatcher matcher = mock(ServerWebExchangeMatcher.class);
    this.csrfFilter.setRequireCsrfProtectionMatcher(matcher);
    MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.post("/post").build());
    CsrfWebFilter.skipExchange(exchange);
    this.csrfFilter.filter(exchange, this.chain).block();
    verifyZeroInteractions(matcher);
}
Also used : ServerWebExchangeMatcher(org.springframework.security.web.server.util.matcher.ServerWebExchangeMatcher) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) Test(org.junit.jupiter.api.Test)

Example 20 with MockServerWebExchange

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

the class LoginPageGeneratingWebFilterTests method filterWhenLoginWithNoContextPathThenActionDoesNotContainsContextPath.

@Test
public void filterWhenLoginWithNoContextPathThenActionDoesNotContainsContextPath() throws Exception {
    LoginPageGeneratingWebFilter filter = new LoginPageGeneratingWebFilter();
    filter.setFormLoginEnabled(true);
    MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/login"));
    filter.filter(exchange, (e) -> Mono.empty()).block();
    assertThat(exchange.getResponse().getBodyAsString().block()).contains("action=\"/login\"");
}
Also used : Test(org.junit.jupiter.api.Test) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Mono(reactor.core.publisher.Mono) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) 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