MockMvcHtmlUnitDriverBuilderTests.java 5.0 KB
Newer Older
1 2 3 4 5 6 7
/*
 * Copyright 2002-2015 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. 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
package org.springframework.test.web.servlet.htmlunit.webdriver;

import java.io.IOException;
S
Sam Brannen 已提交
20

21 22 23 24 25
import javax.servlet.http.HttpServletRequest;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
S
Sam Brannen 已提交
26

27 28 29 30 31 32 33 34 35
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;
36 37
import org.springframework.tests.Assume;
import org.springframework.tests.TestGroup;
38
import org.springframework.web.bind.annotation.CookieValue;
39 40 41 42 43
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;

44 45
import com.gargoylesoftware.htmlunit.util.Cookie;

S
Sam Brannen 已提交
46 47
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
48
import static org.springframework.test.web.servlet.htmlunit.webdriver.MockMvcHtmlUnitDriverBuilder.*;
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 71 72

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

	@Test(expected = IllegalArgumentException.class)
77 78
	public void webAppContextSetupNull() {
		webAppContextSetup(null);
79 80 81
	}

	@Test(expected = IllegalArgumentException.class)
82 83
	public void mockMvcSetupNull() {
		mockMvcSetup(null);
84 85 86
	}

	@Test
87 88 89
	public void mockMvcSetupWithCustomDriverDelegate() throws Exception {
		WebConnectionHtmlUnitDriver preconfiguredDriver = new WebConnectionHtmlUnitDriver();
		this.driver = mockMvcSetup(this.mockMvc).withDelegate(preconfiguredDriver).build();
90 91

		assertMvcProcessed("http://localhost/test");
92
		Assume.group(TestGroup.PERFORMANCE, () -> assertDelegateProcessed("http://example.com/"));
93 94 95
	}

	@Test
96 97
	public void mockMvcSetupWithDefaultDriverDelegate() throws Exception {
		this.driver = mockMvcSetup(this.mockMvc).build();
98 99

		assertMvcProcessed("http://localhost/test");
100
		Assume.group(TestGroup.PERFORMANCE, () -> assertDelegateProcessed("http://example.com/"));
101 102 103
	}

	@Test
S
Sam Brannen 已提交
104
	public void javaScriptEnabledByDefault() {
105
		this.driver = mockMvcSetup(this.mockMvc).build();
106

S
Sam Brannen 已提交
107
		assertTrue(this.driver.isJavascriptEnabled());
108 109 110
	}

	@Test
S
Sam Brannen 已提交
111
	public void javaScriptDisabled() {
112
		this.driver = mockMvcSetup(this.mockMvc).javascriptEnabled(false).build();
113

S
Sam Brannen 已提交
114
		assertFalse(this.driver.isJavascriptEnabled());
115 116
	}

117 118 119 120 121 122 123 124 125 126 127 128 129 130
	// SPR-14066
	@Test
	public void cookieManagerShared() throws Exception {
		WebConnectionHtmlUnitDriver delegateDriver = new WebConnectionHtmlUnitDriver();
		this.mockMvc = MockMvcBuilders.standaloneSetup(new CookieController()).build();
		this.driver = mockMvcSetup(this.mockMvc)
				.withDelegate(delegateDriver)
				.build();

		assertThat(get("http://localhost/"), equalTo(""));
		delegateDriver.getWebClient().getCookieManager().addCookie(new Cookie("localhost", "cookie", "cookieManagerShared"));
		assertThat(get("http://localhost/"), equalTo("cookieManagerShared"));
	}

131 132 133 134 135 136 137 138 139
	private void assertMvcProcessed(String url) throws Exception {
		assertThat(get(url), containsString(EXPECTED_BODY));
	}

	private void assertDelegateProcessed(String url) throws Exception {
		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 161 162 163 164 165
	@RestController
	static class CookieController {
		@RequestMapping("/")
		public String cookie(@CookieValue("cookie") String cookie) {
			return cookie;
		}
	}
166
}