diff --git a/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/messaging/TraceChannelInterceptorTests.java b/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/messaging/TraceChannelInterceptorTests.java index e80bc69f2..0cf2c2e83 100644 --- a/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/messaging/TraceChannelInterceptorTests.java +++ b/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/messaging/TraceChannelInterceptorTests.java @@ -42,11 +42,16 @@ import org.springframework.integration.channel.DirectChannel; import org.springframework.integration.core.MessagingTemplate; import org.springframework.integration.support.MessageBuilder; import org.springframework.messaging.Message; +import org.springframework.messaging.MessageChannel; import org.springframework.messaging.MessageHandler; import org.springframework.messaging.MessagingException; +import org.springframework.messaging.support.ChannelInterceptor; +import org.springframework.messaging.support.ChannelInterceptorAdapter; +import org.springframework.messaging.support.GenericMessage; import org.springframework.messaging.support.MessageHeaderAccessor; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.util.SerializationUtils; import static org.junit.Assert.assertNotNull; import static org.springframework.cloud.sleuth.assertions.SleuthAssertions.then; @@ -314,6 +319,28 @@ public class TraceChannelInterceptorTests implements MessageHandler { then(TestSpanContextHolder.getCurrentSpan()).isNull(); } + @Test + public void serializeMutableHeaders() throws Exception { + Map headers = new HashMap<>(); + headers.put("foo", "bar"); + Message message = new GenericMessage<>("test", headers); + ChannelInterceptor immutableMessageInterceptor = new ChannelInterceptorAdapter() { + @Override + public Message preSend(Message message, MessageChannel channel) { + MessageHeaderAccessor headers = MessageHeaderAccessor.getMutableAccessor(message); + return new GenericMessage(message.getPayload(), headers.toMessageHeaders()); + } + }; + this.tracedChannel.addInterceptor(immutableMessageInterceptor); + + this.tracedChannel.send(message); + + Message output = (Message) SerializationUtils.deserialize(SerializationUtils.serialize(this.message)); + then(output.getPayload()).isEqualTo("test"); + then(output.getHeaders().get("foo")).isEqualTo("bar"); + this.tracedChannel.removeInterceptor(immutableMessageInterceptor); + } + @Configuration @EnableAutoConfiguration static class App {