Skip to content

Commit edb70b4

Browse files
committed
Merge pull request #1504 from bbohl/stringindexoutofbounds_resttemplate_fix
2 parents 26284ca + 3d61f7b commit edb70b4

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

spring-web/src/main/java/org/springframework/web/client/RestTemplate.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,7 @@ protected <T> T doExecute(URI url, @Nullable HttpMethod method, @Nullable Reques
708708
catch (IOException ex) {
709709
String resource = url.toString();
710710
String query = url.getRawQuery();
711-
resource = (query != null ? resource.substring(0, resource.indexOf(query) - 1) : resource);
711+
resource = (query != null ? resource.substring(0, resource.indexOf('?')) : resource);
712712
throw new ResourceAccessException("I/O error on " + method.name() +
713713
" request for \"" + resource + "\": " + ex.getMessage(), ex);
714714
}

spring-web/src/test/java/org/springframework/web/client/RestTemplateTests.java

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,18 @@
4545
import org.springframework.util.StreamUtils;
4646
import org.springframework.web.util.DefaultUriBuilderFactory;
4747

48-
import static org.junit.Assert.*;
49-
import static org.mockito.BDDMockito.*;
48+
import static org.junit.Assert.assertEquals;
49+
import static org.junit.Assert.assertFalse;
50+
import static org.junit.Assert.assertNull;
51+
import static org.junit.Assert.assertSame;
52+
import static org.junit.Assert.fail;
53+
import static org.mockito.BDDMockito.any;
54+
import static org.mockito.BDDMockito.eq;
55+
import static org.mockito.BDDMockito.given;
56+
import static org.mockito.BDDMockito.mock;
57+
import static org.mockito.BDDMockito.verify;
58+
import static org.mockito.BDDMockito.willThrow;
59+
import static org.springframework.http.MediaType.parseMediaType;
5060

5161
/**
5262
* @author Arjen Poutsma
@@ -706,9 +716,7 @@ public void optionsForAllow() throws Exception {
706716
verify(response).close();
707717
}
708718

709-
// Issue: SPR-9325, SPR-13860
710-
711-
@Test
719+
@Test // Issue: SPR-9325, SPR-13860
712720
public void ioException() throws Exception {
713721
String url = "http://example.com/resource?access_token=123";
714722

@@ -730,6 +738,29 @@ public void ioException() throws Exception {
730738
}
731739
}
732740

741+
@Test // SPR-15900
742+
public void ioExceptionWithEmptyQueryString() throws Exception {
743+
744+
// http://example.com/resource?
745+
URI uri = new URI("http", "example.com", "/resource", "", null);
746+
747+
given(converter.canRead(String.class, null)).willReturn(true);
748+
given(converter.getSupportedMediaTypes()).willReturn(Collections.singletonList(parseMediaType("foo/bar")));
749+
given(requestFactory.createRequest(uri, HttpMethod.GET)).willReturn(request);
750+
given(request.getHeaders()).willReturn(new HttpHeaders());
751+
given(request.execute()).willThrow(new IOException("Socket failure"));
752+
753+
try {
754+
template.getForObject(uri, String.class);
755+
fail("RestClientException expected");
756+
}
757+
catch (ResourceAccessException ex) {
758+
assertEquals("I/O error on GET request for \"http://example.com/resource\": " +
759+
"Socket failure; nested exception is java.io.IOException: Socket failure",
760+
ex.getMessage());
761+
}
762+
}
763+
733764
@Test
734765
public void exchange() throws Exception {
735766
given(converter.canRead(Integer.class, null)).willReturn(true);

0 commit comments

Comments
 (0)