@ -74,39 +74,39 @@ public class Saml2MetadataFilterTests {
@@ -74,39 +74,39 @@ public class Saml2MetadataFilterTests {
@Test
public void doFilterWhenMatcherSucceedsThenResolverInvoked ( ) throws Exception {
this . request . setPathInfo ( "/saml2/service-provider-metadata/registration-id" ) ;
this . filter . doFilter ( this . request , this . response , this . chain ) ;
MockHttpServletRequest request = uri ( "/saml2/service-provider-metadata/registration-id" ) ;
this . filter . doFilter ( request , this . response , this . chain ) ;
verifyNoInteractions ( this . chain ) ;
verify ( this . repository ) . findByRegistrationId ( "registration-id" ) ;
}
@Test
public void doFilterWhenMatcherFailsThenProcessesFilterChain ( ) throws Exception {
this . request . setPathInfo ( "/saml2/authenticate/registration-id" ) ;
this . filter . doFilter ( this . request , this . response , this . chain ) ;
verify ( this . chain ) . doFilter ( this . request , this . response ) ;
MockHttpServletRequest request = uri ( "/saml2/authenticate/registration-id" ) ;
this . filter . doFilter ( request , this . response , this . chain ) ;
verify ( this . chain ) . doFilter ( request , this . response ) ;
}
@Test
public void doFilterWhenNoRelyingPartyRegistrationThenUnauthorized ( ) throws Exception {
this . request . setPathInfo ( "/saml2/service-provider-metadata/invalidRegistration" ) ;
MockHttpServletRequest request = uri ( "/saml2/service-provider-metadata/invalidRegistration" ) ;
given ( this . repository . findByRegistrationId ( "invalidRegistration" ) ) . willReturn ( null ) ;
this . filter . doFilter ( this . request , this . response , this . chain ) ;
this . filter . doFilter ( request , this . response , this . chain ) ;
verifyNoInteractions ( this . chain ) ;
assertThat ( this . response . getStatus ( ) ) . isEqualTo ( 401 ) ;
}
@Test
public void doFilterWhenRelyingPartyRegistrationFoundThenInvokesMetadataResolver ( ) throws Exception {
this . request . setPathInfo ( "/saml2/service-provider-metadata/validRegistration" ) ;
MockHttpServletRequest request = uri ( "/saml2/service-provider-metadata/validRegistration" ) ;
RelyingPartyRegistration validRegistration = TestRelyingPartyRegistrations . noCredentials ( )
. assertingPartyDetails ( ( party ) - > party
. verificationX509Credentials ( ( c ) - > c . add ( TestSaml2X509Credentials . relyingPartyVerifyingCredential ( ) ) ) )
. build ( ) ;
String generatedMetadata = "<xml>test</xml>" ;
given ( this . resolver . resolve ( validRegistration ) ) . willReturn ( generatedMetadata ) ;
this . filter = new Saml2MetadataFilter ( ( request , registrationId ) - > validRegistration , this . resolver ) ;
this . filter . doFilter ( this . request , this . response , this . chain ) ;
this . filter = new Saml2MetadataFilter ( ( r , registrationId ) - > validRegistration , this . resolver ) ;
this . filter . doFilter ( request , this . response , this . chain ) ;
verifyNoInteractions ( this . chain ) ;
assertThat ( this . response . getStatus ( ) ) . isEqualTo ( 200 ) ;
assertThat ( this . response . getContentAsString ( ) ) . isEqualTo ( generatedMetadata ) ;
@ -128,9 +128,9 @@ public class Saml2MetadataFilterTests {
@@ -128,9 +128,9 @@ public class Saml2MetadataFilterTests {
@Test
public void doFilterWhenCustomRequestMatcherThenUses ( ) throws Exception {
this . request . setPathInfo ( "/path" ) ;
MockHttpServletRequest request = uri ( "/path" ) ;
this . filter . setRequestMatcher ( new AntPathRequestMatcher ( "/path" ) ) ;
this . filter . doFilter ( this . request , this . response , this . chain ) ;
this . filter . doFilter ( request , this . response , this . chain ) ;
verifyNoInteractions ( this . chain ) ;
verify ( this . repository ) . findByRegistrationId ( "path" ) ;
}
@ -142,11 +142,11 @@ public class Saml2MetadataFilterTests {
@@ -142,11 +142,11 @@ public class Saml2MetadataFilterTests {
String fileName = testMetadataFilename . replace ( "{registrationId}" , validRegistration . getRegistrationId ( ) ) ;
String encodedFileName = URLEncoder . encode ( fileName , StandardCharsets . UTF_8 . name ( ) ) ;
String generatedMetadata = "<xml>test</xml>" ;
this . request . setPathInfo ( "/saml2/service-provider-metadata/registration-id" ) ;
MockHttpServletRequest request = uri ( "/saml2/service-provider-metadata/registration-id" ) ;
given ( this . resolver . resolve ( validRegistration ) ) . willReturn ( generatedMetadata ) ;
this . filter = new Saml2MetadataFilter ( ( request , registrationId ) - > validRegistration , this . resolver ) ;
this . filter = new Saml2MetadataFilter ( ( r , registrationId ) - > validRegistration , this . resolver ) ;
this . filter . setMetadataFilename ( testMetadataFilename ) ;
this . filter . doFilter ( this . request , this . response , this . chain ) ;
this . filter . doFilter ( request , this . response , this . chain ) ;
assertThat ( this . response . getHeaderValue ( HttpHeaders . CONTENT_DISPOSITION ) ) . asString ( )
. isEqualTo ( "attachment; filename=\"%s\"; filename*=UTF-8''%s" , fileName , encodedFileName ) ;
}
@ -160,8 +160,8 @@ public class Saml2MetadataFilterTests {
@@ -160,8 +160,8 @@ public class Saml2MetadataFilterTests {
( id ) - > this . repository . findByRegistrationId ( "registration-id" ) ) ;
this . filter = new Saml2MetadataFilter ( resolver , this . resolver ) ;
this . filter . setRequestMatcher ( new AntPathRequestMatcher ( "/metadata" ) ) ;
this . request . setPathInfo ( "/metadata" ) ;
this . filter . doFilter ( this . request , this . response , new MockFilterChain ( ) ) ;
MockHttpServletRequest request = uri ( "/metadata" ) ;
this . filter . doFilter ( request , this . response , new MockFilterChain ( ) ) ;
verify ( this . repository ) . findByRegistrationId ( "registration-id" ) ;
}
@ -174,8 +174,8 @@ public class Saml2MetadataFilterTests {
@@ -174,8 +174,8 @@ public class Saml2MetadataFilterTests {
this . filter = new Saml2MetadataFilter ( ( id ) - > this . repository . findByRegistrationId ( "registration-id" ) ,
this . resolver ) ;
this . filter . setRequestMatcher ( new AntPathRequestMatcher ( "/metadata" ) ) ;
this . request . setPathInfo ( "/metadata" ) ;
this . filter . doFilter ( this . request , this . response , new MockFilterChain ( ) ) ;
MockHttpServletRequest request = uri ( "/metadata" ) ;
this . filter . doFilter ( request , this . response , new MockFilterChain ( ) ) ;
verify ( this . repository ) . findByRegistrationId ( "registration-id" ) ;
}
@ -185,11 +185,11 @@ public class Saml2MetadataFilterTests {
@@ -185,11 +185,11 @@ public class Saml2MetadataFilterTests {
RelyingPartyRegistration validRegistration = TestRelyingPartyRegistrations . full ( ) . build ( ) ;
String testMetadataFilename = "test-{registrationId}-metadata.xml" ;
String generatedMetadata = "<xml>testäöü</xml>" ;
this . request . setPathInfo ( "/saml2/service-provider-metadata/registration-id" ) ;
MockHttpServletRequest request = uri ( "/saml2/service-provider-metadata/registration-id" ) ;
given ( this . resolver . resolve ( validRegistration ) ) . willReturn ( generatedMetadata ) ;
this . filter = new Saml2MetadataFilter ( ( req , id ) - > validRegistration , this . resolver ) ;
this . filter . setMetadataFilename ( testMetadataFilename ) ;
this . filter . doFilter ( this . request , this . response , this . chain ) ;
this . filter . doFilter ( request , this . response , this . chain ) ;
assertThat ( this . response . getCharacterEncoding ( ) ) . isEqualTo ( StandardCharsets . UTF_8 . name ( ) ) ;
assertThat ( this . response . getContentAsString ( StandardCharsets . UTF_8 ) ) . isEqualTo ( generatedMetadata ) ;
assertThat ( this . response . getContentLength ( ) )
@ -218,9 +218,15 @@ public class Saml2MetadataFilterTests {
@@ -218,9 +218,15 @@ public class Saml2MetadataFilterTests {
public void constructorWhenRelyingPartyRegistrationRepositoryThenUses ( ) throws Exception {
RelyingPartyRegistrationRepository repository = mock ( RelyingPartyRegistrationRepository . class ) ;
this . filter = new Saml2MetadataFilter ( repository , this . resolver ) ;
this . request . setPathInfo ( "/saml2/service-provider-metadata/one" ) ;
this . filter . doFilter ( this . request , this . response , this . chain ) ;
MockHttpServletRequest request = uri ( "/saml2/service-provider-metadata/one" ) ;
this . filter . doFilter ( request , this . response , this . chain ) ;
verify ( repository ) . findByRegistrationId ( "one" ) ;
}
private MockHttpServletRequest uri ( String uri ) {
MockHttpServletRequest request = new MockHttpServletRequest ( "GET" , uri ) ;
request . setPathInfo ( uri ) ;
return request ;
}
}