-
Notifications
You must be signed in to change notification settings - Fork 264
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #142
- Loading branch information
Willi Schönborn
committed
Mar 20, 2017
1 parent
5b1585e
commit 6aa5d33
Showing
6 changed files
with
189 additions
and
13 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
42 changes: 42 additions & 0 deletions
42
logbook-core/src/main/java/org/zalando/logbook/ChunkingHttpLogWriter.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,42 @@ | ||
package org.zalando.logbook; | ||
|
||
import org.zalando.logbook.DefaultLogbook.SimpleCorrelation; | ||
import org.zalando.logbook.DefaultLogbook.SimplePrecorrelation; | ||
|
||
import java.io.IOException; | ||
import java.util.regex.Pattern; | ||
|
||
public final class ChunkingHttpLogWriter implements HttpLogWriter { | ||
|
||
private final Pattern pattern; | ||
private final HttpLogWriter writer; | ||
|
||
public ChunkingHttpLogWriter(final int size, final HttpLogWriter writer) { | ||
this.pattern = Pattern.compile("(?<=\\G.{" + size + "})"); | ||
this.writer = writer; | ||
} | ||
|
||
@Override | ||
public boolean isActive(final RawHttpRequest request) throws IOException { | ||
return writer.isActive(request); | ||
} | ||
|
||
@Override | ||
public void writeRequest(final Precorrelation<String> precorrelation) throws IOException { | ||
for (final String part : split(precorrelation.getRequest())) { | ||
writer.writeRequest(new SimplePrecorrelation<>(precorrelation.getId(), part)); | ||
} | ||
} | ||
|
||
@Override | ||
public void writeResponse(final Correlation<String, String> correlation) throws IOException { | ||
for (final String part : split(correlation.getResponse())) { | ||
writer.writeResponse(new SimpleCorrelation<>(correlation.getId(), correlation.getRequest(), part)); | ||
} | ||
} | ||
|
||
private String[] split(final String s) { | ||
return pattern.split(s); | ||
} | ||
|
||
} |
87 changes: 87 additions & 0 deletions
87
logbook-core/src/test/java/org/zalando/logbook/ChunkingHttpLogWriterTest.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,87 @@ | ||
package org.zalando.logbook; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.ArgumentCaptor; | ||
import org.mockito.Captor; | ||
import org.mockito.runners.MockitoJUnitRunner; | ||
import org.zalando.logbook.DefaultLogbook.SimplePrecorrelation; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
import static java.util.stream.Collectors.toList; | ||
import static org.hamcrest.Matchers.contains; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.junit.Assert.assertThat; | ||
import static org.mockito.Mockito.atLeastOnce; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public final class ChunkingHttpLogWriterTest { | ||
|
||
private final HttpLogWriter delegate = mock(HttpLogWriter.class); | ||
private final HttpLogWriter unit = new ChunkingHttpLogWriter(10, delegate); | ||
|
||
@Captor | ||
private ArgumentCaptor<Precorrelation<String>> requestCaptor; | ||
|
||
@Captor | ||
private ArgumentCaptor<Correlation<String, String>> responseCaptor; | ||
|
||
@Test | ||
public void shouldDelegateActive() throws IOException { | ||
final RawHttpRequest request = mock(RawHttpRequest.class); | ||
assertThat(unit.isActive(request), is(false)); | ||
} | ||
|
||
@Test | ||
public void shouldWriteSingleRequestIfLengthNotExceeded() throws IOException { | ||
final List<String> precorrelation = captureRequest("Hello"); | ||
assertThat(precorrelation, contains("Hello")); | ||
} | ||
|
||
@Test | ||
public void shouldWriteRequestInChunksIfLengthExceeded() throws IOException { | ||
final List<String> precorrelation = captureRequest("Lorem ipsum dolor sit amet, consectetur adipiscing elit"); | ||
assertThat(precorrelation, | ||
contains("Lorem ipsu", "m dolor si", "t amet, co", "nsectetur ", "adipiscing", " elit")); | ||
} | ||
|
||
private List<String> captureRequest(final String request) throws IOException { | ||
unit.writeRequest(new SimplePrecorrelation<>("id", request)); | ||
|
||
verify(delegate, atLeastOnce()).writeRequest(requestCaptor.capture()); | ||
|
||
return requestCaptor.getAllValues().stream() | ||
.map(Precorrelation::getRequest) | ||
.collect(toList()); | ||
} | ||
|
||
@Test | ||
public void shouldWriteSingleResponseIfLengthNotExceeded() throws IOException { | ||
final List<String> precorrelation = captureResponse("Hello"); | ||
assertThat(precorrelation, contains("Hello")); | ||
|
||
} | ||
|
||
@Test | ||
public void shouldWriteResponseInChunksIfLengthExceeded() throws IOException { | ||
final List<String> precorrelation = captureResponse("Lorem ipsum dolor sit amet, consectetur adipiscing elit"); | ||
assertThat(precorrelation, | ||
contains("Lorem ipsu", "m dolor si", "t amet, co", "nsectetur ", "adipiscing", " elit")); | ||
} | ||
|
||
private List<String> captureResponse(final String response) throws IOException { | ||
unit.writeResponse(new DefaultLogbook.SimpleCorrelation<>("id", "", response)); | ||
|
||
verify(delegate, atLeastOnce()).writeResponse(responseCaptor.capture()); | ||
|
||
return responseCaptor.getAllValues().stream() | ||
.map(Correlation::getResponse) | ||
.collect(toList()); | ||
} | ||
|
||
|
||
} |
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
28 changes: 28 additions & 0 deletions
28
logbook-spring-boot-starter/src/test/java/org/zalando/logbook/spring/WriteChunkingTest.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,28 @@ | ||
package org.zalando.logbook.spring; | ||
|
||
import org.junit.Test; | ||
import org.slf4j.Logger; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.test.context.TestPropertySource; | ||
import org.zalando.logbook.ChunkingHttpLogWriter; | ||
import org.zalando.logbook.HttpLogWriter; | ||
|
||
import java.io.IOException; | ||
|
||
import static org.hamcrest.Matchers.instanceOf; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.hobsoft.hamcrest.compose.ComposeMatchers.hasFeature; | ||
import static org.junit.Assert.assertThat; | ||
|
||
@TestPropertySource(properties = "logbook.write.chunk-size = 100") | ||
public final class WriteChunkingTest extends AbstractTest { | ||
|
||
@Autowired | ||
private HttpLogWriter writer; | ||
|
||
@Test | ||
public void shouldUseChunkingWriter() throws IOException { | ||
assertThat(writer, is(instanceOf(ChunkingHttpLogWriter.class))); | ||
} | ||
|
||
} |