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:nl.ctrlaltdev.harbinger.validator.TripwiredValidatorTest.java

@Test
public void shouldFullReportWithSpringWithLogInjection() {
    SecurityContextHolder.setContext(new SecurityContextImpl());
    SecurityContextHolder.getContext().setAuthentication(new AnonymousAuthenticationToken("key", "user",
            Collections.singletonList(new SimpleGrantedAuthority("x"))));
    MockHttpServletRequest request = new MockHttpServletRequest();
    RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
    request.setRemoteAddr("192.168.1.1\n\r");
    request.addHeader("X-Forwarded-For", "\n\r\t8.8.8.8");
    request.setSession(new MockHttpSession());

    assertFalse(validator.isValid("../../etc/passwd\n\r\t", null));
}

From source file:org.openmrs.module.referenceapplication.page.controller.LoginPageControllerTest.java

private PageRequest createPageRequest(HttpServletRequest httpRequest, HttpServletResponse httpResponse) {
    HttpServletRequest request = (httpRequest != null) ? httpRequest : new MockHttpServletRequest();
    HttpServletResponse response = (httpResponse != null) ? httpResponse : new MockHttpServletResponse();
    return new PageRequest(null, null, request, response, new Session(new MockHttpSession()));
}

From source file:org.openmrs.web.controller.person.PersonAttributeTypeListControllerTest.java

License:asdf

/**
 * @see PersonAttributeTypeListController#moveUp(null,HttpSession)
 *///from  w  w  w  .  j  av a 2 s . c  o m
@Test
@Verifies(value = "should move selected ids up one in the list", method = "moveUp(null,HttpSession)")
public void moveUp_shouldMoveSelectedIdsUpOneInTheList() throws Exception {

    // sanity check
    List<PersonAttributeType> allTypes = Context.getPersonService().getAllPersonAttributeTypes();
    Assert.assertEquals(8, allTypes.get(1).getId().intValue());

    // the test
    Integer[] ids = new Integer[] { 8 };
    new PersonAttributeTypeListController().moveUp(ids, new MockHttpSession());
    allTypes = Context.getPersonService().getAllPersonAttributeTypes();
    Assert.assertEquals("The types didn't move correctly", 8, allTypes.get(0).getId().intValue());
}

From source file:cn.org.once.cstack.servers.AbstractApplicationControllerTestIT.java

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

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

    User user = null;//from   w  w w .  j  av a  2 s.  co  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:Controllers.AdministrateControllerTest.java

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

From source file:org.openmrs.module.radiology.web.controller.RadiologyObsFormControllerTest.java

@Before
public void runBeforeAllTests() {

    mockPatient = RadiologyTestData.getMockPatient1();
    mockStudy = RadiologyTestData.getMockStudy1PostSave();
    mockRadiologyOrder = RadiologyTestData.getMockRadiologyOrder1();
    mockRadiologyOrder.setStudy(mockStudy);
    mockRadiologyOrder.setPatient(mockPatient);

    mockObs = RadiologyTestData.getMockObs();
    mockObs.setPatient(mockPatient);/*from   w ww.  ja  v a  2s .c  om*/
    mockObs.setOrder(mockRadiologyOrder);

    obsErrors = mock(BindingResult.class);

    mockSession = new MockHttpSession();
    mockRequest = new MockHttpServletRequest();
    mockRequest.setSession(mockSession);

    mockMultipartFile = RadiologyTestData.getMockMultipartFileForMockObsWithComplexConcept();
    mockMultipartHttpServletRequestRequest = new MockMultipartHttpServletRequest();
    mockMultipartHttpServletRequestRequest.addParameter("saveComplexObs", "saveComplexObs");
    mockMultipartHttpServletRequestRequest.setSession(mockSession);
    mockMultipartHttpServletRequestRequest.addFile(mockMultipartFile);

    when(obsErrors.hasErrors()).thenReturn(false);
}

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

@Test
public void backChannelLogoutFailsIfNoSessionIndex() {
    final String logoutMessage = LogoutMessageGenerator.generateBackChannelLogoutMessage("");
    request.setParameter(LOGOUT_PARAMETER_NAME, logoutMessage);
    request.setMethod("POST");
    final MockHttpSession session = new MockHttpSession();
    handler.getSessionMappingStorage().addSessionById(TICKET, session);
    assertFalse(handler.process(request, response));
    assertFalse(session.isInvalid());/* www.  j a v a2s.c  o m*/
}

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

private PageRequest pageRequest(String provider, String page) {
    MockHttpSession httpSession = new MockHttpSession();
    Session uiSession = new Session(httpSession);
    MockHttpServletRequest req = new MockHttpServletRequest();
    req.setSession(httpSession);//from   w w w .ja v a 2s  . c  o m
    return new PageRequest(provider, page, req, new MockHttpServletResponse(), uiSession);
}

From source file:org.terasoluna.gfw.web.token.transaction.HttpSessionTransactionTokenStoreTest.java

@Test
public void testRemove() {
    // setup parameters
    HttpSession session = new MockHttpSession();
    request.setSession(session);/*  ww  w.  j  a va  2  s.co m*/
    TransactionToken token = new TransactionToken("tokenName", "tokenKey", "tokenValue");

    // prepare store instance
    store = new HttpSessionTransactionTokenStore();
    store.store(token);

    // run
    store.remove(token);

    // assert
    assertNull(session.getAttribute(HttpSessionTransactionTokenStore.TOKEN_HOLDER_SESSION_ATTRIBUTE_PREFIX
            + token.getTokenName() + token.getTokenKey()));
}

From source file:com.xemantic.tadedon.guice.servlet.mock.FakeServletContainer.java

/**
 * Starts the container with using given {@code servletContext}.
 *
 * @param context the servlet context./*from w  w w . ja v a2s . c  o m*/
 * @throws ServletException if {@link GuiceFilter} cannot be initialized.
 * @see #start()
 */
public void start(ServletContext context) throws ServletException {
    m_session = new MockHttpSession();
    m_context = context;
    MockFilterConfig config = new MockFilterConfig(m_context);
    m_filter.init(config);
    m_listener.contextInitialized(new ServletContextEvent(m_context));
}