Search in sources :

Example 1 with MockWebSession

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");
}
Also used : MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) MockWebSession(org.springframework.web.testfixture.server.MockWebSession) HandshakeInfo(org.springframework.web.reactive.socket.HandshakeInfo) Test(org.junit.jupiter.api.Test)

Example 2 with MockWebSession

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")));
}
Also used : MockWebSession(org.springframework.web.testfixture.server.MockWebSession) WebSession(org.springframework.web.server.WebSession) TestBean(org.springframework.beans.testfixture.beans.TestBean) MockWebSession(org.springframework.web.testfixture.server.MockWebSession) Test(org.junit.jupiter.api.Test)

Example 3 with MockWebSession

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();
}
Also used : MockWebSession(org.springframework.web.testfixture.server.MockWebSession) WebSession(org.springframework.web.server.WebSession) TestBean(org.springframework.beans.testfixture.beans.TestBean) ModelMap(org.springframework.ui.ModelMap) MockWebSession(org.springframework.web.testfixture.server.MockWebSession) Test(org.junit.jupiter.api.Test)

Example 4 with MockWebSession

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();
}
Also used : MockWebSession(org.springframework.web.testfixture.server.MockWebSession) WebSession(org.springframework.web.server.WebSession) TestBean(org.springframework.beans.testfixture.beans.TestBean) MockWebSession(org.springframework.web.testfixture.server.MockWebSession) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)4 MockWebSession (org.springframework.web.testfixture.server.MockWebSession)4 TestBean (org.springframework.beans.testfixture.beans.TestBean)3 WebSession (org.springframework.web.server.WebSession)3 ModelMap (org.springframework.ui.ModelMap)1 HandshakeInfo (org.springframework.web.reactive.socket.HandshakeInfo)1 MockServerHttpRequest (org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest)1 MockServerWebExchange (org.springframework.web.testfixture.server.MockServerWebExchange)1