11package io .dropwizard .web .conf ;
22
33import com .google .common .collect .ImmutableList ;
4- import io .dropwizard .jetty .setup . ServletEnvironment ;
4+ import io .dropwizard .jetty .MutableServletContextHandler ;
55import io .dropwizard .core .setup .Environment ;
6- import org .eclipse .jetty .ee10 .servlets .CrossOriginFilter ;
7- import org .hamcrest .CoreMatchers ;
6+ import java .util .Set ;
7+ import org .eclipse .jetty .server .Handler ;
8+ import org .eclipse .jetty .server .handler .CrossOriginHandler ;
89import org .junit .jupiter .api .BeforeEach ;
910import org .junit .jupiter .api .Test ;
1011import org .mockito .ArgumentCaptor ;
1112import org .mockito .Captor ;
1213import org .mockito .Mock ;
1314import org .mockito .MockitoAnnotations ;
1415
15- import java .util .Collections ;
16- import java .util .EnumSet ;
17- import java .util .Map ;
18-
19- import jakarta .servlet .Filter ;
20- import jakarta .servlet .FilterRegistration ;
21-
2216import static org .hamcrest .MatcherAssert .assertThat ;
23- import static org .hamcrest .Matchers .aMapWithSize ;
24- import static org .hamcrest .Matchers .hasEntry ;
2517import static org .hamcrest .Matchers .is ;
26-
18+ import static org . hamcrest . Matchers . notNullValue ;
2719import static org .mockito .ArgumentMatchers .any ;
28- import static org .mockito .ArgumentMatchers .anyBoolean ;
29- import static org .mockito .ArgumentMatchers .anyMap ;
30- import static org .mockito .ArgumentMatchers .anyString ;
31- import static org .mockito .ArgumentMatchers .eq ;
3220import static org .mockito .Mockito .doNothing ;
3321import static org .mockito .Mockito .verify ;
3422import static org .mockito .Mockito .when ;
@@ -37,30 +25,20 @@ public class CorsFilterFactoryTest {
3725 @ Mock
3826 Environment env ;
3927 @ Mock
40- ServletEnvironment servlets ;
41- @ Mock
42- FilterRegistration .Dynamic registration ;
43- @ Captor
44- ArgumentCaptor <String > filterNameCaptor ;
28+ MutableServletContextHandler contextHandler ;
4529 @ Captor
46- ArgumentCaptor <Class <? extends Filter >> filterClassCaptor ;
47- @ Captor
48- ArgumentCaptor <String > urlPatternCaptor ;
49- @ Captor
50- ArgumentCaptor <Map <String , String >> initParamsCaptor ;
30+ ArgumentCaptor <Handler .Singleton > handlerCaptor ;
5131
5232 @ BeforeEach
5333 public void setUp () throws Exception {
5434 MockitoAnnotations .initMocks (this );
5535 }
5636
5737 @ Test
58- public void configureFilter () {
38+ public void configureHandler () {
5939 // given
60- when (env .servlets ()).thenReturn (servlets );
61- when (servlets .addFilter (anyString (), any (Class .class ))).thenReturn (registration );
62- doNothing ().when (registration ).addMappingForUrlPatterns (any (EnumSet .class ), anyBoolean (), anyString ());
63- when (registration .setInitParameters (anyMap ())).thenReturn (Collections .emptySet ());
40+ when (env .getApplicationContext ()).thenReturn (contextHandler );
41+ doNothing ().when (contextHandler ).insertHandler (any (Handler .Singleton .class ));
6442 String urlPattern = "/example/*" ;
6543 CorsFilterFactory factory = new CorsFilterFactory ();
6644 factory .setAllowedOrigins (ImmutableList .of ("example.com" , "foo.com" ));
@@ -69,13 +47,13 @@ public void configureFilter() {
6947 factory .build (env , urlPattern );
7048
7149 // then
72- verify (servlets ). addFilter ( filterNameCaptor . capture (), filterClassCaptor .capture ());
73- verify ( registration ). addMappingForUrlPatterns ( any ( ), eq ( true ), urlPatternCaptor . capture ( ));
74- verify ( registration ). setInitParameters ( initParamsCaptor . capture ( ));
75- assertThat (filterNameCaptor . getValue (), is ( "cross-origin-filter" ));
76- assertThat ( filterClassCaptor . getValue (), is ( CoreMatchers .< Class <?>> equalTo ( CrossOriginFilter . class )));
77- assertThat ( urlPatternCaptor . getValue (), is ( urlPattern ));
78- assertThat ( initParamsCaptor . getValue (), aMapWithSize ( 1 ));
79- assertThat ( initParamsCaptor . getValue (), hasEntry ( CrossOriginFilter . ALLOWED_ORIGINS_PARAM , "example.com,foo.com" ));
50+ verify (contextHandler ). insertHandler ( handlerCaptor .capture ());
51+ assertThat ( handlerCaptor . getValue ( ), is ( notNullValue () ));
52+ assertThat ( handlerCaptor . getValue () instanceof CrossOriginHandler , is ( true ));
53+ assertThat ((( CrossOriginHandler ) handlerCaptor . getValue ())
54+ . getAllowedOriginPatterns ()
55+ , is ( Set . of ( "example.com" , "foo.com" ) ));
56+
57+
8058 }
8159}
0 commit comments