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:od.controller.tests.ControllerTests.java

protected MockHttpSession createSession() {
    MockHttpSession session = new MockHttpSession();
    return session;
}

From source file:Controllers.AdministrateControllerTest.java

@Test
public void testAdminAccountAsGuest() throws Exception {
    MockHttpSession mockHttpSession = new MockHttpSession();
    User user = new User();
    user.setEmail("GUEST");
    mockHttpSession.setAttribute("user", user);
    this.mockMvc.perform(get("/administrateAccount").session(mockHttpSession)).andExpect(view().name("result"));
}

From source file:org.openmrs.web.controller.concept.ConceptStopWordListControllerTest.java

/**
 * @see {@link ConceptStopWordListController#handleSubmission(javax.servlet.http.HttpSession, String[])
 *///from   w ww  . ja v a2  s.  co m
@Test
@Verifies(value = "should add the success delete message in session attribute", method = "handleSubmission(HttpSession, String[])")
public void handleSubmission_shouldAddTheDeleteSuccessMessageInSession() throws Exception {
    ConceptStopWordListController controller = (ConceptStopWordListController) applicationContext
            .getBean("conceptStopWordListController");

    HttpSession mockSession = new MockHttpSession();

    controller.handleSubmission(mockSession, new String[] { "2" });

    String successMessage = (String) mockSession.getAttribute(WebConstants.OPENMRS_MSG_ATTR);
    String errorMessage = (String) mockSession.getAttribute(WebConstants.OPENMRS_ERROR_ATTR);

    Assert.assertNotNull(successMessage);
    Assert.assertNull(errorMessage);
    Assert.assertEquals("general.deleted", successMessage);
}

From source file:org.jasig.cas.client.session.SingleSignOutHandlerTests.java

@Test
public void backChannelLogoutFailsIfMultipart() {
    final String logoutMessage = LogoutMessageGenerator.generateBackChannelLogoutMessage(TICKET);
    request.setParameter(LOGOUT_PARAMETER_NAME, logoutMessage);
    request.setMethod("POST");
    request.setContentType("multipart/form-data");
    final MockHttpSession session = new MockHttpSession();
    handler.getSessionMappingStorage().addSessionById(TICKET, session);
    assertTrue(handler.process(request, response));
    assertFalse(session.isInvalid());// ww  w  . j a va 2 s .com
}

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

/**
 * @see ChangePasswordFormController#handleSubmission(HttpSession, String, String, String, String, String, User, BindingResult)
 *           test =/*from  w  w w  . j  a v a 2s. c  o m*/
 */
@Test
@Verifies(value = "display error message when the password is empty", method = "handleSubmission()")
public void handleSubmission_shouldDisplayErrorMessageWhenPasswordIsEmpty() throws Exception {
    ChangePasswordFormController controller = new ChangePasswordFormController();
    BindException errors = new BindException(controller.formBackingObject(), "user");

    String result = controller.handleSubmission(new MockHttpSession(), oldPassword, "", "", "", "", "",
            Context.getAuthenticatedUser(), errors);

    assertTrue(errors.hasErrors());
    assertEquals("error.password.weak", errors.getGlobalError().getCode());
}

From source file:cn.org.once.cstack.users.UserControllerTestIT.java

@Before
public void setup() {
    logger.info("setup");

    this.mockMvc = MockMvcBuilders.webAppContextSetup(context).addFilters(springSecurityFilterChain).build();

    User user = null;//ww  w . ja  v  a 2 s  .  c o m
    try {
        user = userService.findByLogin("johndoe");
    } catch (ServiceException e) {
        logger.error(e.getLocalizedMessage());
    }

    Authentication authentication = null;
    if (user != null) {
        authentication = new UsernamePasswordAuthenticationToken(user.getLogin(), user.getPassword());
    }
    Authentication result = authenticationManager.authenticate(authentication);
    SecurityContext securityContext = SecurityContextHolder.getContext();
    securityContext.setAuthentication(result);
    session = new MockHttpSession();
    session.setAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY, securityContext);
}

From source file:com.github.cherimojava.orchidae.controller._PictureController.java

@Before
public void setup() {
    controller = new PictureController();
    controller.userUtil = userUtil;/*from w w  w  . j a v a  2 s.  c  o  m*/
    controller.factory = factory;
    controller.fileUtil = fileUtil;
    controller.latestPictureLimit = 10;

    mvc = MockMvcBuilders.standaloneSetup(controller).setMessageConverters(new EntityConverter(factory),
            new StringHttpMessageConverter(), new ResourceHttpMessageConverter()).build();

    setAuthentication(owner);

    factory.create(User.class).setUsername(ownr).setPassword("1").setMemberSince(DateTime.now()).save();

    session = new MockHttpSession();
    session.setAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY,
            SecurityContextHolder.getContext());
}

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

@Test
public void testSaveSnappedImage() throws IOException {
    String deviceId = "000000000001";
    MockHttpSession session = new MockHttpSession();
    MockHttpServletResponse response = new MockHttpServletResponse();
    UniqueIndexedCache<BufferedImage> imgCache = ClientCache.getClientCache(session).getImgCache();
    String imageId = imgCache.storeItem(PC_IMG);

    VideoSnap videoSnap = new VideoSnap();
    videoSnap.saveSnappedImage(imageId, deviceId, response, session);

    String header = response.getHeader("Content-Disposition");
    Assert.assertTrue(header.contains("attachment"), "Response header does not indicates attachment");
    Assert.assertTrue(header.contains(".jpg"), "Attached file is not in jpg format");

    ByteArrayOutputStream bao = new ByteArrayOutputStream();
    ImageIO.write(PC_IMG, "jpg", bao);
    Assert.assertEquals(response.getContentAsByteArray(), bao.toByteArray(),
            "Saved image size is not matching with expected size");
}

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

@Test
public void testAssertion() 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
        }//from  ww  w  .j a  va  2s .  c o m
    };

    request.setSession(session);
    session.setAttribute(AbstractCasFilter.CONST_CAS_ASSERTION, new AssertionImpl("test"));
    this.filter.doFilter(request, response, filterChain);

    assertNull(response.getRedirectedUrl());
}

From source file:cn.org.once.cstack.logs.LogsControllerTestIT.java

@Before
public void setup() throws Exception {
    logger.info("setup");

    applicationName = "app" + new Random().nextInt(100000);
    this.mockMvc = MockMvcBuilders.webAppContextSetup(context).addFilters(springSecurityFilterChain).build();

    User user = null;//from w w w .ja va2s .  c om
    try {
        user = userService.findByLogin("johndoe");
    } catch (ServiceException e) {
        logger.error(e.getLocalizedMessage());
    }

    Authentication authentication = null;
    if (user != null) {
        authentication = new UsernamePasswordAuthenticationToken(user.getLogin(), user.getPassword());
    }
    Authentication result = authenticationManager.authenticate(authentication);
    SecurityContext securityContext = SecurityContextHolder.getContext();
    securityContext.setAuthentication(result);
    session = new MockHttpSession();
    session.setAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY, securityContext);
}