Skip to content
Closed
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
73 changes: 73 additions & 0 deletions src/test/java/org/juv25d/filter/TimeoutFilterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package org.juv25d.filter;

import org.juv25d.http.HttpRequest;
import org.juv25d.http.HttpResponse;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
class TimeoutFilterTest {

@Mock HttpRequest req;
@Mock FilterChain chain;

@Test
void fastRequest_keepsDefault200() throws IOException {
when(req.path()).thenReturn("/fast");
HttpResponse res = new HttpResponse();
TimeoutFilter filter = new TimeoutFilter();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== Candidate timeout filter classes in main sources =="
fd -a --full-path 'src/main/java/.*Timeout.*Filter.*\.java$' || echo "(no matches)"

echo
echo "== Class declarations that look like timeout filters =="
rg -nP --type=java '^\s*(public\s+)?class\s+Timeout\w*Filter\b' src/main/java || echo "(no matches)"

echo
echo "== Package declarations for java files in src/main/java =="
rg -nP --type=java '^\s*package\s+org\.juv25d\..*;' src/main/java || echo "(no matches)"

echo
echo "== Test instantiations expecting TimeoutFilter =="
rg -nP --type=java '\bnew\s+TimeoutFilter\s*\(' src/test/java || echo "(no matches)"

echo
echo "== Check imports and full content of test file (first 70 lines) =="
if [ -f "src/test/java/org/juv25d/filter/TimeoutFilterTest.java" ]; then
  head -70 "src/test/java/org/juv25d/filter/TimeoutFilterTest.java"
else
  echo "Test file not found"
fi

Repository: ithsjava25/project-webserver-juv25d

Length of output: 7846


Create the missing TimeoutFilter class in src/main/java/org/juv25d/filter/ or remove the test.

The TimeoutFilter class does not exist in the production code. Lines 29, 44, and 64 in the test file attempt to instantiate this missing class, which prevents compilation and blocks CI. The test logic itself is sound and expects a filter that implements request timeout handling (returning 504 for slow requests), but the production class must be implemented.

🧰 Tools
🪛 GitHub Actions: CI Pipeline

[error] 29-29: Cannot find symbol: TimeoutFilter. Likely missing class or incorrect import. Command: './mvnw -B test'.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/test/java/org/juv25d/filter/TimeoutFilterTest.java` at line 29, Create a
new production class named TimeoutFilter in package org.juv25d.filter
(src/main/java/org/juv25d/filter/) that matches the test's expectations:
implement javax.servlet.Filter (or the project's servlet Filter interface),
perform request handling by executing the filter chain with a bounded timeout,
and if the chain does not complete within the timeout send an HTTP 504 response
and do not continue processing; also provide sensible init/destroy behavior and
a default timeout value so the test instantiating new TimeoutFilter() compiles
and the slow-request test passes.


doNothing().when(chain).doFilter(req, res);

filter.doFilter(req, res, chain);

verify(chain).doFilter(req, res);
assertThat(res.statusCode()).isEqualTo(200);
}

@Test
@Timeout(value = 4, unit = TimeUnit.SECONDS)
void slowRequest_sets504() throws IOException {
when(req.path()).thenReturn("/slow");
HttpResponse res = new HttpResponse();
TimeoutFilter filter = new TimeoutFilter();

doAnswer(inv -> {
Thread.sleep(3_000); // > 2000ms => timeout
return null;
}).when(chain).doFilter(req, res);

filter.doFilter(req, res, chain);

verify(chain).doFilter(req, res);
assertThat(res.statusCode()).isEqualTo(504);
assertThat(res.statusText()).isEqualTo("Gateway Timeout");
assertThat(new String(res.body(), StandardCharsets.UTF_8))
.isEqualTo("504 - Gateway Timeout");
}

@Test
void downstreamIOException_throwsRuntimeException() throws IOException {
when(req.path()).thenReturn("/boom");
HttpResponse res = new HttpResponse();
TimeoutFilter filter = new TimeoutFilter();

doThrow(new IOException("fail")).when(chain).doFilter(req, res);

assertThatThrownBy(() -> filter.doFilter(req, res, chain))
.isInstanceOf(RuntimeException.class);

verify(chain).doFilter(req, res);
}
}
Loading