@Test public void simpleRegistryMappings() { loadConfig(SockJsSecurityConfig.class); clientInboundChannel().send(message("/permitAll")); try { clientInboundChannel().send(message("/denyAll")); fail("Expected Exception"); } catch (MessageDeliveryException expected) { assertThat(expected.getCause()).isInstanceOf(AccessDeniedException.class); } }
@Test public void defaultPatternMatcher() throws Exception { loadConfig(DefaultPatternMatcherConfig.class); clientInboundChannel().send(message("/app/a/b")); try { clientInboundChannel().send(message("/app/a/b/c")); fail("Expected Exception"); } catch (MessageDeliveryException expected) { assertThat(expected.getCause()).isInstanceOf(AccessDeniedException.class); } }
@Test public void securityMappings() { loadConfig(WebSocketSecurityConfig.class); clientInboundChannel().send( message("/user/queue/errors", SimpMessageType.SUBSCRIBE)); try { clientInboundChannel().send(message("/denyAll", SimpMessageType.MESSAGE)); fail("Expected Exception"); } catch (MessageDeliveryException expected) { assertThat(expected.getCause()).isInstanceOf(AccessDeniedException.class); } }
@Test public void msmsRegistryCustomPatternMatcher() throws Exception { loadConfig(MsmsRegistryCustomPatternMatcherConfig.class); clientInboundChannel().send(message("/app/a.b")); try { clientInboundChannel().send(message("/app/a.b.c")); fail("Expected Exception"); } catch (MessageDeliveryException expected) { assertThat(expected.getCause()).isInstanceOf(AccessDeniedException.class); } }
@Test public void overrideMsmsRegistryCustomPatternMatcher() throws Exception { loadConfig(OverrideMsmsRegistryCustomPatternMatcherConfig.class); clientInboundChannel().send(message("/app/a/b")); try { clientInboundChannel().send(message("/app/a/b/c")); fail("Expected Exception"); } catch (MessageDeliveryException expected) { assertThat(expected.getCause()).isInstanceOf(AccessDeniedException.class); } }
@Test public void addsCsrfProtectionWhenNoAuthorization() throws InterruptedException { loadConfig(NoInboundSecurityConfig.class); SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor .create(SimpMessageType.CONNECT); Message<?> message = message(headers, "/authentication"); MessageChannel messageChannel = clientInboundChannel(); try { messageChannel.send(message); fail("Expected Exception"); } catch (MessageDeliveryException success) { assertThat(success.getCause()).isInstanceOf(MissingCsrfTokenException.class); } }
@Test public void csrfProtectionForConnect() throws InterruptedException { loadConfig(SockJsSecurityConfig.class); SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor .create(SimpMessageType.CONNECT); Message<?> message = message(headers, "/authentication"); MessageChannel messageChannel = clientInboundChannel(); try { messageChannel.send(message); fail("Expected Exception"); } catch (MessageDeliveryException success) { assertThat(success.getCause()).isInstanceOf(MissingCsrfTokenException.class); } }
@Test public void customExpression() throws Exception { loadConfig(CustomExpressionConfig.class); clientInboundChannel().send(message("/denyRob")); this.messageUser = new TestingAuthenticationToken("rob", "password", "ROLE_USER"); try { clientInboundChannel().send(message("/denyRob")); fail("Expected Exception"); } catch (MessageDeliveryException expected) { assertThat(expected.getCause()).isInstanceOf(AccessDeniedException.class); } }
@Test public void failurePropagates() { RuntimeException ex = new RuntimeException(); willThrow(ex).given(this.handler).handleMessage(this.message); MessageHandler secondHandler = mock(MessageHandler.class); this.channel.subscribe(this.handler); this.channel.subscribe(secondHandler); try { this.channel.send(message); } catch (MessageDeliveryException actualException) { assertThat(actualException.getCause(), equalTo(ex)); } verifyZeroInteractions(secondHandler); }
@Test public void interceptorWithException() { IllegalStateException expected = new IllegalStateException("Fake exception"); willThrow(expected).given(this.handler).handleMessage(this.message); BeforeHandleInterceptor interceptor = new BeforeHandleInterceptor(); this.channel.addInterceptor(interceptor); this.channel.subscribe(this.handler); try { this.channel.send(this.message); } catch (MessageDeliveryException actual) { assertSame(expected, actual.getCause()); } verify(this.handler).handleMessage(this.message); assertEquals(1, interceptor.getCounter().get()); assertTrue(interceptor.wasAfterHandledInvoked()); }
@Test public void errorChannelHeaderAndHandlerThrowsExceptionWithDelay() { this.delayHandler.setRetryDelay(1); DirectChannel errorChannel = new DirectChannel(); MessagePublishingErrorHandler errorHandler = new MessagePublishingErrorHandler(); errorHandler.setDefaultErrorChannel(errorChannel); taskScheduler.setErrorHandler(errorHandler); this.setDelayExpression(); startDelayerHandler(); output.unsubscribe(resultHandler); errorChannel.subscribe(resultHandler); output.subscribe(message -> { throw new UnsupportedOperationException("intentional test failure"); }); Message<?> message = MessageBuilder.withPayload("test") .setHeader("delay", "10") .setErrorChannel(errorChannel).build(); input.send(message); waitForLatch(10000); Message<?> errorMessage = resultHandler.lastMessage; assertEquals(MessageDeliveryException.class, errorMessage.getPayload().getClass()); MessageDeliveryException exceptionPayload = (MessageDeliveryException) errorMessage.getPayload(); assertSame(message.getPayload(), exceptionPayload.getFailedMessage().getPayload()); assertEquals(UnsupportedOperationException.class, exceptionPayload.getCause().getClass()); assertNotSame(Thread.currentThread(), resultHandler.lastThread); }
@Test public void defaultErrorChannelAndHandlerThrowsExceptionWithDelay() { this.delayHandler.setRetryDelay(1); StaticApplicationContext context = new StaticApplicationContext(); context.registerSingleton(IntegrationContextUtils.ERROR_CHANNEL_BEAN_NAME, DirectChannel.class); context.refresh(); DirectChannel defaultErrorChannel = (DirectChannel) context .getBean(IntegrationContextUtils.ERROR_CHANNEL_BEAN_NAME); MessagePublishingErrorHandler errorHandler = new MessagePublishingErrorHandler(); errorHandler.setBeanFactory(context); taskScheduler.setErrorHandler(errorHandler); this.setDelayExpression(); startDelayerHandler(); output.unsubscribe(resultHandler); defaultErrorChannel.subscribe(resultHandler); output.subscribe(message -> { throw new UnsupportedOperationException("intentional test failure"); }); Message<?> message = MessageBuilder.withPayload("test") .setHeader("delay", "10").build(); input.send(message); waitForLatch(10000); Message<?> errorMessage = resultHandler.lastMessage; assertEquals(MessageDeliveryException.class, errorMessage.getPayload().getClass()); MessageDeliveryException exceptionPayload = (MessageDeliveryException) errorMessage.getPayload(); assertSame(message.getPayload(), exceptionPayload.getFailedMessage().getPayload()); assertEquals(UnsupportedOperationException.class, exceptionPayload.getCause().getClass()); assertNotSame(Thread.currentThread(), resultHandler.lastThread); }
@Test public void interceptorWithException() { ExecutorChannel channel = new ExecutorChannel(new SyncTaskExecutor()); channel.setBeanFactory(mock(BeanFactory.class)); channel.afterPropertiesSet(); Message<Object> message = new GenericMessage<Object>("foo"); MessageHandler handler = mock(MessageHandler.class); IllegalStateException expected = new IllegalStateException("Fake exception"); willThrow(expected).given(handler).handleMessage(message); BeforeHandleInterceptor interceptor = new BeforeHandleInterceptor(); channel.addInterceptor(interceptor); channel.subscribe(handler); try { channel.send(message); } catch (MessageDeliveryException actual) { assertSame(expected, actual.getCause()); } verify(handler).handleMessage(message); assertEquals(1, interceptor.getCounter().get()); assertTrue(interceptor.wasAfterHandledInvoked()); }
MessageDeliveryException exceptionPayload = (MessageDeliveryException) errorMessage.getPayload(); assertSame(message.getPayload(), exceptionPayload.getFailedMessage().getPayload()); assertEquals(UnsupportedOperationException.class, exceptionPayload.getCause().getClass()); assertNotSame(Thread.currentThread(), resultHandler.lastThread);