Skip to content

Commit

Permalink
Fix empty response body when using Spring RestTemplate with LogbookCl…
Browse files Browse the repository at this point in the history
…ientHttpRequestInterceptor (#948)

* Add test which ensures that response body is not empty. This test is currently failing.

* Adapt tests to ensure the support of empty and non-empty GET responses.

* Fixed null response body by resetting response body stream when copying for caching.

Co-authored-by: Thomas Wimmer <thomas.wimmer@netcetera.com>
  • Loading branch information
Thomas Wimmer and Thomas Wimmer authored Feb 8, 2021
1 parent 70037b8 commit 21b3736
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import javax.annotation.Nullable;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;
Expand Down Expand Up @@ -60,8 +61,9 @@ public State without() {

@Override
public State buffer(final ClientHttpResponse response) throws IOException {
response.getBody();
byte[] data = ByteStreams.toByteArray(response.getBody());
InputStream responseBodyStream = response.getBody();
byte[] data = ByteStreams.toByteArray(responseBodyStream);
responseBodyStream.reset();
return new Buffering(data);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.client.ExpectedCount.once;
Expand Down Expand Up @@ -79,7 +80,7 @@ void mockHttpVerify() {
void get200() throws IOException {
serviceServer.expect(once(), requestTo("/test/get")).andExpect(method(HttpMethod.GET))
.andRespond(withSuccess().body("response"));
restTemplate.getForObject("/test/get", Void.class);
restTemplate.getForObject("/test/get", String.class);

verify(writer).write(precorrelationCaptor.capture(), requestCaptor.capture());
verify(writer).write(correlationCaptor.capture(), responseCaptor.capture());
Expand All @@ -95,6 +96,26 @@ void get200() throws IOException {
assertTrue(responseCaptor.getValue().contains("response"));
}

@Test
void get200WithEmptyResponseBody(){
serviceServer.expect(once(), requestTo("/test/get")).andExpect(method(HttpMethod.GET))
.andRespond(withSuccess());

restTemplate.getForObject("/test/get", Void.class);
}

@Test
void get200WithNonEmptyResponseBody() {
String expectedResponseBody = "response";
serviceServer.expect(once(), requestTo("/test/get")).andExpect(method(HttpMethod.GET))
.andRespond(withSuccess().body(expectedResponseBody));

String actualResponseBody = restTemplate.getForObject("/test/get", String.class);

assertNotNull(actualResponseBody);
assertEquals(expectedResponseBody, actualResponseBody);
}

@Test
void post400() throws IOException {
serviceServer.expect(once(), requestTo("/test/post")).andExpect(method(HttpMethod.POST))
Expand Down

0 comments on commit 21b3736

Please sign in to comment.