|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2019 the original author or authors. |
| 2 | + * Copyright 2002-2020 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.
|
|
25 | 25 | import org.springframework.http.HttpMethod;
|
26 | 26 |
|
27 | 27 | import static org.assertj.core.api.Assertions.assertThat;
|
28 |
| -import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; |
29 | 28 |
|
30 | 29 | /**
|
31 | 30 | * Unit tests for {@link CorsConfiguration}.
|
@@ -61,29 +60,13 @@ public void setValues() {
|
61 | 60 | assertThat(config.getAllowedHeaders()).isEqualTo(Arrays.asList("*"));
|
62 | 61 | config.addAllowedMethod("*");
|
63 | 62 | assertThat(config.getAllowedMethods()).isEqualTo(Arrays.asList("*"));
|
64 |
| - config.addExposedHeader("header1"); |
65 |
| - config.addExposedHeader("header2"); |
66 |
| - assertThat(config.getExposedHeaders()).isEqualTo(Arrays.asList("header1", "header2")); |
| 63 | + config.addExposedHeader("*"); |
67 | 64 | config.setAllowCredentials(true);
|
68 | 65 | assertThat((boolean) config.getAllowCredentials()).isTrue();
|
69 | 66 | config.setMaxAge(123L);
|
70 | 67 | assertThat(config.getMaxAge()).isEqualTo(new Long(123));
|
71 | 68 | }
|
72 | 69 |
|
73 |
| - @Test |
74 |
| - public void asteriskWildCardOnAddExposedHeader() { |
75 |
| - CorsConfiguration config = new CorsConfiguration(); |
76 |
| - assertThatIllegalArgumentException().isThrownBy(() -> |
77 |
| - config.addExposedHeader("*")); |
78 |
| - } |
79 |
| - |
80 |
| - @Test |
81 |
| - public void asteriskWildCardOnSetExposedHeaders() { |
82 |
| - CorsConfiguration config = new CorsConfiguration(); |
83 |
| - assertThatIllegalArgumentException().isThrownBy(() -> |
84 |
| - config.setExposedHeaders(Arrays.asList("*"))); |
85 |
| - } |
86 |
| - |
87 | 70 | @Test
|
88 | 71 | public void combineWithNull() {
|
89 | 72 | CorsConfiguration config = new CorsConfiguration();
|
@@ -120,47 +103,64 @@ public void combineWithDefaultPermitValues() {
|
120 | 103 | other.addAllowedMethod(HttpMethod.PUT.name());
|
121 | 104 |
|
122 | 105 | CorsConfiguration combinedConfig = config.combine(other);
|
123 |
| - assertThat(combinedConfig.getAllowedOrigins()).isEqualTo(Arrays.asList("https://domain.com")); |
124 |
| - assertThat(combinedConfig.getAllowedHeaders()).isEqualTo(Arrays.asList("header1")); |
125 |
| - assertThat(combinedConfig.getAllowedMethods()).isEqualTo(Arrays.asList(HttpMethod.PUT.name())); |
| 106 | + assertThat(combinedConfig).isNotNull(); |
| 107 | + assertThat(combinedConfig.getAllowedOrigins()).containsExactly("https://domain.com"); |
| 108 | + assertThat(combinedConfig.getAllowedHeaders()).containsExactly("header1"); |
| 109 | + assertThat(combinedConfig.getAllowedMethods()).containsExactly(HttpMethod.PUT.name()); |
| 110 | + assertThat(combinedConfig.getExposedHeaders()).isEmpty(); |
126 | 111 |
|
127 | 112 | combinedConfig = other.combine(config);
|
128 |
| - assertThat(combinedConfig.getAllowedOrigins()).isEqualTo(Arrays.asList("https://domain.com")); |
129 |
| - assertThat(combinedConfig.getAllowedHeaders()).isEqualTo(Arrays.asList("header1")); |
130 |
| - assertThat(combinedConfig.getAllowedMethods()).isEqualTo(Arrays.asList(HttpMethod.PUT.name())); |
| 113 | + assertThat(combinedConfig).isNotNull(); |
| 114 | + assertThat(combinedConfig.getAllowedOrigins()).containsExactly("https://domain.com"); |
| 115 | + assertThat(combinedConfig.getAllowedHeaders()).containsExactly("header1"); |
| 116 | + assertThat(combinedConfig.getAllowedMethods()).containsExactly(HttpMethod.PUT.name()); |
| 117 | + assertThat(combinedConfig.getExposedHeaders()).isEmpty(); |
131 | 118 |
|
132 | 119 | combinedConfig = config.combine(new CorsConfiguration());
|
133 |
| - assertThat(config.getAllowedOrigins()).isEqualTo(Arrays.asList("*")); |
134 |
| - assertThat(config.getAllowedHeaders()).isEqualTo(Arrays.asList("*")); |
135 |
| - assertThat(combinedConfig.getAllowedMethods()).isEqualTo(Arrays.asList(HttpMethod.GET.name(), HttpMethod.HEAD.name(), |
136 |
| - HttpMethod.POST.name())); |
| 120 | + assertThat(config.getAllowedOrigins()).containsExactly("*"); |
| 121 | + assertThat(config.getAllowedHeaders()).containsExactly("*"); |
| 122 | + assertThat(combinedConfig).isNotNull(); |
| 123 | + assertThat(combinedConfig.getAllowedMethods()) |
| 124 | + .containsExactly(HttpMethod.GET.name(), HttpMethod.HEAD.name(), HttpMethod.POST.name()); |
| 125 | + assertThat(combinedConfig.getExposedHeaders()).isEmpty(); |
137 | 126 |
|
138 | 127 | combinedConfig = new CorsConfiguration().combine(config);
|
139 |
| - assertThat(config.getAllowedOrigins()).isEqualTo(Arrays.asList("*")); |
140 |
| - assertThat(config.getAllowedHeaders()).isEqualTo(Arrays.asList("*")); |
141 |
| - assertThat(combinedConfig.getAllowedMethods()).isEqualTo(Arrays.asList(HttpMethod.GET.name(), HttpMethod.HEAD.name(), |
142 |
| - HttpMethod.POST.name())); |
| 128 | + assertThat(config.getAllowedOrigins()).containsExactly("*"); |
| 129 | + assertThat(config.getAllowedHeaders()).containsExactly("*"); |
| 130 | + assertThat(combinedConfig).isNotNull(); |
| 131 | + assertThat(combinedConfig.getAllowedMethods()) |
| 132 | + .containsExactly(HttpMethod.GET.name(), HttpMethod.HEAD.name(), HttpMethod.POST.name()); |
| 133 | + assertThat(combinedConfig.getExposedHeaders()).isEmpty(); |
143 | 134 | }
|
144 | 135 |
|
145 | 136 | @Test
|
146 | 137 | public void combineWithAsteriskWildCard() {
|
147 | 138 | CorsConfiguration config = new CorsConfiguration();
|
148 | 139 | config.addAllowedOrigin("*");
|
149 | 140 | config.addAllowedHeader("*");
|
| 141 | + config.addExposedHeader("*"); |
150 | 142 | config.addAllowedMethod("*");
|
151 | 143 | CorsConfiguration other = new CorsConfiguration();
|
152 | 144 | other.addAllowedOrigin("https://domain.com");
|
153 | 145 | other.addAllowedHeader("header1");
|
154 | 146 | other.addExposedHeader("header2");
|
| 147 | + other.addAllowedHeader("anotherHeader1"); |
| 148 | + other.addExposedHeader("anotherHeader2"); |
155 | 149 | other.addAllowedMethod(HttpMethod.PUT.name());
|
156 | 150 | CorsConfiguration combinedConfig = config.combine(other);
|
157 |
| - assertThat(combinedConfig.getAllowedOrigins()).isEqualTo(Arrays.asList("*")); |
158 |
| - assertThat(combinedConfig.getAllowedHeaders()).isEqualTo(Arrays.asList("*")); |
159 |
| - assertThat(combinedConfig.getAllowedMethods()).isEqualTo(Arrays.asList("*")); |
| 151 | + assertThat(combinedConfig).isNotNull(); |
| 152 | + assertThat(combinedConfig.getAllowedOrigins()).containsExactly("*"); |
| 153 | + assertThat(combinedConfig.getAllowedHeaders()).containsExactly("*"); |
| 154 | + assertThat(combinedConfig.getExposedHeaders()).containsExactly("*"); |
| 155 | + assertThat(combinedConfig.getAllowedMethods()).containsExactly("*"); |
| 156 | + |
160 | 157 | combinedConfig = other.combine(config);
|
161 |
| - assertThat(combinedConfig.getAllowedOrigins()).isEqualTo(Arrays.asList("*")); |
162 |
| - assertThat(combinedConfig.getAllowedHeaders()).isEqualTo(Arrays.asList("*")); |
163 |
| - assertThat(combinedConfig.getAllowedMethods()).isEqualTo(Arrays.asList("*")); |
| 158 | + assertThat(combinedConfig).isNotNull(); |
| 159 | + assertThat(combinedConfig.getAllowedOrigins()).containsExactly("*"); |
| 160 | + assertThat(combinedConfig.getAllowedHeaders()).containsExactly("*"); |
| 161 | + assertThat(combinedConfig.getExposedHeaders()).containsExactly("*"); |
| 162 | + assertThat(combinedConfig.getAllowedMethods()).containsExactly("*"); |
| 163 | + assertThat(combinedConfig.getAllowedHeaders()).containsExactly("*"); |
164 | 164 | }
|
165 | 165 |
|
166 | 166 | @Test // SPR-14792
|
|
0 commit comments