Search in sources :

Example 81 with MockServerWebExchange

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

the class CookieServerCsrfTokenRepositoryTests method saveTokenWhenSecureFlagFalseAndSslInfoThenNotSecure.

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

Example 82 with MockServerWebExchange

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

the class CookieServerCsrfTokenRepositoryTests method loadTokenWhenNoCookiesThenNullToken.

@Test
public void loadTokenWhenNoCookiesThenNullToken() {
    MockServerWebExchange exchange = MockServerWebExchange.from(this.request);
    CsrfToken csrfToken = this.csrfTokenRepository.loadToken(exchange).block();
    assertThat(csrfToken).isNull();
}
Also used : MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) Test(org.junit.jupiter.api.Test)

Example 83 with MockServerWebExchange

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

the class CookieServerCsrfTokenRepositoryTests method loadAndAssertExpectedValues.

private void loadAndAssertExpectedValues() {
    MockServerHttpRequest.BodyBuilder request = MockServerHttpRequest.post("/someUri").cookie(new HttpCookie(this.expectedCookieName, this.expectedCookieValue));
    MockServerWebExchange exchange = MockServerWebExchange.from(request);
    CsrfToken csrfToken = this.csrfTokenRepository.loadToken(exchange).block();
    if (StringUtils.hasText(this.expectedCookieValue)) {
        assertThat(csrfToken).isNotNull();
        assertThat(csrfToken.getHeaderName()).isEqualTo(this.expectedHeaderName);
        assertThat(csrfToken.getParameterName()).isEqualTo(this.expectedParameterName);
        assertThat(csrfToken.getToken()).isEqualTo(this.expectedCookieValue);
    } else {
        assertThat(csrfToken).isNull();
    }
}
Also used : MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) HttpCookie(org.springframework.http.HttpCookie)

Example 84 with MockServerWebExchange

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

the class CookieServerCsrfTokenRepositoryTests method generateTokenAndAssertExpectedValues.

private void generateTokenAndAssertExpectedValues() {
    MockServerWebExchange exchange = MockServerWebExchange.from(this.request);
    CsrfToken csrfToken = this.csrfTokenRepository.generateToken(exchange).block();
    assertThat(csrfToken).isNotNull();
    assertThat(csrfToken.getHeaderName()).isEqualTo(this.expectedHeaderName);
    assertThat(csrfToken.getParameterName()).isEqualTo(this.expectedParameterName);
    assertThat(csrfToken.getToken()).isNotBlank();
}
Also used : MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange)

Example 85 with MockServerWebExchange

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

the class CookieServerCsrfTokenRepositoryTests method saveTokenWhenSecureFlagTrueThenSecure.

@Test
public void saveTokenWhenSecureFlagTrueThenSecure() {
    MockServerWebExchange exchange = MockServerWebExchange.from(this.request);
    this.csrfTokenRepository.setSecure(true);
    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)

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