Skip to content

Commit

Permalink
RestClient exposes full URI template as attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
sonallux committed Nov 20, 2024
1 parent 082a8cf commit f40b67e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -313,19 +313,22 @@ public DefaultRequestBodyUriSpec(HttpMethod httpMethod) {

@Override
public RequestBodySpec uri(String uriTemplate, Object... uriVariables) {
attribute(URI_TEMPLATE_ATTRIBUTE, uriTemplate);
UriBuilder uriBuilder = uriBuilderFactory.uriString(uriTemplate);
attribute(URI_TEMPLATE_ATTRIBUTE, uriBuilder.toUriString());
return uri(DefaultRestClient.this.uriBuilderFactory.expand(uriTemplate, uriVariables));
}

@Override
public RequestBodySpec uri(String uriTemplate, Map<String, ?> uriVariables) {
attribute(URI_TEMPLATE_ATTRIBUTE, uriTemplate);
UriBuilder uriBuilder = uriBuilderFactory.uriString(uriTemplate);
attribute(URI_TEMPLATE_ATTRIBUTE, uriBuilder.toUriString());
return uri(DefaultRestClient.this.uriBuilderFactory.expand(uriTemplate, uriVariables));
}

@Override
public RequestBodySpec uri(String uriTemplate, Function<UriBuilder, URI> uriFunction) {
attribute(URI_TEMPLATE_ATTRIBUTE, uriTemplate);
UriBuilder uriBuilder = uriBuilderFactory.uriString(uriTemplate);
attribute(URI_TEMPLATE_ATTRIBUTE, uriBuilder.toUriString());
return uri(uriFunction.apply(DefaultRestClient.this.uriBuilderFactory.uriString(uriTemplate)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,33 +83,45 @@ void setupEach() {

RestClient.Builder createBuilder() {
return RestClient.builder()
.baseUrl("https://example.com/base")
.messageConverters(converters -> converters.add(0, this.converter))
.requestFactory(this.requestFactory)
.observationRegistry(this.observationRegistry);
}

@Test
void shouldContributeTemplateWhenUriVariables() throws Exception {
mockSentRequest(GET, "https://example.com/hotels/42/bookings/21");
mockSentRequest(GET, "https://example.com/base/hotels/42/bookings/21");
mockResponseStatus(HttpStatus.OK);

client.get().uri("https://example.com/hotels/{hotel}/bookings/{booking}", "42", "21")
client.get().uri("/hotels/{hotel}/bookings/{booking}", "42", "21")
.retrieve().toBodilessEntity();

assertThatHttpObservation().hasLowCardinalityKeyValue("uri", "/hotels/{hotel}/bookings/{booking}");
assertThatHttpObservation().hasLowCardinalityKeyValue("uri", "/base/hotels/{hotel}/bookings/{booking}");
}

@Test
void shouldContributeTemplateWhenMap() throws Exception {
mockSentRequest(GET, "https://example.com/hotels/42/bookings/21");
mockSentRequest(GET, "https://example.com/base/hotels/42/bookings/21");
mockResponseStatus(HttpStatus.OK);

Map<String, String> vars = Map.of("hotel", "42", "booking", "21");

client.get().uri("https://example.com/hotels/{hotel}/bookings/{booking}", vars)
client.get().uri("/hotels/{hotel}/bookings/{booking}", vars)
.retrieve().toBodilessEntity();

assertThatHttpObservation().hasLowCardinalityKeyValue("uri", "/hotels/{hotel}/bookings/{booking}");
assertThatHttpObservation().hasLowCardinalityKeyValue("uri", "/base/hotels/{hotel}/bookings/{booking}");
}

@Test
void shouldContributeTemplateWhenFunction() throws Exception {
mockSentRequest(GET, "https://example.com/base/hotels/42/bookings/21");
mockResponseStatus(HttpStatus.OK);

client.get().uri("/hotels/{hotel}/bookings/{booking}", builder -> builder.build("42", "21"))
.retrieve().toBodilessEntity();

assertThatHttpObservation().hasLowCardinalityKeyValue("uri", "/base/hotels/{hotel}/bookings/{booking}");
}

@Test
Expand Down

0 comments on commit f40b67e

Please sign in to comment.