use of org.springframework.web.testfixture.server.MockWebSession in project spring-framework by spring-projects.
the class HandshakeWebSocketServiceTests method sessionAttributePredicate.
@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;
assertThat(info).isNotNull();
Map<String, Object> attributes = info.getAttributes();
assertThat(attributes).hasSize(3).containsEntry("a1", "v1").containsEntry("a3", "v3").containsEntry("a5", "v5");
}
use of org.springframework.web.testfixture.server.MockWebSession in project spring-framework by spring-projects.
the class SessionAttributesHandlerTests method retrieveAttributes.
@Test
public void retrieveAttributes() {
WebSession session = new MockWebSession();
session.getAttributes().put("attr1", "value1");
session.getAttributes().put("attr2", "value2");
session.getAttributes().put("attr3", new TestBean());
session.getAttributes().put("attr4", new TestBean());
assertThat(sessionAttributesHandler.retrieveAttributes(session).keySet()).as("Named attributes (attr1, attr2) should be 'known' right away").isEqualTo(new HashSet<>(asList("attr1", "attr2")));
// Resolve 'attr3' by type
sessionAttributesHandler.isHandlerSessionAttribute("attr3", TestBean.class);
assertThat(sessionAttributesHandler.retrieveAttributes(session).keySet()).as("Named attributes (attr1, attr2) and resolved attribute (att3) should be 'known'").isEqualTo(new HashSet<>(asList("attr1", "attr2", "attr3")));
}
use of org.springframework.web.testfixture.server.MockWebSession in project spring-framework by spring-projects.
the class SessionAttributesHandlerTests method storeAttributes.
@Test
public void storeAttributes() {
ModelMap model = new ModelMap();
model.put("attr1", "value1");
model.put("attr2", "value2");
model.put("attr3", new TestBean());
WebSession session = new MockWebSession();
sessionAttributesHandler.storeAttributes(session, model);
assertThat(session.getAttributes().get("attr1")).isEqualTo("value1");
assertThat(session.getAttributes().get("attr2")).isEqualTo("value2");
boolean condition = session.getAttributes().get("attr3") instanceof TestBean;
assertThat(condition).isTrue();
}
use of org.springframework.web.testfixture.server.MockWebSession in project spring-framework by spring-projects.
the class SessionAttributesHandlerTests method cleanupAttributes.
@Test
public void cleanupAttributes() {
WebSession session = new MockWebSession();
session.getAttributes().put("attr1", "value1");
session.getAttributes().put("attr2", "value2");
session.getAttributes().put("attr3", new TestBean());
this.sessionAttributesHandler.cleanupAttributes(session);
assertThat(session.getAttributes().get("attr1")).isNull();
assertThat(session.getAttributes().get("attr2")).isNull();
assertThat(session.getAttributes().get("attr3")).isNotNull();
// Resolve 'attr3' by type
this.sessionAttributesHandler.isHandlerSessionAttribute("attr3", TestBean.class);
this.sessionAttributesHandler.cleanupAttributes(session);
assertThat(session.getAttributes().get("attr3")).isNull();
}
Aggregations