### Question:
CORSFilter implements Filter { @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest)servletRequest; HttpServletResponse response = (HttpServletResponse)servletResponse; String origin = request.getHeader("Origin"); if (origin != null) { response.addHeader("Access-Control-Allow-Origin", origin); response.addHeader("Access-Control-Allow-Headers", "Origin, Content-Type, Accept"); response.setHeader("Access-Control-Max-Age", "1728000"); if (request.getMethod().equals("OPTIONS")) return; } chain.doFilter(request, response); }  @Override void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain); @Override void destroy(); @Override void init(FilterConfig filterConfig);  }### Answer:
@Test void doNothingForRegularRequests() throws Exception { filter.doFilter(request, response, chain); verify(response, never()).addHeader(anyString(), anyString()); verify(chain).doFilter(request, response); }
@Test void addAllowOriginForCORSRequests() throws Exception { when(request.getMethod()).thenReturn("GET"); when(request.getHeader("Origin")).thenReturn("http: filter.doFilter(request, response, chain); verify(response).addHeader("Access-Control-Allow-Origin", "http: verify(chain).doFilter(request, response); }
@Test void doNotGenerateResponseForOPTIONSRequests() throws Exception { when(request.getMethod()).thenReturn("OPTIONS"); when(request.getHeader("Origin")).thenReturn("http: filter.doFilter(request, response, chain); verify(response).addHeader("Access-Control-Allow-Origin", "http: verify(chain, never()).doFilter(request, response); }