Skip to content

Commit

Permalink
Fix handling of UriTemplateRequestEntity in TestRestTemplate
Browse files Browse the repository at this point in the history
A change [1] in Spring Framework 5.3 means that getUrl() on a
RequestEntity will throw an UnsupportedOperationException if the
entity was created using a template.

This commit updates TestRestTemplate to check for instances of
UriTemplateRequestEntity and to resolve the URI using the
entity's UriTemplateHandler instead of calling getUrl() directly.

Fixes gh-25097

[1] spring-projects/spring-framework@a0f4d81
  • Loading branch information
wilkinsona committed Feb 4, 2021
1 parent 788a42d commit 297e307
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -41,6 +41,7 @@
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.RequestEntity;
import org.springframework.http.RequestEntity.UriTemplateRequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.ClientHttpResponse;
Expand Down Expand Up @@ -965,7 +966,7 @@ public TestRestTemplate withBasicAuth(String username, String password) {
@SuppressWarnings({ "rawtypes", "unchecked" })
private RequestEntity<?> createRequestEntityWithRootAppliedUri(RequestEntity<?> requestEntity) {
return new RequestEntity(requestEntity.getBody(), requestEntity.getHeaders(), requestEntity.getMethod(),
applyRootUriIfNecessary(requestEntity.getUrl()), requestEntity.getType());
applyRootUriIfNecessary(resolveUri(requestEntity)), requestEntity.getType());
}

private URI applyRootUriIfNecessary(URI uri) {
Expand All @@ -976,6 +977,23 @@ private URI applyRootUriIfNecessary(URI uri) {
return uri;
}

private URI resolveUri(RequestEntity<?> entity) {
if (entity instanceof UriTemplateRequestEntity) {
UriTemplateRequestEntity<?> templatedUriEntity = (UriTemplateRequestEntity<?>) entity;
if (templatedUriEntity.getVars() != null) {
return this.restTemplate.getUriTemplateHandler().expand(templatedUriEntity.getUriTemplate(),
templatedUriEntity.getVars());
}
else if (templatedUriEntity.getVarsMap() != null) {
return this.restTemplate.getUriTemplateHandler().expand(templatedUriEntity.getUriTemplate(),
templatedUriEntity.getVarsMap());
}
throw new IllegalStateException(
"No variables specified for URI template: " + templatedUriEntity.getUriTemplate());
}
return entity.getUrl();
}

/**
* Options used to customize the Apache HTTP Client.
*/
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -233,6 +233,36 @@ void withBasicAuthShouldUseNoOpErrorHandler() throws Exception {
Class.forName("org.springframework.boot.test.web.client.TestRestTemplate$NoOpResponseErrorHandler"));
}

@Test
void exchangeWithRelativeTemplatedUrlRequestEntity() throws Exception {
RequestEntity<Void> entity = RequestEntity.get("/a/b/c.{ext}", "txt").build();
TestRestTemplate template = new TestRestTemplate();
ClientHttpRequestFactory requestFactory = mock(ClientHttpRequestFactory.class);
MockClientHttpRequest request = new MockClientHttpRequest();
request.setResponse(new MockClientHttpResponse(new byte[0], HttpStatus.OK));
URI absoluteUri = URI.create("http://localhost:8080/a/b/c.txt");
given(requestFactory.createRequest(eq(absoluteUri), eq(HttpMethod.GET))).willReturn(request);
template.getRestTemplate().setRequestFactory(requestFactory);
LocalHostUriTemplateHandler uriTemplateHandler = new LocalHostUriTemplateHandler(new MockEnvironment());
template.setUriTemplateHandler(uriTemplateHandler);
template.exchange(entity, String.class);
verify(requestFactory).createRequest(eq(absoluteUri), eq(HttpMethod.GET));
}

@Test
void exchangeWithAbsoluteTemplatedUrlRequestEntity() throws Exception {
RequestEntity<Void> entity = RequestEntity.get("https://api.example.com/a/b/c.{ext}", "txt").build();
TestRestTemplate template = new TestRestTemplate();
ClientHttpRequestFactory requestFactory = mock(ClientHttpRequestFactory.class);
MockClientHttpRequest request = new MockClientHttpRequest();
request.setResponse(new MockClientHttpResponse(new byte[0], HttpStatus.OK));
URI absoluteUri = URI.create("https://api.example.com/a/b/c.txt");
given(requestFactory.createRequest(eq(absoluteUri), eq(HttpMethod.GET))).willReturn(request);
template.getRestTemplate().setRequestFactory(requestFactory);
template.exchange(entity, String.class);
verify(requestFactory).createRequest(eq(absoluteUri), eq(HttpMethod.GET));
}

@Test
void deleteHandlesRelativeUris() throws IOException {
verifyRelativeUriHandling(TestRestTemplate::delete);
Expand Down

0 comments on commit 297e307

Please sign in to comment.