Fix default target destination when using "." as path separator

Issue: SPR-11660
This commit is contained in:
Sebastien Deleuze 2014-07-17 07:51:36 +02:00 committed by Rossen Stoyanchev
parent e8d8c3390a
commit 59f39706dc
2 changed files with 37 additions and 1 deletions

View File

@ -32,6 +32,7 @@ import org.springframework.messaging.simp.user.DestinationUserNameProvider;
import org.springframework.messaging.support.MessageHeaderInitializer;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import java.lang.annotation.Annotation;
import java.security.Principal;
@ -187,7 +188,11 @@ public class SendToMethodReturnValueHandler implements HandlerMethodReturnValueH
}
}
String name = DestinationPatternsMessageCondition.LOOKUP_DESTINATION_HEADER;
return new String[] { defaultPrefix + message.getHeaders().get(name) };
String destination = (String)message.getHeaders().get(name);
if (StringUtils.hasLength(destination) && !destination.startsWith("/")) {
destination = "/" + destination;
}
return new String[] { defaultPrefix + destination };
}
private MessageHeaders createHeaders(String sessionId) {

View File

@ -185,6 +185,21 @@ public class SendToMethodReturnValueHandlerTests {
assertNull("Subscription id should not be copied", headers.getSubscriptionId());
}
@Test
public void sendToDefaultDestinationWithoutLeadingSlash() throws Exception {
when(this.messageChannel.send(any(Message.class))).thenReturn(true);
Message<?> inputMessage = createInputMessage("sess1", "sub1", "/app", "dest", null);
this.handler.handleReturnValue(PAYLOAD, this.sendToDefaultDestReturnType, inputMessage);
verify(this.messageChannel, times(1)).send(this.messageCaptor.capture());
Message<?> message = this.messageCaptor.getAllValues().get(0);
SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.wrap(message);
assertEquals("/topic/dest", headers.getDestination());
}
@Test
public void testHeadersToSend() throws Exception {
@ -297,6 +312,22 @@ public class SendToMethodReturnValueHandlerTests {
assertEquals("/user/" + user.getName() + "/queue/dest", headers.getDestination());
}
@Test
public void sendToUserDefaultDestinationWithoutLeadingSlash() throws Exception {
when(this.messageChannel.send(any(Message.class))).thenReturn(true);
TestUser user = new TestUser();
Message<?> inputMessage = createInputMessage("sess1", "sub1", "/app", "dest", user);
this.handler.handleReturnValue(PAYLOAD, this.sendToUserDefaultDestReturnType, inputMessage);
verify(this.messageChannel, times(1)).send(this.messageCaptor.capture());
Message<?> message = this.messageCaptor.getAllValues().get(0);
SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.wrap(message);
assertEquals("/user/" + user.getName() + "/queue/dest", headers.getDestination());
}
@Test
public void sendToUserDefaultDestinationSingleSession() throws Exception {