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

Attempt to write a response log when flushBuffer() fails #1997

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
Expand Up @@ -85,7 +85,11 @@ public void doFilter(final HttpServletRequest httpRequest, final HttpServletResp
private void write(RemoteRequest request, LocalResponse response, ResponseWritingStage writing) throws IOException {
final AtomicBoolean attribute = (AtomicBoolean) request.getAttribute(responseWritingStageSynchronizationName);
if (!attribute.getAndSet(true)) {
response.flushBuffer();
try {
response.flushBuffer();
} catch (IOException e) {
// ignore and try to log the response anyway
}
writing.write();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
package org.zalando.logbook.servlet;

import io.undertow.servlet.util.EmptyEnumeration;
import jakarta.servlet.DispatcherType;
import jakarta.servlet.FilterChain;
import jakarta.servlet.FilterConfig;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.junit.jupiter.api.Test;
import org.zalando.logbook.Logbook;

import java.io.IOException;
import java.util.concurrent.atomic.AtomicBoolean;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

final class LogbookFilterTest {

Expand All @@ -27,4 +40,28 @@ void shouldCallDestroy() {
new LogbookFilter().destroy();
}

@Test
void shouldHandleIOExceptionOnFlushBufferAndWriteResponse() throws Exception {
Logbook logbook = mock(Logbook.class);
Logbook.RequestWritingStage requestWritingStage = mock(Logbook.RequestWritingStage.class);
Logbook.ResponseWritingStage responseWritingStage = mock(Logbook.ResponseWritingStage .class);
LogbookFilter filter = new LogbookFilter(logbook);
HttpServletRequest request = mock(HttpServletRequest.class);
HttpServletResponse response = mock(HttpServletResponse.class);
FilterChain chain = mock(FilterChain.class);

when(logbook.process(any())).thenReturn(requestWritingStage);
when(requestWritingStage.write()).thenReturn(requestWritingStage);
when(requestWritingStage.process(any())).thenReturn(responseWritingStage);
when(request.getHeaderNames()).thenReturn(EmptyEnumeration.instance());
when(request.getDispatcherType()).thenReturn(DispatcherType.REQUEST);
when(request.getAttribute(any())).thenReturn(new AtomicBoolean(false));

doThrow(new IOException("Simulated IOException")).when(response).flushBuffer();

filter.doFilter(request, response, chain);

verify(responseWritingStage).write();
}

}
Loading