Search in sources :

Example 6 with MockFilterChain

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

use of org.springframework.mock.web.MockFilterChain 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 8 with MockFilterChain

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

the class ErrorPageFilterTests method exceptionError.

@Test
public void exceptionError() throws Exception {
    this.filter.addErrorPages(new ErrorPage(RuntimeException.class, "/500"));
    this.chain = new MockFilterChain() {

        @Override
        public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
            super.doFilter(request, response);
            throw new RuntimeException("BAD");
        }
    };
    this.filter.doFilter(this.request, this.response, this.chain);
    assertThat(((HttpServletResponseWrapper) this.chain.getResponse()).getStatus()).isEqualTo(500);
    assertThat(this.request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE)).isEqualTo(500);
    assertThat(this.request.getAttribute(RequestDispatcher.ERROR_MESSAGE)).isEqualTo("BAD");
    Map<String, Object> requestAttributes = getAttributesForDispatch("/500");
    assertThat(requestAttributes.get(RequestDispatcher.ERROR_EXCEPTION_TYPE)).isEqualTo(RuntimeException.class);
    assertThat(requestAttributes.get(RequestDispatcher.ERROR_EXCEPTION)).isInstanceOf(RuntimeException.class);
    assertThat(this.request.getAttribute(RequestDispatcher.ERROR_EXCEPTION_TYPE)).isNull();
    assertThat(this.request.getAttribute(RequestDispatcher.ERROR_EXCEPTION)).isNull();
    assertThat(this.request.getAttribute(RequestDispatcher.ERROR_REQUEST_URI)).isEqualTo("/test/path");
    assertThat(this.response.isCommitted()).isTrue();
    assertThat(this.response.getForwardedUrl()).isEqualTo("/500");
}
Also used : ServletException(javax.servlet.ServletException) NestedServletException(org.springframework.web.util.NestedServletException) ServletRequest(javax.servlet.ServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) ErrorPage(org.springframework.boot.web.server.ErrorPage) HttpServletResponseWrapper(javax.servlet.http.HttpServletResponseWrapper) IOException(java.io.IOException) MockFilterChain(org.springframework.mock.web.MockFilterChain) Test(org.junit.Test)

Example 9 with MockFilterChain

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

the class ErrorPageFilterTests method statusError.

@Test
public void statusError() throws Exception {
    this.filter.addErrorPages(new ErrorPage(HttpStatus.BAD_REQUEST, "/400"));
    this.chain = new MockFilterChain() {

        @Override
        public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
            ((HttpServletResponse) response).sendError(400, "BAD");
            super.doFilter(request, response);
        }
    };
    this.filter.doFilter(this.request, this.response, this.chain);
    assertThat(((HttpServletResponseWrapper) this.chain.getResponse()).getStatus()).isEqualTo(400);
    assertThat(this.request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE)).isEqualTo(400);
    assertThat(this.request.getAttribute(RequestDispatcher.ERROR_MESSAGE)).isEqualTo("BAD");
    assertThat(this.request.getAttribute(RequestDispatcher.ERROR_REQUEST_URI)).isEqualTo("/test/path");
    assertThat(this.response.isCommitted()).isTrue();
    assertThat(this.response.getForwardedUrl()).isEqualTo("/400");
}
Also used : ServletException(javax.servlet.ServletException) NestedServletException(org.springframework.web.util.NestedServletException) ServletRequest(javax.servlet.ServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) ErrorPage(org.springframework.boot.web.server.ErrorPage) HttpServletResponseWrapper(javax.servlet.http.HttpServletResponseWrapper) IOException(java.io.IOException) MockFilterChain(org.springframework.mock.web.MockFilterChain) Test(org.junit.Test)

Example 10 with MockFilterChain

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

the class ErrorPageFilterTests method responseUncommittedWithoutErrorPage.

@Test
public void responseUncommittedWithoutErrorPage() throws Exception {
    this.chain = new MockFilterChain() {

        @Override
        public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
            ((HttpServletResponse) response).sendError(400, "BAD");
            super.doFilter(request, response);
        }
    };
    this.filter.doFilter(this.request, this.response, this.chain);
    assertThat(this.chain.getRequest()).isEqualTo(this.request);
    assertThat(((HttpServletResponseWrapper) this.chain.getResponse()).getResponse()).isEqualTo(this.response);
    assertThat(((HttpServletResponseWrapper) this.chain.getResponse()).getStatus()).isEqualTo(400);
    assertThat(this.response.getForwardedUrl()).isNull();
    assertThat(this.response.isCommitted()).isTrue();
}
Also used : ServletException(javax.servlet.ServletException) NestedServletException(org.springframework.web.util.NestedServletException) ServletRequest(javax.servlet.ServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) HttpServletResponseWrapper(javax.servlet.http.HttpServletResponseWrapper) IOException(java.io.IOException) MockFilterChain(org.springframework.mock.web.MockFilterChain) Test(org.junit.Test)

Aggregations

MockFilterChain (org.springframework.mock.web.MockFilterChain)103 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)102 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)102 Test (org.junit.Test)72 ServletRequest (javax.servlet.ServletRequest)28 ServletResponse (javax.servlet.ServletResponse)28 IOException (java.io.IOException)24 ServletException (javax.servlet.ServletException)24 HttpServletResponse (javax.servlet.http.HttpServletResponse)22 NestedServletException (org.springframework.web.util.NestedServletException)19 Before (org.junit.Before)17 ErrorPage (org.springframework.boot.web.server.ErrorPage)15 HttpServletResponseWrapper (javax.servlet.http.HttpServletResponseWrapper)14 MockFilterConfig (org.springframework.mock.web.MockFilterConfig)11 AuthenticationManager (org.springframework.security.authentication.AuthenticationManager)11 HttpServletRequest (javax.servlet.http.HttpServletRequest)10 MockHttpSession (org.springframework.mock.web.MockHttpSession)9 MockServletContext (org.springframework.mock.web.MockServletContext)9 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)9 WebStatFilter (com.alibaba.druid.support.http.WebStatFilter)8