-
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.
Added possibility to delay logging of requests until response can be …
…inspected. As a side effect a writer now has access to the request/response object in case any decisions need to be taken based on them. Fixes #165
- Loading branch information
Willi Schönborn
committed
Jun 29, 2017
1 parent
ce126b7
commit cc1ff03
Showing
20 changed files
with
160 additions
and
98 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
27 changes: 27 additions & 0 deletions
27
logbook-core/src/test/java/org/zalando/logbook/DelayedResponseLogWriter.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,27 @@ | ||
package org.zalando.logbook; | ||
|
||
import org.zalando.logbook.DefaultLogbook.SimplePrecorrelation; | ||
|
||
import java.io.IOException; | ||
|
||
// proof of concept | ||
final class DelayedResponseLogWriter implements HttpLogWriter { | ||
|
||
private final HttpLogWriter delegate; | ||
|
||
DelayedResponseLogWriter(final HttpLogWriter delegate) { | ||
this.delegate = delegate; | ||
} | ||
|
||
@Override | ||
public void write(final HttpRequest request, final HttpResponse response, | ||
final Correlation<String, String> correlation) throws IOException { | ||
|
||
if (response.getStatus() >= 400) { | ||
// delayed request logging until we have the response at hand | ||
delegate.write(request, new SimplePrecorrelation<>(correlation.getId(), correlation.getRequest())); | ||
delegate.write(request, response, correlation); | ||
} | ||
} | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
logbook-core/src/test/java/org/zalando/logbook/DelayedResponseLogWriterTest.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,31 @@ | ||
package org.zalando.logbook; | ||
|
||
import org.junit.Test; | ||
import org.mockito.Mockito; | ||
|
||
import java.io.IOException; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.never; | ||
import static org.mockito.Mockito.verify; | ||
|
||
public final class DelayedResponseLogWriterTest { | ||
|
||
private final HttpLogWriter delegate = Mockito.mock(HttpLogWriter.class); | ||
private final HttpLogWriter unit = new DelayedResponseLogWriter(delegate); | ||
|
||
@Test | ||
public void shouldDelayRequestLogging() throws IOException { | ||
final Logbook logbook = Logbook.builder().writer(unit).build(); | ||
|
||
final Correlator correlator = logbook.write(MockRawHttpRequest.create()).orElseThrow(AssertionError::new); | ||
|
||
verify(delegate, never()).write(any(), any()); | ||
|
||
correlator.write(MockRawHttpResponse.create().withStatus(404)); | ||
|
||
verify(delegate).write(any(), any()); | ||
verify(delegate).write(any(), any(), any()); | ||
} | ||
|
||
} |
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
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
Oops, something went wrong.