From 71e4be571f388dfee86deac463339a50e8877efe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Willi=20Sch=C3=B6nborn?= Date: Tue, 29 Sep 2020 14:15:27 +0200 Subject: [PATCH] Fixes chunking of empty bodies. Fixes #842 --- .../main/java/org/zalando/logbook/ChunkingSink.java | 4 ++++ .../java/org/zalando/logbook/ChunkingSinkTest.java | 11 ++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/logbook-core/src/main/java/org/zalando/logbook/ChunkingSink.java b/logbook-core/src/main/java/org/zalando/logbook/ChunkingSink.java index b5676309a..617786f22 100644 --- a/logbook-core/src/main/java/org/zalando/logbook/ChunkingSink.java +++ b/logbook-core/src/main/java/org/zalando/logbook/ChunkingSink.java @@ -48,6 +48,10 @@ public void write(final Correlation correlation, final HttpRequest request, fina } private Stream chunk(final HttpMessage message) throws IOException { + if (message.getBodyAsString().isEmpty()) { + return Stream.of(""); + } + return stream(new ChunkingSpliterator(message.getBodyAsString(), minChunkSize, maxChunkSize), false); } diff --git a/logbook-core/src/test/java/org/zalando/logbook/ChunkingSinkTest.java b/logbook-core/src/test/java/org/zalando/logbook/ChunkingSinkTest.java index 13bfe06dd..76ea51672 100644 --- a/logbook-core/src/test/java/org/zalando/logbook/ChunkingSinkTest.java +++ b/logbook-core/src/test/java/org/zalando/logbook/ChunkingSinkTest.java @@ -29,10 +29,19 @@ final class ChunkingSinkTest { private final Sink unit = new ChunkingSink(delegate, 20); @Test - void shouldDelegateActive() { + void delegatesActive() { assertThat(unit.isActive(), is(false)); } + @Test + void ignoresEmptyBodies() throws IOException { + final List strings = captureRequest(""); + + assertThat(strings, contains( + allOf(startsWith("Incoming Request")) + )); + } + @Test void shouldWriteSingleRequestIfLengthNotExceeded() throws IOException { final List chunks = captureRequest("HelloWorld");