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"));
}
use of org.springframework.mock.web.MockFilterChain in project spring-framework by spring-projects.
the class ConditionalDelegatingFilterProxyTests method assertFilterNotInvoked.
private void assertFilterNotInvoked(String requestUri, String pattern) throws Exception {
request.setRequestURI(request.getContextPath() + requestUri);
filter = new PatternMappingFilterProxy(delegate, pattern);
filter.doFilter(request, response, filterChain);
assertThat(delegate.request, equalTo((ServletRequest) null));
assertThat(delegate.response, equalTo((ServletResponse) null));
assertThat(delegate.chain, equalTo((FilterChain) null));
assertThat(filterChain.getRequest(), equalTo((ServletRequest) request));
assertThat(filterChain.getResponse(), equalTo((ServletResponse) response));
filterChain = new 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");
}
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");
}
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();
}
Aggregations