Fix tests for deferred CSRF tokens

Issue gh-4001
This commit is contained in:
Steve Riesenberg 2022-10-05 16:09:18 -05:00
parent 521cdfd738
commit ee9449dbfe
No known key found for this signature in database
GPG Key ID: 5F311AB48A55D521
1 changed files with 10 additions and 5 deletions

View File

@ -105,9 +105,11 @@ public class XorCsrfTokenRequestAttributeHandlerTests {
@Test
public void handleWhenCsrfTokenIsNullThenThrowsIllegalStateException() {
this.handler.handle(this.request, this.response, () -> null);
CsrfToken csrfTokenAttribute = (CsrfToken) this.request.getAttribute("_csrf");
// @formatter:off
assertThatIllegalStateException()
.isThrownBy(() -> this.handler.handle(this.request, this.response, () -> null))
.isThrownBy(csrfTokenAttribute::getToken)
.withMessage("csrfToken supplier returned null");
// @formatter:on
}
@ -128,8 +130,12 @@ public class XorCsrfTokenRequestAttributeHandlerTests {
@Test
public void handleWhenSecureRandomSetThenUsed() {
willAnswer(fillByteArray()).given(this.secureRandom).nextBytes(anyByteArray());
this.handler.setSecureRandom(this.secureRandom);
this.handler.handle(this.request, this.response, () -> this.token);
CsrfToken csrfTokenAttribute = (CsrfToken) this.request.getAttribute(CsrfToken.class.getName());
assertThat(csrfTokenAttribute.getToken()).isEqualTo(XOR_CSRF_TOKEN_VALUE);
verify(this.secureRandom).nextBytes(anyByteArray());
verifyNoMoreInteractions(this.secureRandom);
}
@ -140,12 +146,11 @@ public class XorCsrfTokenRequestAttributeHandlerTests {
this.handler.setSecureRandom(this.secureRandom);
this.handler.handle(this.request, this.response, () -> this.token);
verify(this.secureRandom).nextBytes(anyByteArray());
assertThat(this.request.getAttribute(CsrfToken.class.getName())).isNotNull();
assertThat(this.request.getAttribute(this.token.getParameterName())).isNotNull();
CsrfToken csrfTokenAttribute = (CsrfToken) this.request.getAttribute(CsrfToken.class.getName());
assertThat(csrfTokenAttribute.getToken()).isEqualTo(XOR_CSRF_TOKEN_VALUE);
verify(this.secureRandom).nextBytes(anyByteArray());
assertThat(this.request.getAttribute(CsrfToken.class.getName())).isNotNull();
assertThat(this.request.getAttribute("_csrf")).isNotNull();
}
@Test