This commit is contained in:
Rossen Stoyanchev 2016-03-18 12:00:45 -04:00
parent 7d96ad1f6e
commit d3822c8d19
5 changed files with 115 additions and 198 deletions

View File

@ -37,8 +37,7 @@ import org.springframework.util.Assert;
* WebClient webClient = new WebClient(); * WebClient webClient = new WebClient();
* *
* MockMvc mockMvc = ... * MockMvc mockMvc = ...
* MockMvcWebConnection mockConnection = new MockMvcWebConnection(mockMvc); * MockMvcWebConnection mockConnection = new MockMvcWebConnection(mockMvc, webClient);
* mockConnection.setWebClient(webClient);
* *
* WebRequestMatcher cdnMatcher = new UrlRegexRequestMatcher(".*?//code.jquery.com/.*"); * WebRequestMatcher cdnMatcher = new UrlRegexRequestMatcher(".*?//code.jquery.com/.*");
* WebConnection httpConnection = new HttpWebConnection(webClient); * WebConnection httpConnection = new HttpWebConnection(webClient);

View File

@ -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 * 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 * use this file except in compliance with the License. You may obtain a copy of
@ -23,6 +23,7 @@ import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebConnection; import com.gargoylesoftware.htmlunit.WebConnection;
import org.springframework.test.web.servlet.MockMvc; 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.MockMvcBuilders;
import org.springframework.test.web.servlet.setup.MockMvcConfigurer; import org.springframework.test.web.servlet.setup.MockMvcConfigurer;
import org.springframework.util.Assert; import org.springframework.util.Assert;
@ -44,7 +45,7 @@ public abstract class MockMvcWebConnectionBuilderSupport<T extends MockMvcWebCon
private final MockMvc mockMvc; private final MockMvc mockMvc;
private final List<WebRequestMatcher> mockMvcRequestMatchers = new ArrayList<WebRequestMatcher>(); private final List<WebRequestMatcher> requestMatchers = new ArrayList<WebRequestMatcher>();
private String contextPath = ""; private String contextPath = "";
@ -58,7 +59,7 @@ public abstract class MockMvcWebConnectionBuilderSupport<T extends MockMvcWebCon
protected MockMvcWebConnectionBuilderSupport(MockMvc mockMvc) { protected MockMvcWebConnectionBuilderSupport(MockMvc mockMvc) {
Assert.notNull(mockMvc, "MockMvc must not be null"); Assert.notNull(mockMvc, "MockMvc must not be null");
this.mockMvc = mockMvc; this.mockMvc = mockMvc;
this.mockMvcRequestMatchers.add(new HostRequestMatcher("localhost")); this.requestMatchers.add(new HostRequestMatcher("localhost"));
} }
/** /**
@ -117,7 +118,7 @@ public abstract class MockMvcWebConnectionBuilderSupport<T extends MockMvcWebCon
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public T useMockMvc(WebRequestMatcher... matchers) { public T useMockMvc(WebRequestMatcher... matchers) {
for (WebRequestMatcher matcher : matchers) { for (WebRequestMatcher matcher : matchers) {
this.mockMvcRequestMatchers.add(matcher); this.requestMatchers.add(matcher);
} }
return (T) this; return (T) this;
} }
@ -131,7 +132,7 @@ public abstract class MockMvcWebConnectionBuilderSupport<T extends MockMvcWebCon
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public T useMockMvcForHosts(String... hosts) { public T useMockMvcForHosts(String... hosts) {
this.mockMvcRequestMatchers.add(new HostRequestMatcher(hosts)); this.requestMatchers.add(new HostRequestMatcher(hosts));
return (T) this; return (T) this;
} }
@ -165,38 +166,23 @@ public abstract class MockMvcWebConnectionBuilderSupport<T extends MockMvcWebCon
* @see #alwaysUseMockMvc() * @see #alwaysUseMockMvc()
* @see #useMockMvc(WebRequestMatcher...) * @see #useMockMvc(WebRequestMatcher...)
* @see #useMockMvcForHosts(String...) * @see #useMockMvcForHosts(String...)
* @since 4.3
*/ */
protected final WebConnection createConnection(WebClient webClient) { protected final WebConnection createConnection(WebClient webClient) {
Assert.notNull(webClient, "WebClient must not be null"); Assert.notNull(webClient, "WebClient must not be null");
return createConnection(webClient, webClient.getWebConnection()); return createConnection(webClient, webClient.getWebConnection());
} }
/**
* 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}
* @param defaultConnection the WebConnection to use
* @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...)
*/
private WebConnection createConnection(WebClient webClient, WebConnection defaultConnection) { private WebConnection createConnection(WebClient webClient, WebConnection defaultConnection) {
MockMvcWebConnection mockMvcWebConnection = new MockMvcWebConnection(this.mockMvc, webClient, this.contextPath); WebConnection connection = new MockMvcWebConnection(this.mockMvc, webClient, this.contextPath);
if (this.alwaysUseMockMvc) { if (this.alwaysUseMockMvc) {
return mockMvcWebConnection; return connection;
} }
List<DelegateWebConnection> delegates = new ArrayList<DelegateWebConnection>(this.requestMatchers.size());
List<DelegatingWebConnection.DelegateWebConnection> delegates = new ArrayList<DelegatingWebConnection.DelegateWebConnection>( for (WebRequestMatcher matcher : this.requestMatchers) {
this.mockMvcRequestMatchers.size()); delegates.add(new DelegateWebConnection(matcher, connection));
for (WebRequestMatcher matcher : this.mockMvcRequestMatchers) {
delegates.add(new DelegatingWebConnection.DelegateWebConnection(matcher, mockMvcWebConnection));
} }
return new DelegatingWebConnection(defaultConnection, delegates); return new DelegatingWebConnection(defaultConnection, delegates);
} }
} }

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 java.net.URL;
import javax.servlet.http.HttpServletRequest; 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.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -36,11 +40,6 @@ import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.config.annotation.EnableWebMvc; 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;
import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.nullValue; import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.notNullValue;
@ -52,166 +51,101 @@ import static org.mockito.Mockito.when;
* Integration tests for {@link MockMvcWebConnectionBuilderSupport}. * Integration tests for {@link MockMvcWebConnectionBuilderSupport}.
* *
* @author Rob Winch * @author Rob Winch
* @author Rossen Stoyanchev
* @since 4.2 * @since 4.2
*/ */
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration @ContextConfiguration
@WebAppConfiguration @WebAppConfiguration
@SuppressWarnings({"rawtypes","deprecation"})
public class MockMvcConnectionBuilderSupportTests { public class MockMvcConnectionBuilderSupportTests {
private WebConnection delegateConnection;
private WebClient webClient;
@Autowired @Autowired
private WebApplicationContext wac; private WebApplicationContext wac;
private MockMvc mockMvc; private WebClient client;
private MockMvcWebConnectionBuilderSupport builder;
private WebConnection connection;
@Before @Before
public void setup() { public void setup() {
delegateConnection = mock(WebConnection.class); this.client = mock(WebClient.class);
when(this.client.getWebConnection()).thenReturn(mock(WebConnection.class));
webClient = mock(WebClient.class); this.builder = new MockMvcWebConnectionBuilderSupport(this.wac) {};
when(webClient.getWebConnection()).thenReturn(delegateConnection);
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
connection = new MockMvcWebConnectionBuilderSupport(mockMvc){}
.createConnection(webClient);
} }
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void constructorMockMvcNull() { public void constructorMockMvcNull() {
new MockMvcWebConnectionBuilderSupport((MockMvc)null){}; new MockMvcWebConnectionBuilderSupport((MockMvc) null){};
} }
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void constructorContextNull() { public void constructorContextNull() {
new MockMvcWebConnectionBuilderSupport((WebApplicationContext)null){}; new MockMvcWebConnectionBuilderSupport((WebApplicationContext) null){};
}
@Test
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 @Test
public void context() throws Exception { public void context() throws Exception {
connection = new MockMvcWebConnectionBuilderSupport(wac) {} WebConnection conn = this.builder.createConnection(this.client);
.createConnection(webClient);
assertMvcProcessed("http://localhost/"); assertMockMvcUsed(conn, "http://localhost/");
assertDelegateProcessed("http://example.com/"); assertMockMvcNotUsed(conn, "http://example.com/");
} }
@Test @Test
public void mockMvc() throws Exception { public void mockMvc() throws Exception {
assertMvcProcessed("http://localhost/"); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
assertDelegateProcessed("http://example.com/"); WebConnection conn = new MockMvcWebConnectionBuilderSupport(mockMvc) {}.createConnection(this.client);
assertMockMvcUsed(conn, "http://localhost/");
assertMockMvcNotUsed(conn, "http://example.com/");
} }
@Test @Test
public void mockMvcExampleDotCom() throws Exception { public void mockMvcExampleDotCom() throws Exception {
connection = new MockMvcWebConnectionBuilderSupport(wac) {} WebConnection conn = this.builder.useMockMvcForHosts("example.com").createConnection(this.client);
.useMockMvcForHosts("example.com")
.createConnection(webClient);
assertMvcProcessed("http://localhost/"); assertMockMvcUsed(conn, "http://localhost/");
assertMvcProcessed("http://example.com/"); assertMockMvcUsed(conn, "http://example.com/");
assertDelegateProcessed("http://other.com/"); assertMockMvcNotUsed(conn, "http://other.com/");
} }
@Test @Test
public void mockMvcAlwaysUseMockMvc() throws Exception { public void mockMvcAlwaysUseMockMvc() throws Exception {
connection = new MockMvcWebConnectionBuilderSupport(wac) {} WebConnection conn = this.builder.alwaysUseMockMvc().createConnection(this.client);
.alwaysUseMockMvc() assertMockMvcUsed(conn, "http://other.com/");
.createConnection(webClient);
assertMvcProcessed("http://other.com/");
} }
@Test @Test
public void defaultContextPathEmpty() throws Exception { public void defaultContextPathEmpty() throws Exception {
connection = new MockMvcWebConnectionBuilderSupport(wac) {} WebConnection conn = this.builder.createConnection(this.client);
.createConnection(webClient); assertThat(getResponse(conn, "http://localhost/abc").getContentAsString(), equalTo(""));
assertThat(getWebResponse("http://localhost/abc").getContentAsString(), equalTo(""));
} }
@Test @Test
public void defaultContextPathCustom() throws Exception { public void defaultContextPathCustom() throws Exception {
connection = new MockMvcWebConnectionBuilderSupport(wac) {} WebConnection conn = this.builder.contextPath("/abc").createConnection(this.client);
.contextPath("/abc").createConnection(webClient); assertThat(getResponse(conn, "http://localhost/abc/def").getContentAsString(), equalTo("/abc"));
assertThat(getWebResponse("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 { private void assertMockMvcNotUsed(WebConnection connection, String url) throws Exception {
assertThat(getWebResponse(url), nullValue()); 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))); return connection.getResponse(new WebRequest(new URL(url)));
} }
@Configuration @Configuration
@EnableWebMvc @EnableWebMvc
@SuppressWarnings("unused")
static class Config { static class Config {
@RestController @RestController

View File

@ -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 * 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 * 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.io.IOException;
import java.net.URL; import java.net.URL;
import javax.servlet.http.HttpServletRequest; 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.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -40,20 +43,16 @@ import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import com.gargoylesoftware.htmlunit.WebClient; import static org.hamcrest.CoreMatchers.equalTo;
import com.gargoylesoftware.htmlunit.WebRequest; import static org.hamcrest.CoreMatchers.not;
import com.gargoylesoftware.htmlunit.WebResponse; import static org.junit.Assert.assertThat;
import com.gargoylesoftware.htmlunit.util.Cookie;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.springframework.test.web.servlet.htmlunit.MockMvcWebClientBuilder.*;
/** /**
* Integration tests for {@link MockMvcWebClientBuilder}. * Integration tests for {@link MockMvcWebClientBuilder}.
* *
* @author Rob Winch * @author Rob Winch
* @author Sam Brannen * @author Sam Brannen
* @author Rossen Stoyanchev
* @since 4.2 * @since 4.2
*/ */
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ -61,8 +60,6 @@ import static org.springframework.test.web.servlet.htmlunit.MockMvcWebClientBuil
@WebAppConfiguration @WebAppConfiguration
public class MockMvcWebClientBuilderTests { public class MockMvcWebClientBuilderTests {
private WebClient webClient;
@Autowired @Autowired
private WebApplicationContext wac; private WebApplicationContext wac;
@ -74,54 +71,55 @@ public class MockMvcWebClientBuilderTests {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
} }
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void mockMvcSetupNull() { public void mockMvcSetupNull() {
mockMvcSetup(null); MockMvcWebClientBuilder.mockMvcSetup(null);
} }
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void webAppContextSetupNull() { public void webAppContextSetupNull() {
webAppContextSetup(null); MockMvcWebClientBuilder.webAppContextSetup(null);
} }
@Test @Test
public void mockMvcSetupWithDefaultWebClientDelegate() throws Exception { public void mockMvcSetupWithDefaultWebClientDelegate() throws Exception {
this.webClient = mockMvcSetup(this.mockMvc).build(); WebClient client = MockMvcWebClientBuilder.mockMvcSetup(this.mockMvc).build();
assertMvcProcessed("http://localhost/test"); assertMockMvcUsed(client, "http://localhost/test");
Assume.group(TestGroup.PERFORMANCE, () -> assertDelegateProcessed("http://example.com/")); Assume.group(TestGroup.PERFORMANCE, () -> assertMockMvcNotUsed(client, "http://example.com/"));
} }
@Test @Test
public void mockMvcSetupWithCustomWebClientDelegate() throws Exception { public void mockMvcSetupWithCustomWebClientDelegate() throws Exception {
WebClient preconfiguredWebClient = new WebClient(); WebClient otherClient = new WebClient();
this.webClient = mockMvcSetup(this.mockMvc).withDelegate(preconfiguredWebClient).build(); WebClient client = MockMvcWebClientBuilder.mockMvcSetup(this.mockMvc).withDelegate(otherClient).build();
assertMvcProcessed("http://localhost/test"); assertMockMvcUsed(client, "http://localhost/test");
Assume.group(TestGroup.PERFORMANCE, () -> assertDelegateProcessed("http://example.com/")); Assume.group(TestGroup.PERFORMANCE, () -> assertMockMvcNotUsed(client, "http://example.com/"));
} }
// SPR-14066 @Test // SPR-14066
@Test
public void cookieManagerShared() throws Exception { public void cookieManagerShared() throws Exception {
this.mockMvc = MockMvcBuilders.standaloneSetup(new CookieController()).build(); this.mockMvc = MockMvcBuilders.standaloneSetup(new CookieController()).build();
this.webClient = mockMvcSetup(this.mockMvc).build(); WebClient client = MockMvcWebClientBuilder.mockMvcSetup(this.mockMvc).build();
assertThat(getWebResponse("http://localhost/").getContentAsString(), equalTo("")); assertThat(getResponse(client, "http://localhost/").getContentAsString(), equalTo(""));
this.webClient.getCookieManager().addCookie(new Cookie("localhost", "cookie", "cookieManagerShared")); client.getCookieManager().addCookie(new Cookie("localhost", "cookie", "cookieManagerShared"));
assertThat(getWebResponse("http://localhost/").getContentAsString(), equalTo("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 { private void assertMockMvcNotUsed(WebClient client, String url) throws Exception {
assertThat(getWebResponse(url).getContentAsString(), not(equalTo("mvc"))); assertThat(getResponse(client, url).getContentAsString(), not(equalTo("mvc")));
} }
private WebResponse getWebResponse(String url) throws IOException { private WebResponse getResponse(WebClient client, String url) throws IOException {
return this.webClient.getWebConnection().getResponse(new WebRequest(new URL(url))); return client.getWebConnection().getResponse(new WebRequest(new URL(url)));
} }

View File

@ -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 * 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 * 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; package org.springframework.test.web.servlet.htmlunit.webdriver;
import java.io.IOException; import java.io.IOException;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import com.gargoylesoftware.htmlunit.util.Cookie;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.openqa.selenium.htmlunit.HtmlUnitDriver; import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -41,11 +40,12 @@ import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import com.gargoylesoftware.htmlunit.util.Cookie; import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.*; import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.*; import static org.junit.Assert.assertFalse;
import static org.springframework.test.web.servlet.htmlunit.webdriver.MockMvcHtmlUnitDriverBuilder.*; import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
/** /**
* Integration tests for {@link MockMvcHtmlUnitDriverBuilder}. * Integration tests for {@link MockMvcHtmlUnitDriverBuilder}.
@ -68,71 +68,71 @@ public class MockMvcHtmlUnitDriverBuilderTests {
private HtmlUnitDriver driver; private HtmlUnitDriver driver;
@Before @Before
public void setup() { public void setup() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
} }
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void webAppContextSetupNull() { public void webAppContextSetupNull() {
webAppContextSetup(null); MockMvcHtmlUnitDriverBuilder.webAppContextSetup(null);
} }
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void mockMvcSetupNull() { public void mockMvcSetupNull() {
mockMvcSetup(null); MockMvcHtmlUnitDriverBuilder.mockMvcSetup(null);
} }
@Test @Test
public void mockMvcSetupWithCustomDriverDelegate() throws Exception { public void mockMvcSetupWithCustomDriverDelegate() throws Exception {
WebConnectionHtmlUnitDriver preconfiguredDriver = new WebConnectionHtmlUnitDriver(); WebConnectionHtmlUnitDriver otherDriver = new WebConnectionHtmlUnitDriver();
this.driver = mockMvcSetup(this.mockMvc).withDelegate(preconfiguredDriver).build(); this.driver = MockMvcHtmlUnitDriverBuilder.mockMvcSetup(this.mockMvc).withDelegate(otherDriver).build();
assertMvcProcessed("http://localhost/test"); assertMockMvcUsed("http://localhost/test");
Assume.group(TestGroup.PERFORMANCE, () -> assertDelegateProcessed("http://example.com/")); Assume.group(TestGroup.PERFORMANCE, () -> assertMockMvcNotUsed("http://example.com/"));
} }
@Test @Test
public void mockMvcSetupWithDefaultDriverDelegate() throws Exception { public void mockMvcSetupWithDefaultDriverDelegate() throws Exception {
this.driver = mockMvcSetup(this.mockMvc).build(); this.driver = MockMvcHtmlUnitDriverBuilder.mockMvcSetup(this.mockMvc).build();
assertMvcProcessed("http://localhost/test"); assertMockMvcUsed("http://localhost/test");
Assume.group(TestGroup.PERFORMANCE, () -> assertDelegateProcessed("http://example.com/")); Assume.group(TestGroup.PERFORMANCE, () -> assertMockMvcNotUsed("http://example.com/"));
} }
@Test @Test
public void javaScriptEnabledByDefault() { public void javaScriptEnabledByDefault() {
this.driver = mockMvcSetup(this.mockMvc).build(); this.driver = MockMvcHtmlUnitDriverBuilder.mockMvcSetup(this.mockMvc).build();
assertTrue(this.driver.isJavascriptEnabled()); assertTrue(this.driver.isJavascriptEnabled());
} }
@Test @Test
public void javaScriptDisabled() { public void javaScriptDisabled() {
this.driver = mockMvcSetup(this.mockMvc).javascriptEnabled(false).build(); this.driver = MockMvcHtmlUnitDriverBuilder.mockMvcSetup(this.mockMvc).javascriptEnabled(false).build();
assertFalse(this.driver.isJavascriptEnabled()); assertFalse(this.driver.isJavascriptEnabled());
} }
// SPR-14066 @Test // SPR-14066
@Test
public void cookieManagerShared() throws Exception { public void cookieManagerShared() throws Exception {
WebConnectionHtmlUnitDriver delegateDriver = new WebConnectionHtmlUnitDriver(); WebConnectionHtmlUnitDriver otherDriver = new WebConnectionHtmlUnitDriver();
this.mockMvc = MockMvcBuilders.standaloneSetup(new CookieController()).build(); this.mockMvc = MockMvcBuilders.standaloneSetup(new CookieController()).build();
this.driver = mockMvcSetup(this.mockMvc) this.driver = MockMvcHtmlUnitDriverBuilder.mockMvcSetup(this.mockMvc)
.withDelegate(delegateDriver) .withDelegate(otherDriver).build();
.build();
assertThat(get("http://localhost/"), equalTo("")); assertThat(get("http://localhost/"), equalTo(""));
delegateDriver.getWebClient().getCookieManager().addCookie(new Cookie("localhost", "cookie", "cookieManagerShared")); Cookie cookie = new Cookie("localhost", "cookie", "cookieManagerShared");
otherDriver.getWebClient().getCookieManager().addCookie(cookie);
assertThat(get("http://localhost/"), equalTo("cookieManagerShared")); assertThat(get("http://localhost/"), equalTo("cookieManagerShared"));
} }
private void assertMvcProcessed(String url) throws Exception {
private void assertMockMvcUsed(String url) throws Exception {
assertThat(get(url), containsString(EXPECTED_BODY)); 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))); assertThat(get(url), not(containsString(EXPECTED_BODY)));
} }