Repository navigation
Keep mock response output lazy after reset - #16490
Conversation
|
Noticed this when testing downloads in my application on 8.0.0 |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ 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
🚀 New features to boost your workflow:
|
This comment has been minimized.
This comment has been minimized.
jamesfredley
left a comment
There was a problem hiding this comment.
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.
|
The current test suite for 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 grails-test-suite-uber/src/test/groovy/org/grails/plugins/testing/GrailsMockHttpServletResponseTests.groovy |
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.
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 boundGrailsWebRequest'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 withIllegalStateException: 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
GrailsWebRequestfor: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:codeStyleandvalidateRepositoryConventionspassed.:grails-test-suite-uber:testAll: 638 passed, 4 skipped, 0 failures across the main and isolated test tasks.git diff --checkpassed.