Polish CsrfWebFilterTests

Issue gh-9113
This commit is contained in:
Josh Cummings 2021-06-04 16:37:25 -06:00
parent 0c8b6df82a
commit ca76c54471
No known key found for this signature in database
GPG Key ID: 49EF60DD7FF83443

View File

@ -16,9 +16,6 @@
package org.springframework.security.web.server.csrf; package org.springframework.security.web.server.csrf;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
@ -233,21 +230,16 @@ public class CsrfWebFilterTests {
// gh-9113 // gh-9113
@Test @Test
public void filterWhenSubscribingCsrfTokenMultipleTimesThenGenerateOnlyOnce() { public void filterWhenSubscribingCsrfTokenMultipleTimesThenGenerateOnlyOnce() {
PublisherProbe<CsrfToken> chainResult = PublisherProbe.empty();
this.csrfFilter.setCsrfTokenRepository(this.repository); this.csrfFilter.setCsrfTokenRepository(this.repository);
given(this.repository.loadToken(any())).willReturn(Mono.empty()); given(this.repository.loadToken(any())).willReturn(Mono.empty());
AtomicInteger count = new AtomicInteger(); given(this.repository.generateToken(any())).willReturn(chainResult.mono());
given(this.repository.generateToken(any())).willReturn(Mono.fromCallable(() -> { given(this.chain.filter(any())).willReturn(Mono.empty());
count.incrementAndGet();
return this.token;
}));
given(this.repository.saveToken(any(), any())).willReturn(Mono.empty());
AtomicReference<Mono<CsrfToken>> tokenFromExchange = new AtomicReference<>();
given(this.chain.filter(any())).willReturn(
Mono.fromRunnable(() -> tokenFromExchange.set(this.get.getAttribute(CsrfToken.class.getName()))));
this.csrfFilter.filter(this.get, this.chain).block(); this.csrfFilter.filter(this.get, this.chain).block();
tokenFromExchange.get().block(); Mono<CsrfToken> result = this.get.getAttribute(CsrfToken.class.getName());
tokenFromExchange.get().block(); result.block();
assertThat(count).hasValue(1); result.block();
assertThat(chainResult.subscribeCount()).isEqualTo(1);
} }
@RestController @RestController