Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix JAX-RS filter NPE for empty entities #1909

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}

}
Loading