From 12c63a7b150552ea8396e628124a1f42a5476f4a Mon Sep 17 00:00:00 2001 From: Karen Asmarian Date: Mon, 6 Jan 2025 18:36:18 +0100 Subject: [PATCH] add isolated unit test --- .../logbook/servlet/LogbookFilterTest.java | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/logbook-servlet/src/test/java/org/zalando/logbook/servlet/LogbookFilterTest.java b/logbook-servlet/src/test/java/org/zalando/logbook/servlet/LogbookFilterTest.java index d2a6f81e3..ce42ed072 100644 --- a/logbook-servlet/src/test/java/org/zalando/logbook/servlet/LogbookFilterTest.java +++ b/logbook-servlet/src/test/java/org/zalando/logbook/servlet/LogbookFilterTest.java @@ -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 { @@ -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(); + } + }