Skip to content

Commit

Permalink
[ANCHOR-410] Refactored the RequestLoggingFilterTest to stabilize the…
Browse files Browse the repository at this point in the history
… Gradle test (#1094)

### Description
Refactored the test to stabilize the test.
  • Loading branch information
lijamie98 authored Sep 6, 2023
2 parents 4aa31af + b79750b commit 6442a92
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jetbrains.annotations.NotNull;
import org.springframework.web.filter.OncePerRequestFilter;
import org.springframework.web.util.ContentCachingRequestWrapper;
import org.springframework.web.util.ContentCachingResponseWrapper;
Expand All @@ -26,16 +27,9 @@ public RequestLoggerFilter(AppLoggingConfig appLoggingConfig) {
this.appLoggingConfig = appLoggingConfig;
}

@Override
protected void doFilterInternal(
public void doFilterWithLogging(
HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {

if (!appLoggingConfig.isRequestLoggerEnabled()) {
filterChain.doFilter(request, response);
return;
}

throws IOException, ServletException {
long startTime = System.currentTimeMillis();

// ========= Log request and response payload ("body") ========
Expand Down Expand Up @@ -77,6 +71,21 @@ protected void doFilterInternal(
wrappedResponse.copyBodyToResponse();
}

@Override
protected void doFilterInternal(
@NotNull HttpServletRequest request,
@NotNull HttpServletResponse response,
@NotNull FilterChain filterChain)
throws ServletException, IOException {

if (!appLoggingConfig.isRequestLoggerEnabled()) {
filterChain.doFilter(request, response);
return;
}

this.doFilterWithLogging(request, response, filterChain);
}

/**
* getBody will get the response body (if it's an error) or omit it if it's not an error.
*
Expand All @@ -99,7 +108,7 @@ private String getBody(ContentCachingResponseWrapper wrappedResponse) {
}
}

public static String getClientIpAddress(HttpServletRequest request) {
private static String getClientIpAddress(HttpServletRequest request) {
final String[] IP_HEADER_CANDIDATES = {
"X-Forwarded-For",
"X-Real-IP",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,19 @@ import org.junit.jupiter.api.Test
import org.springframework.mock.web.MockHttpServletRequest
import org.springframework.mock.web.MockHttpServletResponse
import org.stellar.anchor.platform.config.AppLoggingConfig
import org.stellar.anchor.util.Log

class RequestLoggerFilterTest {
private lateinit var appLoggingConfig: AppLoggingConfig
private lateinit var requestLoggerFilter: RequestLoggerFilter
private lateinit var mockConfig: AppLoggingConfig
private lateinit var request: MockHttpServletRequest
private lateinit var response: MockHttpServletResponse
private lateinit var filterChain: FilterChain

@BeforeEach
fun setUp() {
appLoggingConfig = AppLoggingConfig()
requestLoggerFilter = RequestLoggerFilter(appLoggingConfig)
mockConfig = mockk<AppLoggingConfig>(relaxed = true)
request = MockHttpServletRequest()
response = MockHttpServletResponse()
filterChain = mockk<FilterChain>(relaxed = true)
mockkStatic(Log::class)
}

@AfterEach
Expand All @@ -34,30 +30,30 @@ class RequestLoggerFilterTest {
}

@Test
fun `doFilterInternal when RequestLogger disabled should not send debug and trace logs`() {
// Arrange
appLoggingConfig.isRequestLoggerEnabled = false
fun `test when disabled the doFilterWithLogging() is not called`() {
// arrange
every { mockConfig.isRequestLoggerEnabled } returns false
val a = spyk(RequestLoggerFilter(mockConfig))

// Act
requestLoggerFilter.doFilterInternal(request, response, filterChain)
// act
a.doFilterInternal(request, response, filterChain)

// Assert
// assert
verify(exactly = 1) { filterChain.doFilter(any(), any()) }
verify(exactly = 0) { Log.debugF(any(), *varargAny { true }) }
verify(exactly = 0) { Log.trace(any()) }
verify(exactly = 0) { a.doFilterWithLogging(any(), any(), any()) }
}

@Test
fun `doFilterInternal when RequestLogger enabled should send debug and trace logs`() {
// Arrange
appLoggingConfig.isRequestLoggerEnabled = true
fun `test when enabled the doFilterWithLogging() is called`() {
// arrange
every { mockConfig.isRequestLoggerEnabled } returns true
val a = spyk(RequestLoggerFilter(mockConfig))

// Act
requestLoggerFilter.doFilterInternal(request, response, filterChain)
// act
a.doFilterInternal(request, response, filterChain)

// Assert
// assert
verify(exactly = 1) { filterChain.doFilter(any(), any()) }
verify(exactly = 1) { Log.debugF(any(), *varargAny { true }) }
verify(exactly = 1) { Log.trace(any()) }
verify(exactly = 1) { a.doFilterWithLogging(any(), any(), any()) }
}
}

0 comments on commit 6442a92

Please sign in to comment.