diff --git a/saml2/saml2-service-provider/src/test/java/org/springframework/security/saml2/provider/service/web/Saml2MetadataFilterTests.java b/saml2/saml2-service-provider/src/test/java/org/springframework/security/saml2/provider/service/web/Saml2MetadataFilterTests.java index b411f1925e..33d9cabff1 100644 --- a/saml2/saml2-service-provider/src/test/java/org/springframework/security/saml2/provider/service/web/Saml2MetadataFilterTests.java +++ b/saml2/saml2-service-provider/src/test/java/org/springframework/security/saml2/provider/service/web/Saml2MetadataFilterTests.java @@ -120,6 +120,22 @@ public class Saml2MetadataFilterTests { verify(this.repository).findByRegistrationId("path"); } + @Test + public void doFilterWhenSetMetadataFilenameThenUses() throws Exception { + RelyingPartyRegistration validRegistration = TestRelyingPartyRegistrations.full().build(); + String testMetadataFilename = "test-{registrationId}-metadata.xml"; + String fileName = testMetadataFilename.replace("{registrationId}", validRegistration.getRegistrationId()); + String encodedFileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8.name()); + String generatedMetadata = "test"; + this.request.setPathInfo("/saml2/service-provider-metadata/validRegistration"); + given(this.resolver.resolve(validRegistration)).willReturn(generatedMetadata); + this.filter = new Saml2MetadataFilter((request) -> validRegistration, this.resolver); + this.filter.setMetadataFilename(testMetadataFilename); + this.filter.doFilter(this.request, this.response, this.chain); + assertThat(this.response.getHeaderValue(HttpHeaders.CONTENT_DISPOSITION)).asString() + .isEqualTo("attachment; filename=\"%s\"; filename*=UTF-8''%s", fileName, encodedFileName); + } + @Test public void setRequestMatcherWhenNullThenIllegalArgument() { assertThatIllegalArgumentException().isThrownBy(() -> this.filter.setRequestMatcher(null)); @@ -138,30 +154,4 @@ public class Saml2MetadataFilterTests { .withMessage("metadataFilename must contain a {registrationId} match variable"); } - @Test - public void doFilterWhenSetMetadataFilenameThenUses() throws Exception { - String testMetadataFilename = "test-{registrationId}-metadata.xml"; - this.request.setPathInfo("/saml2/service-provider-metadata/validRegistration"); - RelyingPartyRegistration validRegistration = TestRelyingPartyRegistrations.noCredentials() - .assertingPartyDetails((party) -> party.verificationX509Credentials( - (c) -> c.add(TestSaml2X509Credentials.relyingPartyVerifyingCredential()))) - .build(); - String generatedMetadata = "test"; - given(this.resolver.resolve(validRegistration)).willReturn(generatedMetadata); - - this.filter = new Saml2MetadataFilter((request) -> validRegistration, this.resolver); - this.filter.setMetadataFilename(testMetadataFilename); - this.filter.doFilter(this.request, this.response, this.chain); - - verifyNoInteractions(this.chain); - assertThat(this.response.getStatus()).isEqualTo(200); - assertThat(this.response.getContentAsString()).isEqualTo(generatedMetadata); - - String fileName = testMetadataFilename.replace("{registrationId}", validRegistration.getRegistrationId()); - String encodedFileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8.name()); - assertThat(this.response.getHeaderValue(HttpHeaders.CONTENT_DISPOSITION)).asString() - .isEqualTo("attachment; filename=\"%s\"; filename*=UTF-8''%s", fileName, encodedFileName); - verify(this.resolver).resolve(validRegistration); - } - }