Search in sources :

Example 16 with MockHttpSession

use of org.springframework.mock.web.MockHttpSession in project spring-security by spring-projects.

the class WebAuthenticationDetailsMixinTests method buildWebAuthenticationDetailsUsingDifferentConstructors.

// @formatter:on
@Test
public void buildWebAuthenticationDetailsUsingDifferentConstructors() throws IOException {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setRemoteAddr("localhost");
    request.setSession(new MockHttpSession(null, "1"));
    WebAuthenticationDetails details = new WebAuthenticationDetails(request);
    WebAuthenticationDetails authenticationDetails = mapper.readValue(AUTHENTICATION_DETAILS_JSON, WebAuthenticationDetails.class);
    assertThat(details.equals(authenticationDetails));
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) WebAuthenticationDetails(org.springframework.security.web.authentication.WebAuthenticationDetails) MockHttpSession(org.springframework.mock.web.MockHttpSession) Test(org.junit.Test)

Example 17 with MockHttpSession

use of org.springframework.mock.web.MockHttpSession in project spring-framework by spring-projects.

the class AopNamespaceHandlerScopeIntegrationTests method testSessionScoping.

@Test
public void testSessionScoping() throws Exception {
    MockHttpSession oldSession = new MockHttpSession();
    MockHttpSession newSession = new MockHttpSession();
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setSession(oldSession);
    RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
    ITestBean scoped = (ITestBean) this.context.getBean("sessionScoped");
    assertTrue("Should be AOP proxy", AopUtils.isAopProxy(scoped));
    assertFalse("Should not be target class proxy", scoped instanceof TestBean);
    ITestBean scopedAlias = (ITestBean) this.context.getBean("sessionScopedAlias");
    assertSame(scoped, scopedAlias);
    ITestBean testBean = (ITestBean) this.context.getBean("testBean");
    assertTrue("Should be AOP proxy", AopUtils.isAopProxy(testBean));
    assertFalse("Regular bean should be JDK proxy", testBean instanceof TestBean);
    String rob = "Rob Harrop";
    String bram = "Bram Smeets";
    assertEquals(rob, scoped.getName());
    scoped.setName(bram);
    request.setSession(newSession);
    assertEquals(rob, scoped.getName());
    request.setSession(oldSession);
    assertEquals(bram, scoped.getName());
    assertTrue("Should have advisors", ((Advised) scoped).getAdvisors().length > 0);
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) MockHttpSession(org.springframework.mock.web.MockHttpSession) Test(org.junit.Test)

Example 18 with MockHttpSession

use of org.springframework.mock.web.MockHttpSession in project spring-framework by spring-projects.

the class ClassPathBeanDefinitionScannerScopeIntegrationTests method setUp.

@Before
public void setUp() {
    MockHttpServletRequest oldRequestWithSession = new MockHttpServletRequest();
    oldRequestWithSession.setSession(new MockHttpSession());
    this.oldRequestAttributesWithSession = new ServletRequestAttributes(oldRequestWithSession);
    MockHttpServletRequest newRequestWithSession = new MockHttpServletRequest();
    newRequestWithSession.setSession(new MockHttpSession());
    this.newRequestAttributesWithSession = new ServletRequestAttributes(newRequestWithSession);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) MockHttpSession(org.springframework.mock.web.MockHttpSession) Before(org.junit.Before)

Example 19 with MockHttpSession

use of org.springframework.mock.web.MockHttpSession in project ORCID-Source by ORCID.

the class FundingsControllerTest method testGetFundingsJson.

@Test
public void testGetFundingsJson() {
    when(localeManager.getLocale()).thenReturn(new Locale("us", "EN"));
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpSession session = new MockHttpSession();
    request.setSession(session);
    request.addPreferredLocale(new Locale("us", "EN"));
    List<FundingForm> fundings = fundingController.getFundingsJson(request, "1");
    assertNotNull(fundings);
    assertEquals(1, fundings.size());
    FundingForm funding = fundings.get(0);
    List<Contributor> contributors = funding.getContributors();
    Contributor contributor = contributors.get(0);
    assertNull(contributor.getEmail());
    assertEquals("Jaylen Kessler", contributor.getCreditName().getValue());
    contributor = contributors.get(1);
    assertNull(contributor.getEmail());
    assertEquals("John Smith", contributor.getCreditName().getValue());
    contributor = contributors.get(2);
    assertNull(contributor.getEmail());
    assertEquals("Credit Name", contributor.getCreditName().getValue());
    // contributor is an ORCID user with private name
    contributor = contributors.get(3);
    assertNull(contributor.getEmail());
    assertNull(contributor.getCreditName().getValue());
}
Also used : Locale(java.util.Locale) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) FundingForm(org.orcid.pojo.ajaxForm.FundingForm) MockHttpSession(org.springframework.mock.web.MockHttpSession) Contributor(org.orcid.pojo.ajaxForm.Contributor) Test(org.junit.Test) BaseControllerTest(org.orcid.frontend.web.util.BaseControllerTest)

Example 20 with MockHttpSession

use of org.springframework.mock.web.MockHttpSession in project druid by alibaba.

the class DruidStatServiceTest method test_statService_getResetAll.

