7 changed files with 268 additions and 38 deletions
@ -0,0 +1,97 @@
@@ -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)); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,71 @@
@@ -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()); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,56 @@
@@ -0,0 +1,56 @@
|
||||
/* |
||||
* 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.web.socket.support; |
||||
|
||||
import java.security.Principal; |
||||
|
||||
|
||||
/** |
||||
* An implementation of Prinicipal for testing. |
||||
* @author Rossen Stoyanchev |
||||
*/ |
||||
public class TestPrincipal implements Principal { |
||||
|
||||
private String name; |
||||
|
||||
public TestPrincipal(String name) { |
||||
this.name = name; |
||||
} |
||||
|
||||
@Override |
||||
public String getName() { |
||||
return this.name; |
||||
} |
||||
|
||||
@Override |
||||
public boolean equals(Object obj) { |
||||
if (obj == this) { |
||||
return true; |
||||
} |
||||
if (!(obj instanceof TestPrincipal)) { |
||||
return false; |
||||
} |
||||
TestPrincipal p = (TestPrincipal) obj; |
||||
return this.name.equals(p.name); |
||||
} |
||||
|
||||
@Override |
||||
public int hashCode() { |
||||
return this.name.hashCode(); |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue