MockMvc HtmlUnit support shares CookieManager

Previously MockMvc builders failed to share the WebConnection used for
managing cookies in the MockMvcWebConnection. This meant that the various
CookieManagers would have different states.

This commit ensures that the WebConnection is set on the
MockMvcWebConnection.

Fixes SPR-14066
This commit is contained in:
Rob Winch
2016-03-18 09:09:17 -05:00
committed by Rossen Stoyanchev
parent 8246fe4534
commit 7d96ad1f6e
9 changed files with 199 additions and 21 deletions

View File

@@ -36,6 +36,7 @@ import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebConnection;
import com.gargoylesoftware.htmlunit.WebRequest;
import com.gargoylesoftware.htmlunit.WebResponse;
@@ -45,6 +46,7 @@ import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* Integration tests for {@link MockMvcWebConnectionBuilderSupport}.
@@ -55,10 +57,12 @@ import static org.mockito.Mockito.mock;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@WebAppConfiguration
@SuppressWarnings("rawtypes")
@SuppressWarnings({"rawtypes","deprecation"})
public class MockMvcConnectionBuilderSupportTests {
private final WebConnection delegateConnection = mock(WebConnection.class);
private WebConnection delegateConnection;
private WebClient webClient;
@Autowired
private WebApplicationContext wac;
@@ -69,10 +73,16 @@ public class MockMvcConnectionBuilderSupportTests {
@Before
public void setup() {
delegateConnection = mock(WebConnection.class);
webClient = mock(WebClient.class);
when(webClient.getWebConnection()).thenReturn(delegateConnection);
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
connection = new MockMvcWebConnectionBuilderSupport(mockMvc){}
.createConnection(delegateConnection);
.createConnection(webClient);
}
@Test(expected = IllegalArgumentException.class)
@@ -86,10 +96,61 @@ public class MockMvcConnectionBuilderSupportTests {
}
@Test
public void context() throws Exception {
public void contextDeprecated() throws Exception {
connection = new MockMvcWebConnectionBuilderSupport(wac) {}
.createConnection(webClient);
assertMvcProcessed("http://localhost/");
assertDelegateProcessed("http://example.com/");
}
@Test
public void mockMvcDeprecated() throws Exception {
assertMvcProcessed("http://localhost/");
assertDelegateProcessed("http://example.com/");
}
@Test
public void mockMvcExampleDotComDeprecated() throws Exception {
connection = new MockMvcWebConnectionBuilderSupport(wac) {}
.useMockMvcForHosts("example.com")
.createConnection(delegateConnection);
assertMvcProcessed("http://localhost/");
assertMvcProcessed("http://example.com/");
assertDelegateProcessed("http://other.com/");
}
@Test
public void mockMvcAlwaysUseMockMvcDeprecated() throws Exception {
connection = new MockMvcWebConnectionBuilderSupport(wac) {}
.alwaysUseMockMvc()
.createConnection(delegateConnection);
assertMvcProcessed("http://other.com/");
}
@Test
public void defaultContextPathEmptyDeprecated() throws Exception {
connection = new MockMvcWebConnectionBuilderSupport(wac) {}
.createConnection(delegateConnection);
assertThat(getWebResponse("http://localhost/abc").getContentAsString(), equalTo(""));
}
@Test
public void defaultContextPathCustomDeprecated() throws Exception {
connection = new MockMvcWebConnectionBuilderSupport(wac) {}
.contextPath("/abc").createConnection(delegateConnection);
assertThat(getWebResponse("http://localhost/abc/def").getContentAsString(), equalTo("/abc"));
}
@Test
public void context() throws Exception {
connection = new MockMvcWebConnectionBuilderSupport(wac) {}
.createConnection(webClient);
assertMvcProcessed("http://localhost/");
assertDelegateProcessed("http://example.com/");
}
@@ -104,7 +165,7 @@ public class MockMvcConnectionBuilderSupportTests {
public void mockMvcExampleDotCom() throws Exception {
connection = new MockMvcWebConnectionBuilderSupport(wac) {}
.useMockMvcForHosts("example.com")
.createConnection(delegateConnection);
.createConnection(webClient);
assertMvcProcessed("http://localhost/");
assertMvcProcessed("http://example.com/");
@@ -115,7 +176,7 @@ public class MockMvcConnectionBuilderSupportTests {
public void mockMvcAlwaysUseMockMvc() throws Exception {
connection = new MockMvcWebConnectionBuilderSupport(wac) {}
.alwaysUseMockMvc()
.createConnection(delegateConnection);
.createConnection(webClient);
assertMvcProcessed("http://other.com/");
}
@@ -123,7 +184,7 @@ public class MockMvcConnectionBuilderSupportTests {
@Test
public void defaultContextPathEmpty() throws Exception {
connection = new MockMvcWebConnectionBuilderSupport(wac) {}
.createConnection(delegateConnection);
.createConnection(webClient);
assertThat(getWebResponse("http://localhost/abc").getContentAsString(), equalTo(""));
}
@@ -131,7 +192,7 @@ public class MockMvcConnectionBuilderSupportTests {
@Test
public void defaultContextPathCustom() throws Exception {
connection = new MockMvcWebConnectionBuilderSupport(wac) {}
.contextPath("/abc").createConnection(delegateConnection);
.contextPath("/abc").createConnection(webClient);
assertThat(getWebResponse("http://localhost/abc/def").getContentAsString(), equalTo("/abc"));
}

View File

@@ -34,6 +34,7 @@ import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.tests.Assume;
import org.springframework.tests.TestGroup;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.WebApplicationContext;
@@ -42,6 +43,7 @@ import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebRequest;
import com.gargoylesoftware.htmlunit.WebResponse;
import com.gargoylesoftware.htmlunit.util.Cookie;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
@@ -99,6 +101,17 @@ public class MockMvcWebClientBuilderTests {
Assume.group(TestGroup.PERFORMANCE, () -> assertDelegateProcessed("http://example.com/"));
}
// SPR-14066
@Test
public void cookieManagerShared() throws Exception {
this.mockMvc = MockMvcBuilders.standaloneSetup(new CookieController()).build();
this.webClient = mockMvcSetup(this.mockMvc).build();
assertThat(getWebResponse("http://localhost/").getContentAsString(), equalTo(""));
this.webClient.getCookieManager().addCookie(new Cookie("localhost", "cookie", "cookieManagerShared"));
assertThat(getWebResponse("http://localhost/").getContentAsString(), equalTo("cookieManagerShared"));
}
private void assertMvcProcessed(String url) throws Exception {
assertThat(getWebResponse(url).getContentAsString(), equalTo("mvc"));
}
@@ -126,4 +139,11 @@ public class MockMvcWebClientBuilderTests {
}
}
@RestController
static class CookieController {
@RequestMapping("/")
public String cookie(@CookieValue("cookie") String cookie) {
return cookie;
}
}
}

View File

@@ -36,6 +36,7 @@ import static org.junit.Assert.assertThat;
* @author Rob Winch
* @since 4.2
*/
@SuppressWarnings("deprecation")
public class MockMvcWebConnectionTests {
private final WebClient webClient = new WebClient();
@@ -50,7 +51,7 @@ public class MockMvcWebConnectionTests {
@Test
public void contextPathNull() throws IOException {
this.webClient.setWebConnection(new MockMvcWebConnection(this.mockMvc, null));
this.webClient.setWebConnection(new MockMvcWebConnection(this.mockMvc, (String) null));
Page page = this.webClient.getPage("http://localhost/context/a");

View File

@@ -35,11 +35,14 @@ import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.tests.Assume;
import org.springframework.tests.TestGroup;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import com.gargoylesoftware.htmlunit.util.Cookie;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.springframework.test.web.servlet.htmlunit.webdriver.MockMvcHtmlUnitDriverBuilder.*;
@@ -111,6 +114,20 @@ public class MockMvcHtmlUnitDriverBuilderTests {
assertFalse(this.driver.isJavascriptEnabled());
}
// SPR-14066
@Test
public void cookieManagerShared() throws Exception {
WebConnectionHtmlUnitDriver delegateDriver = new WebConnectionHtmlUnitDriver();
this.mockMvc = MockMvcBuilders.standaloneSetup(new CookieController()).build();
this.driver = mockMvcSetup(this.mockMvc)
.withDelegate(delegateDriver)
.build();
assertThat(get("http://localhost/"), equalTo(""));
delegateDriver.getWebClient().getCookieManager().addCookie(new Cookie("localhost", "cookie", "cookieManagerShared"));
assertThat(get("http://localhost/"), equalTo("cookieManagerShared"));
}
private void assertMvcProcessed(String url) throws Exception {
assertThat(get(url), containsString(EXPECTED_BODY));
}
@@ -139,4 +156,11 @@ public class MockMvcHtmlUnitDriverBuilderTests {
}
}
@RestController
static class CookieController {
@RequestMapping("/")
public String cookie(@CookieValue("cookie") String cookie) {
return cookie;
}
}
}