MockMvcHtmlUnitDriverBuilderTests.java 5.1 KB
Newer Older
1
/*
R
Polish  
Rossen Stoyanchev 已提交
2
 * Copyright 2002-2016 the original author or authors.
3
 *
J
Juergen Hoeller 已提交
4 5 6
 * 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
7
 *
S
Sam Brannen 已提交
8
 *      http://www.apache.org/licenses/LICENSE-2.0
9 10
 *
 * Unless required by applicable law or agreed to in writing, software
J
Juergen Hoeller 已提交
11 12 13 14
 * 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.
15
 */
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
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;
S
Sam Brannen 已提交
31
import org.springframework.test.context.junit4.SpringRunner;
32 33 34
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;

J
Juergen Hoeller 已提交
43 44
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
S
Sam Brannen 已提交
45

46
/**
S
Sam Brannen 已提交
47 48
 * Integration tests for {@link MockMvcHtmlUnitDriverBuilder}.
 *
49
 * @author Rob Winch
S
Sam Brannen 已提交
50
 * @author Sam Brannen
S
Sam Brannen 已提交
51
 * @since 4.2
52
 */
S
Sam Brannen 已提交
53
@RunWith(SpringRunner.class)
54 55 56 57
@ContextConfiguration
@WebAppConfiguration
public class MockMvcHtmlUnitDriverBuilderTests {

S
Sam Brannen 已提交
58
	private static final String EXPECTED_BODY = "MockMvcHtmlUnitDriverBuilderTests mvc";
59 60

	@Autowired
S
Sam Brannen 已提交
61
	private WebApplicationContext wac;
62

S
Sam Brannen 已提交
63
	private MockMvc mockMvc;
64

S
Sam Brannen 已提交
65
	private HtmlUnitDriver driver;
66

R
Polish  
Rossen Stoyanchev 已提交
67

68 69
	@Before
	public void setup() {
S
Sam Brannen 已提交
70
		this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
71 72
	}

R
Polish  
Rossen Stoyanchev 已提交
73

74
	@Test(expected = IllegalArgumentException.class)
75
	public void webAppContextSetupNull() {
R
Polish  
Rossen Stoyanchev 已提交
76
		MockMvcHtmlUnitDriverBuilder.webAppContextSetup(null);
77 78 79
	}

	@Test(expected = IllegalArgumentException.class)
80
	public void mockMvcSetupNull() {
R
Polish  
Rossen Stoyanchev 已提交
81
		MockMvcHtmlUnitDriverBuilder.mockMvcSetup(null);
82 83 84
	}

	@Test
85
	public void mockMvcSetupWithCustomDriverDelegate() throws Exception {
R
Polish  
Rossen Stoyanchev 已提交
86 87
		WebConnectionHtmlUnitDriver otherDriver = new WebConnectionHtmlUnitDriver();
		this.driver = MockMvcHtmlUnitDriverBuilder.mockMvcSetup(this.mockMvc).withDelegate(otherDriver).build();
88

R
Polish  
Rossen Stoyanchev 已提交
89
		assertMockMvcUsed("http://localhost/test");
S
Spring Operator 已提交
90
		Assume.group(TestGroup.PERFORMANCE, () -> assertMockMvcNotUsed("https://example.com/"));
91 92 93
	}

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

R
Polish  
Rossen Stoyanchev 已提交
97
		assertMockMvcUsed("http://localhost/test");
S
Spring Operator 已提交
98
		Assume.group(TestGroup.PERFORMANCE, () -> assertMockMvcNotUsed("https://example.com/"));
99 100 101
	}

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

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

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

		assertThat(get("http://localhost/"), equalTo(""));
R
Polish  
Rossen Stoyanchev 已提交
121 122
		Cookie cookie = new Cookie("localhost", "cookie", "cookieManagerShared");
		otherDriver.getWebClient().getCookieManager().addCookie(cookie);
S
Sam Brannen 已提交
123
		assertThat(get("http://localhost/"), equalTo("cookieManagerShared"));
124 125
	}

R
Polish  
Rossen Stoyanchev 已提交
126 127

	private void assertMockMvcUsed(String url) throws Exception {
128 129 130
		assertThat(get(url), containsString(EXPECTED_BODY));
	}

R
Polish  
Rossen Stoyanchev 已提交
131
	private void assertMockMvcNotUsed(String url) throws Exception {
132 133 134 135
		assertThat(get(url), not(containsString(EXPECTED_BODY)));
	}

	private String get(String url) throws IOException {
S
Sam Brannen 已提交
136 137
		this.driver.get(url);
		return this.driver.getPageSource();
138 139
	}

S
Sam Brannen 已提交
140

141 142 143
	@Configuration
	@EnableWebMvc
	static class Config {
S
Sam Brannen 已提交
144

145 146
		@RestController
		static class ContextPathController {
S
Sam Brannen 已提交
147

148 149 150 151 152 153 154
			@RequestMapping
			public String contextPath(HttpServletRequest request) {
				return EXPECTED_BODY;
			}
		}
	}

155 156
	@RestController
	static class CookieController {
S
Sam Brannen 已提交
157 158 159

		@RequestMapping(path = "/", produces = "text/plain")
		String cookie(@CookieValue("cookie") String cookie) {
160 161 162
			return cookie;
		}
	}
S
Sam Brannen 已提交
163 164

}