MockMvcHtmlUnitDriverBuilderTests.java 5.3 KB
Newer Older
1
/*
R
Polish  
Rossen Stoyanchev 已提交
2
 * Copyright 2002-2016 the original author or authors.
3 4 5 6 7
 *
 * 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
 * the License at
 *
S
Sam Brannen 已提交
8
 *      http://www.apache.org/licenses/LICENSE-2.0
9 10 11 12 13 14 15
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
S
Sam Brannen 已提交
16

17 18 19 20 21
package org.springframework.test.web.servlet.htmlunit.webdriver;

import java.io.IOException;
import javax.servlet.http.HttpServletRequest;

R
Polish  
Rossen Stoyanchev 已提交
22
import com.gargoylesoftware.htmlunit.util.Cookie;
23 24 25 26 27 28 29 30 31 32 33 34
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;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
35 36
import org.springframework.tests.Assume;
import org.springframework.tests.TestGroup;
37
import org.springframework.web.bind.annotation.CookieValue;
38 39 40 41 42
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;

R
Polish  
Rossen Stoyanchev 已提交
43 44 45 46 47 48
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;
S
Sam Brannen 已提交
49

50
/**
S
Sam Brannen 已提交
51 52
 * Integration tests for {@link MockMvcHtmlUnitDriverBuilder}.
 *
53
 * @author Rob Winch
S
Sam Brannen 已提交
54
 * @author Sam Brannen
S
Sam Brannen 已提交
55
 * @since 4.2
56 57 58 59 60 61
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@WebAppConfiguration
public class MockMvcHtmlUnitDriverBuilderTests {

S
Sam Brannen 已提交
62
	private static final String EXPECTED_BODY = "MockMvcHtmlUnitDriverBuilderTests mvc";
63 64

	@Autowired
S
Sam Brannen 已提交
65
	private WebApplicationContext wac;
66

S
Sam Brannen 已提交
67
	private MockMvc mockMvc;
68

S
Sam Brannen 已提交
69
	private HtmlUnitDriver driver;
70

R
Polish  
Rossen Stoyanchev 已提交
71

72 73
	@Before
	public void setup() {
S
Sam Brannen 已提交
74
		this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
75 76
	}

R
Polish  
Rossen Stoyanchev 已提交
77

78
	@Test(expected = IllegalArgumentException.class)
79
	public void webAppContextSetupNull() {
R
Polish  
Rossen Stoyanchev 已提交
80
		MockMvcHtmlUnitDriverBuilder.webAppContextSetup(null);
81 82 83
	}

	@Test(expected = IllegalArgumentException.class)
84
	public void mockMvcSetupNull() {
R
Polish  
Rossen Stoyanchev 已提交
85
		MockMvcHtmlUnitDriverBuilder.mockMvcSetup(null);
86 87 88
	}

	@Test
89
	public void mockMvcSetupWithCustomDriverDelegate() throws Exception {
R
Polish  
Rossen Stoyanchev 已提交
90 91
		WebConnectionHtmlUnitDriver otherDriver = new WebConnectionHtmlUnitDriver();
		this.driver = MockMvcHtmlUnitDriverBuilder.mockMvcSetup(this.mockMvc).withDelegate(otherDriver).build();
92

R
Polish  
Rossen Stoyanchev 已提交
93 94
		assertMockMvcUsed("http://localhost/test");
		Assume.group(TestGroup.PERFORMANCE, () -> assertMockMvcNotUsed("http://example.com/"));
95 96 97
	}

	@Test
98
	public void mockMvcSetupWithDefaultDriverDelegate() throws Exception {
R
Polish  
Rossen Stoyanchev 已提交
99
		this.driver = MockMvcHtmlUnitDriverBuilder.mockMvcSetup(this.mockMvc).build();
100

R
Polish  
Rossen Stoyanchev 已提交
101 102
		assertMockMvcUsed("http://localhost/test");
		Assume.group(TestGroup.PERFORMANCE, () -> assertMockMvcNotUsed("http://example.com/"));
103 104 105
	}

	@Test
S
Sam Brannen 已提交
106
	public void javaScriptEnabledByDefault() {
R
Polish  
Rossen Stoyanchev 已提交
107
		this.driver = MockMvcHtmlUnitDriverBuilder.mockMvcSetup(this.mockMvc).build();
S
Sam Brannen 已提交
108
		assertTrue(this.driver.isJavascriptEnabled());
109 110 111
	}

	@Test
S
Sam Brannen 已提交
112
	public void javaScriptDisabled() {
R
Polish  
Rossen Stoyanchev 已提交
113
		this.driver = MockMvcHtmlUnitDriverBuilder.mockMvcSetup(this.mockMvc).javascriptEnabled(false).build();
S
Sam Brannen 已提交
114
		assertFalse(this.driver.isJavascriptEnabled());
115 116
	}

R
Polish  
Rossen Stoyanchev 已提交
117
	@Test // SPR-14066
118
	public void cookieManagerShared() throws Exception {
R
Polish  
Rossen Stoyanchev 已提交
119
		WebConnectionHtmlUnitDriver otherDriver = new WebConnectionHtmlUnitDriver();
120
		this.mockMvc = MockMvcBuilders.standaloneSetup(new CookieController()).build();
R
Polish  
Rossen Stoyanchev 已提交
121 122
		this.driver = MockMvcHtmlUnitDriverBuilder.mockMvcSetup(this.mockMvc)
				.withDelegate(otherDriver).build();
123 124

		assertThat(get("http://localhost/"), equalTo(""));
R
Polish  
Rossen Stoyanchev 已提交
125 126
		Cookie cookie = new Cookie("localhost", "cookie", "cookieManagerShared");
		otherDriver.getWebClient().getCookieManager().addCookie(cookie);
127
		assertThat(get("http://localhost/"), containsString("cookieManagerShared"));
128 129
	}

R
Polish  
Rossen Stoyanchev 已提交
130 131

	private void assertMockMvcUsed(String url) throws Exception {
132 133 134
		assertThat(get(url), containsString(EXPECTED_BODY));
	}

R
Polish  
Rossen Stoyanchev 已提交
135
	private void assertMockMvcNotUsed(String url) throws Exception {
136 137 138 139
		assertThat(get(url), not(containsString(EXPECTED_BODY)));
	}

	private String get(String url) throws IOException {
S
Sam Brannen 已提交
140 141
		this.driver.get(url);
		return this.driver.getPageSource();
142 143
	}

S
Sam Brannen 已提交
144

145 146 147
	@Configuration
	@EnableWebMvc
	static class Config {
S
Sam Brannen 已提交
148

149 150
		@RestController
		static class ContextPathController {
S
Sam Brannen 已提交
151

152 153 154 155 156 157 158
			@RequestMapping
			public String contextPath(HttpServletRequest request) {
				return EXPECTED_BODY;
			}
		}
	}

159 160
	@RestController
	static class CookieController {
R
Rob Winch 已提交
161
		@RequestMapping(value="/", produces="text/plain")
162 163 164 165
		public String cookie(@CookieValue("cookie") String cookie) {
			return cookie;
		}
	}
166
}