|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2015 the original author or authors. |
| 2 | + * Copyright 2002-2016 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
20 | 20 | import java.net.URL;
|
21 | 21 | import javax.servlet.http.HttpServletRequest;
|
22 | 22 |
|
| 23 | +import com.gargoylesoftware.htmlunit.WebClient; |
| 24 | +import com.gargoylesoftware.htmlunit.WebConnection; |
| 25 | +import com.gargoylesoftware.htmlunit.WebRequest; |
| 26 | +import com.gargoylesoftware.htmlunit.WebResponse; |
23 | 27 | import org.junit.Before;
|
24 | 28 | import org.junit.Test;
|
25 | 29 | import org.junit.runner.RunWith;
|
|
36 | 40 | import org.springframework.web.context.WebApplicationContext;
|
37 | 41 | import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
38 | 42 |
|
39 |
| -import com.gargoylesoftware.htmlunit.WebConnection; |
40 |
| -import com.gargoylesoftware.htmlunit.WebRequest; |
41 |
| -import com.gargoylesoftware.htmlunit.WebResponse; |
42 |
| - |
43 | 43 | import static org.hamcrest.CoreMatchers.equalTo;
|
44 | 44 | import static org.hamcrest.CoreMatchers.nullValue;
|
45 | 45 | import static org.hamcrest.Matchers.notNullValue;
|
46 | 46 | import static org.junit.Assert.assertThat;
|
47 | 47 | import static org.mockito.Mockito.mock;
|
| 48 | +import static org.mockito.Mockito.when; |
48 | 49 |
|
49 | 50 | /**
|
50 | 51 | * Integration tests for {@link MockMvcWebConnectionBuilderSupport}.
|
51 | 52 | *
|
52 | 53 | * @author Rob Winch
|
| 54 | + * @author Rossen Stoyanchev |
53 | 55 | * @since 4.2
|
54 | 56 | */
|
55 | 57 | @RunWith(SpringJUnit4ClassRunner.class)
|
56 | 58 | @ContextConfiguration
|
57 | 59 | @WebAppConfiguration
|
58 |
| -@SuppressWarnings("rawtypes") |
59 | 60 | public class MockMvcConnectionBuilderSupportTests {
|
60 | 61 |
|
61 |
| - private final WebConnection delegateConnection = mock(WebConnection.class); |
62 |
| - |
63 | 62 | @Autowired
|
64 | 63 | private WebApplicationContext wac;
|
65 | 64 |
|
66 |
| - private MockMvc mockMvc; |
| 65 | + private WebClient client; |
| 66 | + |
| 67 | + private MockMvcWebConnectionBuilderSupport builder; |
67 | 68 |
|
68 |
| - private WebConnection connection; |
69 | 69 |
|
70 | 70 | @Before
|
71 | 71 | public void setup() {
|
72 |
| - mockMvc = MockMvcBuilders.webAppContextSetup(wac).build(); |
73 |
| - |
74 |
| - connection = new MockMvcWebConnectionBuilderSupport(mockMvc){} |
75 |
| - .createConnection(delegateConnection); |
| 72 | + this.client = mock(WebClient.class); |
| 73 | + when(this.client.getWebConnection()).thenReturn(mock(WebConnection.class)); |
| 74 | + this.builder = new MockMvcWebConnectionBuilderSupport(this.wac) {}; |
76 | 75 | }
|
77 | 76 |
|
| 77 | + |
78 | 78 | @Test(expected = IllegalArgumentException.class)
|
79 | 79 | public void constructorMockMvcNull() {
|
80 |
| - new MockMvcWebConnectionBuilderSupport((MockMvc)null){}; |
| 80 | + new MockMvcWebConnectionBuilderSupport((MockMvc) null){}; |
81 | 81 | }
|
82 | 82 |
|
83 | 83 | @Test(expected = IllegalArgumentException.class)
|
84 | 84 | public void constructorContextNull() {
|
85 |
| - new MockMvcWebConnectionBuilderSupport((WebApplicationContext)null){}; |
| 85 | + new MockMvcWebConnectionBuilderSupport((WebApplicationContext) null){}; |
86 | 86 | }
|
87 | 87 |
|
88 | 88 | @Test
|
89 | 89 | public void context() throws Exception {
|
90 |
| - connection = new MockMvcWebConnectionBuilderSupport(wac) {} |
91 |
| - .createConnection(delegateConnection); |
| 90 | + WebConnection conn = this.builder.createConnection(this.client); |
92 | 91 |
|
93 |
| - assertMvcProcessed("http://localhost/"); |
94 |
| - assertDelegateProcessed("http://example.com/"); |
| 92 | + assertMockMvcUsed(conn, "http://localhost/"); |
| 93 | + assertMockMvcNotUsed(conn, "http://example.com/"); |
95 | 94 | }
|
96 | 95 |
|
97 | 96 | @Test
|
98 | 97 | public void mockMvc() throws Exception {
|
99 |
| - assertMvcProcessed("http://localhost/"); |
100 |
| - assertDelegateProcessed("http://example.com/"); |
| 98 | + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(wac).build(); |
| 99 | + WebConnection conn = new MockMvcWebConnectionBuilderSupport(mockMvc) {}.createConnection(this.client); |
| 100 | + |
| 101 | + assertMockMvcUsed(conn, "http://localhost/"); |
| 102 | + assertMockMvcNotUsed(conn, "http://example.com/"); |
101 | 103 | }
|
102 | 104 |
|
103 | 105 | @Test
|
104 | 106 | public void mockMvcExampleDotCom() throws Exception {
|
105 |
| - connection = new MockMvcWebConnectionBuilderSupport(wac) {} |
106 |
| - .useMockMvcForHosts("example.com") |
107 |
| - .createConnection(delegateConnection); |
| 107 | + WebConnection conn = this.builder.useMockMvcForHosts("example.com").createConnection(this.client); |
108 | 108 |
|
109 |
| - assertMvcProcessed("http://localhost/"); |
110 |
| - assertMvcProcessed("http://example.com/"); |
111 |
| - assertDelegateProcessed("http://other.com/"); |
| 109 | + assertMockMvcUsed(conn, "http://localhost/"); |
| 110 | + assertMockMvcUsed(conn, "http://example.com/"); |
| 111 | + assertMockMvcNotUsed(conn, "http://other.com/"); |
112 | 112 | }
|
113 | 113 |
|
114 | 114 | @Test
|
115 | 115 | public void mockMvcAlwaysUseMockMvc() throws Exception {
|
116 |
| - connection = new MockMvcWebConnectionBuilderSupport(wac) {} |
117 |
| - .alwaysUseMockMvc() |
118 |
| - .createConnection(delegateConnection); |
119 |
| - |
120 |
| - assertMvcProcessed("http://other.com/"); |
| 116 | + WebConnection conn = this.builder.alwaysUseMockMvc().createConnection(this.client); |
| 117 | + assertMockMvcUsed(conn, "http://other.com/"); |
121 | 118 | }
|
122 | 119 |
|
123 | 120 | @Test
|
124 | 121 | public void defaultContextPathEmpty() throws Exception {
|
125 |
| - connection = new MockMvcWebConnectionBuilderSupport(wac) {} |
126 |
| - .createConnection(delegateConnection); |
127 |
| - |
128 |
| - assertThat(getWebResponse("http://localhost/abc").getContentAsString(), equalTo("")); |
| 122 | + WebConnection conn = this.builder.createConnection(this.client); |
| 123 | + assertThat(getResponse(conn, "http://localhost/abc").getContentAsString(), equalTo("")); |
129 | 124 | }
|
130 | 125 |
|
131 | 126 | @Test
|
132 | 127 | public void defaultContextPathCustom() throws Exception {
|
133 |
| - connection = new MockMvcWebConnectionBuilderSupport(wac) {} |
134 |
| - .contextPath("/abc").createConnection(delegateConnection); |
135 |
| - |
136 |
| - assertThat(getWebResponse("http://localhost/abc/def").getContentAsString(), equalTo("/abc")); |
| 128 | + WebConnection conn = this.builder.contextPath("/abc").createConnection(this.client); |
| 129 | + assertThat(getResponse(conn, "http://localhost/abc/def").getContentAsString(), equalTo("/abc")); |
137 | 130 | }
|
138 | 131 |
|
139 |
| - private void assertMvcProcessed(String url) throws Exception { |
140 |
| - assertThat(getWebResponse(url), notNullValue()); |
| 132 | + |
| 133 | + private void assertMockMvcUsed(WebConnection connection, String url) throws Exception { |
| 134 | + assertThat(getResponse(connection, url), notNullValue()); |
141 | 135 | }
|
142 | 136 |
|
143 |
| - private void assertDelegateProcessed(String url) throws Exception { |
144 |
| - assertThat(getWebResponse(url), nullValue()); |
| 137 | + private void assertMockMvcNotUsed(WebConnection connection, String url) throws Exception { |
| 138 | + assertThat(getResponse(connection, url), nullValue()); |
145 | 139 | }
|
146 | 140 |
|
147 |
| - private WebResponse getWebResponse(String url) throws IOException { |
| 141 | + private WebResponse getResponse(WebConnection connection, String url) throws IOException { |
148 | 142 | return connection.getResponse(new WebRequest(new URL(url)));
|
149 | 143 | }
|
150 | 144 |
|
151 | 145 |
|
152 | 146 | @Configuration
|
153 | 147 | @EnableWebMvc
|
| 148 | + @SuppressWarnings("unused") |
154 | 149 | static class Config {
|
155 | 150 |
|
156 | 151 | @RestController
|
|
0 commit comments