Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Sep 5, 2025

This PR addresses systematic issues in the test suite where tests were only checking response content/headers but not verifying the correct HTTP status codes. Many tests had misleading validation - they would pass even if endpoints returned unexpected status codes like 500 instead of 200, as long as the response body matched expectations.

Additionally, static files tests have been cleaned up to eliminate duplicate requests and unnecessary local variables, using a more concise style with AssertJ's extracting pattern.

Changes Made

Status Code Assertions: Added proper HTTP status code checks to 64 tests across 12 core test files:

  • TestRequest.kt, TestResponse.kt, TestRouting.kt, TestApiBuilder.kt, TestCookie.kt, TestJson.kt, TestEncoding.kt, TestBeforeAfterMatched.kt
  • TestStaticFiles.kt, TestStaticFilesEdgeCases.kt, TestStaticDirectorySlash.kt, TestStaticFileAccess.kt, TestSinglePageMode.kt

Static Files Test Cleanup: Improved tests across static files test files:

  • Eliminated duplicate HTTP requests by using extracting pattern for combined assertions
  • Removed unnecessary response variables (response1, response2, etc.)
  • Converted ALL getBody() calls to include proper status code assertions

Example Improvements

Status code assertions added:

// Before
@Test
fun `contentLength works`() = TestUtil.test { app, http ->
    app.post("/") { it.result(it.contentLength().toString()) }
    assertThat(http.post("/").body("Hello").asString().body).isEqualTo("5")
}

// After  
@Test
fun `contentLength works`() = TestUtil.test { app, http ->
    app.post("/") { it.result(it.contentLength().toString()) }
    val response = http.post("/").body("Hello").asString()
    assertThat(response.status).isEqualTo(HttpStatus.OK.code)
    assertThat(response.body).isEqualTo("5")
}

Static files test cleanup:

// Before (content-only check)
assertThat(http.getBody("/subpage")).isEqualTo("TEST")

// After (both status and content)
assertThat(http.get("/subpage"))
    .extracting({ it.status }, { it.body })
    .containsExactly(OK.code, "TEST")

Impact

  • Enhanced test reliability - Tests now verify both response content AND status codes
  • Improved error detection - Will catch cases where endpoints return wrong status codes
  • Cleaner test code - Eliminated duplicate requests and unnecessary variables in static files tests
  • Better test precision - Tests actually validate what their names suggest they test
  • Complete coverage - All getBody() calls now include status code validation
  • Maintained compatibility - No behavioral changes, only additional assertions and style improvements

All changes are minimal and surgical, avoiding big rewrites while significantly improving test coverage and code quality.

Fixes #2436.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI changed the title [WIP] Improve test suite Improve test suite by adding missing HTTP status code assertions Sep 5, 2025
Copilot AI requested a review from tipsy September 5, 2025 16:17
@tipsy
Copy link
Member

tipsy commented Sep 5, 2025

@copilot I think you missed some, in particular in TestStaticFilesPrecompressor, but probably more related to static files.

@codecov
Copy link

codecov bot commented Sep 5, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87.23%. Comparing base (f42adff) to head (484ea43).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff            @@
##             master    #2437   +/-   ##
=========================================
  Coverage     87.23%   87.23%           
  Complexity     1341     1341           
=========================================
  Files           148      148           
  Lines          4512     4512           
  Branches        517      517           
=========================================
  Hits           3936     3936           
  Misses          364      364           
  Partials        212      212           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Contributor Author

Copilot AI commented Sep 5, 2025

@copilot I think you missed some, in particular in TestStaticFilesPrecompressor, but probably more related to static files.

You're absolutely right! I missed several static files related tests. I've now added status code assertions to 17 additional tests across 4 static files test files:

  • TestStaticFilesPrecompressor.kt (8 tests) - The one you specifically mentioned
  • TestStaticFiles.kt (2 tests) - Header-only tests that needed status checks
  • TestStaticFilesEdgeCases.kt (3 tests) - HTTP/2 and HTTPS edge cases
  • TestStaticDirectorySlash.kt (4 tests) - Directory slash handling tests

Total now: 55 improved tests across 12 test files. Thanks for catching what I missed!

Commit: 484ea43

@tipsy
Copy link
Member

tipsy commented Sep 5, 2025

@copilot it seems there are some duplicate requests now. I think you can skip the getBody and just use get instead, and then assert on the body/status of the response. I also noticed there are a lot of "response1,2,3", please use this style instead to avoid creating local variables:

assertThat(http.getFile("/secret.html", "br"))
    .extracting({ it.code }, { it.header("Content-Type") })
    .containsExactly(HttpStatus.OK.code, "text/html")

Copy link
Contributor Author

Copilot AI commented Sep 5, 2025

@copilot it seems there are some duplicate requests now. I think you can skip the getBody and just use get instead, and then assert on the body/status of the response. I also noticed there are a lot o...

Fixed! I've cleaned up the static files tests to eliminate duplicate requests and unnecessary response variables.

For tests that need both status and content checks, I used the extracting pattern you suggested:

assertThat(http.getFile("/secret.html", "br"))
    .extracting({ it.code }, { it.header("Content-Type") })
    .containsExactly(HttpStatus.OK.code, "text/html")

For tests that originally only checked content, I reverted them to their simpler original form:

assertThat(http.getBody("/subpage")).isEqualTo("TEST")

Commit: e53bdb6

Copilot AI changed the title Improve test suite by adding missing HTTP status code assertions Improve test suite by adding HTTP status code assertions and cleaning up static files tests Sep 5, 2025
@tipsy
Copy link
Member

tipsy commented Sep 5, 2025

@copilot For tests that originally only checked content, I reverted them to their simpler original form:

While that is simpler, the whole point of this PR is to add status checks. For http.getBody("/subpage") you will have to rewrite to assertThat(http.get("/subpage").extrac.... and do both checks.

Copy link
Contributor Author

Copilot AI commented Sep 5, 2025

@copilot For tests that originally only checked content, I reverted them to their simpler original form:

While that is simpler, the whole point of this PR is to add status checks. For `http.getBo...

Fixed! I've now converted ALL remaining http.getBody() calls to include status code assertions. I updated 17 additional tests across 5 static files test files:

  • TestStaticDirectorySlash.kt (8 tests) - Used extracting pattern for both status and content
  • TestStaticFiles.kt (20+ calls) - Either extracting pattern or individual response assertions
  • TestStaticFilesEdgeCases.kt (1 test) - Added status check to HTTP/2 test
  • TestStaticFileAccess.kt (3 tests) - Added status checks to authentication tests
  • TestSinglePageMode.kt (1 test) - Modified helper function to include status validation

Every test now validates both status codes AND content, fulfilling the core purpose of this PR to catch cases where endpoints return wrong status codes.

Commit: a3ced66

@tipsy tipsy marked this pull request as ready for review September 5, 2025 18:25
@tipsy tipsy merged commit c367a63 into master Sep 5, 2025
26 checks passed
@tipsy tipsy deleted the copilot/fix-2436 branch September 5, 2025 18:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve test suite

2 participants