Encode user names in user destanations

Issue: SPR-11302
This commit is contained in:
Rossen Stoyanchev
2014-01-21 14:57:13 -05:00
parent d96b91a57b
commit d03fb8954b
4 changed files with 104 additions and 9 deletions

View File

@@ -0,0 +1,72 @@
/*
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.messaging.simp;
import org.junit.Before;
import org.junit.Test;
import org.springframework.messaging.Message;
import org.springframework.messaging.StubMessageChannel;
import java.util.List;
import static org.junit.Assert.assertEquals;
/**
* Unit tests for {@link org.springframework.messaging.simp.SimpMessagingTemplate}.
*
* @author Rossen Stoyanchev
*/
public class SimpMessagingTemplateTests {
private SimpMessagingTemplate messagingTemplate;
private StubMessageChannel messageChannel;
@Before
public void setup() {
this.messageChannel = new StubMessageChannel();
this.messagingTemplate = new SimpMessagingTemplate(messageChannel);
}
@Test
public void convertAndSendToUser() {
this.messagingTemplate.convertAndSendToUser("joe", "/queue/foo", "data");
List<Message<byte[]>> messages = this.messageChannel.getMessages();
assertEquals(1, messages.size());
Message<byte[]> message = messages.get(0);
SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.wrap(message);
assertEquals(SimpMessageType.MESSAGE, headers.getMessageType());
assertEquals("/user/joe/queue/foo", headers.getDestination());
}
@Test
public void convertAndSendToUserWithEncoding() {
this.messagingTemplate.convertAndSendToUser("http://joe.openid.example.org/", "/queue/foo", "data");
List<Message<byte[]>> messages = this.messageChannel.getMessages();
assertEquals(1, messages.size());
Message<byte[]> message = messages.get(0);
SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.wrap(message);
assertEquals("/user/http:%2F%2Fjoe.openid.example.org%2F/queue/foo", headers.getDestination());
}
}

View File

@@ -23,6 +23,7 @@ import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
import org.springframework.messaging.simp.SimpMessageType;
import org.springframework.messaging.simp.TestPrincipal;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.util.StringUtils;
import java.util.Set;
@@ -30,6 +31,8 @@ import static org.junit.Assert.assertEquals;
/**
* Unit tests for {@link org.springframework.messaging.simp.user.DefaultUserDestinationResolver}.
*
* @author Rossen Stoyanchev
*/
public class DefaultUserDestinationResolverTests {
@@ -93,6 +96,18 @@ public class DefaultUserDestinationResolverTests {
assertEquals("/queue/foo-user123", actual.iterator().next());
}
@Test
public void handleMessageEncodedUserName() {
String userName = "http://joe.openid.example.org/";
this.registry.registerSessionId(userName, "openid123");
String destination = "/user/" + StringUtils.replace(userName, "/", "%2F") + "/queue/foo";
Message<?> message = createMessage(SimpMessageType.MESSAGE, this.user, SESSION_ID, destination);
Set<String> actual = this.resolver.resolveDestination(message);
assertEquals(1, actual.size());
assertEquals("/queue/foo-useropenid123", actual.iterator().next());
}
@Test
public void ignoreMessage() {