WebFilter implements Filter { @Override public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException { HttpServletRequest httpRequest = (HttpServletRequest)request; if ("application/x-www-form-urlencoded".equals(httpRequest.getContentType())) { httpRequest = new InputStreamHttpServletRequestWrapper(httpRequest); } chain.doFilter(httpRequest, response); }  @Override void init(final FilterConfig filterConfig); @Override void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain); @Override void destroy(); FilterConfig getFilterConfig();  }
@Test public void testRequestContentTypeNull() throws Exception { ServletRequest servletRequest = mock(ServletRequest.class, withSettings().extraInterfaces(HttpServletRequest.class)); HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest; when(httpServletRequest.getContentType()).thenReturn(null); webFilter.doFilter(servletRequest, servletResponse, filterChain); verify(filterChain).doFilter(servletRequest, servletResponse); }
@Test public void testDoFilter() throws Exception { ServletRequest servletRequest = mock(ServletRequest.class, withSettings().extraInterfaces(HttpServletRequest.class)); HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest; when(httpServletRequest.getContentType()).thenReturn("application/json"); webFilter.doFilter(servletRequest, servletResponse, filterChain); verify(filterChain).doFilter(servletRequest, servletResponse); }
@Test public void testDoFilterWrapper() throws Exception { ServletRequest servletRequest = mock(ServletRequest.class, withSettings().extraInterfaces(HttpServletRequest.class)); HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest; when(httpServletRequest.getContentType()).thenReturn("application/x-www-form-urlencoded"); whenNew(InputStreamHttpServletRequestWrapper.class).withArguments(any(HttpServletRequest.class)). thenReturn(inputStreamHttpServletRequestWrapper); webFilter.doFilter(servletRequest, servletResponse, filterChain); verify(filterChain).doFilter(inputStreamHttpServletRequestWrapper, servletResponse); }