Example usage for org.springframework.mock.web MockHttpServletRequest getSession

List of usage examples for org.springframework.mock.web MockHttpServletRequest getSession

Introduction

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

Prototype

@Override
    @Nullable
    public HttpSession getSession() 

Source Link

Usage

From source file:fi.okm.mpass.idp.authn.impl.OpenIdConnectIdentityTest.java

@Test
public void failsAuthenticationNoStateGetSubject() throws Exception {
    MockHttpServletRequest mockHttpServletRequest = getRequest();
    mockHttpServletRequest.setQueryString("code=1234abcd");
    openIdConnectIdentity.setTokenEndpoint(token_endpoint);
    mockHttpServletRequest.getSession().setAttribute("fi.okm.mpass.state", new State("234abcd"));
    openIdConnectIdentity.setClientId("oauth2ClientId");
    openIdConnectIdentity.setClientSecret("oauth2ClientSecret");
    try {/*from  ww w .  j  a v  a 2 s .  c  o m*/
        openIdConnectIdentity.getSubject(mockHttpServletRequest);
    } catch (SocialUserAuthenticationException e) {
        Assert.assertEquals(e.getMessage(), "State parameter not satisfied");
    }

}

From source file:fi.okm.mpass.idp.authn.impl.SocialUserOpenIdConnectStartServletTest.java

/**
 * Run servlet without {@link ProfileRequestContext}.
 * @throws Exception//from   www.j  a  v a 2s  .c om
 */
@Test
public void testNoProfileRequestContext() throws Exception {
    final MockHttpServletRequest httpRequest = new MockHttpServletRequest();
    httpRequest.setParameter(ExternalAuthentication.CONVERSATION_KEY, conversationKey);
    httpRequest.getSession().setAttribute(ExternalAuthentication.CONVERSATION_KEY + conversationKey,
            new MockExternalAuthentication());
    Assert.assertTrue(runService(servlet, httpRequest, new MockHttpServletResponse()));
}

From source file:org.openmrs.module.idgen.web.controller.IdentifierSourceControllerTest.java

@Test
public void importIdentifiers_shouldAcceptJson() throws Exception {
    Mockito.doNothing().when(iss).addIdentifiersToPool(Mockito.any(IdentifierPool.class),
            (List<String>) Mockito.anyCollectionOf(String.class));

    IdentifierPool identifierPool = new IdentifierPool();
    String identifiers = "{\"identifiers\":[\"1\",\"2\",\"3\"]}";
    InputStream inputStream = new ByteArrayInputStream(identifiers.getBytes());

    MockMultipartFile mockMultipartFile = new MockMultipartFile("inputFile", inputStream);
    MockHttpServletRequest mockRequest = new MockHttpServletRequest();
    MockHttpServletResponse mockResponse = new MockHttpServletResponse();

    controller.addIdentifiersFromFile(null, mockRequest, mockResponse, identifierPool, mockMultipartFile);
    String response = (String) mockRequest.getSession().getAttribute(WebConstants.OPENMRS_MSG_ATTR);
    Assert.assertEquals(response, "Success: Identifiers successfully uploaded.");
}

From source file:fi.okm.mpass.idp.authn.impl.AbstractSpringSocialOAuth2IdentityTest.java

protected MockHttpServletRequest initHttpRequestWithState() {
    MockHttpServletRequest httpRequest = Mockito.mock(MockHttpServletRequest.class);
    final String sessionId = "mockSessionId";
    HttpSession httpSession = Mockito.mock(HttpSession.class);
    Mockito.when(httpSession.getId()).thenReturn(sessionId);
    Mockito.when(httpRequest.getSession()).thenReturn(httpSession);
    Mockito.when(httpRequest.getParameter("code")).thenReturn("mockCode");
    Mockito.when(httpRequest.getParameter("state"))
            .thenReturn(AbstractSpringSocialOAuth2Identity.calculateHash(sessionId));
    Mockito.when(httpRequest.getRequestURL()).thenReturn(new StringBuffer());
    return httpRequest;
}

From source file:org.openmrs.contrib.metadatarepository.webapp.filter.LocaleFilterTest.java

