Skip to content

Commit

Permalink
Merge pull request #1909 from zalando/fix-jaxrs-issue
Browse files Browse the repository at this point in the history
Fix JAX-RS filter NPE for empty entities
  • Loading branch information
lukasniemeier-zalando authored Nov 5, 2024
2 parents 15b8de2 + 511bdb0 commit f2183b4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.zalando.logbook.jaxrs;

import javax.annotation.Nullable;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
Expand All @@ -12,31 +13,35 @@ final class TeeOutputStream extends OutputStream {
private final OutputStream original;
private final ByteArrayOutputStream copy = new ByteArrayOutputStream();

TeeOutputStream(final OutputStream original) {
TeeOutputStream(@Nullable final OutputStream original) {
this.original = original;
}

@Override
public void write(final int b) throws IOException {
original.write(b);
if (original != null)
original.write(b);
copy.write(b);
}

@Override
public void write(final byte[] b, final int off, final int len) throws IOException {
original.write(b, off, len);
if (original != null)
original.write(b, off, len);
copy.write(b, off, len);
}

@Override
public void flush() throws IOException {
original.flush();
if (original != null)
original.flush();
copy.flush();
}

@Override
public void close() throws IOException {
original.close();
if (original != null)
original.close();
copy.close();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,56 @@
import java.io.IOException;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

class TeeOutputStreamTest {

private final ByteArrayOutputStream output = new ByteArrayOutputStream();
private final TeeOutputStream unit = new TeeOutputStream(output);
private final ByteArrayOutputStream output = spy(new ByteArrayOutputStream());
private final TeeOutputStream unit1 = new TeeOutputStream(output);
private final TeeOutputStream unit2 = new TeeOutputStream(null);

@Test
void shouldWriteByte() throws IOException {
unit.write(17);
unit1.write(17);
unit2.write(17);

assertArrayEquals(unit.toByteArray(), output.toByteArray());
assertArrayEquals(unit1.toByteArray(), output.toByteArray());
assertArrayEquals(unit2.toByteArray(), output.toByteArray());
}

@Test
void shouldWriteBytesWithoutOffsets() throws IOException {
unit.write(new byte[]{17});
unit1.write(new byte[]{17});
unit2.write(new byte[]{17});

assertArrayEquals(unit.toByteArray(), output.toByteArray());
assertArrayEquals(unit1.toByteArray(), output.toByteArray());
assertArrayEquals(unit2.toByteArray(), output.toByteArray());

}

@Test
void shouldWriteBytesWithOffsets() throws IOException {
unit.write(new byte[]{17}, 0, 1);
unit1.write(new byte[]{17}, 0, 1);
unit2.write(new byte[]{17}, 0, 1);

assertArrayEquals(unit.toByteArray(), output.toByteArray());
assertArrayEquals(unit1.toByteArray(), output.toByteArray());
assertArrayEquals(unit2.toByteArray(), output.toByteArray());

}

@Test
void shouldFlushAndClose() throws IOException {
unit1.flush();
verify(output, times(1)).flush();

unit1.close();
verify(output, times(1)).close();

// This is just added to make coverage 100%
unit2.flush();
unit2.close();
}

}

0 comments on commit f2183b4

Please sign in to comment.