/** * Create a new WebSocket session. */ protected AbstractWebSocketSession(T delegate, String id, HandshakeInfo info, DataBufferFactory bufferFactory) { Assert.notNull(delegate, "Native session is required."); Assert.notNull(id, "Session id is required."); Assert.notNull(info, "HandshakeInfo is required."); Assert.notNull(bufferFactory, "DataBuffer factory is required."); this.delegate = delegate; this.id = id; this.handshakeInfo = info; this.bufferFactory = bufferFactory; this.attributes.putAll(info.getAttributes()); this.logPrefix = initLogPrefix(info, id); if (logger.isDebugEnabled()) { logger.debug(getLogPrefix() + "Session id \"" + getId() + "\" for " + getHandshakeInfo().getUri()); } }
@Test public void sessionAttributePredicate() { MockWebSession session = new MockWebSession(); session.getAttributes().put("a1", "v1"); session.getAttributes().put("a2", "v2"); session.getAttributes().put("a3", "v3"); session.getAttributes().put("a4", "v4"); session.getAttributes().put("a5", "v5"); MockServerHttpRequest request = initHandshakeRequest(); MockServerWebExchange exchange = MockServerWebExchange.builder(request).session(session).build(); TestRequestUpgradeStrategy upgradeStrategy = new TestRequestUpgradeStrategy(); HandshakeWebSocketService service = new HandshakeWebSocketService(upgradeStrategy); service.setSessionAttributePredicate(name -> Arrays.asList("a1", "a3", "a5").contains(name)); service.handleRequest(exchange, mock(WebSocketHandler.class)).block(); HandshakeInfo info = upgradeStrategy.handshakeInfo; assertNotNull(info); Map<String, Object> attributes = info.getAttributes(); assertEquals(3, attributes.size()); assertThat(attributes, Matchers.hasEntry("a1", "v1")); assertThat(attributes, Matchers.hasEntry("a3", "v3")); assertThat(attributes, Matchers.hasEntry("a5", "v5")); }