Remove blank lines from all tests

Remove all blank lines from test code so that test methods are
visually grouped together. This generally helps to make the test
classes easer to scan, however, the "given" / "when" / "then"
blocks used by some tests are now not as easy to discern.

Issue gh-8945
This commit is contained in:
Phillip Webb
2020-08-01 19:33:21 -07:00
committed by Rob Winch
parent 5bdd757108
commit a5aa6b3d7f
787 changed files with 9 additions and 10241 deletions

View File

@@ -56,7 +56,6 @@ public class DefaultMessageSecurityExpressionHandlerTests {
@Before
public void setup() {
this.handler = new DefaultMessageSecurityExpressionHandler<>();
this.message = new GenericMessage<>("");
this.authentication = new AnonymousAuthenticationToken("key", "anonymous",
AuthorityUtils.createAuthorityList("ROLE_ANONYMOUS"));
@@ -67,7 +66,6 @@ public class DefaultMessageSecurityExpressionHandlerTests {
public void trustResolverPopulated() {
EvaluationContext context = this.handler.createEvaluationContext(this.authentication, this.message);
Expression expression = this.handler.getExpressionParser().parseExpression("authenticated");
assertThat(ExpressionUtils.evaluateAsBoolean(expression, context)).isFalse();
}
@@ -82,7 +80,6 @@ public class DefaultMessageSecurityExpressionHandlerTests {
EvaluationContext context = this.handler.createEvaluationContext(this.authentication, this.message);
Expression expression = this.handler.getExpressionParser().parseExpression("authenticated");
given(this.trustResolver.isAnonymous(this.authentication)).willReturn(false);
assertThat(ExpressionUtils.evaluateAsBoolean(expression, context)).isTrue();
}
@@ -94,7 +91,6 @@ public class DefaultMessageSecurityExpressionHandlerTests {
this.handler.setRoleHierarchy(roleHierarchy);
EvaluationContext context = this.handler.createEvaluationContext(this.authentication, this.message);
Expression expression = this.handler.getExpressionParser().parseExpression("hasRole('ROLE_USER')");
assertThat(ExpressionUtils.evaluateAsBoolean(expression, context)).isTrue();
}
@@ -104,7 +100,6 @@ public class DefaultMessageSecurityExpressionHandlerTests {
EvaluationContext context = this.handler.createEvaluationContext(this.authentication, this.message);
Expression expression = this.handler.getExpressionParser().parseExpression("hasPermission(message, 'read')");
given(this.permissionEvaluator.hasPermission(this.authentication, this.message, "read")).willReturn(true);
assertThat(ExpressionUtils.evaluateAsBoolean(expression, context)).isTrue();
}

View File

@@ -66,7 +66,6 @@ public class ExpressionBasedMessageSecurityMetadataSourceFactoryTests {
this.matcherToExpression = new LinkedHashMap<>();
this.matcherToExpression.put(this.matcher1, this.expression1);
this.matcherToExpression.put(this.matcher2, this.expression2);
this.source = ExpressionBasedMessageSecurityMetadataSourceFactory
.createExpressionMessageMetadataSource(this.matcherToExpression);
this.rootObject = new MessageSecurityExpressionRoot(this.authentication, this.message);
@@ -74,18 +73,14 @@ public class ExpressionBasedMessageSecurityMetadataSourceFactoryTests {
@Test
public void createExpressionMessageMetadataSourceNoMatch() {
Collection<ConfigAttribute> attrs = this.source.getAttributes(this.message);
assertThat(attrs).isNull();
}
@Test
public void createExpressionMessageMetadataSourceMatchFirst() {
given(this.matcher1.matches(this.message)).willReturn(true);
Collection<ConfigAttribute> attrs = this.source.getAttributes(this.message);
assertThat(attrs).hasSize(1);
ConfigAttribute attr = attrs.iterator().next();
assertThat(attr).isInstanceOf(MessageExpressionConfigAttribute.class);
@@ -96,9 +91,7 @@ public class ExpressionBasedMessageSecurityMetadataSourceFactoryTests {
@Test
public void createExpressionMessageMetadataSourceMatchSecond() {
given(this.matcher2.matches(this.message)).willReturn(true);
Collection<ConfigAttribute> attrs = this.source.getAttributes(this.message);
assertThat(attrs).hasSize(1);
ConfigAttribute attr = attrs.iterator().next();
assertThat(attr).isInstanceOf(MessageExpressionConfigAttribute.class);

View File

@@ -74,7 +74,6 @@ public class MessageExpressionConfigAttributeTests {
@Test
public void toStringUsesExpressionString() {
given(this.expression.getExpressionString()).willReturn("toString");
assertThat(this.attribute.toString()).isEqualTo(this.expression.getExpressionString());
}
@@ -84,10 +83,8 @@ public class MessageExpressionConfigAttributeTests {
Message<?> message = MessageBuilder.withPayload("M")
.setHeader(SimpMessageHeaderAccessor.DESTINATION_HEADER, "/topics/someTopic/sub1").build();
EvaluationContext context = mock(EvaluationContext.class);
this.attribute = new MessageExpressionConfigAttribute(this.expression, matcher);
this.attribute.postProcess(context, message);
verify(context).setVariable("topic", "someTopic");
}

View File

@@ -71,7 +71,6 @@ public class MessageExpressionVoterTests {
public void setup() {
this.attributes = Arrays
.<ConfigAttribute>asList(new MessageExpressionConfigAttribute(this.expression, this.matcher));
this.voter = new MessageExpressionVoter();
}
@@ -127,10 +126,8 @@ public class MessageExpressionVoterTests {
given(this.expressionHandler.createEvaluationContext(this.authentication, this.message))
.willReturn(this.evaluationContext);
given(this.expression.getValue(this.evaluationContext, Boolean.class)).willReturn(true);
assertThat(this.voter.vote(this.authentication, this.message, this.attributes))
.isEqualTo(AccessDecisionVoter.ACCESS_GRANTED);
verify(this.expressionHandler).createEvaluationContext(this.authentication, this.message);
}
@@ -144,7 +141,6 @@ public class MessageExpressionVoterTests {
this.attributes = Arrays.<ConfigAttribute>asList(configAttribute);
given(configAttribute.postProcess(this.evaluationContext, this.message)).willReturn(this.evaluationContext);
given(this.expression.getValue(any(EvaluationContext.class), eq(Boolean.class))).willReturn(true);
assertThat(this.voter.vote(this.authentication, this.message, this.attributes))
.isEqualTo(AccessDecisionVoter.ACCESS_GRANTED);
verify(configAttribute).postProcess(this.evaluationContext, this.message);

View File

@@ -77,7 +77,6 @@ public class ChannelSecurityInterceptorTests {
this.interceptor = new ChannelSecurityInterceptor(this.source);
this.interceptor.setAccessDecisionManager(this.accessDecisionManager);
this.interceptor.setRunAsManager(this.runAsManager);
this.originalAuth = new TestingAuthenticationToken("user", "pass", "ROLE_USER");
SecurityContextHolder.getContext().setAuthentication(this.originalAuth);
}
@@ -110,9 +109,7 @@ public class ChannelSecurityInterceptorTests {
@Test
public void preSendGrant() {
given(this.source.getAttributes(this.message)).willReturn(this.attrs);
Message<?> result = this.interceptor.preSend(this.message, this.channel);
assertThat(result).isSameAs(this.message);
}
@@ -121,7 +118,6 @@ public class ChannelSecurityInterceptorTests {
given(this.source.getAttributes(this.message)).willReturn(this.attrs);
willThrow(new AccessDeniedException("")).given(this.accessDecisionManager).decide(any(Authentication.class),
eq(this.message), eq(this.attrs));
this.interceptor.preSend(this.message, this.channel);
}
@@ -131,13 +127,9 @@ public class ChannelSecurityInterceptorTests {
given(this.source.getAttributes(this.message)).willReturn(this.attrs);
given(this.runAsManager.buildRunAs(any(Authentication.class), any(), any(Collection.class)))
.willReturn(this.runAs);
Message<?> preSend = this.interceptor.preSend(this.message, this.channel);
assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(this.runAs);
this.interceptor.postSend(preSend, this.channel, true);
assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(this.originalAuth);
}
@@ -152,13 +144,9 @@ public class ChannelSecurityInterceptorTests {
given(this.source.getAttributes(this.message)).willReturn(this.attrs);
given(this.runAsManager.buildRunAs(any(Authentication.class), any(), any(Collection.class)))
.willReturn(this.runAs);
Message<?> preSend = this.interceptor.preSend(this.message, this.channel);
assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(this.runAs);
this.interceptor.afterSendCompletion(preSend, this.channel, true, new RuntimeException());
assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(this.originalAuth);
}

View File

@@ -63,7 +63,6 @@ public class DefaultMessageSecurityMetadataSourceTests {
this.messageMap = new LinkedHashMap<>();
this.messageMap.put(this.matcher1, Arrays.<ConfigAttribute>asList(this.config1));
this.messageMap.put(this.matcher2, Arrays.<ConfigAttribute>asList(this.config2));
this.source = new DefaultMessageSecurityMetadataSource(this.messageMap);
}
@@ -75,14 +74,12 @@ public class DefaultMessageSecurityMetadataSourceTests {
@Test
public void getAttributesFirst() {
given(this.matcher1.matches(this.message)).willReturn(true);
assertThat(this.source.getAttributes(this.message)).containsOnly(this.config1);
}
@Test
public void getAttributesSecond() {
given(this.matcher1.matches(this.message)).willReturn(true);
assertThat(this.source.getAttributes(this.message)).containsOnly(this.config2);
}

View File

@@ -63,7 +63,6 @@ public class SecurityContextChannelInterceptorTests {
this.messageBuilder = MessageBuilder.withPayload("payload");
this.expectedAnonymous = new AnonymousAuthenticationToken("key", "anonymous",
AuthorityUtils.createAuthorityList("ROLE_ANONYMOUS"));
this.interceptor = new SecurityContextChannelInterceptor();
}
@@ -82,18 +81,14 @@ public class SecurityContextChannelInterceptorTests {
String headerName = "header";
this.interceptor = new SecurityContextChannelInterceptor(headerName);
this.messageBuilder.setHeader(headerName, this.authentication);
this.interceptor.preSend(this.messageBuilder.build(), this.channel);
assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(this.authentication);
}
@Test
public void preSendUserSet() {
this.messageBuilder.setHeader(SimpMessageHeaderAccessor.USER_HEADER, this.authentication);
this.interceptor.preSend(this.messageBuilder.build(), this.channel);
assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(this.authentication);
}
@@ -107,9 +102,7 @@ public class SecurityContextChannelInterceptorTests {
this.expectedAnonymous = new AnonymousAuthenticationToken("customKey", "customAnonymous",
AuthorityUtils.createAuthorityList("ROLE_CUSTOM"));
this.interceptor.setAnonymousAuthentication(this.expectedAnonymous);
this.interceptor.preSend(this.messageBuilder.build(), this.channel);
assertAnonymous();
}
@@ -117,9 +110,7 @@ public class SecurityContextChannelInterceptorTests {
@Test
public void preSendUserNotAuthentication() {
this.messageBuilder.setHeader(SimpMessageHeaderAccessor.USER_HEADER, this.principal);
this.interceptor.preSend(this.messageBuilder.build(), this.channel);
assertAnonymous();
}
@@ -127,7 +118,6 @@ public class SecurityContextChannelInterceptorTests {
@Test
public void preSendUserNotSet() {
this.interceptor.preSend(this.messageBuilder.build(), this.channel);
assertAnonymous();
}
@@ -135,32 +125,26 @@ public class SecurityContextChannelInterceptorTests {
@Test
public void preSendUserNotSetCustomAnonymous() {
this.interceptor.preSend(this.messageBuilder.build(), this.channel);
assertAnonymous();
}
@Test
public void afterSendCompletion() {
SecurityContextHolder.getContext().setAuthentication(this.authentication);
this.interceptor.afterSendCompletion(this.messageBuilder.build(), this.channel, true, null);
assertThat(SecurityContextHolder.getContext().getAuthentication()).isNull();
}
@Test
public void afterSendCompletionNullAuthentication() {
this.interceptor.afterSendCompletion(this.messageBuilder.build(), this.channel, true, null);
assertThat(SecurityContextHolder.getContext().getAuthentication()).isNull();
}
@Test
public void beforeHandleUserSet() {
this.messageBuilder.setHeader(SimpMessageHeaderAccessor.USER_HEADER, this.authentication);
this.interceptor.beforeHandle(this.messageBuilder.build(), this.channel, this.handler);
assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(this.authentication);
}
@@ -168,9 +152,7 @@ public class SecurityContextChannelInterceptorTests {
@Test
public void beforeHandleUserNotAuthentication() {
this.messageBuilder.setHeader(SimpMessageHeaderAccessor.USER_HEADER, this.principal);
this.interceptor.beforeHandle(this.messageBuilder.build(), this.channel, this.handler);
assertAnonymous();
}
@@ -178,23 +160,19 @@ public class SecurityContextChannelInterceptorTests {
@Test
public void beforeHandleUserNotSet() {
this.interceptor.beforeHandle(this.messageBuilder.build(), this.channel, this.handler);
assertAnonymous();
}
@Test
public void afterMessageHandledUserNotSet() {
this.interceptor.afterMessageHandled(this.messageBuilder.build(), this.channel, this.handler, null);
assertThat(SecurityContextHolder.getContext().getAuthentication()).isNull();
}
@Test
public void afterMessageHandled() {
SecurityContextHolder.getContext().setAuthentication(this.authentication);
this.interceptor.afterMessageHandled(this.messageBuilder.build(), this.channel, this.handler, null);
assertThat(SecurityContextHolder.getContext().getAuthentication()).isNull();
}
@@ -203,14 +181,10 @@ public class SecurityContextChannelInterceptorTests {
public void restoresOriginalContext() {
TestingAuthenticationToken original = new TestingAuthenticationToken("original", "original", "ROLE_USER");
SecurityContextHolder.getContext().setAuthentication(original);
this.messageBuilder.setHeader(SimpMessageHeaderAccessor.USER_HEADER, this.authentication);
this.interceptor.beforeHandle(this.messageBuilder.build(), this.channel, this.handler);
assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(this.authentication);
this.interceptor.afterMessageHandled(this.messageBuilder.build(), this.channel, this.handler, null);
assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(original);
}
@@ -222,35 +196,25 @@ public class SecurityContextChannelInterceptorTests {
public void restoresOriginalContextNestedThreeDeep() {
AnonymousAuthenticationToken anonymous = new AnonymousAuthenticationToken("key", "anonymous",
AuthorityUtils.createAuthorityList("ROLE_USER"));
TestingAuthenticationToken origional = new TestingAuthenticationToken("original", "origional", "ROLE_USER");
SecurityContextHolder.getContext().setAuthentication(origional);
this.messageBuilder.setHeader(SimpMessageHeaderAccessor.USER_HEADER, this.authentication);
this.interceptor.beforeHandle(this.messageBuilder.build(), this.channel, this.handler);
assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(this.authentication);
// start send websocket
this.messageBuilder.setHeader(SimpMessageHeaderAccessor.USER_HEADER, null);
this.interceptor.beforeHandle(this.messageBuilder.build(), this.channel, this.handler);
assertThat(SecurityContextHolder.getContext().getAuthentication().getName()).isEqualTo(anonymous.getName());
this.interceptor.afterMessageHandled(this.messageBuilder.build(), this.channel, this.handler, null);
assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(this.authentication);
// end send websocket
this.interceptor.afterMessageHandled(this.messageBuilder.build(), this.channel, this.handler, null);
assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(origional);
}
private void assertAnonymous() {
Authentication currentAuthentication = SecurityContextHolder.getContext().getAuthentication();
assertThat(currentAuthentication).isInstanceOf(AnonymousAuthenticationToken.class);
AnonymousAuthenticationToken anonymous = (AnonymousAuthenticationToken) currentAuthentication;
assertThat(anonymous.getName()).isEqualTo(this.expectedAnonymous.getName());
assertThat(anonymous.getAuthorities()).containsOnlyElementsOf(this.expectedAnonymous.getAuthorities());

View File

@@ -265,17 +265,14 @@ public final class ResolvableMethod {
factory.addAdvice(interceptor);
return (T) factory.getProxy();
}
else {
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(type);
enhancer.setInterfaces(new Class<?>[] { Supplier.class });
enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE);
enhancer.setCallbackType(org.springframework.cglib.proxy.MethodInterceptor.class);
Class<?> proxyClass = enhancer.createClass();
Object proxy = null;
if (objenesis.isWorthTrying()) {
try {
proxy = objenesis.newInstance(proxyClass, enhancer.getUseCache());
@@ -284,7 +281,6 @@ public final class ResolvableMethod {
logger.debug("Objenesis failed, falling back to default constructor", ex);
}
}
if (proxy == null) {
try {
proxy = ReflectionUtils.accessibleConstructor(proxyClass).newInstance();
@@ -295,7 +291,6 @@ public final class ResolvableMethod {
ex);
}
}
((Factory) proxy).setCallbacks(new Callback[] { interceptor });
return (T) proxy;
}
@@ -440,7 +435,6 @@ public final class ResolvableMethod {
}
// Build & resolve shortcuts...
/**
* Resolve and return the {@code Method} equivalent to:
* <p>
@@ -489,7 +483,6 @@ public final class ResolvableMethod {
*/
public MethodParameter resolveReturnType(Class<?> returnType, ResolvableType generic,
ResolvableType... generics) {
return returning(returnType, generic, generics).method().returnType();
}

View File

@@ -79,7 +79,6 @@ public class AndMessageMatcherTests {
public void matchesSingleTrue() {
given(this.delegate.matches(this.message)).willReturn(true);
this.matcher = new AndMessageMatcher<>(this.delegate);
assertThat(this.matcher.matches(this.message)).isTrue();
}
@@ -88,7 +87,6 @@ public class AndMessageMatcherTests {
given(this.delegate.matches(this.message)).willReturn(true);
given(this.delegate2.matches(this.message)).willReturn(true);
this.matcher = new AndMessageMatcher<>(this.delegate, this.delegate2);
assertThat(this.matcher.matches(this.message)).isTrue();
}
@@ -96,7 +94,6 @@ public class AndMessageMatcherTests {
public void matchesSingleFalse() {
given(this.delegate.matches(this.message)).willReturn(false);
this.matcher = new AndMessageMatcher<>(this.delegate);
assertThat(this.matcher.matches(this.message)).isFalse();
}
@@ -104,7 +101,6 @@ public class AndMessageMatcherTests {
public void matchesMultiBothFalse() {
given(this.delegate.matches(this.message)).willReturn(false);
this.matcher = new AndMessageMatcher<>(this.delegate, this.delegate2);
assertThat(this.matcher.matches(this.message)).isFalse();
}
@@ -113,7 +109,6 @@ public class AndMessageMatcherTests {
given(this.delegate.matches(this.message)).willReturn(true);
given(this.delegate2.matches(this.message)).willReturn(false);
this.matcher = new AndMessageMatcher<>(this.delegate, this.delegate2);
assertThat(this.matcher.matches(this.message)).isFalse();
}

View File

@@ -79,7 +79,6 @@ public class OrMessageMatcherTests {
public void matchesSingleTrue() {
given(this.delegate.matches(this.message)).willReturn(true);
this.matcher = new OrMessageMatcher<>(this.delegate);
assertThat(this.matcher.matches(this.message)).isTrue();
}
@@ -87,7 +86,6 @@ public class OrMessageMatcherTests {
public void matchesMultiTrue() {
given(this.delegate.matches(this.message)).willReturn(true);
this.matcher = new OrMessageMatcher<>(this.delegate, this.delegate2);
assertThat(this.matcher.matches(this.message)).isTrue();
}
@@ -95,7 +93,6 @@ public class OrMessageMatcherTests {
public void matchesSingleFalse() {
given(this.delegate.matches(this.message)).willReturn(false);
this.matcher = new OrMessageMatcher<>(this.delegate);
assertThat(this.matcher.matches(this.message)).isFalse();
}
@@ -104,7 +101,6 @@ public class OrMessageMatcherTests {
given(this.delegate.matches(this.message)).willReturn(false);
given(this.delegate2.matches(this.message)).willReturn(false);
this.matcher = new OrMessageMatcher<>(this.delegate, this.delegate2);
assertThat(this.matcher.matches(this.message)).isFalse();
}
@@ -112,7 +108,6 @@ public class OrMessageMatcherTests {
public void matchesMultiSingleFalse() {
given(this.delegate.matches(this.message)).willReturn(true);
this.matcher = new OrMessageMatcher<>(this.delegate, this.delegate2);
assertThat(this.matcher.matches(this.message)).isTrue();
}

View File

@@ -59,74 +59,59 @@ public class SimpDestinationMessageMatcherTests {
@Test
public void matchesAllWithDestination() {
this.messageBuilder.setHeader(SimpMessageHeaderAccessor.DESTINATION_HEADER, "/destination/1");
assertThat(this.matcher.matches(this.messageBuilder.build())).isTrue();
}
@Test
public void matchesSpecificWithDestination() {
this.matcher = new SimpDestinationMessageMatcher("/destination/1");
this.messageBuilder.setHeader(SimpMessageHeaderAccessor.DESTINATION_HEADER, "/destination/1");
assertThat(this.matcher.matches(this.messageBuilder.build())).isTrue();
}
@Test
public void matchesFalseWithDestination() {
this.matcher = new SimpDestinationMessageMatcher("/nomatch");
this.messageBuilder.setHeader(SimpMessageHeaderAccessor.DESTINATION_HEADER, "/destination/1");
assertThat(this.matcher.matches(this.messageBuilder.build())).isFalse();
}
@Test
public void matchesFalseMessageTypeNotDisconnectType() {
this.matcher = SimpDestinationMessageMatcher.createMessageMatcher("/match", this.pathMatcher);
this.messageBuilder.setHeader(SimpMessageHeaderAccessor.MESSAGE_TYPE_HEADER, SimpMessageType.DISCONNECT);
assertThat(this.matcher.matches(this.messageBuilder.build())).isFalse();
}
@Test
public void matchesTrueMessageType() {
this.matcher = SimpDestinationMessageMatcher.createMessageMatcher("/match", this.pathMatcher);
this.messageBuilder.setHeader(SimpMessageHeaderAccessor.DESTINATION_HEADER, "/match");
this.messageBuilder.setHeader(SimpMessageHeaderAccessor.MESSAGE_TYPE_HEADER, SimpMessageType.MESSAGE);
assertThat(this.matcher.matches(this.messageBuilder.build())).isTrue();
}
@Test
public void matchesTrueSubscribeType() {
this.matcher = SimpDestinationMessageMatcher.createSubscribeMatcher("/match", this.pathMatcher);
this.messageBuilder.setHeader(SimpMessageHeaderAccessor.DESTINATION_HEADER, "/match");
this.messageBuilder.setHeader(SimpMessageHeaderAccessor.MESSAGE_TYPE_HEADER, SimpMessageType.SUBSCRIBE);
assertThat(this.matcher.matches(this.messageBuilder.build())).isTrue();
}
@Test
public void matchesNullMessageType() {
this.matcher = new SimpDestinationMessageMatcher("/match");
this.messageBuilder.setHeader(SimpMessageHeaderAccessor.DESTINATION_HEADER, "/match");
this.messageBuilder.setHeader(SimpMessageHeaderAccessor.MESSAGE_TYPE_HEADER, SimpMessageType.MESSAGE);
assertThat(this.matcher.matches(this.messageBuilder.build())).isTrue();
}
@Test
public void extractPathVariablesFromDestination() {
this.matcher = new SimpDestinationMessageMatcher("/topics/{topic}/**");
this.messageBuilder.setHeader(SimpMessageHeaderAccessor.DESTINATION_HEADER, "/topics/someTopic/sub1");
this.messageBuilder.setHeader(SimpMessageHeaderAccessor.MESSAGE_TYPE_HEADER, SimpMessageType.MESSAGE);
assertThat(this.matcher.extractPathVariables(this.messageBuilder.build()).get("topic")).isEqualTo("someTopic");
}
@@ -139,11 +124,8 @@ public class SimpDestinationMessageMatcherTests {
@Test
public void typeConstructorParameterIsTransmitted() {
this.matcher = SimpDestinationMessageMatcher.createMessageMatcher("/match", this.pathMatcher);
MessageMatcher<Object> expectedTypeMatcher = new SimpMessageTypeMatcher(SimpMessageType.MESSAGE);
assertThat(this.matcher.getMessageTypeMatcher()).isEqualTo(expectedTypeMatcher);
}
}

View File

@@ -44,7 +44,6 @@ public class SimpMessageTypeMatcherTests {
public void matchesMessageMessageTrue() {
Message<String> message = MessageBuilder.withPayload("Hi")
.setHeader(SimpMessageHeaderAccessor.MESSAGE_TYPE_HEADER, SimpMessageType.MESSAGE).build();
assertThat(this.matcher.matches(message)).isTrue();
}
@@ -52,14 +51,12 @@ public class SimpMessageTypeMatcherTests {
public void matchesMessageConnectFalse() {
Message<String> message = MessageBuilder.withPayload("Hi")
.setHeader(SimpMessageHeaderAccessor.MESSAGE_TYPE_HEADER, SimpMessageType.CONNECT).build();
assertThat(this.matcher.matches(message)).isFalse();
}
@Test
public void matchesMessageNullFalse() {
Message<String> message = MessageBuilder.withPayload("Hi").build();
assertThat(this.matcher.matches(message)).isFalse();
}

View File

@@ -51,7 +51,6 @@ public class CsrfChannelInterceptorTests {
public void setup() {
this.token = new DefaultCsrfToken("header", "param", "token");
this.interceptor = new CsrfChannelInterceptor();
this.messageHeaders = SimpMessageHeaderAccessor.create(SimpMessageType.CONNECT);
this.messageHeaders.setNativeHeader(this.token.getHeaderName(), this.token.getToken());
this.messageHeaders.setSessionAttributes(new HashMap<>());
@@ -66,84 +65,72 @@ public class CsrfChannelInterceptorTests {
@Test
public void preSendIgnoresConnectAck() {
this.messageHeaders = SimpMessageHeaderAccessor.create(SimpMessageType.CONNECT_ACK);
this.interceptor.preSend(message(), this.channel);
}
@Test
public void preSendIgnoresDisconnect() {
this.messageHeaders = SimpMessageHeaderAccessor.create(SimpMessageType.DISCONNECT);
this.interceptor.preSend(message(), this.channel);
}
@Test
public void preSendIgnoresDisconnectAck() {
this.messageHeaders = SimpMessageHeaderAccessor.create(SimpMessageType.DISCONNECT_ACK);
this.interceptor.preSend(message(), this.channel);
}
@Test
public void preSendIgnoresHeartbeat() {
this.messageHeaders = SimpMessageHeaderAccessor.create(SimpMessageType.HEARTBEAT);
this.interceptor.preSend(message(), this.channel);
}
@Test
public void preSendIgnoresMessage() {
this.messageHeaders = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
this.interceptor.preSend(message(), this.channel);
}
@Test
public void preSendIgnoresOther() {
this.messageHeaders = SimpMessageHeaderAccessor.create(SimpMessageType.OTHER);
this.interceptor.preSend(message(), this.channel);
}
@Test
public void preSendIgnoresSubscribe() {
this.messageHeaders = SimpMessageHeaderAccessor.create(SimpMessageType.SUBSCRIBE);
this.interceptor.preSend(message(), this.channel);
}
@Test
public void preSendIgnoresUnsubscribe() {
this.messageHeaders = SimpMessageHeaderAccessor.create(SimpMessageType.UNSUBSCRIBE);
this.interceptor.preSend(message(), this.channel);
}
@Test(expected = InvalidCsrfTokenException.class)
public void preSendNoToken() {
this.messageHeaders.removeNativeHeader(this.token.getHeaderName());
this.interceptor.preSend(message(), this.channel);
}
@Test(expected = InvalidCsrfTokenException.class)
public void preSendInvalidToken() {
this.messageHeaders.setNativeHeader(this.token.getHeaderName(), this.token.getToken() + "invalid");
this.interceptor.preSend(message(), this.channel);
}
@Test(expected = MissingCsrfTokenException.class)
public void preSendMissingToken() {
this.messageHeaders.getSessionAttributes().clear();
this.interceptor.preSend(message(), this.channel);
}
@Test(expected = MissingCsrfTokenException.class)
public void preSendMissingTokenNullSessionAttributes() {
this.messageHeaders.setSessionAttributes(null);
this.interceptor.preSend(message(), this.channel);
}

View File

@@ -60,14 +60,12 @@ public class CsrfTokenHandshakeInterceptorTests {
this.httpRequest = new MockHttpServletRequest();
this.attributes = new HashMap<>();
this.request = new ServletServerHttpRequest(this.httpRequest);
this.interceptor = new CsrfTokenHandshakeInterceptor();
}
@Test
public void beforeHandshakeNoAttribute() throws Exception {
this.interceptor.beforeHandshake(this.request, this.response, this.wsHandler, this.attributes);
assertThat(this.attributes).isEmpty();
}
@@ -75,9 +73,7 @@ public class CsrfTokenHandshakeInterceptorTests {
public void beforeHandshake() throws Exception {
CsrfToken token = new DefaultCsrfToken("header", "param", "token");
this.httpRequest.setAttribute(CsrfToken.class.getName(), token);
this.interceptor.beforeHandshake(this.request, this.response, this.wsHandler, this.attributes);
assertThat(this.attributes.keySet()).containsOnly(CsrfToken.class.getName());
assertThat(this.attributes.values()).containsOnly(token);
}