|
| 1 | +package eu.webeid.example.config; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 6 | + |
| 7 | +import org.apache.commons.lang3.StringUtils; |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | +import org.junit.jupiter.params.ParameterizedTest; |
| 10 | +import org.junit.jupiter.params.provider.ValueSource; |
| 11 | + |
| 12 | +class YAMLConfigTest { |
| 13 | + |
| 14 | + @ValueSource(strings = { |
| 15 | + "http://localhost", |
| 16 | + "http://localhost:8080", |
| 17 | + "http://127.0.0.1", |
| 18 | + "http://127.0.0.1:8080", |
| 19 | + "http://::1", |
| 20 | + "http://[::1]:8080" |
| 21 | + }) |
| 22 | + @ParameterizedTest |
| 23 | + void givenLocalOriginHttpLoopbackAddress_whenParsingLocalOrigin_thenItIsReplacedWithHttps(String origin) { |
| 24 | + YAMLConfig yamlConfig = new YAMLConfig(); |
| 25 | + yamlConfig.setLocalOrigin(origin); |
| 26 | + assertThat(yamlConfig.getLocalOrigin()).isEqualTo(origin.replaceFirst("^http:", "https:")); |
| 27 | + } |
| 28 | + |
| 29 | + @ValueSource(strings = { |
| 30 | + "https://localhost", |
| 31 | + "https://localhost:8080", |
| 32 | + "https://127.0.0.1", |
| 33 | + "https://127.0.0.1:8080", |
| 34 | + "https://::1", |
| 35 | + "https://[::1]:8080", |
| 36 | + }) |
| 37 | + @ParameterizedTest |
| 38 | + void givenLocalOriginHttpsLoopbackAddress_whenParsingLocalOrigin_thenOriginalIsKept(String origin) { |
| 39 | + YAMLConfig yamlConfig = new YAMLConfig(); |
| 40 | + yamlConfig.setLocalOrigin(origin); |
| 41 | + assertThat(yamlConfig.getLocalOrigin()).isEqualTo(origin); |
| 42 | + } |
| 43 | + |
| 44 | + @ValueSource(strings = { |
| 45 | + "http://somename.app", |
| 46 | + "http://somename.app:8080", |
| 47 | + "http://8.8.8.8", |
| 48 | + "http://8.8.8.8:8080", |
| 49 | + "http://[2001:4860:4860::8888]", |
| 50 | + "http://[2001:4860:4860::8888]:8080", |
| 51 | + }) |
| 52 | + @ParameterizedTest |
| 53 | + void givenLocalOriginHttpNonLoopbackAddress_whenParsingLocalOrigin_thenOriginalIsKept(String origin) { |
| 54 | + YAMLConfig yamlConfig = new YAMLConfig(); |
| 55 | + yamlConfig.setLocalOrigin(origin); |
| 56 | + assertThat(yamlConfig.getLocalOrigin()).isEqualTo(origin); |
| 57 | + } |
| 58 | + |
| 59 | + @ValueSource(strings = { |
| 60 | + "https://localhost/", |
| 61 | + "https://localhost:8080/" |
| 62 | + }) |
| 63 | + @ParameterizedTest |
| 64 | + void givenLocalOriginThatEndsWithSlash_whenParsingLocalOrigin_thenExceptionIsThrown(String origin) { |
| 65 | + YAMLConfig yamlConfig = new YAMLConfig(); |
| 66 | + assertThatThrownBy(() -> yamlConfig.setLocalOrigin(origin)) |
| 67 | + .hasMessage("Configuration parameter local-origin cannot end with '/': " + origin); |
| 68 | + } |
| 69 | +} |
0 commit comments