MockMvcHtmlUnitDriverBuilderTests.java 4.2 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 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;

S
Sam Brannen 已提交
43 44
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
45
import static org.springframework.test.web.servlet.htmlunit.webdriver.MockMvcHtmlUnitDriverBuilder.*;
S
Sam Brannen 已提交
46

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

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

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

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

S
Sam Brannen 已提交
66
	private HtmlUnitDriver driver;
67 68 69

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

	@Test(expected = IllegalArgumentException.class)
74 75
	public void webAppContextSetupNull() {
		webAppContextSetup(null);
76 77 78
	}

	@Test(expected = IllegalArgumentException.class)
79 80
	public void mockMvcSetupNull() {
		mockMvcSetup(null);
81 82 83
	}

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

		assertMvcProcessed("http://localhost/test");
89
		Assume.group(TestGroup.PERFORMANCE, () -> assertDelegateProcessed("http://example.com/"));
90 91 92
	}

	@Test
93 94
	public void mockMvcSetupWithDefaultDriverDelegate() throws Exception {
		this.driver = mockMvcSetup(this.mockMvc).build();
95 96

		assertMvcProcessed("http://localhost/test");
97
		Assume.group(TestGroup.PERFORMANCE, () -> assertDelegateProcessed("http://example.com/"));
98 99 100
	}

	@Test
S
Sam Brannen 已提交
101
	public void javaScriptEnabledByDefault() {
102
		this.driver = mockMvcSetup(this.mockMvc).build();
103

S
Sam Brannen 已提交
104
		assertTrue(this.driver.isJavascriptEnabled());
105 106 107
	}

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

S
Sam Brannen 已提交
111
		assertFalse(this.driver.isJavascriptEnabled());
112 113 114 115 116 117 118 119 120 121 122
	}

	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 已提交
123 124
		this.driver.get(url);
		return this.driver.getPageSource();
125 126
	}

S
Sam Brannen 已提交
127

128 129 130
	@Configuration
	@EnableWebMvc
	static class Config {
S
Sam Brannen 已提交
131

132 133
		@RestController
		static class ContextPathController {
S
Sam Brannen 已提交
134

135 136 137 138 139 140 141 142
			@RequestMapping
			public String contextPath(HttpServletRequest request) {
				return EXPECTED_BODY;
			}
		}
	}

}