public void test_statService_getResetAll() throws Exception {
    // data source mock
    String sql = "select 1";
    Connection conn = dataSource.getConnection();
    PreparedStatement stmt = conn.prepareStatement(sql);
    ResultSet rs = stmt.executeQuery();
    rs.next();
    rs.close();
    stmt.close();
    conn.close();
    String resultSQL = DruidStatService.getInstance().service("/sql.json");
    Map<String, Object> resultSQLMap = (Map<String, Object>) JSONUtils.parse(resultSQL);
    List<Map<String, Object>> sqlList = (List<Map<String, Object>>) resultSQLMap.get("Content");
    assertThat(sqlList.size(), equalTo(1));
    Map<String, Object> sqlStat = sqlList.get(0);
    assertThat((Integer) sqlStat.get("RunningCount"), equalTo(0));
    // http request mock
    String uri = "/";
    MockServletContext servletContext = new MockServletContext();
    MockFilterConfig filterConfig = new MockFilterConfig(servletContext);
    WebStatFilter filter = new WebStatFilter();
    filter.init(filterConfig);
    // first request test
    MockHttpServletRequest request = new MockHttpServletRequest("GET", uri);
    MockHttpSession session = new MockHttpSession();
    request.setSession(session);
    String sessionId = session.getId();
    MockHttpServletResponse response = new MockHttpServletResponse();
    MockFilterChain chain = new MockFilterChain();
    filter.doFilter(request, response, chain);
    String resultWebSession = DruidStatService.getInstance().service("/websession.json");
    Map<String, Object> resultWebSessionMap = (Map<String, Object>) JSONUtils.parse(resultWebSession);
    List<Map<String, Object>> contentWebSessionList = (List<Map<String, Object>>) resultWebSessionMap.get("Content");
    assertThat(contentWebSessionList.size(), equalTo(1));
    Map<String, Object> contentWebSessionMap = contentWebSessionList.get(0);
    assertThat((String) contentWebSessionMap.get("SESSIONID"), equalTo(sessionId));
    // spring mock
    ApplicationContext context = new ClassPathXmlApplicationContext("classpath:com/alibaba/druid/stat/spring-config-stat.xml");
    UserService userService = (UserService) context.getBean("userService");
    userService.save();
    String resultSpring = DruidStatService.getInstance().service("/spring.json");
    Map<String, Object> resultSpringMap = (Map<String, Object>) JSONUtils.parse(resultSpring);
    List<Map<String, Object>> contentSpringList = (List<Map<String, Object>>) resultSpringMap.get("Content");
    assertThat(contentSpringList.size(), equalTo(1));
    Map<String, Object> contentMap = contentSpringList.get(0);
    assertThat((String) contentMap.get("Class"), is(not(nullValue())));
    assertThat((Integer) contentMap.get("ExecuteCount"), equalTo(1));
    // reset all test
    String result = DruidStatService.getInstance().service("/reset-all.json");
    Map<String, Object> resultMap = (Map<String, Object>) JSONUtils.parse(result);
    assertThat(resultMap.get("content"), is(nullValue()));
    // assert sql
    resultSQL = DruidStatService.getInstance().service("/sql.json");
    resultSQLMap = (Map<String, Object>) JSONUtils.parse(resultSQL);
    sqlList = (List<Map<String, Object>>) resultSQLMap.get("Content");
    assertThat(sqlList, is(nullValue()));
    // assert web session
    resultWebSession = DruidStatService.getInstance().service("/websession.json");
    resultWebSessionMap = (Map<String, Object>) JSONUtils.parse(resultWebSession);
    contentWebSessionList = (List<Map<String, Object>>) resultWebSessionMap.get("Content");
    assertThat(contentWebSessionList, is(nullValue()));
    // assert spring
    resultSpring = DruidStatService.getInstance().service("/spring.json");
    resultSpringMap = (Map<String, Object>) JSONUtils.parse(resultSpring);
    contentSpringList = (List<Map<String, Object>>) resultSpringMap.get("Content");
    assertThat(contentSpringList, is(nullValue()));
}
Also used : UserService(com.alibaba.druid.stat.spring.UserService) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) MockServletContext(org.springframework.mock.web.MockServletContext) WebStatFilter(com.alibaba.druid.support.http.WebStatFilter) MockFilterConfig(org.springframework.mock.web.MockFilterConfig) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ResultSet(java.sql.ResultSet) MockHttpSession(org.springframework.mock.web.MockHttpSession) List(java.util.List) MockFilterChain(org.springframework.mock.web.MockFilterChain) Map(java.util.Map) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse)

Aggregations

MockHttpSession (org.springframework.mock.web.MockHttpSession)106 Test (org.junit.Test)84 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)44 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)34 DhisWebSpringTest (org.hisp.dhis.webapi.DhisWebSpringTest)23 HashMap (java.util.HashMap)13 AbstractWebApiTest (org.hisp.dhis.webapi.documentation.controller.AbstractWebApiTest)13 MockFilterChain (org.springframework.mock.web.MockFilterChain)12 FieldDescriptor (org.springframework.restdocs.payload.FieldDescriptor)11 ModelAndView (org.springframework.web.servlet.ModelAndView)11 OAuthRegisteredService (org.apereo.cas.support.oauth.services.OAuthRegisteredService)9 FilterChain (javax.servlet.FilterChain)8 MockServletContext (org.springframework.mock.web.MockServletContext)8 SessionRegistry (org.springframework.security.core.session.SessionRegistry)8 ConcurrentSessionFilter (org.springframework.security.web.session.ConcurrentSessionFilter)8 Principal (org.apereo.cas.authentication.principal.Principal)7 DataElement (org.hisp.dhis.dataelement.DataElement)7 CasProfile (org.pac4j.cas.profile.CasProfile)7 InputStream (java.io.InputStream)6 Before (org.junit.Before)6