ResponseCorsFilter implements ContainerResponseFilter { @Override public ContainerResponse filter( ContainerRequest req, ContainerResponse contResp ) { ResponseBuilder resp = Response.fromResponse( contResp.getResponse()); Map<String,String> headers = buildHeaders( req.getHeaderValue( CORS_REQ_HEADERS ), req.getHeaderValue( ORIGIN )); for( Map.Entry<String,String> h : headers.entrySet()) resp.header( h.getKey(), h.getValue()); contResp.setResponse( resp.build()); return contResp; }  @Override ContainerResponse filter( ContainerRequest req, ContainerResponse contResp ); static Map<String,String> buildHeaders( String reqCorsHeader, String requestUri ); static final String CORS_REQ_HEADERS; static final String ORIGIN; }
@Test public void testHeadersUpdate() { final String origin = "http: final String reqHeader = "POST"; ResponseCorsFilter filter = new ResponseCorsFilter(); ContainerRequest req = Mockito.mock( ContainerRequest.class ); ContainerResponse resp = new ContainerResponse( null, null, null ); Mockito.verifyZeroInteractions( req ); Mockito.when( req.getHeaderValue( ORIGIN )).thenReturn( origin ); Mockito.when( req.getHeaderValue( CORS_REQ_HEADERS )).thenReturn( reqHeader ); ContainerResponse result = filter.filter( req, resp ); Assert.assertNotNull( result ); Assert.assertEquals( 4, result.getHttpHeaders().size()); Assert.assertEquals( Arrays.asList( VALUE_ALLOW_CREDENTIALS ), result.getHttpHeaders().get( CORS_ALLOW_CREDENTIALS )); Assert.assertEquals( Arrays.asList( reqHeader ), result.getHttpHeaders().get( CORS_ALLOW_HEADERS )); Assert.assertEquals( Arrays.asList( VALUE_ALLOWED_METHODS ), result.getHttpHeaders().get( CORS_ALLOW_METHODS )); Assert.assertEquals( Arrays.asList( origin ), result.getHttpHeaders().get( CORS_ALLOW_ORIGIN )); Mockito.verify( req ).getHeaderValue( CORS_REQ_HEADERS ); Mockito.verify( req ).getHeaderValue( ORIGIN ); Mockito.verifyNoMoreInteractions( req ); resp = new ContainerResponse( null, null, null ); Mockito.reset( req ); Mockito.when( req.getHeaderValue( ORIGIN )).thenReturn( "http: result = filter.filter( req, resp ); Assert.assertNotNull( result ); Assert.assertEquals( 3, result.getHttpHeaders().size()); Assert.assertEquals( Arrays.asList( VALUE_ALLOW_CREDENTIALS ), result.getHttpHeaders().get( CORS_ALLOW_CREDENTIALS )); Assert.assertEquals( Arrays.asList( VALUE_ALLOWED_METHODS ), result.getHttpHeaders().get( CORS_ALLOW_METHODS )); Assert.assertEquals( Arrays.asList( origin ), result.getHttpHeaders().get( CORS_ALLOW_ORIGIN )); Mockito.verify( req ).getHeaderValue( ResponseCorsFilter.CORS_REQ_HEADERS ); Mockito.verify( req ).getHeaderValue( ResponseCorsFilter.ORIGIN ); Mockito.verifyNoMoreInteractions( req ); }