RestTemplate provides patchForObject operations
Issue: SPR-14857
This commit is contained in:
@@ -154,6 +154,15 @@ public class RestTemplateIntegrationTests extends AbstractMockWebServerTestCase
|
||||
assertEquals("Invalid content", helloWorld, s);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void patchForObject() throws URISyntaxException {
|
||||
// JDK client does not support the PATCH method
|
||||
Assume.assumeThat(this.clientHttpRequestFactory,
|
||||
Matchers.not(Matchers.instanceOf(SimpleClientHttpRequestFactory.class)));
|
||||
String s = template.patchForObject(baseUrl + "/{method}", helloWorld, String.class, "patch");
|
||||
assertEquals("Invalid content", helloWorld, s);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void notFound() {
|
||||
try {
|
||||
|
||||
@@ -67,6 +67,7 @@ public class RestTemplateTests {
|
||||
@SuppressWarnings("rawtypes")
|
||||
private HttpMessageConverter converter;
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
requestFactory = mock(ClientHttpRequestFactory.class);
|
||||
@@ -79,6 +80,7 @@ public class RestTemplateTests {
|
||||
template.setErrorHandler(errorHandler);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void varArgsTemplateVariables() throws Exception {
|
||||
given(requestFactory.createRequest(new URI("http://example.com/hotels/42/bookings/21"), HttpMethod.GET))
|
||||
@@ -590,6 +592,69 @@ public class RestTemplateTests {
|
||||
verify(response).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void patchForObject() throws Exception {
|
||||
MediaType textPlain = new MediaType("text", "plain");
|
||||
given(converter.canRead(Integer.class, null)).willReturn(true);
|
||||
given(converter.getSupportedMediaTypes()).willReturn(Collections.singletonList(textPlain));
|
||||
given(requestFactory.createRequest(new URI("http://example.com"), HttpMethod.PATCH)).willReturn(this.request);
|
||||
HttpHeaders requestHeaders = new HttpHeaders();
|
||||
given(this.request.getHeaders()).willReturn(requestHeaders);
|
||||
String request = "Hello World";
|
||||
given(converter.canWrite(String.class, null)).willReturn(true);
|
||||
converter.write(request, null, this.request);
|
||||
given(this.request.execute()).willReturn(response);
|
||||
given(errorHandler.hasError(response)).willReturn(false);
|
||||
Integer expected = 42;
|
||||
HttpHeaders responseHeaders = new HttpHeaders();
|
||||
responseHeaders.setContentType(textPlain);
|
||||
responseHeaders.setContentLength(10);
|
||||
given(response.getStatusCode()).willReturn(HttpStatus.OK);
|
||||
given(response.getHeaders()).willReturn(responseHeaders);
|
||||
given(response.getBody()).willReturn(new ByteArrayInputStream(expected.toString().getBytes()));
|
||||
given(converter.canRead(Integer.class, textPlain)).willReturn(true);
|
||||
given(converter.read(eq(Integer.class), any(HttpInputMessage.class))).willReturn(expected);
|
||||
HttpStatus status = HttpStatus.OK;
|
||||
given(response.getStatusCode()).willReturn(status);
|
||||
given(response.getStatusText()).willReturn(status.getReasonPhrase());
|
||||
|
||||
Integer result = template.patchForObject("http://example.com", request, Integer.class);
|
||||
assertEquals("Invalid POST result", expected, result);
|
||||
assertEquals("Invalid Accept header", textPlain.toString(), requestHeaders.getFirst("Accept"));
|
||||
|
||||
verify(response).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void patchForObjectNull() throws Exception {
|
||||
MediaType textPlain = new MediaType("text", "plain");
|
||||
given(converter.canRead(Integer.class, null)).willReturn(true);
|
||||
given(converter.getSupportedMediaTypes()).willReturn(Collections.singletonList(textPlain));
|
||||
given(requestFactory.createRequest(new URI("http://example.com"), HttpMethod.PATCH)).willReturn(request);
|
||||
HttpHeaders requestHeaders = new HttpHeaders();
|
||||
given(request.getHeaders()).willReturn(requestHeaders);
|
||||
given(request.execute()).willReturn(response);
|
||||
given(errorHandler.hasError(response)).willReturn(false);
|
||||
HttpHeaders responseHeaders = new HttpHeaders();
|
||||
responseHeaders.setContentType(textPlain);
|
||||
responseHeaders.setContentLength(10);
|
||||
given(response.getStatusCode()).willReturn(HttpStatus.OK);
|
||||
given(response.getHeaders()).willReturn(responseHeaders);
|
||||
given(converter.canRead(Integer.class, textPlain)).willReturn(true);
|
||||
given(converter.read(Integer.class, response)).willReturn(null);
|
||||
HttpStatus status = HttpStatus.OK;
|
||||
given(response.getStatusCode()).willReturn(status);
|
||||
given(response.getStatusText()).willReturn(status.getReasonPhrase());
|
||||
|
||||
Integer result = template.patchForObject("http://example.com", null, Integer.class);
|
||||
assertNull("Invalid POST result", result);
|
||||
assertEquals("Invalid content length", 0, requestHeaders.getContentLength());
|
||||
|
||||
verify(response).close();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
public void delete() throws Exception {
|
||||
given(requestFactory.createRequest(new URI("http://example.com"), HttpMethod.DELETE)).willReturn(request);
|
||||
@@ -730,4 +795,5 @@ public class RestTemplateTests {
|
||||
|
||||
verify(response).close();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user