Example usage for org.springframework.mock.web MockHttpSession MockHttpSession

List of usage examples for org.springframework.mock.web MockHttpSession MockHttpSession

Introduction

In this page you can find the example usage for org.springframework.mock.web MockHttpSession MockHttpSession.

Prototype

public MockHttpSession() 

Source Link

Document

Create a new MockHttpSession with a default MockServletContext .

Usage

From source file:com.vdenotaris.spring.boot.security.saml.web.CommonTestSupport.java

public MockHttpSession mockHttpSession(boolean secured) {
    MockHttpSession mockSession = new MockHttpSession();
    SecurityContext mockSecurityContext = mock(SecurityContext.class);

    if (secured) {
        ExpiringUsernameAuthenticationToken principal = new ExpiringUsernameAuthenticationToken(null,
                USER_DETAILS, USER_NAME, AUTHORITIES);
        principal.setDetails(USER_DETAILS);
        when(mockSecurityContext.getAuthentication()).thenReturn(principal);
    }/*ww  w. j av a  2s.c  o  m*/

    SecurityContextHolder.setContext(mockSecurityContext);
    mockSession.setAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY,
            mockSecurityContext);

    return mockSession;
}

From source file:org.openmrs.web.controller.user.UserFormControllerTest.java

/**
 * @see UserFormController#handleSubmission(WebRequest,HttpSession,String,String,String,null, String, User,BindingResult)
 *
 *///from   w  w  w .  j ava  2  s . com
@Test
@Verifies(value = "should work for an example", method = "handleSubmission(WebRequest,HttpSession,String,String,String,null, String, User,BindingResult)")
public void handleSubmission_shouldWorkForAnExample() throws Exception {
    WebRequest request = new ServletWebRequest(new MockHttpServletRequest());
    User user = controller.formBackingObject(request, null);
    user.addName(new PersonName("This", "is", "Test"));
    user.getPerson().setGender("F");
    controller.handleSubmission(request, new MockHttpSession(), new ModelMap(), "", "Save User", "pass123",
            "pass123", null, null, null, new String[0], "true", null, user, new BindException(user, "user"));
}

From source file:org.slc.sli.dashboard.web.unit.interceptor.SessionCheckInterceptorTest.java

@Test
public void testPreHandle() throws Exception {
    SessionCheckInterceptor scInterceptor = new SessionCheckInterceptor();
    PowerMockito.mockStatic(SecurityUtil.class);
    PowerMockito.when(SecurityUtil.getToken()).thenReturn("sravan");
    MockHttpServletRequest request = new MockHttpServletRequest() {
        public HttpSession getSession() {
            return new MockHttpSession();
        }//  w  w w. j ava 2 s  .  c  o m

        public Cookie[] getCookies() {
            Cookie c = new Cookie(SLIAuthenticationEntryPoint.DASHBOARD_COOKIE, "fake");
            return new Cookie[] { c };
        }

        public String getRequestURI() {
            return "fake_uri";
        }

    };

    MockHttpServletResponse response = new MockHttpServletResponse() {

        public void sendRedirect(String url) {
            assertEquals(url, "fake_uri");
        }
    };

    RESTClient restClient = new RESTClient() {
        public JsonObject sessionCheck(String token) {
            JsonObject json = new JsonObject();
            json.addProperty("authenticated", false);
            return json;
        }
    };

    scInterceptor.setRestClient(restClient);

    assertEquals(scInterceptor.preHandle(request, response, null), false);
}

From source file:org.openmrs.ui.framework.IntegrationTest.java

@Test
public void integrationTest() throws Exception {
    MockHttpSession httpSession = new MockHttpSession();
    Session session = sessionFactory.getSession(httpSession);
    MockHttpServletResponse response = new MockHttpServletResponse();
    PageRequest req = new PageRequest("uiframework", "home", new MockHttpServletRequest(), response, session);

    String html = pageFactory.handle(req);
    System.out.println("Result = " + html);

    // should not be cached
    assertThat((String) response.getHeader("Cache-Control"), is("no-cache,no-store,must-revalidate"));
}

From source file:org.jasig.cas.client.util.HttpServletRequestWrapperFilterTests.java

public void testWrappedRequest() throws Exception {
    final HttpServletRequestWrapperFilter filter = new HttpServletRequestWrapperFilter();
    filter.init(new MockFilterConfig());
    final MockHttpServletRequest request = new MockHttpServletRequest();
    final MockHttpSession session = new MockHttpSession();

    session.setAttribute(AbstractCasFilter.CONST_CAS_ASSERTION, new AssertionImpl("test"));
    request.setSession(session);/*from   w ww . j  av a  2 s . com*/

    filter.doFilter(request, new MockHttpServletResponse(), createFilterChain());
    assertEquals("test", this.mockRequest.getRemoteUser());

    filter.destroy();
}

From source file:com.spstudio.controller.test.MemberControllerTest.java

@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
    mockMvc = MockMvcBuilders.standaloneSetup(memberController)
            .setControllerAdvice(new GlobalExceptionController()).build();
    this.session = new MockHttpSession();
    this.session.setAttribute("username", "Test1");
    this.session.setAttribute("password", "666");
}

From source file:svc.managers.SMSManagerTest.java

@Before
public void setup() {
    session = new MockHttpSession();
}

From source file:com.expressui.domain.service.SetupSession.java

@Override
public void afterPropertiesSet() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpSession session = new MockHttpSession();
    request.setSession(session);//from  w w w.ja v a 2s.  c o m
    RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
}

From source file:org.jasig.cas.client.authentication.AuthenticationFilterTests.java

@Test
public void testRedirect() throws Exception {
    final MockHttpSession session = new MockHttpSession();
    final MockHttpServletRequest request = new MockHttpServletRequest();
    final MockHttpServletResponse response = new MockHttpServletResponse();
    final FilterChain filterChain = new FilterChain() {

        public void doFilter(ServletRequest request, ServletResponse response)
                throws IOException, ServletException {
            // nothing to do
        }/* w ww  . j av  a2s .c om*/
    };

    request.setSession(session);
    this.filter.doFilter(request, response, filterChain);

    assertEquals(CAS_LOGIN_URL + "?service=" + URLEncoder.encode(CAS_SERVICE_URL, "UTF-8"),
            response.getRedirectedUrl());
}

From source file:com.comcast.video.dawg.show.video.VideoSnapTest.java

@Test
public void testCaptureImage() throws IOException {
    String deviceId = "000000000001";

    final DawgVideoOutput mockDVO = EasyMock.createMock(DawgVideoOutput.class);
    EasyMock.expect(mockDVO.captureScreen()).andReturn(PC_IMG);
    VideoSnap videoSnap = new VideoSnap() {
        @Override/* ww  w  .ja v  a2  s .  c  om*/
        protected DawgVideoOutput createDawgVideoOutput(MetaStb stb) {
            return mockDVO;
        }
    };
    MetaStbCache stbCache = new MetaStbCache();
    MetaStb stb = new MetaStb();
    stbCache.putMetaStb(deviceId, stb);
    MockHttpSession session = new MockHttpSession();

    EasyMock.replay(mockDVO);
    String iid = videoSnap.captureImage(deviceId, session, stbCache);
    Assert.assertNotNull(iid);

    ByteArrayOutputStream bao = new ByteArrayOutputStream();
    ImageIO.write(PC_IMG, "jpg", bao);

    EasyMock.verify(mockDVO);

    UniqueIndexedCache<BufferedImage> imgCache = ClientCache.getClientCache(session).getImgCache();
    Assert.assertSame(imgCache.getItem(iid), PC_IMG, "Captured image is not matching with expected image");

}