Fix StringIndexOutOfBoundsException in RestTemplate

Backport for commits #81dfad and #3d61f7

Issue: SPR-15900
This commit is contained in:
Brian Bohl
2017-08-23 15:16:47 -05:00
committed by Rossen Stoyanchev
parent 681ced8fd3
commit f5d689e764
2 changed files with 37 additions and 6 deletions

View File

@@ -44,8 +44,18 @@ import org.springframework.http.converter.GenericHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.util.DefaultUriTemplateHandler;
import static org.junit.Assert.*;
import static org.mockito.BDDMockito.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.fail;
import static org.mockito.BDDMockito.any;
import static org.mockito.BDDMockito.eq;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.mock;
import static org.mockito.BDDMockito.verify;
import static org.mockito.BDDMockito.willThrow;
import static org.springframework.http.MediaType.parseMediaType;
/**
* @author Arjen Poutsma
@@ -701,9 +711,7 @@ public class RestTemplateTests {
verify(response).close();
}
// Issue: SPR-9325, SPR-13860
@Test
@Test // Issue: SPR-9325, SPR-13860
public void ioException() throws Exception {
String url = "http://example.com/resource?access_token=123";
@@ -725,6 +733,29 @@ public class RestTemplateTests {
}
}
@Test // SPR-15900
public void ioExceptionWithEmptyQueryString() throws Exception {
// http://example.com/resource?
URI uri = new URI("http", "example.com", "/resource", "", null);
given(converter.canRead(String.class, null)).willReturn(true);
given(converter.getSupportedMediaTypes()).willReturn(Collections.singletonList(parseMediaType("foo/bar")));
given(requestFactory.createRequest(uri, HttpMethod.GET)).willReturn(request);
given(request.getHeaders()).willReturn(new HttpHeaders());
given(request.execute()).willThrow(new IOException("Socket failure"));
try {
template.getForObject(uri, String.class);
fail("RestClientException expected");
}
catch (ResourceAccessException ex) {
assertEquals("I/O error on GET request for \"http://example.com/resource\": " +
"Socket failure; nested exception is java.io.IOException: Socket failure",
ex.getMessage());
}
}
@Test
public void exchange() throws Exception {
given(converter.canRead(Integer.class, null)).willReturn(true);