@Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {

        long size = request.getContentLengthLong();

        if (size > maxSize || isChunked(request)) {
            // Size it's either unknown or too large
            HttpServletResponse httpResponse = (HttpServletResponse) response;
            httpResponse.sendError(HttpServletResponse.SC_BAD_REQUEST, "Bad Request");
        } else {
            chain.doFilter(request, response);
        }
    }
@Test
    public void testDoFilterSendError() throws ServletException, IOException {
        MaxRequestSizeFilter maxRequestSizeFilter = new MaxRequestSizeFilter(MAX_SIZE);
        FilterChain mockFilterChain = Mockito.mock(FilterChain.class);
        // the size grater than max size
        HttpServletRequest spyHttpServletRequest = Mockito.spy(HttpServletRequest.class);
        HttpServletResponse spyHttpServletResponse = Mockito.spy(HttpServletResponse.class);
        Mockito.doReturn(ILLEGAL_SIZE).when(spyHttpServletRequest).getContentLengthLong();
        maxRequestSizeFilter.doFilter(spyHttpServletRequest, spyHttpServletResponse, mockFilterChain);
        Mockito.verify(spyHttpServletResponse).sendError(HttpServletResponse.SC_BAD_REQUEST, "Bad Request");
        // the request is chunked
        HttpServletRequest spyHttpServletRequest2 = Mockito.spy(HttpServletRequest.class);
        HttpServletResponse spyHttpServletResponse2 = Mockito.spy(HttpServletResponse.class);
        Mockito.doReturn(LEGAL_SIZE).when(spyHttpServletRequest2).getContentLengthLong();
        Mockito.doReturn("chunked").when(spyHttpServletRequest2).getHeader("Transfer-Encoding");
        maxRequestSizeFilter.doFilter(spyHttpServletRequest2, spyHttpServletResponse2, mockFilterChain);
        Mockito.verify(spyHttpServletResponse).sendError(HttpServletResponse.SC_BAD_REQUEST, "Bad Request");
    }