forked from spring-projects/spring-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
364bc35
commit 39ff1e2
Showing
7 changed files
with
268 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
...ing/src/test/java/org/springframework/messaging/simp/stomp/StompProtocolHandlerTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
* Copyright 2002-2013 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.stomp; | ||
|
||
import java.util.Arrays; | ||
import java.util.HashSet; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.ArgumentCaptor; | ||
import org.mockito.Mockito; | ||
import org.springframework.messaging.Message; | ||
import org.springframework.messaging.MessageChannel; | ||
import org.springframework.web.socket.TextMessage; | ||
import org.springframework.web.socket.support.TestPrincipal; | ||
import org.springframework.web.socket.support.TestWebSocketSession; | ||
|
||
import static org.junit.Assert.*; | ||
import static org.mockito.Mockito.*; | ||
|
||
/** | ||
* Test fixture for {@link StompProtocolHandler} tests. | ||
* | ||
* @author Rossen Stoyanchev | ||
*/ | ||
public class StompProtocolHandlerTests { | ||
|
||
private StompProtocolHandler stompHandler; | ||
|
||
private TestWebSocketSession session; | ||
|
||
private MessageChannel channel; | ||
|
||
private ArgumentCaptor<Message> messageCaptor; | ||
|
||
|
||
@Before | ||
public void setup() { | ||
this.stompHandler = new StompProtocolHandler(); | ||
this.channel = Mockito.mock(MessageChannel.class); | ||
this.messageCaptor = ArgumentCaptor.forClass(Message.class); | ||
|
||
this.session = new TestWebSocketSession(); | ||
this.session.setId("s1"); | ||
this.session.setPrincipal(new TestPrincipal("joe")); | ||
} | ||
|
||
@Test | ||
public void handleConnect() { | ||
|
||
TextMessage textMessage = StompTextMessageBuilder.create(StompCommand.CONNECT).headers( | ||
"login:guest", "passcode:guest", "accept-version:1.1,1.0", "heart-beat:10000,10000").build(); | ||
|
||
this.stompHandler.handleMessageFromClient(this.session, textMessage, this.channel); | ||
|
||
verify(this.channel).send(this.messageCaptor.capture()); | ||
Message<?> actual = this.messageCaptor.getValue(); | ||
assertNotNull(actual); | ||
|
||
StompHeaderAccessor headers = StompHeaderAccessor.wrap(actual); | ||
assertEquals(StompCommand.CONNECT, headers.getCommand()); | ||
assertEquals("s1", headers.getSessionId()); | ||
assertEquals("joe", headers.getUser().getName()); | ||
assertEquals("guest", headers.getLogin()); | ||
assertEquals("PROTECTED", headers.getPasscode()); | ||
assertArrayEquals(new long[] {10000, 10000}, headers.getHeartbeat()); | ||
assertEquals(new HashSet<>(Arrays.asList("1.1","1.0")), headers.getAcceptVersion()); | ||
|
||
// Check CONNECTED reply | ||
|
||
assertEquals(1, this.session.getSentMessages().size()); | ||
textMessage = (TextMessage) this.session.getSentMessages().get(0); | ||
Message<?> message = new StompMessageConverter().toMessage(textMessage.getPayload()); | ||
StompHeaderAccessor replyHeaders = StompHeaderAccessor.wrap(message); | ||
|
||
assertEquals(StompCommand.CONNECTED, replyHeaders.getCommand()); | ||
assertEquals("1.1", replyHeaders.getVersion()); | ||
assertArrayEquals(new long[] {0, 0}, replyHeaders.getHeartbeat()); | ||
assertEquals("joe", replyHeaders.getNativeHeader("user-name").get(0)); | ||
assertEquals("s1", replyHeaders.getNativeHeader("queue-suffix").get(0)); | ||
} | ||
|
||
} |
71 changes: 71 additions & 0 deletions
71
...aging/src/test/java/org/springframework/messaging/simp/stomp/StompTextMessageBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright 2002-2013 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.stomp; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import org.springframework.web.socket.TextMessage; | ||
|
||
|
||
/** | ||
* A builder for creating WebSocket messages with STOMP frame content. | ||
* | ||
* @author Rossen Stoyanchev | ||
*/ | ||
public class StompTextMessageBuilder { | ||
|
||
private StompCommand command; | ||
|
||
private final List<String> headerLines = new ArrayList<String>(); | ||
|
||
private String body; | ||
|
||
|
||
private StompTextMessageBuilder(StompCommand command) { | ||
this.command = command; | ||
} | ||
|
||
public static StompTextMessageBuilder create(StompCommand command) { | ||
return new StompTextMessageBuilder(command); | ||
} | ||
|
||
public StompTextMessageBuilder headers(String... headerLines) { | ||
this.headerLines.addAll(Arrays.asList(headerLines)); | ||
return this; | ||
} | ||
|
||
public StompTextMessageBuilder body(String body) { | ||
this.body = body; | ||
return this; | ||
} | ||
|
||
public TextMessage build() { | ||
StringBuilder sb = new StringBuilder(this.command.name()).append("\n"); | ||
for (String line : this.headerLines) { | ||
sb.append(line).append("\n"); | ||
} | ||
sb.append("\n"); | ||
if (this.body != null) { | ||
sb.append(this.body); | ||
} | ||
sb.append("\u0000"); | ||
return new TextMessage(sb.toString()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.