Search in sources :

Example 26 with MockHttpServletRequest

use of org.springframework.mock.web.MockHttpServletRequest in project spring-boot-admin by codecentric.

the class PrefixHandlerMappingTest method withoutPrefix.

@Test
public void withoutPrefix() throws Exception {
    TestController controller = new TestController();
    PrefixHandlerMapping mapping = new PrefixHandlerMapping(controller);
    mapping.setApplicationContext(this.context);
    mapping.afterPropertiesSet();
    assertThat(mapping.getHandler(new MockHttpServletRequest("GET", "/test")).getHandler(), equalTo((Object) new HandlerMethod(controller, this.method)));
    assertThat(mapping.getHandler(new MockHttpServletRequest("GET", "/noop")), nullValue());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) HandlerMethod(org.springframework.web.method.HandlerMethod) Test(org.junit.Test)

Example 27 with MockHttpServletRequest

use of org.springframework.mock.web.MockHttpServletRequest in project spring-boot-admin by codecentric.

the class ApplicationHeadersFilterTest method creteRequestContext.

private RequestContext creteRequestContext(String uri) {
    RequestContext context = new RequestContext();
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setRequestURI(uri);
    context.setRequest(request);
    return context;
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) RequestContext(com.netflix.zuul.context.RequestContext)

Example 28 with MockHttpServletRequest

use of org.springframework.mock.web.MockHttpServletRequest in project OpenMEAP by OpenMEAP.

the class ServletManagementServletTest method testRefreshApplication.

@Test
public void testRefreshApplication() throws Exception {
    MockHttpServletRequest request = new Request();
    MockHttpServletResponse response = new MockHttpServletResponse();
    String randomUuid = UUID.randomUUID().toString();
    GlobalSettings settings = modelManager.getGlobalSettings();
    /////////////////
    // validate that finding the application, modifying it, and then finding it again 
    // will return an object with the same modifications.
    Application app = modelManager.getModelService().findByPrimaryKey(Application.class, 1L);
    app.setName(randomUuid);
    Assert.assertTrue(modelManager.getModelService().findByPrimaryKey(Application.class, 1L).getName().equals(randomUuid));
    modelManager.refresh(app, null);
    app = modelManager.getModelService().findByPrimaryKey(Application.class, 1L);
    Assert.assertTrue(!modelManager.getModelService().findByPrimaryKey(Application.class, 1L).getName().equals(randomUuid));
    ServiceManagementServlet servlet = new ServiceManagementServlet();
    servlet.setModelManager(modelManager);
    servlet.setModelServiceRefreshHandler(new ModelServiceRefreshHandler());
    servlet.getModelServiceRefreshHandler().setModelManager(modelManager);
    ////////////////////
    // validate the happy path of providing all the required information
    String authSalt = servlet.getAuthSalt();
    String authToken = AuthTokenProvider.newAuthToken(authSalt);
    request.setParameter(UrlParamConstants.REFRESH_TYPE, "Application");
    request.setParameter(UrlParamConstants.REFRESH_OBJ_PKID, "1");
    request.setParameter(UrlParamConstants.AUTH_TOKEN, authToken);
    request.setParameter(UrlParamConstants.ACTION, ModelEntityEventAction.MODEL_REFRESH.getActionName());
    servlet.service(request, response);
    String contentString = response.getContentAsString();
    JSONObjectBuilder job = new JSONObjectBuilder();
    Result result = (Result) job.fromJSON(new JSONObject(contentString), new Result());
    Assert.assertTrue(result.getStatus().equals(Result.Status.SUCCESS));
    Assert.assertTrue(!modelManager.getModelService().findByPrimaryKey(Application.class, 1L).getName().equals(randomUuid));
    ////////////////////
    // validate that failing to provide auth token fails to refresh cache
    app = modelManager.getModelService().findByPrimaryKey(Application.class, 1L);
    app.setName(randomUuid);
    response = new MockHttpServletResponse();
    request.removeParameter(UrlParamConstants.AUTH_TOKEN);
    request.setParameter(UrlParamConstants.ACTION, ModelEntityEventAction.MODEL_REFRESH.getActionName());
    request.setParameter(UrlParamConstants.REFRESH_TYPE, "Application");
    request.setParameter(UrlParamConstants.REFRESH_OBJ_PKID, "1");
    servlet.service(request, response);
    contentString = response.getContentAsString();
    result = (Result) job.fromJSON(new JSONObject(contentString), new Result());
    Assert.assertTrue(result.getStatus().equals(Result.Status.FAILURE));
    Assert.assertTrue(modelManager.getModelService().findByPrimaryKey(Application.class, 1L).getName().equals(randomUuid));
}
Also used : JSONObjectBuilder(com.openmeap.json.JSONObjectBuilder) JSONObject(com.openmeap.thirdparty.org.json.me.JSONObject) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ModelServiceRefreshHandler(com.openmeap.model.event.handler.ModelServiceRefreshHandler) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) GlobalSettings(com.openmeap.model.dto.GlobalSettings) Application(com.openmeap.model.dto.Application) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Result(com.openmeap.services.dto.Result) Test(org.junit.Test)

Example 29 with MockHttpServletRequest

use of org.springframework.mock.web.MockHttpServletRequest in project cobar by alibaba.

the class TestDataNodeControl method testDatanodeControl.

@SuppressWarnings("rawtypes")
public void testDatanodeControl() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("POST");
    MockHttpSession session = new MockHttpSession();
    UserDO user = new UserDO();
    user.setStatus(ConstantDefine.NORMAL);
    user.setUser_role(ConstantDefine.CLUSTER_ADMIN);
    session.setAttribute("user", user);
    request.setSession(session);
    request.addParameter("clusterId", "1");
    try {
        ModelAndView mav = datanodeControl.handleRequest(request, new MockHttpServletResponse());
        Assert.assertEquals("1", String.valueOf(mav.getModel().get("clusterId")));
        Assert.assertEquals("true", String.valueOf(mav.getModel().get("uniform")));
        Assert.assertEquals("true", String.valueOf(mav.getModel().get("connecitonFlag")));
        Assert.assertEquals(1, ((List) mav.getModel().get("cList")).size());
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        Assert.fail();
    }
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) UserDO(com.alibaba.cobar.manager.dataobject.xml.UserDO) ModelAndView(org.springframework.web.servlet.ModelAndView) MockHttpSession(org.springframework.mock.web.MockHttpSession) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse)

Example 30 with MockHttpServletRequest

use of org.springframework.mock.web.MockHttpServletRequest 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

MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)1056 Test (org.junit.Test)827 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)557 Before (org.junit.Before)92 MockFilterChain (org.springframework.mock.web.MockFilterChain)89 FilterChain (javax.servlet.FilterChain)79 MockServletContext (org.springframework.mock.web.MockServletContext)71 Authentication (org.springframework.security.core.Authentication)70 HttpServletRequest (javax.servlet.http.HttpServletRequest)51 MockHttpSession (org.springframework.mock.web.MockHttpSession)49 ServletExternalContext (org.springframework.webflow.context.servlet.ServletExternalContext)47 Cookie (javax.servlet.http.Cookie)44 MockRequestContext (org.springframework.webflow.test.MockRequestContext)42 HttpServletResponse (javax.servlet.http.HttpServletResponse)39 HashMap (java.util.HashMap)32 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)32 ModelAndView (org.springframework.web.servlet.ModelAndView)32 MockPortletWindowId (org.apereo.portal.mock.portlet.om.MockPortletWindowId)30 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)30 RegisteredService (org.apereo.cas.services.RegisteredService)27