Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand relative URI for RestTemplate if possible #27188

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
* @author Juergen Hoeller
* @author Sam Brannen
* @author Sebastien Deleuze
* @author Yanming Zhou
* @since 3.0
* @see HttpMessageConverter
* @see RequestCallback
Expand Down Expand Up @@ -727,7 +728,8 @@ else if (ext.getVarsMap() != null) {
}
}
else {
return entity.getUrl();
URI url = entity.getUrl();
return url.isAbsolute() ? url : this.uriTemplateHandler.expand(url.toString());
}
}

Expand Down Expand Up @@ -800,7 +802,9 @@ public <T> T execute(String uriTemplate, HttpMethod method, @Nullable RequestCal
@Nullable
public <T> T execute(URI url, HttpMethod method, @Nullable RequestCallback requestCallback,
@Nullable ResponseExtractor<T> responseExtractor) throws RestClientException {

if (!url.isAbsolute()) {
url = getUriTemplateHandler().expand(url.toString());
}
return doExecute(url, null, method, requestCallback, responseExtractor);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.ClientHttpRequest;
import org.springframework.http.client.ClientHttpRequestFactory;
Expand Down Expand Up @@ -79,6 +80,7 @@
* @author Rossen Stoyanchev
* @author Brian Clozel
* @author Sam Brannen
* @author Yanming Zhou
*/
@SuppressWarnings("unchecked")
class RestTemplateTests {
Expand Down Expand Up @@ -297,6 +299,36 @@ void getForObjectWithCustomUriTemplateHandler() throws Exception {
verify(response).close();
}

@Test
void getForObjectWithCustomUriTemplateHandlerAndWithoutUriVariables() throws Exception {
DefaultUriBuilderFactory uriTemplateHandler = new DefaultUriBuilderFactory("https://example.com");
template.setUriTemplateHandler(uriTemplateHandler);
mockSentRequest(GET, "https://example.com/hotels/1/pic/logo.png/size/150x150");
mockResponseStatus(HttpStatus.OK);
given(response.getHeaders()).willReturn(new HttpHeaders());
given(response.getBody()).willReturn(InputStream.nullInputStream());

String url = "/hotels/1/pic/logo.png/size/150x150";
template.getForObject(URI.create(url), void.class);

verify(response).close();
}

@Test
void exchangeWithCustomUriTemplateHandlerAndWithoutUriVariables() throws Exception {
DefaultUriBuilderFactory uriTemplateHandler = new DefaultUriBuilderFactory("https://example.com");
template.setUriTemplateHandler(uriTemplateHandler);
mockSentRequest(GET, "https://example.com/hotels/1/pic/logo.png/size/150x150");
mockResponseStatus(HttpStatus.OK);
given(response.getHeaders()).willReturn(new HttpHeaders());
given(response.getBody()).willReturn(InputStream.nullInputStream());

String url = "/hotels/1/pic/logo.png/size/150x150";
template.exchange(RequestEntity.method(GET, URI.create(url)).build(), void.class);

verify(response).close();
}

@Test
void headForHeaders() throws Exception {
mockSentRequest(HEAD, "https://example.com");
Expand Down