diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/DelegatingWebConnection.java b/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/DelegatingWebConnection.java
index 091681a86d1..50f6fa95a79 100644
--- a/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/DelegatingWebConnection.java
+++ b/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/DelegatingWebConnection.java
@@ -37,8 +37,7 @@ import org.springframework.util.Assert;
* WebClient webClient = new WebClient();
*
* MockMvc mockMvc = ...
- * MockMvcWebConnection mockConnection = new MockMvcWebConnection(mockMvc);
- * mockConnection.setWebClient(webClient);
+ * MockMvcWebConnection mockConnection = new MockMvcWebConnection(mockMvc, webClient);
*
* WebRequestMatcher cdnMatcher = new UrlRegexRequestMatcher(".*?//code.jquery.com/.*");
* WebConnection httpConnection = new HttpWebConnection(webClient);
diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebClientBuilder.java b/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebClientBuilder.java
index 5dc12a21182..94cc1ea8653 100644
--- a/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebClientBuilder.java
+++ b/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebClientBuilder.java
@@ -106,7 +106,7 @@ public class MockMvcWebClientBuilder extends MockMvcWebConnectionBuilderSupport<
*/
public MockMvcWebClientBuilder withDelegate(WebClient webClient) {
Assert.notNull(webClient, "WebClient must not be null");
- webClient.setWebConnection(createConnection(webClient.getWebConnection()));
+ webClient.setWebConnection(createConnection(webClient));
this.webClient = webClient;
return this;
}
diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebConnection.java b/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebConnection.java
index d7a6fe60872..184d0b7ec7d 100644
--- a/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebConnection.java
+++ b/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebConnection.java
@@ -63,16 +63,16 @@ public final class MockMvcWebConnection implements WebConnection {
private WebClient webClient;
-
/**
* Create a new instance that assumes the context path of the application
* is {@code ""} (i.e., the root context).
*
For example, the URL {@code http://localhost/test/this} would use
* {@code ""} as the context path.
* @param mockMvc the {@code MockMvc} instance to use; never {@code null}
+ * @param webClient the {@link WebClient} to use. never {@code null}
*/
- public MockMvcWebConnection(MockMvc mockMvc) {
- this(mockMvc, "");
+ public MockMvcWebConnection(MockMvc mockMvc, WebClient webClient) {
+ this(mockMvc, webClient, "");
}
/**
@@ -83,17 +83,47 @@ public final class MockMvcWebConnection implements WebConnection {
* which states that it can be an empty string and otherwise must start
* with a "/" character and not end with a "/" character.
* @param mockMvc the {@code MockMvc} instance to use; never {@code null}
+ * @param webClient the {@link WebClient} to use. never {@code null}
* @param contextPath the contextPath to use
*/
- public MockMvcWebConnection(MockMvc mockMvc, String contextPath) {
+ public MockMvcWebConnection(MockMvc mockMvc, WebClient webClient, String contextPath) {
Assert.notNull(mockMvc, "MockMvc must not be null");
+ Assert.notNull(webClient, "WebClient must not be null");
validateContextPath(contextPath);
- this.webClient = new WebClient();
+ this.webClient = webClient;
this.mockMvc = mockMvc;
this.contextPath = contextPath;
}
+ /**
+ * Create a new instance that assumes the context path of the application
+ * is {@code ""} (i.e., the root context).
+ *
For example, the URL {@code http://localhost/test/this} would use
+ * {@code ""} as the context path.
+ * @param mockMvc the {@code MockMvc} instance to use; never {@code null}
+ * @deprecated Use {@link #MockMvcWebConnection(MockMvc, WebClient)}
+ */
+ @Deprecated
+ public MockMvcWebConnection(MockMvc mockMvc) {
+ this(mockMvc, "");
+ }
+
+ /**
+ * Create a new instance with the specified context path.
+ *
The path may be {@code null} in which case the first path segment
+ * of the URL is turned into the contextPath. Otherwise it must conform
+ * to {@link javax.servlet.http.HttpServletRequest#getContextPath()}
+ * which states that it can be an empty string and otherwise must start
+ * with a "/" character and not end with a "/" character.
+ * @param mockMvc the {@code MockMvc} instance to use; never {@code null}
+ * @param contextPath the contextPath to use
+ * @deprecated use {@link #MockMvcWebConnection(MockMvc, WebClient, String)}
+ */
+ @Deprecated
+ public MockMvcWebConnection(MockMvc mockMvc, String contextPath) {
+ this(mockMvc, new WebClient(), contextPath);
+ }
public void setWebClient(WebClient webClient) {
Assert.notNull(webClient, "WebClient must not be null");
diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebConnectionBuilderSupport.java b/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebConnectionBuilderSupport.java
index 6575e6a26ea..baef40419bd 100644
--- a/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebConnectionBuilderSupport.java
+++ b/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebConnectionBuilderSupport.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2015 the original author or authors.
+ * Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
@@ -19,9 +19,11 @@ package org.springframework.test.web.servlet.htmlunit;
import java.util.ArrayList;
import java.util.List;
+import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebConnection;
import org.springframework.test.web.servlet.MockMvc;
+import org.springframework.test.web.servlet.htmlunit.DelegatingWebConnection.DelegateWebConnection;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.test.web.servlet.setup.MockMvcConfigurer;
import org.springframework.util.Assert;
@@ -43,7 +45,7 @@ public abstract class MockMvcWebConnectionBuilderSupport mockMvcRequestMatchers = new ArrayList();
+ private final List requestMatchers = new ArrayList();
private String contextPath = "";
@@ -57,7 +59,7 @@ public abstract class MockMvcWebConnectionBuilderSupport delegates = new ArrayList(
- this.mockMvcRequestMatchers.size());
- for (WebRequestMatcher matcher : this.mockMvcRequestMatchers) {
- delegates.add(new DelegatingWebConnection.DelegateWebConnection(matcher, mockMvcWebConnection));
+ List delegates = new ArrayList(this.requestMatchers.size());
+ for (WebRequestMatcher matcher : this.requestMatchers) {
+ delegates.add(new DelegateWebConnection(matcher, connection));
}
-
return new DelegatingWebConnection(defaultConnection, delegates);
}
diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/webdriver/MockMvcHtmlUnitDriverBuilder.java b/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/webdriver/MockMvcHtmlUnitDriverBuilder.java
index 3b8ded71687..f8b88512bfe 100644
--- a/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/webdriver/MockMvcHtmlUnitDriverBuilder.java
+++ b/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/webdriver/MockMvcHtmlUnitDriverBuilder.java
@@ -129,7 +129,7 @@ public class MockMvcHtmlUnitDriverBuilder extends MockMvcWebConnectionBuilderSup
public MockMvcHtmlUnitDriverBuilder withDelegate(WebConnectionHtmlUnitDriver driver) {
Assert.notNull(driver, "HtmlUnitDriver must not be null");
driver.setJavascriptEnabled(this.javascriptEnabled);
- driver.setWebConnection(createConnection(driver.getWebConnection()));
+ driver.setWebConnection(createConnection(driver.getWebClient()));
this.driver = driver;
return this;
}
diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/webdriver/WebConnectionHtmlUnitDriver.java b/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/webdriver/WebConnectionHtmlUnitDriver.java
index ab04baf2531..ba7a91752ea 100644
--- a/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/webdriver/WebConnectionHtmlUnitDriver.java
+++ b/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/webdriver/WebConnectionHtmlUnitDriver.java
@@ -41,7 +41,6 @@ public class WebConnectionHtmlUnitDriver extends HtmlUnitDriver {
private WebClient webClient;
-
public WebConnectionHtmlUnitDriver(BrowserVersion browserVersion) {
super(browserVersion);
}
@@ -107,4 +106,11 @@ public class WebConnectionHtmlUnitDriver extends HtmlUnitDriver {
this.webClient.setWebConnection(webConnection);
}
+ /**
+ * Gets the current {@link WebClient}
+ * @return the current {@link WebClient}
+ */
+ public WebClient getWebClient() {
+ return this.webClient;
+ }
}
diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/MockMvcConnectionBuilderSupportTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/MockMvcConnectionBuilderSupportTests.java
index c89f4f40f9b..28a16b59192 100644
--- a/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/MockMvcConnectionBuilderSupportTests.java
+++ b/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/MockMvcConnectionBuilderSupportTests.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2015 the original author or authors.
+ * Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,6 +20,10 @@ import java.io.IOException;
import java.net.URL;
import javax.servlet.http.HttpServletRequest;
+import com.gargoylesoftware.htmlunit.WebClient;
+import com.gargoylesoftware.htmlunit.WebConnection;
+import com.gargoylesoftware.htmlunit.WebRequest;
+import com.gargoylesoftware.htmlunit.WebResponse;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -36,121 +40,112 @@ 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.WebConnection;
-import com.gargoylesoftware.htmlunit.WebRequest;
-import com.gargoylesoftware.htmlunit.WebResponse;
-
import static org.hamcrest.CoreMatchers.equalTo;
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}.
*
* @author Rob Winch
+ * @author Rossen Stoyanchev
* @since 4.2
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@WebAppConfiguration
-@SuppressWarnings("rawtypes")
public class MockMvcConnectionBuilderSupportTests {
- private final WebConnection delegateConnection = mock(WebConnection.class);
-
@Autowired
private WebApplicationContext wac;
- private MockMvc mockMvc;
+ private WebClient client;
+
+ private MockMvcWebConnectionBuilderSupport builder;
- private WebConnection connection;
@Before
public void setup() {
- mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
-
- connection = new MockMvcWebConnectionBuilderSupport(mockMvc){}
- .createConnection(delegateConnection);
+ this.client = mock(WebClient.class);
+ when(this.client.getWebConnection()).thenReturn(mock(WebConnection.class));
+ this.builder = new MockMvcWebConnectionBuilderSupport(this.wac) {};
}
+
@Test(expected = IllegalArgumentException.class)
public void constructorMockMvcNull() {
- new MockMvcWebConnectionBuilderSupport((MockMvc)null){};
+ new MockMvcWebConnectionBuilderSupport((MockMvc) null){};
}
@Test(expected = IllegalArgumentException.class)
public void constructorContextNull() {
- new MockMvcWebConnectionBuilderSupport((WebApplicationContext)null){};
+ new MockMvcWebConnectionBuilderSupport((WebApplicationContext) null){};
}
@Test
public void context() throws Exception {
- connection = new MockMvcWebConnectionBuilderSupport(wac) {}
- .createConnection(delegateConnection);
+ WebConnection conn = this.builder.createConnection(this.client);
- assertMvcProcessed("http://localhost/");
- assertDelegateProcessed("http://example.com/");
+ assertMockMvcUsed(conn, "http://localhost/");
+ assertMockMvcNotUsed(conn, "http://example.com/");
}
@Test
public void mockMvc() throws Exception {
- assertMvcProcessed("http://localhost/");
- assertDelegateProcessed("http://example.com/");
+ MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
+ WebConnection conn = new MockMvcWebConnectionBuilderSupport(mockMvc) {}.createConnection(this.client);
+
+ assertMockMvcUsed(conn, "http://localhost/");
+ assertMockMvcNotUsed(conn, "http://example.com/");
}
@Test
public void mockMvcExampleDotCom() throws Exception {
- connection = new MockMvcWebConnectionBuilderSupport(wac) {}
- .useMockMvcForHosts("example.com")
- .createConnection(delegateConnection);
+ WebConnection conn = this.builder.useMockMvcForHosts("example.com").createConnection(this.client);
- assertMvcProcessed("http://localhost/");
- assertMvcProcessed("http://example.com/");
- assertDelegateProcessed("http://other.com/");
+ assertMockMvcUsed(conn, "http://localhost/");
+ assertMockMvcUsed(conn, "http://example.com/");
+ assertMockMvcNotUsed(conn, "http://other.com/");
}
@Test
public void mockMvcAlwaysUseMockMvc() throws Exception {
- connection = new MockMvcWebConnectionBuilderSupport(wac) {}
- .alwaysUseMockMvc()
- .createConnection(delegateConnection);
-
- assertMvcProcessed("http://other.com/");
+ WebConnection conn = this.builder.alwaysUseMockMvc().createConnection(this.client);
+ assertMockMvcUsed(conn, "http://other.com/");
}
@Test
public void defaultContextPathEmpty() throws Exception {
- connection = new MockMvcWebConnectionBuilderSupport(wac) {}
- .createConnection(delegateConnection);
-
- assertThat(getWebResponse("http://localhost/abc").getContentAsString(), equalTo(""));
+ WebConnection conn = this.builder.createConnection(this.client);
+ assertThat(getResponse(conn, "http://localhost/abc").getContentAsString(), equalTo(""));
}
@Test
public void defaultContextPathCustom() throws Exception {
- connection = new MockMvcWebConnectionBuilderSupport(wac) {}
- .contextPath("/abc").createConnection(delegateConnection);
-
- assertThat(getWebResponse("http://localhost/abc/def").getContentAsString(), equalTo("/abc"));
+ WebConnection conn = this.builder.contextPath("/abc").createConnection(this.client);
+ assertThat(getResponse(conn, "http://localhost/abc/def").getContentAsString(), equalTo("/abc"));
}
- private void assertMvcProcessed(String url) throws Exception {
- assertThat(getWebResponse(url), notNullValue());
+
+ private void assertMockMvcUsed(WebConnection connection, String url) throws Exception {
+ assertThat(getResponse(connection, url), notNullValue());
}
- private void assertDelegateProcessed(String url) throws Exception {
- assertThat(getWebResponse(url), nullValue());
+ private void assertMockMvcNotUsed(WebConnection connection, String url) throws Exception {
+ assertThat(getResponse(connection, url), nullValue());
}
- private WebResponse getWebResponse(String url) throws IOException {
+ private WebResponse getResponse(WebConnection connection, String url) throws IOException {
return connection.getResponse(new WebRequest(new URL(url)));
}
@Configuration
@EnableWebMvc
+ @SuppressWarnings("unused")
static class Config {
@RestController
diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebClientBuilderTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebClientBuilderTests.java
index 0f68d9075f5..c8e4b096009 100644
--- a/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebClientBuilderTests.java
+++ b/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebClientBuilderTests.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2015 the original author or authors.
+ * Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
@@ -18,9 +18,12 @@ package org.springframework.test.web.servlet.htmlunit;
import java.io.IOException;
import java.net.URL;
-
import javax.servlet.http.HttpServletRequest;
+import com.gargoylesoftware.htmlunit.WebClient;
+import com.gargoylesoftware.htmlunit.WebRequest;
+import com.gargoylesoftware.htmlunit.WebResponse;
+import com.gargoylesoftware.htmlunit.util.Cookie;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -34,24 +37,22 @@ 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.WebClient;
-import com.gargoylesoftware.htmlunit.WebRequest;
-import com.gargoylesoftware.htmlunit.WebResponse;
-
-import static org.hamcrest.CoreMatchers.*;
-import static org.junit.Assert.*;
-import static org.springframework.test.web.servlet.htmlunit.MockMvcWebClientBuilder.*;
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.not;
+import static org.junit.Assert.assertThat;
/**
* Integration tests for {@link MockMvcWebClientBuilder}.
*
* @author Rob Winch
* @author Sam Brannen
+ * @author Rossen Stoyanchev
* @since 4.2
*/
@RunWith(SpringJUnit4ClassRunner.class)
@@ -59,8 +60,6 @@ import static org.springframework.test.web.servlet.htmlunit.MockMvcWebClientBuil
@WebAppConfiguration
public class MockMvcWebClientBuilderTests {
- private WebClient webClient;
-
@Autowired
private WebApplicationContext wac;
@@ -72,43 +71,55 @@ public class MockMvcWebClientBuilderTests {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
}
+
@Test(expected = IllegalArgumentException.class)
public void mockMvcSetupNull() {
- mockMvcSetup(null);
+ MockMvcWebClientBuilder.mockMvcSetup(null);
}
@Test(expected = IllegalArgumentException.class)
public void webAppContextSetupNull() {
- webAppContextSetup(null);
+ MockMvcWebClientBuilder.webAppContextSetup(null);
}
@Test
public void mockMvcSetupWithDefaultWebClientDelegate() throws Exception {
- this.webClient = mockMvcSetup(this.mockMvc).build();
+ WebClient client = MockMvcWebClientBuilder.mockMvcSetup(this.mockMvc).build();
- assertMvcProcessed("http://localhost/test");
- Assume.group(TestGroup.PERFORMANCE, () -> assertDelegateProcessed("http://example.com/"));
+ assertMockMvcUsed(client, "http://localhost/test");
+ Assume.group(TestGroup.PERFORMANCE, () -> assertMockMvcNotUsed(client, "http://example.com/"));
}
@Test
public void mockMvcSetupWithCustomWebClientDelegate() throws Exception {
- WebClient preconfiguredWebClient = new WebClient();
- this.webClient = mockMvcSetup(this.mockMvc).withDelegate(preconfiguredWebClient).build();
+ WebClient otherClient = new WebClient();
+ WebClient client = MockMvcWebClientBuilder.mockMvcSetup(this.mockMvc).withDelegate(otherClient).build();
+
+ assertMockMvcUsed(client, "http://localhost/test");
+ Assume.group(TestGroup.PERFORMANCE, () -> assertMockMvcNotUsed(client, "http://example.com/"));
+ }
- assertMvcProcessed("http://localhost/test");
- Assume.group(TestGroup.PERFORMANCE, () -> assertDelegateProcessed("http://example.com/"));
+ @Test // SPR-14066
+ public void cookieManagerShared() throws Exception {
+ this.mockMvc = MockMvcBuilders.standaloneSetup(new CookieController()).build();
+ WebClient client = MockMvcWebClientBuilder.mockMvcSetup(this.mockMvc).build();
+
+ assertThat(getResponse(client, "http://localhost/").getContentAsString(), equalTo(""));
+ client.getCookieManager().addCookie(new Cookie("localhost", "cookie", "cookieManagerShared"));
+ assertThat(getResponse(client, "http://localhost/").getContentAsString(), equalTo("cookieManagerShared"));
}
- private void assertMvcProcessed(String url) throws Exception {
- assertThat(getWebResponse(url).getContentAsString(), equalTo("mvc"));
+
+ private void assertMockMvcUsed(WebClient client, String url) throws Exception {
+ assertThat(getResponse(client, url).getContentAsString(), equalTo("mvc"));
}
- private void assertDelegateProcessed(String url) throws Exception {
- assertThat(getWebResponse(url).getContentAsString(), not(equalTo("mvc")));
+ private void assertMockMvcNotUsed(WebClient client, String url) throws Exception {
+ assertThat(getResponse(client, url).getContentAsString(), not(equalTo("mvc")));
}
- private WebResponse getWebResponse(String url) throws IOException {
- return this.webClient.getWebConnection().getResponse(new WebRequest(new URL(url)));
+ private WebResponse getResponse(WebClient client, String url) throws IOException {
+ return client.getWebConnection().getResponse(new WebRequest(new URL(url)));
}
@@ -126,4 +137,11 @@ public class MockMvcWebClientBuilderTests {
}
}
+ @RestController
+ static class CookieController {
+ @RequestMapping("/")
+ public String cookie(@CookieValue("cookie") String cookie) {
+ return cookie;
+ }
+ }
}
diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebConnectionTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebConnectionTests.java
index 726c4f0bb66..78e207b2e15 100644
--- a/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebConnectionTests.java
+++ b/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebConnectionTests.java
@@ -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");
diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/webdriver/MockMvcHtmlUnitDriverBuilderTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/webdriver/MockMvcHtmlUnitDriverBuilderTests.java
index 9fe37224bc2..0f2ec38b953 100644
--- a/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/webdriver/MockMvcHtmlUnitDriverBuilderTests.java
+++ b/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/webdriver/MockMvcHtmlUnitDriverBuilderTests.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2015 the original author or authors.
+ * Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
@@ -17,13 +17,12 @@
package org.springframework.test.web.servlet.htmlunit.webdriver;
import java.io.IOException;
-
import javax.servlet.http.HttpServletRequest;
+import com.gargoylesoftware.htmlunit.util.Cookie;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
-
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.springframework.beans.factory.annotation.Autowired;
@@ -35,14 +34,18 @@ 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 static org.hamcrest.CoreMatchers.*;
-import static org.junit.Assert.*;
-import static org.springframework.test.web.servlet.htmlunit.webdriver.MockMvcHtmlUnitDriverBuilder.*;
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.not;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
/**
* Integration tests for {@link MockMvcHtmlUnitDriverBuilder}.
@@ -65,57 +68,71 @@ public class MockMvcHtmlUnitDriverBuilderTests {
private HtmlUnitDriver driver;
+
@Before
public void setup() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
}
+
@Test(expected = IllegalArgumentException.class)
public void webAppContextSetupNull() {
- webAppContextSetup(null);
+ MockMvcHtmlUnitDriverBuilder.webAppContextSetup(null);
}
@Test(expected = IllegalArgumentException.class)
public void mockMvcSetupNull() {
- mockMvcSetup(null);
+ MockMvcHtmlUnitDriverBuilder.mockMvcSetup(null);
}
@Test
public void mockMvcSetupWithCustomDriverDelegate() throws Exception {
- WebConnectionHtmlUnitDriver preconfiguredDriver = new WebConnectionHtmlUnitDriver();
- this.driver = mockMvcSetup(this.mockMvc).withDelegate(preconfiguredDriver).build();
+ WebConnectionHtmlUnitDriver otherDriver = new WebConnectionHtmlUnitDriver();
+ this.driver = MockMvcHtmlUnitDriverBuilder.mockMvcSetup(this.mockMvc).withDelegate(otherDriver).build();
- assertMvcProcessed("http://localhost/test");
- Assume.group(TestGroup.PERFORMANCE, () -> assertDelegateProcessed("http://example.com/"));
+ assertMockMvcUsed("http://localhost/test");
+ Assume.group(TestGroup.PERFORMANCE, () -> assertMockMvcNotUsed("http://example.com/"));
}
@Test
public void mockMvcSetupWithDefaultDriverDelegate() throws Exception {
- this.driver = mockMvcSetup(this.mockMvc).build();
+ this.driver = MockMvcHtmlUnitDriverBuilder.mockMvcSetup(this.mockMvc).build();
- assertMvcProcessed("http://localhost/test");
- Assume.group(TestGroup.PERFORMANCE, () -> assertDelegateProcessed("http://example.com/"));
+ assertMockMvcUsed("http://localhost/test");
+ Assume.group(TestGroup.PERFORMANCE, () -> assertMockMvcNotUsed("http://example.com/"));
}
@Test
public void javaScriptEnabledByDefault() {
- this.driver = mockMvcSetup(this.mockMvc).build();
-
+ this.driver = MockMvcHtmlUnitDriverBuilder.mockMvcSetup(this.mockMvc).build();
assertTrue(this.driver.isJavascriptEnabled());
}
@Test
public void javaScriptDisabled() {
- this.driver = mockMvcSetup(this.mockMvc).javascriptEnabled(false).build();
-
+ this.driver = MockMvcHtmlUnitDriverBuilder.mockMvcSetup(this.mockMvc).javascriptEnabled(false).build();
assertFalse(this.driver.isJavascriptEnabled());
}
- private void assertMvcProcessed(String url) throws Exception {
+ @Test // SPR-14066
+ public void cookieManagerShared() throws Exception {
+ WebConnectionHtmlUnitDriver otherDriver = new WebConnectionHtmlUnitDriver();
+ this.mockMvc = MockMvcBuilders.standaloneSetup(new CookieController()).build();
+ this.driver = MockMvcHtmlUnitDriverBuilder.mockMvcSetup(this.mockMvc)
+ .withDelegate(otherDriver).build();
+
+ assertThat(get("http://localhost/"), equalTo(""));
+ Cookie cookie = new Cookie("localhost", "cookie", "cookieManagerShared");
+ otherDriver.getWebClient().getCookieManager().addCookie(cookie);
+ assertThat(get("http://localhost/"), equalTo("cookieManagerShared"));
+ }
+
+
+ private void assertMockMvcUsed(String url) throws Exception {
assertThat(get(url), containsString(EXPECTED_BODY));
}
- private void assertDelegateProcessed(String url) throws Exception {
+ private void assertMockMvcNotUsed(String url) throws Exception {
assertThat(get(url), not(containsString(EXPECTED_BODY)));
}
@@ -139,4 +156,11 @@ public class MockMvcHtmlUnitDriverBuilderTests {
}
}
+ @RestController
+ static class CookieController {
+ @RequestMapping("/")
+ public String cookie(@CookieValue("cookie") String cookie) {
+ return cookie;
+ }
+ }
}
\ No newline at end of file