Merge branch '2.5.x'
Closes gh-28198
This commit is contained in:
@@ -22,12 +22,16 @@ import java.util.function.Supplier;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.util.MimeType;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
|
||||
|
||||
/**
|
||||
* Test for {@link ProducibleOperationArgumentResolver}.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
class ProducibleOperationArgumentResolverTests {
|
||||
|
||||
@@ -40,11 +44,21 @@ class ProducibleOperationArgumentResolverTests {
|
||||
assertThat(resolve(acceptHeader())).isEqualTo(ApiVersion.V3);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenAcceptHeaderIsEmptyAndWithDefaultThenDefaultIsReturned() {
|
||||
assertThat(resolve(acceptHeader(), WithDefault.class)).isEqualTo(WithDefault.TWO);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenEverythingIsAcceptableThenHighestOrdinalIsReturned() {
|
||||
assertThat(resolve(acceptHeader("*/*"))).isEqualTo(ApiVersion.V3);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenEverythingIsAcceptableWithDefaultThenDefaultIsReturned() {
|
||||
assertThat(resolve(acceptHeader("*/*"), WithDefault.class)).isEqualTo(WithDefault.TWO);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenNothingIsAcceptableThenNullIsReturned() {
|
||||
assertThat(resolve(acceptHeader("image/png"))).isEqualTo(null);
|
||||
@@ -68,13 +82,72 @@ class ProducibleOperationArgumentResolverTests {
|
||||
assertThat(resolve(acceptHeader(V2_JSON + "," + V3_JSON))).isEqualTo(ApiVersion.V3);
|
||||
}
|
||||
|
||||
@Test
|
||||
void withMultipleValuesOneOfWhichIsAllReturnsDefault() {
|
||||
assertThat(resolve(acceptHeader("one/one", "*/*"), WithDefault.class)).isEqualTo(WithDefault.TWO);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenMultipleDefaultsThrowsException() {
|
||||
assertThatIllegalStateException().isThrownBy(() -> resolve(acceptHeader("one/one"), WithMultipleDefaults.class))
|
||||
.withMessageContaining("Multiple default values");
|
||||
}
|
||||
|
||||
private Supplier<List<String>> acceptHeader(String... types) {
|
||||
List<String> value = Arrays.asList(types);
|
||||
return () -> (value.isEmpty() ? null : value);
|
||||
}
|
||||
|
||||
private ApiVersion resolve(Supplier<List<String>> accepts) {
|
||||
return new ProducibleOperationArgumentResolver(accepts).resolve(ApiVersion.class);
|
||||
return resolve(accepts, ApiVersion.class);
|
||||
}
|
||||
|
||||
private <T> T resolve(Supplier<List<String>> accepts, Class<T> type) {
|
||||
return new ProducibleOperationArgumentResolver(accepts).resolve(type);
|
||||
}
|
||||
|
||||
enum WithDefault implements Producible<WithDefault> {
|
||||
|
||||
ONE("one/one"),
|
||||
|
||||
TWO("two/two") {
|
||||
|
||||
@Override
|
||||
public boolean isDefault() {
|
||||
return true;
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
THREE("three/three");
|
||||
|
||||
private final MimeType mimeType;
|
||||
|
||||
WithDefault(String mimeType) {
|
||||
this.mimeType = MimeType.valueOf(mimeType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public MimeType getProducedMimeType() {
|
||||
return this.mimeType;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
enum WithMultipleDefaults implements Producible<WithMultipleDefaults> {
|
||||
|
||||
ONE, TWO, THREE;
|
||||
|
||||
@Override
|
||||
public boolean isDefault() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MimeType getProducedMimeType() {
|
||||
return MimeType.valueOf("image/jpeg");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -55,6 +55,14 @@ class PrometheusScrapeEndpointIntegrationTests {
|
||||
.contains("counter1_total").contains("counter2_total").contains("counter3_total"));
|
||||
}
|
||||
|
||||
@WebEndpointTest
|
||||
void scrapePrefersToProduceOpenMetrics100(WebTestClient client) {
|
||||
MediaType openMetrics = MediaType.parseMediaType(TextFormat.CONTENT_TYPE_OPENMETRICS_100);
|
||||
MediaType textPlain = MediaType.parseMediaType(TextFormat.CONTENT_TYPE_004);
|
||||
client.get().uri("/actuator/prometheus").accept(openMetrics, textPlain).exchange().expectStatus().isOk()
|
||||
.expectHeader().contentType(openMetrics);
|
||||
}
|
||||
|
||||
@WebEndpointTest
|
||||
void scrapeWithIncludedNames(WebTestClient client) {
|
||||
client.get().uri("/actuator/prometheus?includedNames=counter1_total,counter2_total").exchange().expectStatus()
|
||||
|
||||
Reference in New Issue
Block a user