|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2020 the original author or authors. |
| 2 | + * Copyright 2002-2021 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
16 | 16 |
|
17 | 17 | package org.springframework.security.saml2.provider.service.web;
|
18 | 18 |
|
| 19 | +import java.net.URLEncoder; |
| 20 | +import java.nio.charset.StandardCharsets; |
| 21 | + |
19 | 22 | import javax.servlet.FilterChain;
|
20 | 23 |
|
21 | 24 | import org.junit.Before;
|
22 | 25 | import org.junit.Test;
|
23 | 26 |
|
| 27 | +import org.springframework.http.HttpHeaders; |
24 | 28 | import org.springframework.mock.web.MockHttpServletRequest;
|
25 | 29 | import org.springframework.mock.web.MockHttpServletResponse;
|
26 | 30 | import org.springframework.security.saml2.core.TestSaml2X509Credentials;
|
|
31 | 35 | import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
|
32 | 36 |
|
33 | 37 | import static org.assertj.core.api.Assertions.assertThat;
|
| 38 | +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
34 | 39 | import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
35 | 40 | import static org.mockito.BDDMockito.given;
|
36 | 41 | import static org.mockito.Mockito.mock;
|
@@ -120,4 +125,43 @@ public void setRequestMatcherWhenNullThenIllegalArgument() {
|
120 | 125 | assertThatIllegalArgumentException().isThrownBy(() -> this.filter.setRequestMatcher(null));
|
121 | 126 | }
|
122 | 127 |
|
| 128 | + @Test |
| 129 | + public void setMetadataFilenameWhenEmptyThenThrowsException() { |
| 130 | + assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> this.filter.setMetadataFilename(" ")) |
| 131 | + .withMessage("metadataFilename cannot be empty"); |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + public void setMetadataFilenameWhenMissingRegistrationIdVariableThenThrowsException() { |
| 136 | + assertThatExceptionOfType(IllegalArgumentException.class) |
| 137 | + .isThrownBy(() -> this.filter.setMetadataFilename("metadata-filename.xml")) |
| 138 | + .withMessage("metadataFilename must contain a {registrationId} match variable"); |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + public void doFilterWhenSetMetadataFilenameThenUses() throws Exception { |
| 143 | + String testMetadataFilename = "test-{registrationId}-metadata.xml"; |
| 144 | + this.request.setPathInfo("/saml2/service-provider-metadata/validRegistration"); |
| 145 | + RelyingPartyRegistration validRegistration = TestRelyingPartyRegistrations.noCredentials() |
| 146 | + .assertingPartyDetails((party) -> party.verificationX509Credentials( |
| 147 | + (c) -> c.add(TestSaml2X509Credentials.relyingPartyVerifyingCredential()))) |
| 148 | + .build(); |
| 149 | + String generatedMetadata = "<xml>test</xml>"; |
| 150 | + given(this.resolver.resolve(validRegistration)).willReturn(generatedMetadata); |
| 151 | + |
| 152 | + this.filter = new Saml2MetadataFilter((request) -> validRegistration, this.resolver); |
| 153 | + this.filter.setMetadataFilename(testMetadataFilename); |
| 154 | + this.filter.doFilter(this.request, this.response, this.chain); |
| 155 | + |
| 156 | + verifyNoInteractions(this.chain); |
| 157 | + assertThat(this.response.getStatus()).isEqualTo(200); |
| 158 | + assertThat(this.response.getContentAsString()).isEqualTo(generatedMetadata); |
| 159 | + |
| 160 | + String fileName = testMetadataFilename.replace("{registrationId}", validRegistration.getRegistrationId()); |
| 161 | + String encodedFileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8.name()); |
| 162 | + assertThat(this.response.getHeaderValue(HttpHeaders.CONTENT_DISPOSITION)).asString() |
| 163 | + .isEqualTo("attachment; filename=\"%s\"; filename*=UTF-8''%s", fileName, encodedFileName); |
| 164 | + verify(this.resolver).resolve(validRegistration); |
| 165 | + } |
| 166 | + |
123 | 167 | }
|
0 commit comments