### Question:
CorsDomainResponseFilter implements ContainerResponseFilter { @Override public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException { final String origin = requestContext.getHeaderString("Origin"); if (originMatchUtil.matches(origin)) { MultivaluedMap<String, Object> headers = responseContext.getHeaders(); headers.add("Access-Control-Allow-Origin", origin); headers.add("Access-Control-Allow-Credentials", String.valueOf(corsConfig.getAllowCredentials())); headers.add("Access-Control-Allow-Methods", String.join(",", corsConfig.getAllowedMethods())); final String allowedHeaders; if (corsConfig.getAllowedHeaders() != null) { allowedHeaders = String.join(",", corsConfig.getAllowedHeaders()); } else { allowedHeaders = requestContext.getHeaderString("Access-Control-Request-Headers"); } headers.add("Access-Control-Allow-Headers", allowedHeaders); } } CorsDomainResponseFilter(CrossDomainConfig corsConfig); @Override void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext);  }### Answer:
@Test public void filter() throws Exception { MultivaluedMap<String, Object> headers = new MultivaluedHashMap(); when(responseContext.getHeaders()).thenReturn(headers); when(requestContext.getHeaderString("Origin")).thenReturn(SOME_ORIGIN); domainResponseFilter.filter(requestContext, responseContext); assertThat(headers) .containsKeys( "Access-Control-Allow-Origin", "Access-Control-Allow-Credentials", "Access-Control-Allow-Methods", "Access-Control-Allow-Headers"); assertThat(headers.get("Access-Control-Allow-Origin")).containsExactly(SOME_ORIGIN); assertThat(headers.get("Access-Control-Allow-Credentials")).containsExactly("true"); verify(requestContext).getHeaderString("Origin"); verify(requestContext).getHeaderString("Access-Control-Request-Headers"); verify(responseContext).getHeaders(); }
@Test public void ignoreNoOrigin() throws Exception { when(requestContext.getHeaderString("Origin")).thenReturn(null); domainResponseFilter.filter(requestContext, responseContext); verify(requestContext).getHeaderString("Origin"); }
@Test public void ignoreEmptyOrigin() throws Exception { when(requestContext.getHeaderString("Origin")).thenReturn(""); domainResponseFilter.filter(requestContext, responseContext); verify(requestContext).getHeaderString("Origin"); }