Skip to content

Commit 4a6c2db

Browse files
committed
Merge pull request #1009 from rwinch/SPR-14066
2 parents 8246fe4 + d3822c8 commit 4a6c2db

File tree

10 files changed

+212
-117
lines changed

10 files changed

+212
-117
lines changed

spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/DelegatingWebConnection.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@
3737
* WebClient webClient = new WebClient();
3838
*
3939
* MockMvc mockMvc = ...
40-
* MockMvcWebConnection mockConnection = new MockMvcWebConnection(mockMvc);
41-
* mockConnection.setWebClient(webClient);
40+
* MockMvcWebConnection mockConnection = new MockMvcWebConnection(mockMvc, webClient);
4241
*
4342
* WebRequestMatcher cdnMatcher = new UrlRegexRequestMatcher(".*?//code.jquery.com/.*");
4443
* WebConnection httpConnection = new HttpWebConnection(webClient);

spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebClientBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public static MockMvcWebClientBuilder webAppContextSetup(WebApplicationContext c
106106
*/
107107
public MockMvcWebClientBuilder withDelegate(WebClient webClient) {
108108
Assert.notNull(webClient, "WebClient must not be null");
109-
webClient.setWebConnection(createConnection(webClient.getWebConnection()));
109+
webClient.setWebConnection(createConnection(webClient));
110110
this.webClient = webClient;
111111
return this;
112112
}

spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebConnection.java

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,16 @@ public final class MockMvcWebConnection implements WebConnection {
6363

6464
private WebClient webClient;
6565

66-
6766
/**
6867
* Create a new instance that assumes the context path of the application
6968
* is {@code ""} (i.e., the root context).
7069
* <p>For example, the URL {@code http://localhost/test/this} would use
7170
* {@code ""} as the context path.
7271
* @param mockMvc the {@code MockMvc} instance to use; never {@code null}
72+
* @param webClient the {@link WebClient} to use. never {@code null}
7373
*/
74-
public MockMvcWebConnection(MockMvc mockMvc) {
75-
this(mockMvc, "");
74+
public MockMvcWebConnection(MockMvc mockMvc, WebClient webClient) {
75+
this(mockMvc, webClient, "");
7676
}
7777

7878
/**
@@ -83,17 +83,47 @@ public MockMvcWebConnection(MockMvc mockMvc) {
8383
* which states that it can be an empty string and otherwise must start
8484
* with a "/" character and not end with a "/" character.
8585
* @param mockMvc the {@code MockMvc} instance to use; never {@code null}
86+
* @param webClient the {@link WebClient} to use. never {@code null}
8687
* @param contextPath the contextPath to use
8788
*/
88-
public MockMvcWebConnection(MockMvc mockMvc, String contextPath) {
89+
public MockMvcWebConnection(MockMvc mockMvc, WebClient webClient, String contextPath) {
8990
Assert.notNull(mockMvc, "MockMvc must not be null");
91+
Assert.notNull(webClient, "WebClient must not be null");
9092
validateContextPath(contextPath);
9193

92-
this.webClient = new WebClient();
94+
this.webClient = webClient;
9395
this.mockMvc = mockMvc;
9496
this.contextPath = contextPath;
9597
}
9698

99+
/**
100+
* Create a new instance that assumes the context path of the application
101+
* is {@code ""} (i.e., the root context).
102+
* <p>For example, the URL {@code http://localhost/test/this} would use
103+
* {@code ""} as the context path.
104+
* @param mockMvc the {@code MockMvc} instance to use; never {@code null}
105+
* @deprecated Use {@link #MockMvcWebConnection(MockMvc, WebClient)}
106+
*/
107+
@Deprecated
108+
public MockMvcWebConnection(MockMvc mockMvc) {
109+
this(mockMvc, "");
110+
}
111+
112+
/**
113+
* Create a new instance with the specified context path.
114+
* <p>The path may be {@code null} in which case the first path segment
115+
* of the URL is turned into the contextPath. Otherwise it must conform
116+
* to {@link javax.servlet.http.HttpServletRequest#getContextPath()}
117+
* which states that it can be an empty string and otherwise must start
118+
* with a "/" character and not end with a "/" character.
119+
* @param mockMvc the {@code MockMvc} instance to use; never {@code null}
120+
* @param contextPath the contextPath to use
121+
* @deprecated use {@link #MockMvcWebConnection(MockMvc, WebClient, String)}
122+
*/
123+
@Deprecated
124+
public MockMvcWebConnection(MockMvc mockMvc, String contextPath) {
125+
this(mockMvc, new WebClient(), contextPath);
126+
}
97127

98128
public void setWebClient(WebClient webClient) {
99129
Assert.notNull(webClient, "WebClient must not be null");

spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebConnectionBuilderSupport.java

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
55
* use this file except in compliance with the License. You may obtain a copy of
@@ -19,9 +19,11 @@
1919
import java.util.ArrayList;
2020
import java.util.List;
2121

22+
import com.gargoylesoftware.htmlunit.WebClient;
2223
import com.gargoylesoftware.htmlunit.WebConnection;
2324

2425
import org.springframework.test.web.servlet.MockMvc;
26+
import org.springframework.test.web.servlet.htmlunit.DelegatingWebConnection.DelegateWebConnection;
2527
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
2628
import org.springframework.test.web.servlet.setup.MockMvcConfigurer;
2729
import org.springframework.util.Assert;
@@ -43,7 +45,7 @@ public abstract class MockMvcWebConnectionBuilderSupport<T extends MockMvcWebCon
4345

4446
private final MockMvc mockMvc;
4547

46-
private final List<WebRequestMatcher> mockMvcRequestMatchers = new ArrayList<WebRequestMatcher>();
48+
private final List<WebRequestMatcher> requestMatchers = new ArrayList<WebRequestMatcher>();
4749

4850
private String contextPath = "";
4951

@@ -57,7 +59,7 @@ public abstract class MockMvcWebConnectionBuilderSupport<T extends MockMvcWebCon
5759
protected MockMvcWebConnectionBuilderSupport(MockMvc mockMvc) {
5860
Assert.notNull(mockMvc, "MockMvc must not be null");
5961
this.mockMvc = mockMvc;
60-
this.mockMvcRequestMatchers.add(new HostRequestMatcher("localhost"));
62+
this.requestMatchers.add(new HostRequestMatcher("localhost"));
6163
}
6264

6365
/**
@@ -116,7 +118,7 @@ public T alwaysUseMockMvc() {
116118
@SuppressWarnings("unchecked")
117119
public T useMockMvc(WebRequestMatcher... matchers) {
118120
for (WebRequestMatcher matcher : matchers) {
119-
this.mockMvcRequestMatchers.add(matcher);
121+
this.requestMatchers.add(matcher);
120122
}
121123
return (T) this;
122124
}
@@ -130,7 +132,7 @@ public T useMockMvc(WebRequestMatcher... matchers) {
130132
*/
131133
@SuppressWarnings("unchecked")
132134
public T useMockMvcForHosts(String... hosts) {
133-
this.mockMvcRequestMatchers.add(new HostRequestMatcher(hosts));
135+
this.requestMatchers.add(new HostRequestMatcher(hosts));
134136
return (T) this;
135137
}
136138

@@ -145,21 +147,41 @@ public T useMockMvcForHosts(String... hosts) {
145147
* @see #alwaysUseMockMvc()
146148
* @see #useMockMvc(WebRequestMatcher...)
147149
* @see #useMockMvcForHosts(String...)
150+
* @deprecated Use {@link #createConnection(WebClient)} instead
148151
*/
152+
@Deprecated
149153
protected final WebConnection createConnection(WebConnection defaultConnection) {
150154
Assert.notNull(defaultConnection, "Default WebConnection must not be null");
151-
MockMvcWebConnection mockMvcWebConnection = new MockMvcWebConnection(this.mockMvc, this.contextPath);
155+
return createConnection(new WebClient(), defaultConnection);
156+
}
157+
158+
/**
159+
* Create a new {@link WebConnection} that will use a {@link MockMvc}
160+
* instance if one of the specified {@link WebRequestMatcher} instances
161+
* matches.
162+
* @param webClient the WebClient to use if none of
163+
* the specified {@code WebRequestMatcher} instances matches; never {@code null}
164+
* @return a new {@code WebConnection} that will use a {@code MockMvc}
165+
* instance if one of the specified {@code WebRequestMatcher} matches
166+
* @see #alwaysUseMockMvc()
167+
* @see #useMockMvc(WebRequestMatcher...)
168+
* @see #useMockMvcForHosts(String...)
169+
* @since 4.3
170+
*/
171+
protected final WebConnection createConnection(WebClient webClient) {
172+
Assert.notNull(webClient, "WebClient must not be null");
173+
return createConnection(webClient, webClient.getWebConnection());
174+
}
152175

176+
private WebConnection createConnection(WebClient webClient, WebConnection defaultConnection) {
177+
WebConnection connection = new MockMvcWebConnection(this.mockMvc, webClient, this.contextPath);
153178
if (this.alwaysUseMockMvc) {
154-
return mockMvcWebConnection;
179+
return connection;
155180
}
156-
157-
List<DelegatingWebConnection.DelegateWebConnection> delegates = new ArrayList<DelegatingWebConnection.DelegateWebConnection>(
158-
this.mockMvcRequestMatchers.size());
159-
for (WebRequestMatcher matcher : this.mockMvcRequestMatchers) {
160-
delegates.add(new DelegatingWebConnection.DelegateWebConnection(matcher, mockMvcWebConnection));
181+
List<DelegateWebConnection> delegates = new ArrayList<DelegateWebConnection>(this.requestMatchers.size());
182+
for (WebRequestMatcher matcher : this.requestMatchers) {
183+
delegates.add(new DelegateWebConnection(matcher, connection));
161184
}
162-
163185
return new DelegatingWebConnection(defaultConnection, delegates);
164186
}
165187

spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/webdriver/MockMvcHtmlUnitDriverBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public MockMvcHtmlUnitDriverBuilder javascriptEnabled(boolean javascriptEnabled)
129129
public MockMvcHtmlUnitDriverBuilder withDelegate(WebConnectionHtmlUnitDriver driver) {
130130
Assert.notNull(driver, "HtmlUnitDriver must not be null");
131131
driver.setJavascriptEnabled(this.javascriptEnabled);
132-
driver.setWebConnection(createConnection(driver.getWebConnection()));
132+
driver.setWebConnection(createConnection(driver.getWebClient()));
133133
this.driver = driver;
134134
return this;
135135
}

spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/webdriver/WebConnectionHtmlUnitDriver.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ public class WebConnectionHtmlUnitDriver extends HtmlUnitDriver {
4141

4242
private WebClient webClient;
4343

44-
4544
public WebConnectionHtmlUnitDriver(BrowserVersion browserVersion) {
4645
super(browserVersion);
4746
}
@@ -107,4 +106,11 @@ public void setWebConnection(WebConnection webConnection) {
107106
this.webClient.setWebConnection(webConnection);
108107
}
109108

109+
/**
110+
* Gets the current {@link WebClient}
111+
* @return the current {@link WebClient}
112+
*/
113+
public WebClient getWebClient() {
114+
return this.webClient;
115+
}
110116
}

spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/MockMvcConnectionBuilderSupportTests.java

Lines changed: 41 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,6 +20,10 @@
2020
import java.net.URL;
2121
import javax.servlet.http.HttpServletRequest;
2222

23+
import com.gargoylesoftware.htmlunit.WebClient;
24+
import com.gargoylesoftware.htmlunit.WebConnection;
25+
import com.gargoylesoftware.htmlunit.WebRequest;
26+
import com.gargoylesoftware.htmlunit.WebResponse;
2327
import org.junit.Before;
2428
import org.junit.Test;
2529
import org.junit.runner.RunWith;
@@ -36,121 +40,112 @@
3640
import org.springframework.web.context.WebApplicationContext;
3741
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
3842

39-
import com.gargoylesoftware.htmlunit.WebConnection;
40-
import com.gargoylesoftware.htmlunit.WebRequest;
41-
import com.gargoylesoftware.htmlunit.WebResponse;
42-
4343
import static org.hamcrest.CoreMatchers.equalTo;
4444
import static org.hamcrest.CoreMatchers.nullValue;
4545
import static org.hamcrest.Matchers.notNullValue;
4646
import static org.junit.Assert.assertThat;
4747
import static org.mockito.Mockito.mock;
48+
import static org.mockito.Mockito.when;
4849

4950
/**
5051
* Integration tests for {@link MockMvcWebConnectionBuilderSupport}.
5152
*
5253
* @author Rob Winch
54+
* @author Rossen Stoyanchev
5355
* @since 4.2
5456
*/
5557
@RunWith(SpringJUnit4ClassRunner.class)
5658
@ContextConfiguration
5759
@WebAppConfiguration
58-
@SuppressWarnings("rawtypes")
5960
public class MockMvcConnectionBuilderSupportTests {
6061

61-
private final WebConnection delegateConnection = mock(WebConnection.class);
62-
6362
@Autowired
6463
private WebApplicationContext wac;
6564

66-
private MockMvc mockMvc;
65+
private WebClient client;
66+
67+
private MockMvcWebConnectionBuilderSupport builder;
6768

68-
private WebConnection connection;
6969

7070
@Before
7171
public void setup() {
72-
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
73-
74-
connection = new MockMvcWebConnectionBuilderSupport(mockMvc){}
75-
.createConnection(delegateConnection);
72+
this.client = mock(WebClient.class);
73+
when(this.client.getWebConnection()).thenReturn(mock(WebConnection.class));
74+
this.builder = new MockMvcWebConnectionBuilderSupport(this.wac) {};
7675
}
7776

77+
7878
@Test(expected = IllegalArgumentException.class)
7979
public void constructorMockMvcNull() {
80-
new MockMvcWebConnectionBuilderSupport((MockMvc)null){};
80+
new MockMvcWebConnectionBuilderSupport((MockMvc) null){};
8181
}
8282

8383
@Test(expected = IllegalArgumentException.class)
8484
public void constructorContextNull() {
85-
new MockMvcWebConnectionBuilderSupport((WebApplicationContext)null){};
85+
new MockMvcWebConnectionBuilderSupport((WebApplicationContext) null){};
8686
}
8787

8888
@Test
8989
public void context() throws Exception {
90-
connection = new MockMvcWebConnectionBuilderSupport(wac) {}
91-
.createConnection(delegateConnection);
90+
WebConnection conn = this.builder.createConnection(this.client);
9291

93-
assertMvcProcessed("http://localhost/");
94-
assertDelegateProcessed("http://example.com/");
92+
assertMockMvcUsed(conn, "http://localhost/");
93+
assertMockMvcNotUsed(conn, "http://example.com/");
9594
}
9695

9796
@Test
9897
public void mockMvc() throws Exception {
99-
assertMvcProcessed("http://localhost/");
100-
assertDelegateProcessed("http://example.com/");
98+
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
99+
WebConnection conn = new MockMvcWebConnectionBuilderSupport(mockMvc) {}.createConnection(this.client);
100+
101+
assertMockMvcUsed(conn, "http://localhost/");
102+
assertMockMvcNotUsed(conn, "http://example.com/");
101103
}
102104

103105
@Test
104106
public void mockMvcExampleDotCom() throws Exception {
105-
connection = new MockMvcWebConnectionBuilderSupport(wac) {}
106-
.useMockMvcForHosts("example.com")
107-
.createConnection(delegateConnection);
107+
WebConnection conn = this.builder.useMockMvcForHosts("example.com").createConnection(this.client);
108108

109-
assertMvcProcessed("http://localhost/");
110-
assertMvcProcessed("http://example.com/");
111-
assertDelegateProcessed("http://other.com/");
109+
assertMockMvcUsed(conn, "http://localhost/");
110+
assertMockMvcUsed(conn, "http://example.com/");
111+
assertMockMvcNotUsed(conn, "http://other.com/");
112112
}
113113

114114
@Test
115115
public void mockMvcAlwaysUseMockMvc() throws Exception {
116-
connection = new MockMvcWebConnectionBuilderSupport(wac) {}
117-
.alwaysUseMockMvc()
118-
.createConnection(delegateConnection);
119-
120-
assertMvcProcessed("http://other.com/");
116+
WebConnection conn = this.builder.alwaysUseMockMvc().createConnection(this.client);
117+
assertMockMvcUsed(conn, "http://other.com/");
121118
}
122119

123120
@Test
124121
public void defaultContextPathEmpty() throws Exception {
125-
connection = new MockMvcWebConnectionBuilderSupport(wac) {}
126-
.createConnection(delegateConnection);
127-
128-
assertThat(getWebResponse("http://localhost/abc").getContentAsString(), equalTo(""));
122+
WebConnection conn = this.builder.createConnection(this.client);
123+
assertThat(getResponse(conn, "http://localhost/abc").getContentAsString(), equalTo(""));
129124
}
130125

131126
@Test
132127
public void defaultContextPathCustom() throws Exception {
133-
connection = new MockMvcWebConnectionBuilderSupport(wac) {}
134-
.contextPath("/abc").createConnection(delegateConnection);
135-
136-
assertThat(getWebResponse("http://localhost/abc/def").getContentAsString(), equalTo("/abc"));
128+
WebConnection conn = this.builder.contextPath("/abc").createConnection(this.client);
129+
assertThat(getResponse(conn, "http://localhost/abc/def").getContentAsString(), equalTo("/abc"));
137130
}
138131

139-
private void assertMvcProcessed(String url) throws Exception {
140-
assertThat(getWebResponse(url), notNullValue());
132+
133+
private void assertMockMvcUsed(WebConnection connection, String url) throws Exception {
134+
assertThat(getResponse(connection, url), notNullValue());
141135
}
142136

143-
private void assertDelegateProcessed(String url) throws Exception {
144-
assertThat(getWebResponse(url), nullValue());
137+
private void assertMockMvcNotUsed(WebConnection connection, String url) throws Exception {
138+
assertThat(getResponse(connection, url), nullValue());
145139
}
146140

147-
private WebResponse getWebResponse(String url) throws IOException {
141+
private WebResponse getResponse(WebConnection connection, String url) throws IOException {
148142
return connection.getResponse(new WebRequest(new URL(url)));
149143
}
150144

151145

152146
@Configuration
153147
@EnableWebMvc
148+
@SuppressWarnings("unused")
154149
static class Config {
155150

156151
@RestController

0 commit comments

Comments
 (0)