|
|
|
@ -40,6 +40,8 @@ public class CorsConfigurationTests { |
|
|
|
CorsConfiguration config = new CorsConfiguration(); |
|
|
|
CorsConfiguration config = new CorsConfiguration(); |
|
|
|
config.setAllowedOrigins(null); |
|
|
|
config.setAllowedOrigins(null); |
|
|
|
assertThat(config.getAllowedOrigins()).isNull(); |
|
|
|
assertThat(config.getAllowedOrigins()).isNull(); |
|
|
|
|
|
|
|
config.setAllowedOriginPatterns(null); |
|
|
|
|
|
|
|
assertThat(config.getAllowedOriginPatterns()).isNull(); |
|
|
|
config.setAllowedHeaders(null); |
|
|
|
config.setAllowedHeaders(null); |
|
|
|
assertThat(config.getAllowedHeaders()).isNull(); |
|
|
|
assertThat(config.getAllowedHeaders()).isNull(); |
|
|
|
config.setAllowedMethods(null); |
|
|
|
config.setAllowedMethods(null); |
|
|
|
@ -50,42 +52,39 @@ public class CorsConfigurationTests { |
|
|
|
assertThat(config.getAllowCredentials()).isNull(); |
|
|
|
assertThat(config.getAllowCredentials()).isNull(); |
|
|
|
config.setMaxAge((Long) null); |
|
|
|
config.setMaxAge((Long) null); |
|
|
|
assertThat(config.getMaxAge()).isNull(); |
|
|
|
assertThat(config.getMaxAge()).isNull(); |
|
|
|
config.setAllowedOriginPatterns(null); |
|
|
|
|
|
|
|
assertThat(config.getAllowedOriginPatterns()).isNull(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void setValues() { |
|
|
|
public void setValues() { |
|
|
|
CorsConfiguration config = new CorsConfiguration(); |
|
|
|
CorsConfiguration config = new CorsConfiguration(); |
|
|
|
config.addAllowedOrigin("*"); |
|
|
|
config.addAllowedOrigin("*"); |
|
|
|
assertThat(config.getAllowedOrigins()).containsExactly("*"); |
|
|
|
config.addAllowedOriginPattern("http://*.example.com"); |
|
|
|
config.addAllowedHeader("*"); |
|
|
|
config.addAllowedHeader("*"); |
|
|
|
assertThat(config.getAllowedHeaders()).containsExactly("*"); |
|
|
|
|
|
|
|
config.addAllowedMethod("*"); |
|
|
|
config.addAllowedMethod("*"); |
|
|
|
assertThat(config.getAllowedMethods()).containsExactly("*"); |
|
|
|
|
|
|
|
config.addExposedHeader("header1"); |
|
|
|
config.addExposedHeader("header1"); |
|
|
|
config.addExposedHeader("header2"); |
|
|
|
config.addExposedHeader("header2"); |
|
|
|
assertThat(config.getExposedHeaders()).containsExactly("header1", "header2"); |
|
|
|
|
|
|
|
config.setAllowCredentials(true); |
|
|
|
config.setAllowCredentials(true); |
|
|
|
assertThat(config.getAllowCredentials()).isTrue(); |
|
|
|
|
|
|
|
config.setMaxAge(123L); |
|
|
|
config.setMaxAge(123L); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assertThat(config.getAllowedOrigins()).containsExactly("*"); |
|
|
|
|
|
|
|
assertThat(config.getAllowedOriginPatterns()).containsExactly("http://*.example.com"); |
|
|
|
|
|
|
|
assertThat(config.getAllowedHeaders()).containsExactly("*"); |
|
|
|
|
|
|
|
assertThat(config.getAllowedMethods()).containsExactly("*"); |
|
|
|
|
|
|
|
assertThat(config.getExposedHeaders()).containsExactly("header1", "header2"); |
|
|
|
|
|
|
|
assertThat(config.getAllowCredentials()).isTrue(); |
|
|
|
assertThat(config.getMaxAge()).isEqualTo(new Long(123)); |
|
|
|
assertThat(config.getMaxAge()).isEqualTo(new Long(123)); |
|
|
|
config.addAllowedOriginPattern(".*\\.example\\.com"); |
|
|
|
|
|
|
|
assertThat(config.getAllowedOriginPatterns()).containsExactly(".*\\.example\\.com"); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void asteriskWildCardOnAddExposedHeader() { |
|
|
|
public void asteriskWildCardOnAddExposedHeader() { |
|
|
|
CorsConfiguration config = new CorsConfiguration(); |
|
|
|
assertThatIllegalArgumentException() |
|
|
|
assertThatIllegalArgumentException().isThrownBy(() -> |
|
|
|
.isThrownBy(() -> new CorsConfiguration().addExposedHeader("*")); |
|
|
|
config.addExposedHeader("*")); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void asteriskWildCardOnSetExposedHeaders() { |
|
|
|
public void asteriskWildCardOnSetExposedHeaders() { |
|
|
|
CorsConfiguration config = new CorsConfiguration(); |
|
|
|
|
|
|
|
assertThatIllegalArgumentException() |
|
|
|
assertThatIllegalArgumentException() |
|
|
|
.isThrownBy(() -> config.setExposedHeaders(Collections.singletonList("*"))); |
|
|
|
.isThrownBy(() -> new CorsConfiguration().setExposedHeaders(Collections.singletonList("*"))); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
@ -94,28 +93,31 @@ public class CorsConfigurationTests { |
|
|
|
config.setAllowedOrigins(Collections.singletonList("*")); |
|
|
|
config.setAllowedOrigins(Collections.singletonList("*")); |
|
|
|
config.combine(null); |
|
|
|
config.combine(null); |
|
|
|
assertThat(config.getAllowedOrigins()).containsExactly("*"); |
|
|
|
assertThat(config.getAllowedOrigins()).containsExactly("*"); |
|
|
|
|
|
|
|
assertThat(config.getAllowedOriginPatterns()).isNull(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void combineWithNullProperties() { |
|
|
|
public void combineWithNullProperties() { |
|
|
|
CorsConfiguration config = new CorsConfiguration(); |
|
|
|
CorsConfiguration config = new CorsConfiguration(); |
|
|
|
config.addAllowedOrigin("*"); |
|
|
|
config.addAllowedOrigin("*"); |
|
|
|
|
|
|
|
config.setAllowedOriginPatterns(Collections.singletonList("http://*.example.com")); |
|
|
|
config.addAllowedHeader("header1"); |
|
|
|
config.addAllowedHeader("header1"); |
|
|
|
config.addExposedHeader("header3"); |
|
|
|
config.addExposedHeader("header3"); |
|
|
|
config.addAllowedMethod(HttpMethod.GET.name()); |
|
|
|
config.addAllowedMethod(HttpMethod.GET.name()); |
|
|
|
config.setMaxAge(123L); |
|
|
|
config.setMaxAge(123L); |
|
|
|
config.setAllowCredentials(true); |
|
|
|
config.setAllowCredentials(true); |
|
|
|
config.setAllowedOriginPatterns(Collections.singletonList(".*\\.example\\.com")); |
|
|
|
|
|
|
|
CorsConfiguration other = new CorsConfiguration(); |
|
|
|
CorsConfiguration other = new CorsConfiguration(); |
|
|
|
config = config.combine(other); |
|
|
|
config = config.combine(other); |
|
|
|
|
|
|
|
|
|
|
|
assertThat(config).isNotNull(); |
|
|
|
assertThat(config).isNotNull(); |
|
|
|
assertThat(config.getAllowedOrigins()).containsExactly("*"); |
|
|
|
assertThat(config.getAllowedOrigins()).containsExactly("*"); |
|
|
|
|
|
|
|
assertThat(config.getAllowedOriginPatterns()).containsExactly("http://*.example.com"); |
|
|
|
assertThat(config.getAllowedHeaders()).containsExactly("header1"); |
|
|
|
assertThat(config.getAllowedHeaders()).containsExactly("header1"); |
|
|
|
assertThat(config.getExposedHeaders()).containsExactly("header3"); |
|
|
|
assertThat(config.getExposedHeaders()).containsExactly("header3"); |
|
|
|
assertThat(config.getAllowedMethods()).containsExactly(HttpMethod.GET.name()); |
|
|
|
assertThat(config.getAllowedMethods()).containsExactly(HttpMethod.GET.name()); |
|
|
|
assertThat(config.getMaxAge()).isEqualTo(new Long(123)); |
|
|
|
assertThat(config.getMaxAge()).isEqualTo(new Long(123)); |
|
|
|
assertThat(config.getAllowCredentials()).isTrue(); |
|
|
|
assertThat(config.getAllowCredentials()).isTrue(); |
|
|
|
assertThat(config.getAllowedOriginPatterns()).containsExactly(".*\\.example\\.com"); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test // SPR-15772
|
|
|
|
@Test // SPR-15772
|
|
|
|
@ -157,35 +159,36 @@ public class CorsConfigurationTests { |
|
|
|
public void combinePatternWithDefaultPermitValues() { |
|
|
|
public void combinePatternWithDefaultPermitValues() { |
|
|
|
CorsConfiguration config = new CorsConfiguration().applyPermitDefaultValues(); |
|
|
|
CorsConfiguration config = new CorsConfiguration().applyPermitDefaultValues(); |
|
|
|
CorsConfiguration other = new CorsConfiguration(); |
|
|
|
CorsConfiguration other = new CorsConfiguration(); |
|
|
|
other.addAllowedOriginPattern(".*\\.com"); |
|
|
|
other.addAllowedOriginPattern("http://*.com"); |
|
|
|
|
|
|
|
|
|
|
|
CorsConfiguration combinedConfig = other.combine(config); |
|
|
|
CorsConfiguration combinedConfig = other.combine(config); |
|
|
|
assertThat(combinedConfig).isNotNull(); |
|
|
|
assertThat(combinedConfig).isNotNull(); |
|
|
|
assertThat(combinedConfig.getAllowedOrigins()).isNull(); |
|
|
|
assertThat(combinedConfig.getAllowedOrigins()).isNull(); |
|
|
|
assertThat(combinedConfig.getAllowedOriginPatterns()).containsExactly(".*\\.com"); |
|
|
|
assertThat(combinedConfig.getAllowedOriginPatterns()).containsExactly("http://*.com"); |
|
|
|
|
|
|
|
|
|
|
|
combinedConfig = config.combine(other); |
|
|
|
combinedConfig = config.combine(other); |
|
|
|
assertThat(combinedConfig).isNotNull(); |
|
|
|
assertThat(combinedConfig).isNotNull(); |
|
|
|
assertThat(combinedConfig.getAllowedOrigins()).isNull(); |
|
|
|
assertThat(combinedConfig.getAllowedOrigins()).isNull(); |
|
|
|
assertThat(combinedConfig.getAllowedOriginPatterns()).containsExactly(".*\\.com"); |
|
|
|
assertThat(combinedConfig.getAllowedOriginPatterns()).containsExactly("http://*.com"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void combinePatternWithDefaultPermitValuesAndCustomOrigin() { |
|
|
|
public void combinePatternWithDefaultPermitValuesAndCustomOrigin() { |
|
|
|
CorsConfiguration config = new CorsConfiguration().applyPermitDefaultValues(); |
|
|
|
CorsConfiguration config = new CorsConfiguration().applyPermitDefaultValues(); |
|
|
|
config.setAllowedOrigins(Collections.singletonList("https://domain.com")); |
|
|
|
config.setAllowedOrigins(Collections.singletonList("https://domain.com")); |
|
|
|
|
|
|
|
|
|
|
|
CorsConfiguration other = new CorsConfiguration(); |
|
|
|
CorsConfiguration other = new CorsConfiguration(); |
|
|
|
other.addAllowedOriginPattern(".*\\.com"); |
|
|
|
other.addAllowedOriginPattern("http://*.com"); |
|
|
|
|
|
|
|
|
|
|
|
CorsConfiguration combinedConfig = other.combine(config); |
|
|
|
CorsConfiguration combinedConfig = other.combine(config); |
|
|
|
assertThat(combinedConfig).isNotNull(); |
|
|
|
assertThat(combinedConfig).isNotNull(); |
|
|
|
assertThat(combinedConfig.getAllowedOrigins()).containsExactly("https://domain.com"); |
|
|
|
assertThat(combinedConfig.getAllowedOrigins()).containsExactly("https://domain.com"); |
|
|
|
assertThat(combinedConfig.getAllowedOriginPatterns()).containsExactly(".*\\.com"); |
|
|
|
assertThat(combinedConfig.getAllowedOriginPatterns()).containsExactly("http://*.com"); |
|
|
|
|
|
|
|
|
|
|
|
combinedConfig = config.combine(other); |
|
|
|
combinedConfig = config.combine(other); |
|
|
|
assertThat(combinedConfig).isNotNull(); |
|
|
|
assertThat(combinedConfig).isNotNull(); |
|
|
|
assertThat(combinedConfig.getAllowedOrigins()).containsExactly("https://domain.com"); |
|
|
|
assertThat(combinedConfig.getAllowedOrigins()).containsExactly("https://domain.com"); |
|
|
|
assertThat(combinedConfig.getAllowedOriginPatterns()).containsExactly(".*\\.com"); |
|
|
|
assertThat(combinedConfig.getAllowedOriginPatterns()).containsExactly("http://*.com"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
@ -194,25 +197,28 @@ public class CorsConfigurationTests { |
|
|
|
config.addAllowedOrigin("*"); |
|
|
|
config.addAllowedOrigin("*"); |
|
|
|
config.addAllowedHeader("*"); |
|
|
|
config.addAllowedHeader("*"); |
|
|
|
config.addAllowedMethod("*"); |
|
|
|
config.addAllowedMethod("*"); |
|
|
|
config.addAllowedOriginPattern(".*"); |
|
|
|
config.addAllowedOriginPattern("*"); |
|
|
|
|
|
|
|
|
|
|
|
CorsConfiguration other = new CorsConfiguration(); |
|
|
|
CorsConfiguration other = new CorsConfiguration(); |
|
|
|
other.addAllowedOrigin("https://domain.com"); |
|
|
|
other.addAllowedOrigin("https://domain.com"); |
|
|
|
|
|
|
|
other.addAllowedOriginPattern("http://*.company.com"); |
|
|
|
other.addAllowedHeader("header1"); |
|
|
|
other.addAllowedHeader("header1"); |
|
|
|
other.addExposedHeader("header2"); |
|
|
|
other.addExposedHeader("header2"); |
|
|
|
other.addAllowedOriginPattern(".*\\.company\\.com"); |
|
|
|
|
|
|
|
other.addAllowedMethod(HttpMethod.PUT.name()); |
|
|
|
other.addAllowedMethod(HttpMethod.PUT.name()); |
|
|
|
|
|
|
|
|
|
|
|
CorsConfiguration combinedConfig = config.combine(other); |
|
|
|
CorsConfiguration combinedConfig = config.combine(other); |
|
|
|
assertThat(combinedConfig).isNotNull(); |
|
|
|
assertThat(combinedConfig).isNotNull(); |
|
|
|
assertThat(combinedConfig.getAllowedOrigins()).containsExactly("*"); |
|
|
|
assertThat(combinedConfig.getAllowedOrigins()).containsExactly("*"); |
|
|
|
|
|
|
|
assertThat(combinedConfig.getAllowedOriginPatterns()).containsExactly("*"); |
|
|
|
assertThat(combinedConfig.getAllowedHeaders()).containsExactly("*"); |
|
|
|
assertThat(combinedConfig.getAllowedHeaders()).containsExactly("*"); |
|
|
|
assertThat(combinedConfig.getAllowedMethods()).containsExactly("*"); |
|
|
|
assertThat(combinedConfig.getAllowedMethods()).containsExactly("*"); |
|
|
|
assertThat(combinedConfig.getAllowedOriginPatterns()).containsExactly(".*"); |
|
|
|
|
|
|
|
combinedConfig = other.combine(config); |
|
|
|
combinedConfig = other.combine(config); |
|
|
|
assertThat(combinedConfig).isNotNull(); |
|
|
|
assertThat(combinedConfig).isNotNull(); |
|
|
|
assertThat(combinedConfig.getAllowedOrigins()).containsExactly("*"); |
|
|
|
assertThat(combinedConfig.getAllowedOrigins()).containsExactly("*"); |
|
|
|
|
|
|
|
assertThat(combinedConfig.getAllowedOriginPatterns()).containsExactly("*"); |
|
|
|
assertThat(combinedConfig.getAllowedHeaders()).containsExactly("*"); |
|
|
|
assertThat(combinedConfig.getAllowedHeaders()).containsExactly("*"); |
|
|
|
assertThat(combinedConfig.getAllowedMethods()).containsExactly("*"); |
|
|
|
assertThat(combinedConfig.getAllowedMethods()).containsExactly("*"); |
|
|
|
assertThat(combinedConfig.getAllowedOriginPatterns()).containsExactly(".*"); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test // SPR-14792
|
|
|
|
@Test // SPR-14792
|
|
|
|
@ -226,41 +232,45 @@ public class CorsConfigurationTests { |
|
|
|
config.addExposedHeader("header4"); |
|
|
|
config.addExposedHeader("header4"); |
|
|
|
config.addAllowedMethod(HttpMethod.GET.name()); |
|
|
|
config.addAllowedMethod(HttpMethod.GET.name()); |
|
|
|
config.addAllowedMethod(HttpMethod.PUT.name()); |
|
|
|
config.addAllowedMethod(HttpMethod.PUT.name()); |
|
|
|
config.addAllowedOriginPattern(".*\\.domain1\\.com"); |
|
|
|
config.addAllowedOriginPattern("http://*.domain1.com"); |
|
|
|
config.addAllowedOriginPattern(".*\\.domain2\\.com"); |
|
|
|
config.addAllowedOriginPattern("http://*.domain2.com"); |
|
|
|
|
|
|
|
|
|
|
|
CorsConfiguration other = new CorsConfiguration(); |
|
|
|
CorsConfiguration other = new CorsConfiguration(); |
|
|
|
other.addAllowedOrigin("https://domain1.com"); |
|
|
|
other.addAllowedOrigin("https://domain1.com"); |
|
|
|
|
|
|
|
other.addAllowedOriginPattern("http://*.domain1.com"); |
|
|
|
other.addAllowedHeader("header1"); |
|
|
|
other.addAllowedHeader("header1"); |
|
|
|
other.addExposedHeader("header3"); |
|
|
|
other.addExposedHeader("header3"); |
|
|
|
other.addAllowedMethod(HttpMethod.GET.name()); |
|
|
|
other.addAllowedMethod(HttpMethod.GET.name()); |
|
|
|
other.addAllowedOriginPattern(".*\\.domain1\\.com"); |
|
|
|
|
|
|
|
CorsConfiguration combinedConfig = config.combine(other); |
|
|
|
CorsConfiguration combinedConfig = config.combine(other); |
|
|
|
assertThat(combinedConfig).isNotNull(); |
|
|
|
assertThat(combinedConfig).isNotNull(); |
|
|
|
assertThat(combinedConfig.getAllowedOrigins()).containsExactly("https://domain1.com", "https://domain2.com"); |
|
|
|
assertThat(combinedConfig.getAllowedOrigins()).containsExactly("https://domain1.com", "https://domain2.com"); |
|
|
|
assertThat(combinedConfig.getAllowedHeaders()).containsExactly("header1", "header2"); |
|
|
|
assertThat(combinedConfig.getAllowedHeaders()).containsExactly("header1", "header2"); |
|
|
|
assertThat(combinedConfig.getExposedHeaders()).containsExactly("header3", "header4"); |
|
|
|
assertThat(combinedConfig.getExposedHeaders()).containsExactly("header3", "header4"); |
|
|
|
assertThat(combinedConfig.getAllowedMethods()).containsExactly(HttpMethod.GET.name(), HttpMethod.PUT.name()); |
|
|
|
assertThat(combinedConfig.getAllowedMethods()).containsExactly(HttpMethod.GET.name(), HttpMethod.PUT.name()); |
|
|
|
assertThat(combinedConfig.getAllowedOriginPatterns()).containsExactly(".*\\.domain1\\.com", ".*\\.domain2\\.com"); |
|
|
|
assertThat(combinedConfig.getAllowedOriginPatterns()).containsExactly("http://*.domain1.com", "http://*.domain2.com"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void combine() { |
|
|
|
public void combine() { |
|
|
|
CorsConfiguration config = new CorsConfiguration(); |
|
|
|
CorsConfiguration config = new CorsConfiguration(); |
|
|
|
config.addAllowedOrigin("https://domain1.com"); |
|
|
|
config.addAllowedOrigin("https://domain1.com"); |
|
|
|
|
|
|
|
config.addAllowedOriginPattern("http://*.domain1.com"); |
|
|
|
config.addAllowedHeader("header1"); |
|
|
|
config.addAllowedHeader("header1"); |
|
|
|
config.addExposedHeader("header3"); |
|
|
|
config.addExposedHeader("header3"); |
|
|
|
config.addAllowedMethod(HttpMethod.GET.name()); |
|
|
|
config.addAllowedMethod(HttpMethod.GET.name()); |
|
|
|
config.setMaxAge(123L); |
|
|
|
config.setMaxAge(123L); |
|
|
|
config.setAllowCredentials(true); |
|
|
|
config.setAllowCredentials(true); |
|
|
|
config.addAllowedOriginPattern(".*\\.domain1\\.com"); |
|
|
|
|
|
|
|
CorsConfiguration other = new CorsConfiguration(); |
|
|
|
CorsConfiguration other = new CorsConfiguration(); |
|
|
|
other.addAllowedOrigin("https://domain2.com"); |
|
|
|
other.addAllowedOrigin("https://domain2.com"); |
|
|
|
|
|
|
|
other.addAllowedOriginPattern("http://*.domain2.com"); |
|
|
|
other.addAllowedHeader("header2"); |
|
|
|
other.addAllowedHeader("header2"); |
|
|
|
other.addExposedHeader("header4"); |
|
|
|
other.addExposedHeader("header4"); |
|
|
|
other.addAllowedMethod(HttpMethod.PUT.name()); |
|
|
|
other.addAllowedMethod(HttpMethod.PUT.name()); |
|
|
|
other.setMaxAge(456L); |
|
|
|
other.setMaxAge(456L); |
|
|
|
other.setAllowCredentials(false); |
|
|
|
other.setAllowCredentials(false); |
|
|
|
other.addAllowedOriginPattern(".*\\.domain2\\.com"); |
|
|
|
|
|
|
|
config = config.combine(other); |
|
|
|
config = config.combine(other); |
|
|
|
assertThat(config).isNotNull(); |
|
|
|
assertThat(config).isNotNull(); |
|
|
|
assertThat(config.getAllowedOrigins()).containsExactly("https://domain1.com", "https://domain2.com"); |
|
|
|
assertThat(config.getAllowedOrigins()).containsExactly("https://domain1.com", "https://domain2.com"); |
|
|
|
@ -270,18 +280,21 @@ public class CorsConfigurationTests { |
|
|
|
assertThat(config.getMaxAge()).isEqualTo(new Long(456)); |
|
|
|
assertThat(config.getMaxAge()).isEqualTo(new Long(456)); |
|
|
|
assertThat(config).isNotNull(); |
|
|
|
assertThat(config).isNotNull(); |
|
|
|
assertThat(config.getAllowCredentials()).isFalse(); |
|
|
|
assertThat(config.getAllowCredentials()).isFalse(); |
|
|
|
assertThat(config.getAllowedOriginPatterns()).containsExactly(".*\\.domain1\\.com", ".*\\.domain2\\.com"); |
|
|
|
assertThat(config.getAllowedOriginPatterns()).containsExactly("http://*.domain1.com", "http://*.domain2.com"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void checkOriginAllowed() { |
|
|
|
public void checkOriginAllowed() { |
|
|
|
CorsConfiguration config = new CorsConfiguration(); |
|
|
|
CorsConfiguration config = new CorsConfiguration(); |
|
|
|
config.setAllowedOrigins(Collections.singletonList("*")); |
|
|
|
config.addAllowedOrigin("*"); |
|
|
|
assertThat(config.checkOrigin("https://domain.com")).isEqualTo("*"); |
|
|
|
assertThat(config.checkOrigin("https://domain.com")).isEqualTo("*"); |
|
|
|
|
|
|
|
|
|
|
|
config.setAllowCredentials(true); |
|
|
|
config.setAllowCredentials(true); |
|
|
|
assertThat(config.checkOrigin("https://domain.com")).isEqualTo("https://domain.com"); |
|
|
|
assertThatIllegalArgumentException().isThrownBy(() -> config.checkOrigin("https://domain.com")); |
|
|
|
|
|
|
|
|
|
|
|
config.setAllowedOrigins(Collections.singletonList("https://domain.com")); |
|
|
|
config.setAllowedOrigins(Collections.singletonList("https://domain.com")); |
|
|
|
assertThat(config.checkOrigin("https://domain.com")).isEqualTo("https://domain.com"); |
|
|
|
assertThat(config.checkOrigin("https://domain.com")).isEqualTo("https://domain.com"); |
|
|
|
|
|
|
|
|
|
|
|
config.setAllowCredentials(false); |
|
|
|
config.setAllowCredentials(false); |
|
|
|
assertThat(config.checkOrigin("https://domain.com")).isEqualTo("https://domain.com"); |
|
|
|
assertThat(config.checkOrigin("https://domain.com")).isEqualTo("https://domain.com"); |
|
|
|
} |
|
|
|
} |
|
|
|
@ -291,10 +304,13 @@ public class CorsConfigurationTests { |
|
|
|
CorsConfiguration config = new CorsConfiguration(); |
|
|
|
CorsConfiguration config = new CorsConfiguration(); |
|
|
|
assertThat(config.checkOrigin(null)).isNull(); |
|
|
|
assertThat(config.checkOrigin(null)).isNull(); |
|
|
|
assertThat(config.checkOrigin("https://domain.com")).isNull(); |
|
|
|
assertThat(config.checkOrigin("https://domain.com")).isNull(); |
|
|
|
|
|
|
|
|
|
|
|
config.addAllowedOrigin("*"); |
|
|
|
config.addAllowedOrigin("*"); |
|
|
|
assertThat(config.checkOrigin(null)).isNull(); |
|
|
|
assertThat(config.checkOrigin(null)).isNull(); |
|
|
|
|
|
|
|
|
|
|
|
config.setAllowedOrigins(Collections.singletonList("https://domain1.com")); |
|
|
|
config.setAllowedOrigins(Collections.singletonList("https://domain1.com")); |
|
|
|
assertThat(config.checkOrigin("https://domain2.com")).isNull(); |
|
|
|
assertThat(config.checkOrigin("https://domain2.com")).isNull(); |
|
|
|
|
|
|
|
|
|
|
|
config.setAllowedOrigins(new ArrayList<>()); |
|
|
|
config.setAllowedOrigins(new ArrayList<>()); |
|
|
|
assertThat(config.checkOrigin("https://domain.com")).isNull(); |
|
|
|
assertThat(config.checkOrigin("https://domain.com")).isNull(); |
|
|
|
} |
|
|
|
} |
|
|
|
@ -302,12 +318,17 @@ public class CorsConfigurationTests { |
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void checkOriginPatternAllowed() { |
|
|
|
public void checkOriginPatternAllowed() { |
|
|
|
CorsConfiguration config = new CorsConfiguration(); |
|
|
|
CorsConfiguration config = new CorsConfiguration(); |
|
|
|
config.setAllowedOriginPatterns(Collections.singletonList(".*")); |
|
|
|
assertThat(config.checkOrigin("https://domain.com")).isNull(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
config.applyPermitDefaultValues(); |
|
|
|
assertThat(config.checkOrigin("https://domain.com")).isEqualTo("*"); |
|
|
|
assertThat(config.checkOrigin("https://domain.com")).isEqualTo("*"); |
|
|
|
|
|
|
|
|
|
|
|
config.setAllowCredentials(true); |
|
|
|
config.setAllowCredentials(true); |
|
|
|
assertThat(config.checkOrigin("https://domain.com")).isEqualTo("https://domain.com"); |
|
|
|
assertThatIllegalArgumentException().isThrownBy(() -> config.checkOrigin("https://domain.com")); |
|
|
|
config.setAllowedOriginPatterns(Collections.singletonList(".*\\.domain\\.com")); |
|
|
|
|
|
|
|
|
|
|
|
config.addAllowedOriginPattern("https://*.domain.com"); |
|
|
|
assertThat(config.checkOrigin("https://example.domain.com")).isEqualTo("https://example.domain.com"); |
|
|
|
assertThat(config.checkOrigin("https://example.domain.com")).isEqualTo("https://example.domain.com"); |
|
|
|
|
|
|
|
|
|
|
|
config.setAllowCredentials(false); |
|
|
|
config.setAllowCredentials(false); |
|
|
|
assertThat(config.checkOrigin("https://example.domain.com")).isEqualTo("https://example.domain.com"); |
|
|
|
assertThat(config.checkOrigin("https://example.domain.com")).isEqualTo("https://example.domain.com"); |
|
|
|
} |
|
|
|
} |
|
|
|
@ -317,10 +338,12 @@ public class CorsConfigurationTests { |
|
|
|
CorsConfiguration config = new CorsConfiguration(); |
|
|
|
CorsConfiguration config = new CorsConfiguration(); |
|
|
|
assertThat(config.checkOrigin(null)).isNull(); |
|
|
|
assertThat(config.checkOrigin(null)).isNull(); |
|
|
|
assertThat(config.checkOrigin("https://domain.com")).isNull(); |
|
|
|
assertThat(config.checkOrigin("https://domain.com")).isNull(); |
|
|
|
config.addAllowedOriginPattern(".*"); |
|
|
|
config.addAllowedOriginPattern("*"); |
|
|
|
assertThat(config.checkOrigin(null)).isNull(); |
|
|
|
assertThat(config.checkOrigin(null)).isNull(); |
|
|
|
config.setAllowedOriginPatterns(Collections.singletonList(".*\\.domain1\\.com")); |
|
|
|
|
|
|
|
|
|
|
|
config.setAllowedOriginPatterns(Collections.singletonList("http://*.domain1.com")); |
|
|
|
assertThat(config.checkOrigin("https://domain2.com")).isNull(); |
|
|
|
assertThat(config.checkOrigin("https://domain2.com")).isNull(); |
|
|
|
|
|
|
|
|
|
|
|
config.setAllowedOriginPatterns(new ArrayList<>()); |
|
|
|
config.setAllowedOriginPatterns(new ArrayList<>()); |
|
|
|
assertThat(config.checkOrigin("https://domain.com")).isNull(); |
|
|
|
assertThat(config.checkOrigin("https://domain.com")).isNull(); |
|
|
|
} |
|
|
|
} |
|
|
|
@ -329,8 +352,10 @@ public class CorsConfigurationTests { |
|
|
|
public void checkMethodAllowed() { |
|
|
|
public void checkMethodAllowed() { |
|
|
|
CorsConfiguration config = new CorsConfiguration(); |
|
|
|
CorsConfiguration config = new CorsConfiguration(); |
|
|
|
assertThat(config.checkHttpMethod(HttpMethod.GET)).containsExactly(HttpMethod.GET, HttpMethod.HEAD); |
|
|
|
assertThat(config.checkHttpMethod(HttpMethod.GET)).containsExactly(HttpMethod.GET, HttpMethod.HEAD); |
|
|
|
|
|
|
|
|
|
|
|
config.addAllowedMethod("GET"); |
|
|
|
config.addAllowedMethod("GET"); |
|
|
|
assertThat(config.checkHttpMethod(HttpMethod.GET)).containsExactly(HttpMethod.GET); |
|
|
|
assertThat(config.checkHttpMethod(HttpMethod.GET)).containsExactly(HttpMethod.GET); |
|
|
|
|
|
|
|
|
|
|
|
config.addAllowedMethod("POST"); |
|
|
|
config.addAllowedMethod("POST"); |
|
|
|
assertThat(config.checkHttpMethod(HttpMethod.GET)).containsExactly(HttpMethod.GET, HttpMethod.POST); |
|
|
|
assertThat(config.checkHttpMethod(HttpMethod.GET)).containsExactly(HttpMethod.GET, HttpMethod.POST); |
|
|
|
assertThat(config.checkHttpMethod(HttpMethod.POST)).containsExactly(HttpMethod.GET, HttpMethod.POST); |
|
|
|
assertThat(config.checkHttpMethod(HttpMethod.POST)).containsExactly(HttpMethod.GET, HttpMethod.POST); |
|
|
|
@ -341,6 +366,7 @@ public class CorsConfigurationTests { |
|
|
|
CorsConfiguration config = new CorsConfiguration(); |
|
|
|
CorsConfiguration config = new CorsConfiguration(); |
|
|
|
assertThat(config.checkHttpMethod(null)).isNull(); |
|
|
|
assertThat(config.checkHttpMethod(null)).isNull(); |
|
|
|
assertThat(config.checkHttpMethod(HttpMethod.DELETE)).isNull(); |
|
|
|
assertThat(config.checkHttpMethod(HttpMethod.DELETE)).isNull(); |
|
|
|
|
|
|
|
|
|
|
|
config.setAllowedMethods(new ArrayList<>()); |
|
|
|
config.setAllowedMethods(new ArrayList<>()); |
|
|
|
assertThat(config.checkHttpMethod(HttpMethod.POST)).isNull(); |
|
|
|
assertThat(config.checkHttpMethod(HttpMethod.POST)).isNull(); |
|
|
|
} |
|
|
|
} |
|
|
|
@ -349,8 +375,10 @@ public class CorsConfigurationTests { |
|
|
|
public void checkHeadersAllowed() { |
|
|
|
public void checkHeadersAllowed() { |
|
|
|
CorsConfiguration config = new CorsConfiguration(); |
|
|
|
CorsConfiguration config = new CorsConfiguration(); |
|
|
|
assertThat(config.checkHeaders(Collections.emptyList())).isEqualTo(Collections.emptyList()); |
|
|
|
assertThat(config.checkHeaders(Collections.emptyList())).isEqualTo(Collections.emptyList()); |
|
|
|
|
|
|
|
|
|
|
|
config.addAllowedHeader("header1"); |
|
|
|
config.addAllowedHeader("header1"); |
|
|
|
config.addAllowedHeader("header2"); |
|
|
|
config.addAllowedHeader("header2"); |
|
|
|
|
|
|
|
|
|
|
|
assertThat(config.checkHeaders(Collections.singletonList("header1"))).containsExactly("header1"); |
|
|
|
assertThat(config.checkHeaders(Collections.singletonList("header1"))).containsExactly("header1"); |
|
|
|
assertThat(config.checkHeaders(Arrays.asList("header1", "header2"))).containsExactly("header1", "header2"); |
|
|
|
assertThat(config.checkHeaders(Arrays.asList("header1", "header2"))).containsExactly("header1", "header2"); |
|
|
|
assertThat(config.checkHeaders(Arrays.asList("header1", "header2", "header3"))).containsExactly("header1", "header2"); |
|
|
|
assertThat(config.checkHeaders(Arrays.asList("header1", "header2", "header3"))).containsExactly("header1", "header2"); |
|
|
|
@ -361,8 +389,10 @@ public class CorsConfigurationTests { |
|
|
|
CorsConfiguration config = new CorsConfiguration(); |
|
|
|
CorsConfiguration config = new CorsConfiguration(); |
|
|
|
assertThat(config.checkHeaders(null)).isNull(); |
|
|
|
assertThat(config.checkHeaders(null)).isNull(); |
|
|
|
assertThat(config.checkHeaders(Collections.singletonList("header1"))).isNull(); |
|
|
|
assertThat(config.checkHeaders(Collections.singletonList("header1"))).isNull(); |
|
|
|
|
|
|
|
|
|
|
|
config.setAllowedHeaders(Collections.emptyList()); |
|
|
|
config.setAllowedHeaders(Collections.emptyList()); |
|
|
|
assertThat(config.checkHeaders(Collections.singletonList("header1"))).isNull(); |
|
|
|
assertThat(config.checkHeaders(Collections.singletonList("header1"))).isNull(); |
|
|
|
|
|
|
|
|
|
|
|
config.addAllowedHeader("header2"); |
|
|
|
config.addAllowedHeader("header2"); |
|
|
|
config.addAllowedHeader("header3"); |
|
|
|
config.addAllowedHeader("header3"); |
|
|
|
assertThat(config.checkHeaders(Collections.singletonList("header1"))).isNull(); |
|
|
|
assertThat(config.checkHeaders(Collections.singletonList("header1"))).isNull(); |
|
|
|
@ -374,6 +404,7 @@ public class CorsConfigurationTests { |
|
|
|
config.addAllowedOrigin("https://domain.com"); |
|
|
|
config.addAllowedOrigin("https://domain.com"); |
|
|
|
config.addAllowedHeader("header1"); |
|
|
|
config.addAllowedHeader("header1"); |
|
|
|
config.addAllowedMethod("PATCH"); |
|
|
|
config.addAllowedMethod("PATCH"); |
|
|
|
|
|
|
|
|
|
|
|
assertThat(config.getAllowedOrigins()).containsExactly("*", "https://domain.com"); |
|
|
|
assertThat(config.getAllowedOrigins()).containsExactly("*", "https://domain.com"); |
|
|
|
assertThat(config.getAllowedHeaders()).containsExactly("*", "header1"); |
|
|
|
assertThat(config.getAllowedHeaders()).containsExactly("*", "header1"); |
|
|
|
assertThat(config.getAllowedMethods()).containsExactly("GET", "HEAD", "POST", "PATCH"); |
|
|
|
assertThat(config.getAllowedMethods()).containsExactly("GET", "HEAD", "POST", "PATCH"); |
|
|
|
@ -382,9 +413,10 @@ public class CorsConfigurationTests { |
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void permitDefaultDoesntSetOriginWhenPatternPresent() { |
|
|
|
public void permitDefaultDoesntSetOriginWhenPatternPresent() { |
|
|
|
CorsConfiguration config = new CorsConfiguration(); |
|
|
|
CorsConfiguration config = new CorsConfiguration(); |
|
|
|
config.addAllowedOriginPattern(".*\\.com"); |
|
|
|
config.addAllowedOriginPattern("http://*.com"); |
|
|
|
config = config.applyPermitDefaultValues(); |
|
|
|
config = config.applyPermitDefaultValues(); |
|
|
|
|
|
|
|
|
|
|
|
assertThat(config.getAllowedOrigins()).isNull(); |
|
|
|
assertThat(config.getAllowedOrigins()).isNull(); |
|
|
|
assertThat(config.getAllowedOriginPatterns()).containsExactly(".*\\.com"); |
|
|
|
assertThat(config.getAllowedOriginPatterns()).containsExactly("http://*.com"); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|