Merge pull request #1504 from bbohl/stringindexoutofbounds_resttemplate_fix

This commit is contained in:
Rossen Stoyanchev 2017-09-01 16:58:09 -04:00
commit edb70b4f55
2 changed files with 37 additions and 6 deletions

View File

@ -708,7 +708,7 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
catch (IOException ex) {
String resource = url.toString();
String query = url.getRawQuery();
resource = (query != null ? resource.substring(0, resource.indexOf(query) - 1) : resource);
resource = (query != null ? resource.substring(0, resource.indexOf('?')) : resource);
throw new ResourceAccessException("I/O error on " + method.name() +
" request for \"" + resource + "\": " + ex.getMessage(), ex);
}

View File

@ -45,8 +45,18 @@ import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.util.StreamUtils;
import org.springframework.web.util.DefaultUriBuilderFactory;
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
@ -706,9 +716,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";
@ -730,6 +738,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);