Package org.springframework.mock.web.test

Examples of org.springframework.mock.web.test.MockHttpSession


  @Test
  public void defaultConstructor() throws Exception {
    Map<String, Object> attributes = new HashMap<String, Object>();
    WebSocketHandler wsHandler = Mockito.mock(WebSocketHandler.class);

    this.servletRequest.setSession(new MockHttpSession(null, "123"));
    this.servletRequest.getSession().setAttribute("foo", "bar");
    this.servletRequest.getSession().setAttribute("bar", "baz");

    HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();
    interceptor.beforeHandshake(this.request, this.response, wsHandler, attributes);
View Full Code Here


  @Test
  public void constructorWithAttributeNames() throws Exception {
    Map<String, Object> attributes = new HashMap<String, Object>();
    WebSocketHandler wsHandler = Mockito.mock(WebSocketHandler.class);

    this.servletRequest.setSession(new MockHttpSession(null, "123"));
    this.servletRequest.getSession().setAttribute("foo", "bar");
    this.servletRequest.getSession().setAttribute("bar", "baz");

    Set<String> names = Collections.singleton("foo");
    HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor(names);
View Full Code Here

  @Test
  public void doNotCopyHttpSessionId() throws Exception {
    Map<String, Object> attributes = new HashMap<String, Object>();
    WebSocketHandler wsHandler = Mockito.mock(WebSocketHandler.class);

    this.servletRequest.setSession(new MockHttpSession(null, "123"));
    this.servletRequest.getSession().setAttribute("foo", "bar");

    HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();
    interceptor.setCopyHttpSessionId(false);
    interceptor.beforeHandshake(this.request, this.response, wsHandler, attributes);
View Full Code Here

  @Test
  public void doNotCopyAttributes() throws Exception {
    Map<String, Object> attributes = new HashMap<String, Object>();
    WebSocketHandler wsHandler = Mockito.mock(WebSocketHandler.class);

    this.servletRequest.setSession(new MockHttpSession(null, "123"));
    this.servletRequest.getSession().setAttribute("foo", "bar");

    HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();
    interceptor.setCopyAllAttributes(false);
    interceptor.beforeHandshake(this.request, this.response, wsHandler, attributes);
View Full Code Here

      String name = entry.getKey();
      Object value = entry.getValue();
      it.remove();
      if (value instanceof HttpSessionBindingListener) {
        ((HttpSessionBindingListener) value).valueUnbound(
            new HttpSessionBindingEvent(new MockHttpSession(), name, value));
      }
    }
  }
View Full Code Here

    assertFalse("The requestHandled flag shouldn't change", mavContainer.isRequestHandled());
  }

  @Test
  public void session() throws Exception {
    MockHttpSession session = new MockHttpSession();
    servletRequest.setSession(session);
    MethodParameter sessionParameter = new MethodParameter(method, 2);

    boolean isSupported = resolver.supportsParameter(sessionParameter);
    Object result = resolver.resolveArgument(sessionParameter, mavContainer, webRequest, null);
View Full Code Here

    XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this.beanFactory);
    reader.loadBeanDefinitions(new ClassPathResource("sessionScopeTests.xml", getClass()));
  }

  public void testGetFromScope() throws Exception {
    MockHttpSession session = new MockHttpSession();
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setSession(session);
    ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request);

    RequestContextHolder.setRequestAttributes(requestAttributes);
    try {
      String name = "sessionScopedObject";
      assertNull(session.getAttribute(name));
      TestBean bean = (TestBean) this.beanFactory.getBean(name);
      assertEquals(session.getAttribute(name), bean);
      assertSame(bean, this.beanFactory.getBean(name));
    }
    finally {
      RequestContextHolder.setRequestAttributes(null);
    }
View Full Code Here

      RequestContextHolder.setRequestAttributes(null);
    }
  }

  public void testDestructionAtSessionTermination() throws Exception {
    MockHttpSession session = new MockHttpSession();
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setSession(session);
    ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request);

    RequestContextHolder.setRequestAttributes(requestAttributes);
    try {
      String name = "sessionScopedDisposableObject";
      assertNull(session.getAttribute(name));
      DerivedTestBean bean = (DerivedTestBean) this.beanFactory.getBean(name);
      assertEquals(session.getAttribute(name), bean);
      assertSame(bean, this.beanFactory.getBean(name));

      requestAttributes.requestCompleted();
      session.invalidate();
      assertTrue(bean.wasDestroyed());
    }
    finally {
      RequestContextHolder.setRequestAttributes(null);
    }
View Full Code Here

  }

  private void doTestDestructionWithSessionSerialization(boolean beanNameReset) throws Exception {
    Serializable serializedState = null;

    MockHttpSession session = new MockHttpSession();
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setSession(session);
    ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request);

    RequestContextHolder.setRequestAttributes(requestAttributes);
    try {
      String name = "sessionScopedDisposableObject";
      assertNull(session.getAttribute(name));
      DerivedTestBean bean = (DerivedTestBean) this.beanFactory.getBean(name);
      assertEquals(session.getAttribute(name), bean);
      assertSame(bean, this.beanFactory.getBean(name));

      requestAttributes.requestCompleted();
      serializedState = session.serializeState();
      assertFalse(bean.wasDestroyed());
    }
    finally {
      RequestContextHolder.setRequestAttributes(null);
    }

    serializedState = (Serializable) SerializationTestUtils.serializeAndDeserialize(serializedState);

    session = new MockHttpSession();
    session.deserializeState(serializedState);
    request = new MockHttpServletRequest();
    request.setSession(session);
    requestAttributes = new ServletRequestAttributes(request);

    RequestContextHolder.setRequestAttributes(requestAttributes);
    try {
      String name = "sessionScopedDisposableObject";
      assertNotNull(session.getAttribute(name));
      DerivedTestBean bean = (DerivedTestBean) this.beanFactory.getBean(name);
      assertEquals(session.getAttribute(name), bean);
      assertSame(bean, this.beanFactory.getBean(name));

      requestAttributes.requestCompleted();
      session.invalidate();
      assertTrue(bean.wasDestroyed());

      if (beanNameReset) {
        assertNull(bean.getBeanName());
      }
View Full Code Here

    }
  }

  @Test
  public void setSessionScopedAttribute() throws Exception {
    MockHttpSession session = new MockHttpSession();
    session.setAttribute(KEY, VALUE);
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setSession(session);
    ServletRequestAttributes attrs = new ServletRequestAttributes(request);
    attrs.setAttribute(KEY, VALUE, RequestAttributes.SCOPE_SESSION);
    Object value = session.getAttribute(KEY);
    assertSame(VALUE, value);
  }
View Full Code Here

TOP

Related Classes of org.springframework.mock.web.test.MockHttpSession

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.