public void testSetLocaleInSessionWhenSessionIsNull() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addParameter("locale", "es");

    MockHttpServletResponse response = new MockHttpServletResponse();
    filter.doFilter(request, response, new MockFilterChain());

    // no session, should result in null
    assertNull(request.getSession().getAttribute(Constants.PREFERRED_LOCALE_KEY));
    // thread locale should always have it, regardless of session
    assertNotNull(LocaleContextHolder.getLocale());
}

From source file:org.openmrs.contrib.metadatarepository.webapp.filter.LocaleFilterTest.java

public void testJstlLocaleIsSet() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addParameter("locale", "es");

    MockHttpServletResponse response = new MockHttpServletResponse();
    request.setSession(new MockHttpSession(null));

    filter.doFilter(request, response, new MockFilterChain());

    assertNotNull(Config.get(request.getSession(), Config.FMT_LOCALE));
}

From source file:fi.okm.mpass.idp.authn.impl.SocialUserOpenIdConnectStartServletTest.java

/**
 * Run servlet without {@link SocialUserOpenIdConnectContext}.
 * @throws Exception/*  w  w  w.j a v  a2s . com*/
 */
@Test
public void testNoSocialUserContext() throws Exception {
    final MockHttpServletRequest httpRequest = new MockHttpServletRequest();
    httpRequest.setParameter(ExternalAuthentication.CONVERSATION_KEY, conversationKey);
    final ProfileRequestContext<?, ?> ctx = new ProfileRequestContext<>();
    httpRequest.getSession().setAttribute(ExternalAuthentication.CONVERSATION_KEY + conversationKey,
            new ExternalAuthenticationImpl(ctx));
    final AuthenticationContext authnCtx = ctx.getSubcontext(AuthenticationContext.class, true);
    final AuthenticationFlowDescriptor flow = new AuthenticationFlowDescriptor();
    flow.setId("mock");
    authnCtx.setAttemptedFlow(flow);
    Assert.assertTrue(runService(servlet, httpRequest, new MockHttpServletResponse()));
}

From source file:ejportal.webapp.action.NutzungActionTest.java

/**
 * Test remove./*w w w.  java  2s.  c  o m*/
 * 
 * @throws Exception
 *             the exception
 */
public void testRemove() throws Exception {
    final MockHttpServletRequest request = new MockHttpServletRequest();
    ServletActionContext.setRequest(request);
    this.action.setDelete("");
    final Nutzung nutzung = new Nutzung();
    nutzung.setNutzungId(2L);
    this.action.setNutzungId(2L);
    Assert.assertEquals("success", this.action.delete());
    Assert.assertNotNull(request.getSession().getAttribute("messages"));
}

From source file:com.mtgi.analytics.servlet.SpringSessionContextTest.java

@Test
public void testAuthenticated() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setRemoteUser("testUser");
    ServletRequestAttributes atts = new ServletRequestAttributes(request);
    RequestContextHolder.setRequestAttributes(atts);

    assertEquals("context inherits user from request attributes", "testUser", inst.getContextUserId());
    assertEquals("context inherits session from request attributes", request.getSession().getId(),
            inst.getContextSessionId());
}

From source file:org.openmrs.web.controller.patient.PatientDashboardControllerTest.java

/**
 * @see PatientDashboardController#renderDashboard(String,ModelMap,HttpServletRequest)
 * @verifies redirect to find patient page if given patient id is not an existing id
 *//*from   w w  w.j  a va 2s. c  o  m*/
@Test
public void renderDashboard_shouldRedirectToFindPatientPageIfGivenPatientIdIsNotAnExistingId()
        throws Exception {

    MockHttpServletRequest request = new MockHttpServletRequest();
    ModelMap map = new ModelMap();

    String view = patientDashboardController.renderDashboard(NON_EXISTING_PATIENT_ID, map, request);

    assertThat(view, is(PATIENT_FIND_VIEW));

    assertThat(request.getSession().getAttribute(WebConstants.OPENMRS_ERROR_ATTR),
            is("patientDashboard.noPatientWithId"));
    assertThat(request.getSession().getAttribute(WebConstants.OPENMRS_ERROR_ARGS), is(NON_EXISTING_PATIENT_ID));
}