Skip to content

Keep mock response output lazy after reset - #16490

Merged
jdaugherty merged 2 commits into
8.0.xfrom
fix/mock-response-reset-stream
Oct 3, 2026
Merged

jdaugherty merged 2 commits into
8.0.xfrom
fix/mock-response-reset-stream

Conversation

@jdaugherty

Copy link
Copy Markdown
Contributor

Summary

Keep GrailsMockHttpServletResponse.reset() from selecting a character writer before the next response chooses its output mode.

The reset implementation currently calls getWriter() to replace the bound GrailsWebRequest's cached output. Spring 7 enforces the servlet restriction against using both a writer and an output stream, so a subsequent binary or CSV download fails with IllegalStateException: getWriter() has already been called, even when the test reset the response before invoking the controller.

Clear the cached web-request output with setOut(null) instead. GrailsWebRequest.getOut() already resolves the current response's writer lazily when text output is actually requested.

Regression coverage

Add tests with a bound GrailsWebRequest for:

  • Binary output after resetting a fresh response.
  • Binary output after resetting a response previously used for text.
  • Text output after resetting a response previously used for binary data.
  • Replacing a cached web-request writer without writing to the previous writer.

This was diagnosed in a real controller integration test: the query and CSV generation succeeded, but the mock response had already acquired a writer during reset() before the download reached its output-stream write.

Verification

  • :grails-test-core:codeStyle and validateRepositoryConventions passed.
  • :grails-test-suite-uber:testAll: 638 passed, 4 skipped, 0 failures across the main and isolated test tasks.
  • git diff --check passed.
  • Full-repository test verification is left to CI.

@jdaugherty

Copy link
Copy Markdown
Contributor Author

Noticed this when testing downloads in my application on 8.0.0

@codecov

codecov Bot commented Oct 3, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 59.3700%. Comparing base (80a1ff5) to head (8bd0204).
⚠️ Report is 28 commits behind head on 8.0.x.

Files with missing lines Patch % Lines
...sting/AbstractGrailsMockHttpServletResponse.groovy 0.0000% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@                Coverage Diff                 @@
##                8.0.x     #16490        +/-   ##
==================================================
+ Coverage     59.2827%   59.3700%   +0.0873%     
- Complexity      24770      24830        +60     
==================================================
  Files            2170       2171         +1     
  Lines          108121     108186        +65     
  Branches        19709      19729        +20     
==================================================
+ Hits            64097      64230       +133     
+ Misses          35147      35031       -116     
- Partials         8877       8925        +48     
Files with missing lines Coverage Δ
...sting/AbstractGrailsMockHttpServletResponse.groovy 12.0000% <0.0000%> (ø)

... and 14 files with indirect coverage changes

🚀 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.

@testlens-app

This comment has been minimized.

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Approved. reset() now clears the cached writer with setOut(null) instead of calling getWriter(), so the next write can still choose a writer or an output stream. GrailsWebRequest.getOut() still opens a writer only when text is requested. Redirect clearing, setCommitted(false), and super.reset() are unchanged.

One non-blocking test follow-up is on GrailsMockHttpServletResponseTests. It does not block merge.

@bito-code-review

Copy link
Copy Markdown

The current test suite for GrailsMockHttpServletResponse verifies that reset() correctly clears the webRequest output, allowing for subsequent selection of either a writer or an output stream. To address the concern regarding character encoding, you can add a test case that sets the character encoding to UTF-8 after reset() and verifies that non-ASCII text is correctly handled.

Here is an example of how you could implement this test:

    @Test
    void testResetAllowsUtf8OutputAfterReset() {
        withBoundWebRequest { response, webRequest ->
            response.reset()
            response.characterEncoding = 'UTF-8'
            String text = '€'
            webRequest.out.write(text)
            webRequest.out.flush()

            assertArrayEquals text.getBytes('UTF-8'), response.contentAsByteArray
        }
    }

This test ensures that the lazily opened writer respects the character encoding set after the reset() call.

grails-test-suite-uber/src/test/groovy/org/grails/plugins/testing/GrailsMockHttpServletResponseTests.groovy

@Test
    void testResetAllowsUtf8OutputAfterReset() {
        withBoundWebRequest { response, webRequest ->
            response.reset()
            response.characterEncoding = 'UTF-8'
            String text = '€'
            webRequest.out.write(text)
            webRequest.out.flush()

            assertArrayEquals text.getBytes('UTF-8'), response.contentAsByteArray
        }
    }

Write non-ASCII text after resetting a response that already used a
writer and setting UTF-8, and assert the UTF-8 bytes. A writer opened
during reset() would have captured the default encoding instead.
@jdaugherty
jdaugherty merged commit 009584a into 8.0.x Oct 3, 2026
89 checks passed
@jdaugherty
jdaugherty deleted the fix/mock-response-reset-stream branch October 3, 2026 20:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants