diff options
Diffstat (limited to 'spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebConnectionBuilderSupport.java')
-rw-r--r-- | spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebConnectionBuilderSupport.java | 48 |
1 files changed, 35 insertions, 13 deletions
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 cc957dca..c173fdfb 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. @@ -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<T extends MockMvcWebCon private final MockMvc mockMvc; - private final List<WebRequestMatcher> mockMvcRequestMatchers = new ArrayList<WebRequestMatcher>(); + private final List<WebRequestMatcher> requestMatchers = new ArrayList<WebRequestMatcher>(); private String contextPath = ""; @@ -57,7 +59,7 @@ public abstract class MockMvcWebConnectionBuilderSupport<T extends MockMvcWebCon protected MockMvcWebConnectionBuilderSupport(MockMvc mockMvc) { Assert.notNull(mockMvc, "MockMvc must not be null"); this.mockMvc = mockMvc; - this.mockMvcRequestMatchers.add(new HostRequestMatcher("localhost")); + this.requestMatchers.add(new HostRequestMatcher("localhost")); } /** @@ -116,7 +118,7 @@ public abstract class MockMvcWebConnectionBuilderSupport<T extends MockMvcWebCon @SuppressWarnings("unchecked") public T useMockMvc(WebRequestMatcher... matchers) { for (WebRequestMatcher matcher : matchers) { - this.mockMvcRequestMatchers.add(matcher); + this.requestMatchers.add(matcher); } return (T) this; } @@ -130,7 +132,7 @@ public abstract class MockMvcWebConnectionBuilderSupport<T extends MockMvcWebCon */ @SuppressWarnings("unchecked") public T useMockMvcForHosts(String... hosts) { - this.mockMvcRequestMatchers.add(new HostRequestMatcher(hosts)); + this.requestMatchers.add(new HostRequestMatcher(hosts)); return (T) this; } @@ -145,21 +147,41 @@ public abstract class MockMvcWebConnectionBuilderSupport<T extends MockMvcWebCon * @see #alwaysUseMockMvc() * @see #useMockMvc(WebRequestMatcher...) * @see #useMockMvcForHosts(String...) + * @deprecated Use {@link #createConnection(WebClient)} instead */ + @Deprecated protected final WebConnection createConnection(WebConnection defaultConnection) { Assert.notNull(defaultConnection, "Default WebConnection must not be null"); - MockMvcWebConnection mockMvcWebConnection = new MockMvcWebConnection(this.mockMvc, this.contextPath); + return createConnection(new WebClient(), defaultConnection); + } + /** + * Create a new {@link WebConnection} that will use a {@link MockMvc} + * instance if one of the specified {@link WebRequestMatcher} instances + * matches. + * @param webClient the WebClient to use if none of the specified + * {@code WebRequestMatcher} instances matches (never {@code null}) + * @return a new {@code WebConnection} that will use a {@code MockMvc} + * instance if one of the specified {@code WebRequestMatcher} matches + * @see #alwaysUseMockMvc() + * @see #useMockMvc(WebRequestMatcher...) + * @see #useMockMvcForHosts(String...) + * @since 4.3 + */ + protected final WebConnection createConnection(WebClient webClient) { + Assert.notNull(webClient, "WebClient must not be null"); + return createConnection(webClient, webClient.getWebConnection()); + } + + private WebConnection createConnection(WebClient webClient, WebConnection defaultConnection) { + WebConnection connection = new MockMvcWebConnection(this.mockMvc, webClient, this.contextPath); if (this.alwaysUseMockMvc) { - return mockMvcWebConnection; + return connection; } - - List<DelegatingWebConnection.DelegateWebConnection> delegates = new ArrayList<DelegatingWebConnection.DelegateWebConnection>( - this.mockMvcRequestMatchers.size()); - for (WebRequestMatcher matcher : this.mockMvcRequestMatchers) { - delegates.add(new DelegatingWebConnection.DelegateWebConnection(matcher, mockMvcWebConnection)); + List<DelegateWebConnection> delegates = new ArrayList<DelegateWebConnection>(this.requestMatchers.size()); + for (WebRequestMatcher matcher : this.requestMatchers) { + delegates.add(new DelegateWebConnection(matcher, connection)); } - return new DelegatingWebConnection(defaultConnection, delegates); } |