11/*
2- * Copyright 2002-2013 the original author or authors.
2+ * Copyright 2002-2014 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
1818
1919import java .nio .ByteBuffer ;
2020import java .util .Arrays ;
21+ import java .util .Collections ;
2122import java .util .HashSet ;
2223
2324import org .junit .Before ;
3233import org .springframework .messaging .simp .stomp .StompCommand ;
3334import org .springframework .messaging .simp .stomp .StompDecoder ;
3435import org .springframework .messaging .simp .stomp .StompHeaderAccessor ;
36+ import org .springframework .messaging .simp .user .DefaultUserSessionRegistry ;
37+ import org .springframework .messaging .simp .user .DestinationUserNameProvider ;
38+ import org .springframework .messaging .simp .user .UserSessionRegistry ;
3539import org .springframework .messaging .support .MessageBuilder ;
3640import org .springframework .web .socket .TextMessage ;
3741import org .springframework .web .socket .WebSocketMessage ;
4751 */
4852public class StompSubProtocolHandlerTests {
4953
50- private StompSubProtocolHandler stompHandler ;
54+ private StompSubProtocolHandler protocolHandler ;
5155
5256 private TestWebSocketSession session ;
5357
@@ -58,7 +62,7 @@ public class StompSubProtocolHandlerTests {
5862
5963 @ Before
6064 public void setup () {
61- this .stompHandler = new StompSubProtocolHandler ();
65+ this .protocolHandler = new StompSubProtocolHandler ();
6266 this .channel = Mockito .mock (MessageChannel .class );
6367 this .messageCaptor = ArgumentCaptor .forClass (Message .class );
6468
@@ -68,18 +72,55 @@ public void setup() {
6872 }
6973
7074 @ Test
71- public void connectedResponseIsSentWhenConnectAckIsToBeSentToClient () {
75+ public void handleMessageToClientConnected () {
76+
77+ UserSessionRegistry registry = new DefaultUserSessionRegistry ();
78+ this .protocolHandler .setUserSessionRegistry (registry );
79+
80+ StompHeaderAccessor headers = StompHeaderAccessor .create (StompCommand .CONNECTED );
81+ Message <byte []> message = MessageBuilder .withPayload (new byte [0 ]).setHeaders (headers ).build ();
82+ this .protocolHandler .handleMessageToClient (this .session , message );
83+
84+ assertEquals (1 , this .session .getSentMessages ().size ());
85+ WebSocketMessage <?> textMessage = this .session .getSentMessages ().get (0 );
86+ assertEquals ("CONNECTED\n " + "user-name:joe\n " + "\n " + "\u0000 " , textMessage .getPayload ());
87+
88+ assertEquals (Collections .singleton ("s1" ), registry .getSessionIds ("joe" ));
89+ }
90+
91+ @ Test
92+ public void handleMessageToClientConnectedUniqueUserName () {
93+
94+ this .session .setPrincipal (new UniqueUser ("joe" ));
95+
96+ UserSessionRegistry registry = new DefaultUserSessionRegistry ();
97+ this .protocolHandler .setUserSessionRegistry (registry );
98+
99+ StompHeaderAccessor headers = StompHeaderAccessor .create (StompCommand .CONNECTED );
100+ Message <byte []> message = MessageBuilder .withPayload (new byte [0 ]).setHeaders (headers ).build ();
101+ this .protocolHandler .handleMessageToClient (this .session , message );
102+
103+ assertEquals (1 , this .session .getSentMessages ().size ());
104+ WebSocketMessage <?> textMessage = this .session .getSentMessages ().get (0 );
105+ assertEquals ("CONNECTED\n " + "user-name:joe\n " + "\n " + "\u0000 " , textMessage .getPayload ());
106+
107+ assertEquals (Collections .<String >emptySet (), registry .getSessionIds ("joe" ));
108+ assertEquals (Collections .singleton ("s1" ), registry .getSessionIds ("Me myself and I" ));
109+ }
110+
111+ @ Test
112+ public void handleMessageToClientConnectAck () {
113+
72114 StompHeaderAccessor connectHeaders = StompHeaderAccessor .create (StompCommand .CONNECT );
73115 connectHeaders .setHeartbeat (10000 , 10000 );
74116 connectHeaders .setNativeHeader (StompHeaderAccessor .STOMP_ACCEPT_VERSION_HEADER , "1.0,1.1" );
75-
76117 Message <?> connectMessage = MessageBuilder .withPayload (new byte [0 ]).setHeaders (connectHeaders ).build ();
77118
78119 SimpMessageHeaderAccessor connectAckHeaders = SimpMessageHeaderAccessor .create (SimpMessageType .CONNECT_ACK );
79120 connectAckHeaders .setHeader (SimpMessageHeaderAccessor .CONNECT_MESSAGE_HEADER , connectMessage );
121+ Message <byte []> connectAckMessage = MessageBuilder .withPayload (new byte [0 ]).setHeaders (connectAckHeaders ).build ();
80122
81- Message <byte []> connectAck = MessageBuilder .withPayload (new byte [0 ]).setHeaders (connectAckHeaders ).build ();
82- this .stompHandler .handleMessageToClient (this .session , connectAck );
123+ this .protocolHandler .handleMessageToClient (this .session , connectAckMessage );
83124
84125 verifyNoMoreInteractions (this .channel );
85126
@@ -97,12 +138,12 @@ public void connectedResponseIsSentWhenConnectAckIsToBeSentToClient() {
97138 }
98139
99140 @ Test
100- public void messagesAreAugmentedAndForwarded () {
141+ public void handleMessageFromClient () {
101142
102143 TextMessage textMessage = StompTextMessageBuilder .create (StompCommand .CONNECT ).headers (
103144 "login:guest" , "passcode:guest" , "accept-version:1.1,1.0" , "heart-beat:10000,10000" ).build ();
104145
105- this .stompHandler .handleMessageFromClient (this .session , textMessage , this .channel );
146+ this .protocolHandler .handleMessageFromClient (this .session , textMessage , this .channel );
106147
107148 verify (this .channel ).send (this .messageCaptor .capture ());
108149 Message <?> actual = this .messageCaptor .getValue ();
@@ -121,16 +162,29 @@ public void messagesAreAugmentedAndForwarded() {
121162 }
122163
123164 @ Test
124- public void invalidStompCommand () {
165+ public void handleMessageFromClientInvalidStompCommand () {
125166
126167 TextMessage textMessage = new TextMessage ("FOO" );
127168
128- this .stompHandler .handleMessageFromClient (this .session , textMessage , this .channel );
169+ this .protocolHandler .handleMessageFromClient (this .session , textMessage , this .channel );
129170
130171 verifyZeroInteractions (this .channel );
131172 assertEquals (1 , this .session .getSentMessages ().size ());
132173 TextMessage actual = (TextMessage ) this .session .getSentMessages ().get (0 );
133174 assertTrue (actual .getPayload ().startsWith ("ERROR" ));
134175 }
135176
177+
178+ private static class UniqueUser extends TestPrincipal implements DestinationUserNameProvider {
179+
180+ private UniqueUser (String name ) {
181+ super (name );
182+ }
183+
184+ @ Override
185+ public String getDestinationUserName () {
186+ return "Me myself and I" ;
187+ }
188+ }
189+
136190}
0 commit comments