1919import java .io .IOException ;
2020import java .net .URL ;
2121import javax .servlet .http .HttpServletRequest ;
22+ import javax .servlet .http .HttpServletResponse ;
2223
24+ import com .gargoylesoftware .htmlunit .HttpMethod ;
2325import com .gargoylesoftware .htmlunit .WebClient ;
2426import com .gargoylesoftware .htmlunit .WebRequest ;
2527import com .gargoylesoftware .htmlunit .WebResponse ;
3840import org .springframework .tests .Assume ;
3941import org .springframework .tests .TestGroup ;
4042import org .springframework .web .bind .annotation .CookieValue ;
43+ import org .springframework .web .bind .annotation .DeleteMapping ;
44+ import org .springframework .web .bind .annotation .PostMapping ;
4145import org .springframework .web .bind .annotation .RequestMapping ;
46+ import org .springframework .web .bind .annotation .RequestParam ;
4247import org .springframework .web .bind .annotation .RestController ;
4348import org .springframework .web .context .WebApplicationContext ;
4449import org .springframework .web .servlet .config .annotation .EnableWebMvc ;
@@ -103,11 +108,22 @@ public void cookieManagerShared() throws Exception {
103108 this .mockMvc = MockMvcBuilders .standaloneSetup (new CookieController ()).build ();
104109 WebClient client = MockMvcWebClientBuilder .mockMvcSetup (this .mockMvc ).build ();
105110
106- assertThat (getResponse (client , "http://localhost/" ).getContentAsString (), equalTo ("" ));
111+ assertThat (getResponse (client , "http://localhost/" ).getContentAsString (), equalTo ("NA " ));
107112 client .getCookieManager ().addCookie (new Cookie ("localhost" , "cookie" , "cookieManagerShared" ));
108113 assertThat (getResponse (client , "http://localhost/" ).getContentAsString (), equalTo ("cookieManagerShared" ));
109114 }
110115
116+ @ Test // SPR-14265
117+ public void cookiesAreManaged () throws Exception {
118+ this .mockMvc = MockMvcBuilders .standaloneSetup (new CookieController ()).build ();
119+ WebClient client = MockMvcWebClientBuilder .mockMvcSetup (this .mockMvc ).build ();
120+
121+ assertThat (getResponse (client , "http://localhost/" ).getContentAsString (), equalTo ("NA" ));
122+ assertThat (postResponse (client , "http://localhost/?cookie=foo" ).getContentAsString (), equalTo ("Set" ));
123+ assertThat (getResponse (client , "http://localhost/" ).getContentAsString (), equalTo ("foo" ));
124+ assertThat (deleteResponse (client , "http://localhost/" ).getContentAsString (), equalTo ("Delete" ));
125+ assertThat (getResponse (client , "http://localhost/" ).getContentAsString (), equalTo ("NA" ));
126+ }
111127
112128 private void assertMockMvcUsed (WebClient client , String url ) throws Exception {
113129 assertThat (getResponse (client , url ).getContentAsString (), equalTo ("mvc" ));
@@ -118,7 +134,19 @@ private void assertMockMvcNotUsed(WebClient client, String url) throws Exception
118134 }
119135
120136 private WebResponse getResponse (WebClient client , String url ) throws IOException {
121- return client .getWebConnection ().getResponse (new WebRequest (new URL (url )));
137+ return createResponse (client , new WebRequest (new URL (url )));
138+ }
139+
140+ private WebResponse postResponse (WebClient client , String url ) throws IOException {
141+ return createResponse (client , new WebRequest (new URL (url ), HttpMethod .POST ));
142+ }
143+
144+ private WebResponse deleteResponse (WebClient client , String url ) throws IOException {
145+ return createResponse (client , new WebRequest (new URL (url ), HttpMethod .DELETE ));
146+ }
147+
148+ private WebResponse createResponse (WebClient client , WebRequest request ) throws IOException {
149+ return client .getWebConnection ().getResponse (request );
122150 }
123151
124152
@@ -139,10 +167,26 @@ public String contextPath(HttpServletRequest request) {
139167 @ RestController
140168 static class CookieController {
141169
170+ static final String COOKIE_NAME = "cookie" ;
171+
142172 @ RequestMapping (path = "/" , produces = "text/plain" )
143- String cookie (@ CookieValue ("cookie " ) String cookie ) {
173+ String cookie (@ CookieValue (name = COOKIE_NAME , defaultValue = "NA " ) String cookie ) {
144174 return cookie ;
145175 }
176+
177+ @ PostMapping (path = "/" , produces = "text/plain" )
178+ String setCookie (@ RequestParam String cookie , HttpServletResponse response ) {
179+ response .addCookie (new javax .servlet .http .Cookie (COOKIE_NAME , cookie ));
180+ return "Set" ;
181+ }
182+
183+ @ DeleteMapping (path = "/" , produces = "text/plain" )
184+ String deleteCookie (HttpServletResponse response ) {
185+ javax .servlet .http .Cookie cookie = new javax .servlet .http .Cookie (COOKIE_NAME , "" );
186+ cookie .setMaxAge (0 );
187+ response .addCookie (cookie );
188+ return "Delete" ;
189+ }
146190 }
147191
148192}
0 commit comments