@Override public void onSuccess(@Nullable Void result) { if (accessor.getCommand() == StompCommand.DISCONNECT) { afterDisconnectSent(accessor); } } @Override
@Test public void decodeMultipleFramesFromSameBuffer() { String frame1 = "SEND\ndestination:test\n\nThe body of the message\0"; String frame2 = "DISCONNECT\n\n\0"; ByteBuffer buffer = ByteBuffer.wrap((frame1 + frame2).getBytes()); final List<Message<byte[]>> messages = decoder.decode(buffer); assertEquals(2, messages.size()); assertEquals(StompCommand.SEND, StompHeaderAccessor.wrap(messages.get(0)).getCommand()); assertEquals(StompCommand.DISCONNECT, StompHeaderAccessor.wrap(messages.get(1)).getCommand()); }
@Test public void createWithCommand() { StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.CONNECTED); assertEquals(StompCommand.CONNECTED, accessor.getCommand()); accessor = StompHeaderAccessor.create(StompCommand.CONNECTED, new LinkedMultiValueMap<>()); assertEquals(StompCommand.CONNECTED, accessor.getCommand()); }
@Test public void createWithMessageFrameNativeHeaders() { MultiValueMap<String, String> extHeaders = new LinkedMultiValueMap<>(); extHeaders.add(StompHeaderAccessor.DESTINATION_HEADER, "/d"); extHeaders.add(StompHeaderAccessor.STOMP_SUBSCRIPTION_HEADER, "s1"); extHeaders.add(StompHeaderAccessor.STOMP_CONTENT_TYPE_HEADER, "application/json"); StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.MESSAGE, extHeaders); assertEquals(StompCommand.MESSAGE, headers.getCommand()); assertEquals(SimpMessageType.MESSAGE, headers.getMessageType()); assertEquals("s1", headers.getSubscriptionId()); }
@Override public final boolean match(Message<?> message) { StompHeaderAccessor headers = StompHeaderAccessor.wrap(message); if (!this.command.equals(headers.getCommand()) || (this.sessionId != headers.getSessionId())) { return false; } return matchInternal(headers, message.getPayload()); }
@Test public void decodeFrameWithContentLength() { Message<byte[]> message = decode("SEND\ncontent-length:23\n\nThe body of the message\0"); StompHeaderAccessor headers = StompHeaderAccessor.wrap(message); assertEquals(StompCommand.SEND, headers.getCommand()); assertEquals(1, headers.toNativeHeaderMap().size()); assertEquals(Integer.valueOf(23), headers.getContentLength()); String bodyText = new String(message.getPayload()); assertEquals("The body of the message", bodyText); }
@Test public void decodeFrameWithContentLengthZero() { Message<byte[]> frame = decode("SEND\ncontent-length:0\n\n\0"); StompHeaderAccessor headers = StompHeaderAccessor.wrap(frame); assertEquals(StompCommand.SEND, headers.getCommand()); assertEquals(1, headers.toNativeHeaderMap().size()); assertEquals(Integer.valueOf(0), headers.getContentLength()); String bodyText = new String(frame.getPayload()); assertEquals("", bodyText); }
@Test public void decodeFrameWithNullOctectsInTheBody() { Message<byte[]> frame = decode("SEND\ncontent-length:23\n\nThe b\0dy \0f the message\0"); StompHeaderAccessor headers = StompHeaderAccessor.wrap(frame); assertEquals(StompCommand.SEND, headers.getCommand()); assertEquals(1, headers.toNativeHeaderMap().size()); assertEquals(Integer.valueOf(23), headers.getContentLength()); String bodyText = new String(frame.getPayload()); assertEquals("The b\0dy \0f the message", bodyText); }
@Test public void decodeFrameWithInvalidContentLength() { Message<byte[]> message = decode("SEND\ncontent-length:-1\n\nThe body of the message\0"); StompHeaderAccessor headers = StompHeaderAccessor.wrap(message); assertEquals(StompCommand.SEND, headers.getCommand()); assertEquals(1, headers.toNativeHeaderMap().size()); assertEquals(Integer.valueOf(-1), headers.getContentLength()); String bodyText = new String(message.getPayload()); assertEquals("The body of the message", bodyText); }
@Test public void decodeFrame() throws UnsupportedEncodingException { Message<byte[]> frame = decode("SEND\ndestination:test\n\nThe body of the message\0"); StompHeaderAccessor headers = StompHeaderAccessor.wrap(frame); assertEquals(StompCommand.SEND, headers.getCommand()); assertEquals(headers.toNativeHeaderMap().toString(), 1, headers.toNativeHeaderMap().size()); assertEquals("test", headers.getDestination()); String bodyText = new String(frame.getPayload()); assertEquals("The body of the message", bodyText); }
@Test public void decodeFrameWithCrLfEols() { Message<byte[]> frame = decode("DISCONNECT\r\n\r\n\0"); StompHeaderAccessor headers = StompHeaderAccessor.wrap(frame); assertEquals(StompCommand.DISCONNECT, headers.getCommand()); assertEquals(0, headers.toNativeHeaderMap().size()); assertEquals(0, frame.getPayload().length); }
@Test public void decodeFrameWithNoHeadersAndNoBody() { Message<byte[]> frame = decode("DISCONNECT\n\n\0"); StompHeaderAccessor headers = StompHeaderAccessor.wrap(frame); assertEquals(StompCommand.DISCONNECT, headers.getCommand()); assertEquals(0, headers.toNativeHeaderMap().size()); assertEquals(0, frame.getPayload().length); }
@Test public void createWithUnubscribeNativeHeaders() { MultiValueMap<String, String> extHeaders = new LinkedMultiValueMap<>(); extHeaders.add(StompHeaderAccessor.STOMP_ID_HEADER, "s1"); StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.UNSUBSCRIBE, extHeaders); assertEquals(StompCommand.UNSUBSCRIBE, headers.getCommand()); assertEquals(SimpMessageType.UNSUBSCRIBE, headers.getMessageType()); assertEquals("s1", headers.getSubscriptionId()); }
@Test public void createWithSubscribeNativeHeaders() { MultiValueMap<String, String> extHeaders = new LinkedMultiValueMap<>(); extHeaders.add(StompHeaderAccessor.STOMP_ID_HEADER, "s1"); extHeaders.add(StompHeaderAccessor.STOMP_DESTINATION_HEADER, "/d"); StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.SUBSCRIBE, extHeaders); assertEquals(StompCommand.SUBSCRIBE, headers.getCommand()); assertEquals(SimpMessageType.SUBSCRIBE, headers.getMessageType()); assertEquals("/d", headers.getDestination()); assertEquals("s1", headers.getSubscriptionId()); }
@Test // SPR-16844 public void afterConnectedWithSpecificVersion() { assertFalse(this.session.isConnected()); this.connectHeaders.setAcceptVersion(new String[] {"1.1"}); this.session.afterConnected(this.connection); Message<byte[]> message = this.messageCaptor.getValue(); StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class); assertEquals(StompCommand.CONNECT, accessor.getCommand()); assertThat(accessor.getAcceptVersion(), containsInAnyOrder("1.1")); }
@Test public void decodeFrameWithEscapedHeaders() { Message<byte[]> frame = decode("DISCONNECT\na\\c\\r\\n\\\\b:alpha\\cbravo\\r\\n\\\\\n\n\0"); StompHeaderAccessor headers = StompHeaderAccessor.wrap(frame); assertEquals(StompCommand.DISCONNECT, headers.getCommand()); assertEquals(1, headers.toNativeHeaderMap().size()); assertEquals("alpha:bravo\r\n\\", headers.getFirstNativeHeader("a:\r\n\\b")); }
@Test public void decodeFrameWithNoBody() { String accept = "accept-version:1.1\n"; String host = "host:github.org\n"; Message<byte[]> frame = decode("CONNECT\n" + accept + host + "\n\0"); StompHeaderAccessor headers = StompHeaderAccessor.wrap(frame); assertEquals(StompCommand.CONNECT, headers.getCommand()); assertEquals(2, headers.toNativeHeaderMap().size()); assertEquals("1.1", headers.getFirstNativeHeader("accept-version")); assertEquals("github.org", headers.getHost()); assertEquals(0, frame.getPayload().length); }
@Test public void decodeFrameWithHeaderWithEmptyValue() { String accept = "accept-version:1.1\n"; String valuelessKey = "key:\n"; Message<byte[]> frame = decode("CONNECT\n" + accept + valuelessKey + "\n\0"); StompHeaderAccessor headers = StompHeaderAccessor.wrap(frame); assertEquals(StompCommand.CONNECT, headers.getCommand()); assertEquals(2, headers.toNativeHeaderMap().size()); assertEquals("1.1", headers.getFirstNativeHeader("accept-version")); assertEquals("", headers.getFirstNativeHeader("key")); assertEquals(0, frame.getPayload().length); }
@Test public void connectWhenBrokerNotAvailable() throws Exception { this.brokerRelay.start(); this.brokerRelay.stopInternal(); this.brokerRelay.handleMessage(connectMessage("sess1", "joe")); Message<byte[]> message = this.outboundChannel.getMessages().get(0); StompHeaderAccessor accessor = StompHeaderAccessor.getAccessor(message, StompHeaderAccessor.class); assertEquals(StompCommand.ERROR, accessor.getCommand()); assertEquals("sess1", accessor.getSessionId()); assertEquals("joe", accessor.getUser().getName()); assertEquals("Broker not available.", accessor.getMessage()); }
@Test public void destinationExcluded() throws Exception { this.brokerRelay.start(); SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE); headers.setSessionId("sess1"); headers.setDestination("/user/daisy/foo"); this.brokerRelay.handleMessage(MessageBuilder.createMessage(new byte[0], headers.getMessageHeaders())); assertEquals(1, this.tcpClient.getSentMessages().size()); StompHeaderAccessor headers1 = this.tcpClient.getSentHeaders(0); assertEquals(StompCommand.CONNECT, headers1.getCommand()); assertEquals(StompBrokerRelayMessageHandler.SYSTEM_SESSION_ID, headers1.getSessionId()); }