-
Notifications
You must be signed in to change notification settings - Fork 0
Add unit tests for TimeoutFilter #159
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
|
|
||
| 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); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: ithsjava25/project-webserver-juv25d
Length of output: 7846
Create the missing
TimeoutFilterclass insrc/main/java/org/juv25d/filter/or remove the test.The
TimeoutFilterclass 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