forked from zalando/logbook
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wrap client http response body in buffered input stream to support ma…
…rk/reset (zalando#963) (zalando#1041)
- Loading branch information
Showing
6 changed files
with
208 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
...k-spring/src/main/java/org/zalando/logbook/spring/BufferingClientHttpResponseWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package org.zalando.logbook.spring; | ||
|
||
import java.io.BufferedInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.client.ClientHttpResponse; | ||
|
||
public class BufferingClientHttpResponseWrapper implements ClientHttpResponse { | ||
|
||
private final ClientHttpResponse delegate; | ||
private final InputStream body; | ||
|
||
public BufferingClientHttpResponseWrapper(ClientHttpResponse delegate) throws IOException { | ||
this.delegate = delegate; | ||
final InputStream delegateBody = delegate.getBody(); | ||
this.body = delegateBody.markSupported() ? delegateBody : new BufferedInputStream(delegateBody); | ||
} | ||
|
||
@Override | ||
public HttpStatus getStatusCode() throws IOException { | ||
return delegate.getStatusCode(); | ||
} | ||
|
||
@Override | ||
public int getRawStatusCode() throws IOException { | ||
return delegate.getRawStatusCode(); | ||
} | ||
|
||
@Override | ||
public String getStatusText() throws IOException { | ||
return delegate.getStatusText(); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
try { | ||
body.close(); | ||
} catch (IOException e){ | ||
throw new RuntimeException(e); | ||
} | ||
delegate.close(); | ||
} | ||
|
||
@Override | ||
public InputStream getBody() { | ||
return body; | ||
} | ||
|
||
@Override | ||
public HttpHeaders getHeaders() { | ||
return delegate.getHeaders(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
...ring/src/test/java/org/zalando/logbook/spring/BufferingClientHttpResponseWrapperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package org.zalando.logbook.spring; | ||
|
||
import java.io.BufferedInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.client.ClientHttpResponse; | ||
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.mockito.Mockito.doThrow; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class BufferingClientHttpResponseWrapperTest { | ||
|
||
@Mock | ||
private ClientHttpResponse delegate; | ||
|
||
@Mock | ||
private InputStream inputStream; | ||
|
||
private BufferingClientHttpResponseWrapper wrapper; | ||
|
||
@BeforeEach | ||
void setUp() throws IOException { | ||
when(delegate.getBody()).thenReturn(inputStream); | ||
wrapper = new BufferingClientHttpResponseWrapper(delegate); | ||
} | ||
|
||
@Test | ||
void wrapBodyInBufferedInputStreamWhenMarkNotSupported() throws IOException { | ||
when(inputStream.markSupported()).thenReturn(false); | ||
|
||
assertTrue(new BufferingClientHttpResponseWrapper(delegate).getBody() instanceof BufferedInputStream); | ||
} | ||
|
||
@Test | ||
void dontWrapBodyInBufferedInputStreamWhenMarkSupported() throws IOException { | ||
when(inputStream.markSupported()).thenReturn(true); | ||
|
||
assertEquals(inputStream, new BufferingClientHttpResponseWrapper(delegate).getBody()); | ||
} | ||
|
||
@Test | ||
void getStatusCode() throws IOException { | ||
when(delegate.getStatusCode()).thenReturn(HttpStatus.OK); | ||
|
||
assertEquals(HttpStatus.OK, wrapper.getStatusCode()); | ||
} | ||
|
||
@Test | ||
void getRawStatusCode() throws IOException { | ||
when(delegate.getRawStatusCode()).thenReturn(200); | ||
|
||
assertEquals(200, wrapper.getRawStatusCode()); | ||
} | ||
|
||
@Test | ||
void getStatusText() throws IOException { | ||
when(delegate.getStatusText()).thenReturn("OK"); | ||
|
||
assertEquals("OK", wrapper.getStatusText()); | ||
} | ||
|
||
@Test | ||
void close() { | ||
wrapper.close(); | ||
verify(delegate).close(); | ||
} | ||
|
||
@Test | ||
void close_throws() throws IOException { | ||
doThrow(new IOException()).when(inputStream).close(); | ||
|
||
assertThrows(RuntimeException.class, () -> wrapper.close()); | ||
} | ||
|
||
@Test | ||
void getBody() { | ||
assertTrue(wrapper.getBody().markSupported()); | ||
} | ||
|
||
@Test | ||
void getHeaders() { | ||
final HttpHeaders httpHeaders = new HttpHeaders(); | ||
when(delegate.getHeaders()).thenReturn(httpHeaders); | ||
|
||
assertEquals(httpHeaders, wrapper.getHeaders()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters