Refactoring in ApiVersionInserter

Refine naming of static factory methods, and update them to be
shortcuts for instance creation.

See gh-34919
This commit is contained in:
rstoyanchev
2025-05-19 07:25:21 +01:00
parent f4f0e52003
commit 5b19f6249e
6 changed files with 45 additions and 42 deletions

View File

@@ -59,50 +59,47 @@ public class RestClientVersionTests {
@Test
void header() {
performRequest(ApiVersionInserter.fromHeader("X-API-Version"));
performRequest(ApiVersionInserter.useHeader("X-API-Version"));
expectRequest(request -> assertThat(request.getHeader("X-API-Version")).isEqualTo("1.2"));
}
@Test
void queryParam() {
performRequest(ApiVersionInserter.fromQueryParam("api-version"));
performRequest(ApiVersionInserter.useQueryParam("api-version"));
expectRequest(request -> assertThat(request.getPath()).isEqualTo("/path?api-version=1.2"));
}
@Test
void pathSegmentIndexLessThanSize() {
performRequest(ApiVersionInserter.fromPathSegment(0).withVersionFormatter(v -> "v" + v));
performRequest(ApiVersionInserter.builder().usePathSegment(0).withVersionFormatter(v -> "v" + v).build());
expectRequest(request -> assertThat(request.getPath()).isEqualTo("/v1.2/path"));
}
@Test
void pathSegmentIndexEqualToSize() {
performRequest(ApiVersionInserter.fromPathSegment(1).withVersionFormatter(v -> "v" + v));
performRequest(ApiVersionInserter.builder().usePathSegment(1).withVersionFormatter(v -> "v" + v).build());
expectRequest(request -> assertThat(request.getPath()).isEqualTo("/path/v1.2"));
}
@Test
void pathSegmentIndexGreaterThanSize() {
assertThatIllegalStateException()
.isThrownBy(() -> performRequest(ApiVersionInserter.fromPathSegment(2)))
.isThrownBy(() -> performRequest(ApiVersionInserter.usePathSegment(2)))
.withMessage("Cannot insert version into '/path' at path segment index 2");
}
@Test
void defaultVersion() {
ApiVersionInserter inserter = ApiVersionInserter.fromHeader("X-API-Version").build();
ApiVersionInserter inserter = ApiVersionInserter.useHeader("X-API-Version");
RestClient restClient = restClientBuilder.defaultApiVersion(1.2).apiVersionInserter(inserter).build();
restClient.get().uri("/path").retrieve().body(String.class);
expectRequest(request -> assertThat(request.getHeader("X-API-Version")).isEqualTo("1.2"));
}
private void performRequest(ApiVersionInserter.Builder builder) {
ApiVersionInserter versionInserter = builder.build();
RestClient restClient = restClientBuilder.apiVersionInserter(versionInserter).build();
restClient.get()
.uri("/path")
private void performRequest(ApiVersionInserter versionInserter) {
restClientBuilder.apiVersionInserter(versionInserter).build()
.get().uri("/path")
.apiVersion(1.2)
.retrieve()
.body(String.class);

View File

@@ -274,7 +274,7 @@ class RestClientAdapterTests {
void apiVersion() throws Exception {
RestClient restClient = RestClient.builder()
.baseUrl(anotherServer.url("/").toString())
.apiVersionInserter(ApiVersionInserter.fromHeader("X-API-Version").build())
.apiVersionInserter(ApiVersionInserter.useHeader("X-API-Version"))
.build();
RestClientAdapter adapter = RestClientAdapter.create(restClient);