@Test public void setAttributesFromMessage() { String sessionId = "session1"; ConcurrentHashMap<String, Object> map = new ConcurrentHashMap<>(); SimpMessageHeaderAccessor headerAccessor = SimpMessageHeaderAccessor.create(); headerAccessor.setSessionId(sessionId); headerAccessor.setSessionAttributes(map); Message<?> message = MessageBuilder.createMessage("", headerAccessor.getMessageHeaders()); SimpAttributesContextHolder.setAttributesFromMessage(message); SimpAttributes attrs = SimpAttributesContextHolder.getAttributes(); assertThat(attrs, notNullValue()); assertThat(attrs.getSessionId(), is(sessionId)); attrs.setAttribute("name1", "value1"); assertThat(map.get("name1"), is("value1")); }
private Message<?> createMessage(SimpMessageType messageType, String destination, Map<String, Object> headers) { SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(messageType); accessor.setSessionId("session1"); accessor.setSessionAttributes(new HashMap<>()); accessor.setDestination(destination); if (headers != null) { for (Map.Entry<String, Object> entry : headers.entrySet()) { accessor.setHeader(entry.getKey(), entry.getValue()); } } return MessageBuilder.withPayload(new byte[0]).setHeaders(accessor).build(); }
private Message<?> message(String destination, SimpMessageHeaderAccessor headers) { headers.setSessionId("123"); headers.setSessionAttributes(new HashMap<>()); headers.setDestination(destination); if (SecurityContextHolder.getContext().getAuthentication() != null) { headers.setUser(SecurityContextHolder.getContext().getAuthentication()); } headers.getSessionAttributes().put(CsrfToken.class.getName(), this.token); return new GenericMessage<>("hi", headers.getMessageHeaders()); }
@Test public void preSendNullSessionAttributes() { this.headers.setSessionAttributes(null); assertThat(this.interceptor.preSend(createMessage(), this.channel)) .isSameAs(this.createMessage); verifyZeroInteractions(this.sessionRepository); }
@Test(expected = MissingCsrfTokenException.class) public void preSendMissingTokenNullSessionAttributes() { messageHeaders.setSessionAttributes(null); interceptor.preSend(message(), channel); }
@Before public void setup() { token = new DefaultCsrfToken("header", "param", "token"); interceptor = new CsrfChannelInterceptor(); messageHeaders = SimpMessageHeaderAccessor.create(SimpMessageType.CONNECT); messageHeaders.setNativeHeader(token.getHeaderName(), token.getToken()); messageHeaders.setSessionAttributes(new HashMap<>()); messageHeaders.getSessionAttributes().put(CsrfToken.class.getName(), token); }
@Test public void simpScope() { Map<String, Object> sessionAttributes = new ConcurrentHashMap<>(); sessionAttributes.put("name", "value"); SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create(); headers.setSessionId("session1"); headers.setSessionAttributes(sessionAttributes); headers.setDestination("/pre/scope"); Message<?> message = MessageBuilder.withPayload(new byte[0]).setHeaders(headers).build(); this.messageHandler.registerHandler(this.testController); this.messageHandler.handleMessage(message); assertEquals("scope", this.testController.method); }
private Message<String> message(SimpMessageHeaderAccessor headers, String destination) { headers.setSessionId("123"); headers.setSessionAttributes(new HashMap<>()); if (destination != null) { headers.setDestination(destination); } if (messageUser != null) { headers.setUser(messageUser); } return new GenericMessage<>("hi", headers.getMessageHeaders()); }
private Message<String> message(SimpMessageHeaderAccessor headers, String destination) { headers.setSessionId("123"); headers.setSessionAttributes(new HashMap<>()); if (destination != null) { headers.setDestination(destination); } if (messageUser != null) { headers.setUser(messageUser); } return new GenericMessage<>("hi", headers.getMessageHeaders()); }
@Test public void getLogMessageWithValuesSet() { SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(); accessor.setDestination("/destination"); accessor.setSubscriptionId("subscription"); accessor.setSessionId("session"); accessor.setUser(new TestPrincipal("user")); accessor.setSessionAttributes(Collections.<String, Object>singletonMap("key", "value")); assertEquals("MESSAGE destination=/destination subscriptionId=subscription " + "session=session user=user attributes[1] payload=p", accessor.getShortLogMessage("p")); }
@Test public void getDetailedLogMessageWithValuesSet() { SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(); accessor.setDestination("/destination"); accessor.setSubscriptionId("subscription"); accessor.setSessionId("session"); accessor.setUser(new TestPrincipal("user")); accessor.setSessionAttributes(Collections.<String, Object>singletonMap("key", "value")); accessor.setNativeHeader("nativeKey", "nativeValue"); assertEquals("MESSAGE destination=/destination subscriptionId=subscription " + "session=session user=user attributes={key=value} nativeHeaders=" + "{nativeKey=[nativeValue]} payload=p", accessor.getDetailedLogMessage("p")); }
@Test public void globalException() throws Exception { SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create(); headers.setSessionId("session1"); headers.setSessionAttributes(new ConcurrentHashMap<>()); headers.setDestination("/exception"); Message<?> message = MessageBuilder.withPayload(new byte[0]).setHeaders(headers).build(); this.messageHandler.handleMessage(message); TestControllerAdvice controllerAdvice = this.applicationContext.getBean(TestControllerAdvice.class); assertTrue(controllerAdvice.isExceptionHandled()); }
@Before public void setup() { this.interceptor = new SessionRepositoryMessageInterceptor<>( this.sessionRepository); this.headers = SimpMessageHeaderAccessor.create(); this.headers.setSessionId("session"); this.headers.setSessionAttributes(new HashMap<>()); setMessageType(SimpMessageType.MESSAGE); String sessionId = "http-session"; setSessionId(sessionId); given(this.sessionRepository.findById(sessionId)).willReturn(this.session); }
@Override public void handleMessageFromClient(WebSocketSession session, WebSocketMessage<?> webSocketMessage, MessageChannel outputChannel) throws Exception { SimpMessageHeaderAccessor headerAccessor = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE); headerAccessor.setSessionId(session.getId()); headerAccessor.setSessionAttributes(session.getAttributes()); headerAccessor.setUser(session.getPrincipal()); headerAccessor.setHeader("content-length", webSocketMessage.getPayloadLength()); headerAccessor.setLeaveMutable(true); Message<?> message = MessageBuilder.createMessage(webSocketMessage.getPayload(), headerAccessor.getMessageHeaders()); try { SimpAttributesContextHolder.setAttributesFromMessage(message); outputChannel.send(message); } finally { SimpAttributesContextHolder.resetAttributes(); } }
@Override public void handleMessageFromClient(WebSocketSession session, WebSocketMessage<?> webSocketMessage, MessageChannel outputChannel) throws Exception { SimpMessageHeaderAccessor headerAccessor = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE); headerAccessor.setSessionId(session.getId()); headerAccessor.setSessionAttributes(session.getAttributes()); headerAccessor.setUser(session.getPrincipal()); headerAccessor.setHeader("content-length", webSocketMessage.getPayloadLength()); headerAccessor.setLeaveMutable(true); Message<?> message = MessageBuilder.createMessage(webSocketMessage.getPayload(), headerAccessor.getMessageHeaders()); try { SimpAttributesContextHolder.setAttributesFromMessage(message); outputChannel.send(message); } finally { SimpAttributesContextHolder.resetAttributes(); } }