Search in sources :

Example 16 with MockServletContext

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

the class WebStatFilterTest2 method test_lru.

public void test_lru() throws Exception {
    MockServletContext servletContext = new MockServletContext();
    MockFilterConfig filterConfig = new MockFilterConfig(servletContext);
    filterConfig.addInitParameter(WebStatFilter.PARAM_NAME_SESSION_STAT_MAX_COUNT, "3");
    WebStatFilter filter = new WebStatFilter();
    filter.init(filterConfig);
    WebAppStat appStat = filter.getWebAppStat();
    Assert.assertEquals(3, filter.getSessionStatMaxCount());
    Assert.assertEquals(0, appStat.getSessionStatDataList().size());
    final MockHttpSession session_0 = new MockHttpSession(servletContext);
    final MockHttpSession session_1 = new MockHttpSession(servletContext);
    final MockHttpSession session_2 = new MockHttpSession(servletContext);
    final MockHttpSession session_3 = new MockHttpSession(servletContext);
    final MockHttpSession session_4 = new MockHttpSession(servletContext);
    // 第一个session请求2次
    {
        Assert.assertNull(appStat.getSessionStat(session_0.getId()));
        MockHttpServletRequest request = new MockHttpServletRequest();
        MockHttpServletResponse response = new MockHttpServletResponse();
        MockFilterChain chain = new MockFilterChain() {

            public void doFilter(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response) {
                ((MockHttpServletRequest) request).setSession(session_0);
            }

            ;
        };
        filter.doFilter(request, response, chain);
        Assert.assertEquals(1, appStat.getSessionStatDataList().size());
        Assert.assertEquals(1, appStat.getSessionStat(session_0.getId()).getRequestCount());
        Assert.assertTrue(appStat.getSessionStat(session_0.getId()).getLastAccessTimeMillis() > 0);
    }
    {
        MockHttpServletRequest request = new MockHttpServletRequest();
        MockHttpServletResponse response = new MockHttpServletResponse();
        MockFilterChain chain = new MockFilterChain() {

            public void doFilter(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response) {
                ((MockHttpServletRequest) request).setSession(session_0);
            }

            ;
        };
        filter.doFilter(request, response, chain);
        Assert.assertEquals(1, appStat.getSessionStatDataList().size());
        Assert.assertEquals(2, appStat.getSessionStat(session_0.getId()).getRequestCount());
    }
    // 第2个sesion请求1次
    {
        MockHttpServletRequest request = new MockHttpServletRequest();
        MockHttpServletResponse response = new MockHttpServletResponse();
        MockFilterChain chain = new MockFilterChain() {

            public void doFilter(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response) {
                ((MockHttpServletRequest) request).setSession(session_1);
            }

            ;
        };
        filter.doFilter(request, response, chain);
        Assert.assertEquals(2, appStat.getSessionStatDataList().size());
        Assert.assertEquals(2, appStat.getSessionStat(session_0.getId()).getRequestCount());
        Assert.assertEquals(1, appStat.getSessionStat(session_1.getId()).getRequestCount());
    }
    // 第3个sesion请求1次
    {
        MockHttpServletRequest request = new MockHttpServletRequest();
        MockHttpServletResponse response = new MockHttpServletResponse();
        MockFilterChain chain = new MockFilterChain() {

            public void doFilter(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response) {
                ((MockHttpServletRequest) request).setSession(session_2);
            }

            ;
        };
        filter.doFilter(request, response, chain);
        Assert.assertEquals(3, appStat.getSessionStatDataList().size());
        Assert.assertEquals(2, appStat.getSessionStat(session_0.getId()).getRequestCount());
        Assert.assertEquals(1, appStat.getSessionStat(session_1.getId()).getRequestCount());
        Assert.assertEquals(1, appStat.getSessionStat(session_2.getId()).getRequestCount());
    }
    // 第4个sesion请求1次
    {
        MockHttpServletRequest request = new MockHttpServletRequest();
        MockHttpServletResponse response = new MockHttpServletResponse();
        MockFilterChain chain = new MockFilterChain() {

            public void doFilter(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response) {
                ((MockHttpServletRequest) request).setSession(session_3);
            }

            ;
        };
        filter.doFilter(request, response, chain);
        Assert.assertEquals(3, appStat.getSessionStatDataList().size());
        Assert.assertNull(appStat.getSessionStat(session_0.getId()));
        Assert.assertEquals(1, appStat.getSessionStat(session_1.getId()).getRequestCount());
        Assert.assertEquals(1, appStat.getSessionStat(session_2.getId()).getRequestCount());
        Assert.assertEquals(1, appStat.getSessionStat(session_3.getId()).getRequestCount());
    }
    // 第5个sesion请求1次
    {
        MockHttpServletRequest request = new MockHttpServletRequest();
        MockHttpServletResponse response = new MockHttpServletResponse();
        MockFilterChain chain = new MockFilterChain() {

            public void doFilter(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response) {
                ((MockHttpServletRequest) request).setSession(session_4);
            }

            ;
        };
        filter.doFilter(request, response, chain);
        Assert.assertEquals(3, appStat.getSessionStatDataList().size());
        Assert.assertNull(appStat.getSessionStat(session_0.getId()));
        Assert.assertNull(appStat.getSessionStat(session_1.getId()));
        Assert.assertEquals(1, appStat.getSessionStat(session_2.getId()).getRequestCount());
        Assert.assertEquals(1, appStat.getSessionStat(session_3.getId()).getRequestCount());
        Assert.assertEquals(1, appStat.getSessionStat(session_4.getId()).getRequestCount());
    }
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpSession(org.springframework.mock.web.MockHttpSession) WebAppStat(com.alibaba.druid.support.http.stat.WebAppStat) MockFilterChain(org.springframework.mock.web.MockFilterChain) MockServletContext(org.springframework.mock.web.MockServletContext) WebStatFilter(com.alibaba.druid.support.http.WebStatFilter) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockFilterConfig(org.springframework.mock.web.MockFilterConfig)

Example 17 with MockServletContext

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

the class WebStatFilterTest3_WebURIStatNull method test_sessionStatDisable.

public void test_sessionStatDisable() throws Exception {
    MockServletContext servletContext = new MockServletContext();
    MockFilterConfig filterConfig = new MockFilterConfig(servletContext);
    filterConfig.addInitParameter(WebStatFilter.PARAM_NAME_SESSION_STAT_ENABLE, "false");
    WebStatFilter filter = new WebStatFilter();
    WebAppStat appStat = new WebAppStat() {

        public WebURIStat getURIStat(String uri, boolean create) {
            return null;
        }
    };
    filter.setWebAppStat(appStat);
    filter.setProfileEnable(true);
    Assert.assertNotNull(filter.getWebAppStat());
    filter.init(filterConfig);
    Assert.assertSame(appStat, filter.getWebAppStat());
    Assert.assertFalse(filter.isSessionStatEnable());
    Assert.assertTrue(WebAppStatManager.getInstance().getWebAppStatSet().contains(appStat));
    Assert.assertTrue(StatFilterContext.getInstance().getListeners().contains(filter.getStatFilterContextListener()));
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    MockFilterChain chain = new MockFilterChain();
    Assert.assertNull(filter.getSessionStat(request));
    filter.doFilter(request, response, chain);
    Assert.assertEquals(0, appStat.getSessionStatDataList().size());
    filter.destroy();
    Assert.assertFalse(WebAppStatManager.getInstance().getWebAppStatSet().contains(appStat));
    Assert.assertFalse(StatFilterContext.getInstance().getListeners().contains(filter.getStatFilterContextListener()));
    Map<String, Object> statData = appStat.getStatData();
    Assert.assertEquals(1L, statData.get("RequestCount"));
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) WebAppStat(com.alibaba.druid.support.http.stat.WebAppStat) MockFilterChain(org.springframework.mock.web.MockFilterChain) MockServletContext(org.springframework.mock.web.MockServletContext) WebStatFilter(com.alibaba.druid.support.http.WebStatFilter) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockFilterConfig(org.springframework.mock.web.MockFilterConfig)

Example 18 with MockServletContext

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

the class DruidWebUtilsTest method test_getContextPath_2_5.

public void test_getContextPath_2_5() throws Exception {
    //
    new DruidWebUtils();
    MockServletContext context = new MockServletContext() {

        public int getMajorVersion() {
            return 2;
        }

        public int getMinorVersion() {
            return 4;
        }

        public String getContextPath() {
            throw new NoSuchMethodError();
        }
    };
    Assert.assertNull(DruidWebUtils.getContextPath(context));
}
Also used : DruidWebUtils(com.alibaba.druid.util.DruidWebUtils) MockServletContext(org.springframework.mock.web.MockServletContext)

Example 19 with MockServletContext

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

the class MockMultipartHttpServletRequestBuilderTests method test.

@Test
public void test() {
    MockHttpServletRequestBuilder parent = new MockHttpServletRequestBuilder(HttpMethod.GET, "/");
    parent.characterEncoding("UTF-8");
    Object result = new MockMultipartHttpServletRequestBuilder("/fileUpload").merge(parent);
    assertNotNull(result);
    assertEquals(MockMultipartHttpServletRequestBuilder.class, result.getClass());
    MockMultipartHttpServletRequestBuilder builder = (MockMultipartHttpServletRequestBuilder) result;
    MockHttpServletRequest request = builder.buildRequest(new MockServletContext());
    assertEquals("UTF-8", request.getCharacterEncoding());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockServletContext(org.springframework.mock.web.MockServletContext) Test(org.junit.Test)

Example 20 with MockServletContext

use of org.springframework.mock.web.MockServletContext in project spring-boot by spring-projects.

the class HealthMvcEndpointAutoConfigurationTests method testSetRoles.

@Test
public void testSetRoles() throws Exception {
    // gh-8314
    this.context = new AnnotationConfigWebApplicationContext();
    this.context.setServletContext(new MockServletContext());
    this.context.register(TestConfiguration.class);
    EnvironmentTestUtils.addEnvironment(this.context, "management.security.roles[0]=super");
    this.context.refresh();
    HealthMvcEndpoint health = this.context.getBean(HealthMvcEndpoint.class);
    assertThat(ReflectionTestUtils.getField(health, "roles")).isEqualTo(Arrays.asList("super"));
}
Also used : AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) MockServletContext(org.springframework.mock.web.MockServletContext) HealthMvcEndpoint(org.springframework.boot.actuate.endpoint.mvc.HealthMvcEndpoint) Test(org.junit.Test)

Aggregations

MockServletContext (org.springframework.mock.web.MockServletContext)173 Test (org.junit.Test)126 AnnotationConfigWebApplicationContext (org.springframework.web.context.support.AnnotationConfigWebApplicationContext)79 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)71 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)63 ServletExternalContext (org.springframework.webflow.context.servlet.ServletExternalContext)43 MockRequestContext (org.springframework.webflow.test.MockRequestContext)38 Before (org.junit.Before)14 MockMvc (org.springframework.test.web.servlet.MockMvc)13 Event (org.springframework.webflow.execution.Event)11 TicketGrantingTicket (org.apereo.cas.ticket.TicketGrantingTicket)10 MockFilterConfig (org.springframework.mock.web.MockFilterConfig)9 WebStatFilter (com.alibaba.druid.support.http.WebStatFilter)8 MockFilterChain (org.springframework.mock.web.MockFilterChain)8 MockHttpSession (org.springframework.mock.web.MockHttpSession)8 Map (java.util.Map)7 Credential (org.apereo.cas.authentication.Credential)7 EventFactorySupport (org.springframework.webflow.action.EventFactorySupport)7 FilterChainProxy (org.springframework.security.web.FilterChainProxy)6 GenericWebApplicationContext (org.springframework.web.context.support.GenericWebApplicationContext)6