Description
Christopher Smith opened SPR-14066 and commented
While working on the other side of #18623 (trying to explicitly set a cookie as a given and then loading a page, expecting the controller to see the cookie's value), I discovered that the MockMvcWebConnection
used by the HtmlUnit driver maintains a completely separate copy of WebClient
with its own state. This is presumably because the DelegatingWebConnection
operates as a sort of decorator.
However, this means that any cookies set in the driver.manage().addCookie()
(or equivalent) are simply missing from the context used to issue local MockMvc requests.
The connection object should, when creating the delegate WebClient
, use the same CookieManager
as the parent WebClient
.
This code, inserted in setup()
, appears to induce the expected behavior:
WebClient topLevelWebClient = driver.@webClient
CookieManager topLevelCookieManager = topLevelWebClient.cookieManager
(topLevelWebClient.webConnection as DelegatingWebConnection).@connections
.findAll { DelegatingWebConnection.DelegateWebConnection connection ->
connection.delegate instanceof MockMvcWebConnection
}.each { (it.@delegate as MockMvcWebConnection).@webClient.cookieManager = topLevelCookieManager }
Affects: 4.2.5
Issue Links:
- MockMvcWebConnection should update cookie manager with MockHttpServletResponse cookies [SPR-14265] #18837 MockMvcWebConnection should update cookie manager with MockHttpServletResponse cookies