Skip to content

Commit

Permalink
add isolated unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
kasmarian committed Jan 6, 2025
1 parent ab06454 commit 12c63a7
Showing 1 changed file with 37 additions and 0 deletions.
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();
}

}

0 comments on commit 12c63a7

Please sign in to comment.