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.
Merge pull request #23 from SmartBear/explore-646-limit-response-size
feat: limit response size
- Loading branch information
Showing
8 changed files
with
121 additions
and
17 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
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
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
86 changes: 86 additions & 0 deletions
86
...ok-httpclient5/src/test/java/org/zalando/logbook/httpclient5/LimitedResponseSizeTest.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,86 @@ | ||
package org.zalando.logbook.httpclient5; | ||
|
||
import com.github.restdriver.clientdriver.ClientDriver; | ||
import com.github.restdriver.clientdriver.ClientDriverFactory; | ||
import org.apache.hc.client5.http.classic.methods.HttpGet; | ||
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; | ||
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse; | ||
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder; | ||
import org.apache.hc.core5.http.ParseException; | ||
import org.apache.hc.core5.http.io.entity.EntityUtils; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.ArgumentCaptor; | ||
import org.zalando.logbook.Correlation; | ||
import org.zalando.logbook.DefaultHttpLogFormatter; | ||
import org.zalando.logbook.DefaultSink; | ||
import org.zalando.logbook.HttpLogWriter; | ||
import org.zalando.logbook.Logbook; | ||
import org.zalando.logbook.TestStrategy; | ||
|
||
import java.io.IOException; | ||
|
||
import static com.github.restdriver.clientdriver.ClientDriverRequest.Method.GET; | ||
import static com.github.restdriver.clientdriver.RestClientDriver.giveResponse; | ||
import static com.github.restdriver.clientdriver.RestClientDriver.onRequestTo; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class LimitedResponseSizeTest { | ||
|
||
private static final int MAX_RESPONSE_SIZE = 10; | ||
final ClientDriver driver = new ClientDriverFactory().createClientDriver(); | ||
|
||
final HttpLogWriter writer = mock(HttpLogWriter.class); | ||
|
||
protected final Logbook logbook = Logbook.builder() | ||
.strategy(new TestStrategy()) | ||
.sink(new DefaultSink(new DefaultHttpLogFormatter(), writer)) | ||
.build(); | ||
|
||
private final CloseableHttpClient client = HttpClientBuilder.create() | ||
.addRequestInterceptorFirst(new LogbookHttpRequestInterceptor(logbook)) | ||
.addResponseInterceptorFirst(new LogbookHttpResponseInterceptor(MAX_RESPONSE_SIZE)) | ||
.build(); | ||
|
||
@BeforeEach | ||
void defaultBehaviour() { | ||
when(writer.isActive()).thenReturn(true); | ||
} | ||
|
||
@AfterEach | ||
void stop() throws IOException { | ||
client.close(); | ||
} | ||
|
||
@Test | ||
void shouldLogRequestWithoutBody() throws IOException, ParseException { | ||
driver.addExpectation(onRequestTo("/").withMethod(GET), | ||
giveResponse("Hello, world!", "text/plain")); | ||
|
||
final HttpGet request = new HttpGet(driver.getBaseUrl()); | ||
|
||
final CloseableHttpResponse response = client.execute(request); | ||
|
||
assertThat(response.getCode()).isEqualTo(200); | ||
assertThat(response.getEntity()).isNotNull(); | ||
final String cappedResponse = "Hello, world!".substring(0, 10); | ||
assertThat(EntityUtils.toString(response.getEntity())).isEqualTo(cappedResponse); | ||
|
||
final String message = captureResponse(); | ||
|
||
assertThat(message) | ||
.startsWith("Incoming Response:") | ||
.contains("HTTP/1.1 200 OK", "Content-Type: text/plain", cappedResponse); | ||
} | ||
|
||
private String captureResponse() throws IOException { | ||
final ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); | ||
verify(writer).write(any(Correlation.class), captor.capture()); | ||
return captor.getValue(); | ||
} | ||
} |
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