1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
package info.magnolia.module.blossom.support; |
35 |
|
|
36 |
|
import static org.junit.Assert.assertEquals; |
37 |
|
import static org.junit.Assert.assertFalse; |
38 |
|
import static org.junit.Assert.assertTrue; |
39 |
|
|
40 |
|
import java.io.IOException; |
41 |
|
import java.util.concurrent.atomic.AtomicBoolean; |
42 |
|
import java.util.concurrent.atomic.AtomicReference; |
43 |
|
|
44 |
|
import javax.servlet.RequestDispatcher; |
45 |
|
import javax.servlet.ServletException; |
46 |
|
import javax.servlet.ServletRequest; |
47 |
|
import javax.servlet.ServletResponse; |
48 |
|
|
49 |
|
import org.junit.Test; |
50 |
|
import org.springframework.mock.web.MockHttpServletRequest; |
51 |
|
import org.springframework.mock.web.MockHttpServletResponse; |
52 |
|
import org.springframework.web.util.WebUtils; |
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
@since |
58 |
|
|
|
|
| 98.4% |
Uncovered Elements: 2 (123) |
Complexity: 12 |
Complexity Density: 0.11 |
|
59 |
|
public class ForwardRequestWrapperTest { |
60 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (16) |
Complexity: 1 |
Complexity Density: 0.06 |
1PASS
|
|
61 |
1 |
@Test... |
62 |
|
public void testSetsForwardAttributesAndChangesPathElements() { |
63 |
|
|
64 |
|
|
65 |
1 |
MockHttpServletRequest mock = new MockHttpServletRequest(); |
66 |
1 |
mock.setRequestURI("/previousContext/previousServlet/previousPath"); |
67 |
1 |
mock.setContextPath("/previousContext"); |
68 |
1 |
mock.setServletPath("/previousServlet"); |
69 |
1 |
mock.setPathInfo("/previousPath"); |
70 |
1 |
mock.setQueryString("q=1"); |
71 |
|
|
72 |
|
|
73 |
1 |
ForwardRequestWrapper wrapper = new ForwardRequestWrapper(mock, "/context/servlet/path", "/context", "/servlet", "/path"); |
74 |
|
|
75 |
|
|
76 |
1 |
assertEquals("/context/servlet/path", wrapper.getRequestURI()); |
77 |
1 |
assertEquals("/context", wrapper.getContextPath()); |
78 |
1 |
assertEquals("/servlet", wrapper.getServletPath()); |
79 |
1 |
assertEquals("/path", wrapper.getPathInfo()); |
80 |
|
|
81 |
1 |
assertEquals("/previousContext/previousServlet/previousPath", wrapper.getAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE)); |
82 |
1 |
assertEquals("/previousContext", wrapper.getAttribute(WebUtils.FORWARD_CONTEXT_PATH_ATTRIBUTE)); |
83 |
1 |
assertEquals("/previousServlet", wrapper.getAttribute(WebUtils.FORWARD_SERVLET_PATH_ATTRIBUTE)); |
84 |
1 |
assertEquals("/previousPath", wrapper.getAttribute(WebUtils.FORWARD_PATH_INFO_ATTRIBUTE)); |
85 |
1 |
assertEquals("q=1", wrapper.getAttribute(WebUtils.FORWARD_QUERY_STRING_ATTRIBUTE)); |
86 |
|
} |
87 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (16) |
Complexity: 1 |
Complexity Density: 0.06 |
1PASS
|
|
88 |
1 |
@Test... |
89 |
|
public void testDoesNotSetForwardAttributesIfAlreadyPerformingForwardButChangesPathElements() { |
90 |
|
|
91 |
|
|
92 |
1 |
MockHttpServletRequest mock = new MockHttpServletRequest(); |
93 |
1 |
mock.setAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE, "/previousContext/previousServlet/previousPath"); |
94 |
1 |
mock.setAttribute(WebUtils.FORWARD_CONTEXT_PATH_ATTRIBUTE, "/previousContext"); |
95 |
1 |
mock.setAttribute(WebUtils.FORWARD_SERVLET_PATH_ATTRIBUTE, "/previousServlet"); |
96 |
1 |
mock.setAttribute(WebUtils.FORWARD_PATH_INFO_ATTRIBUTE, "/previousPath"); |
97 |
1 |
mock.setAttribute(WebUtils.FORWARD_QUERY_STRING_ATTRIBUTE, "q=1"); |
98 |
|
|
99 |
|
|
100 |
1 |
ForwardRequestWrapper wrapper = new ForwardRequestWrapper(mock, "/context/servlet/path", "/context", "/servlet", "/path"); |
101 |
|
|
102 |
|
|
103 |
1 |
assertEquals("/context/servlet/path", wrapper.getRequestURI()); |
104 |
1 |
assertEquals("/context", wrapper.getContextPath()); |
105 |
1 |
assertEquals("/servlet", wrapper.getServletPath()); |
106 |
1 |
assertEquals("/path", wrapper.getPathInfo()); |
107 |
|
|
108 |
1 |
assertEquals("/previousContext/previousServlet/previousPath", wrapper.getAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE)); |
109 |
1 |
assertEquals("/previousContext", wrapper.getAttribute(WebUtils.FORWARD_CONTEXT_PATH_ATTRIBUTE)); |
110 |
1 |
assertEquals("/previousServlet", wrapper.getAttribute(WebUtils.FORWARD_SERVLET_PATH_ATTRIBUTE)); |
111 |
1 |
assertEquals("/previousPath", wrapper.getAttribute(WebUtils.FORWARD_PATH_INFO_ATTRIBUTE)); |
112 |
1 |
assertEquals("q=1", wrapper.getAttribute(WebUtils.FORWARD_QUERY_STRING_ATTRIBUTE)); |
113 |
|
} |
114 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 1 |
Complexity Density: 0.08 |
1PASS
|
|
115 |
1 |
@Test... |
116 |
|
public void testHidesUpstreamAttributesButNotPathElementsOnForward() throws ServletException, IOException { |
117 |
|
|
118 |
|
|
119 |
1 |
final AtomicBoolean assertsWereRun = new AtomicBoolean(false); |
120 |
1 |
final AtomicReference<ForwardRequestWrapper> wrapperHolder = new AtomicReference<ForwardRequestWrapper>(); |
121 |
1 |
MockHttpServletRequest mock = new MockHttpServletRequest() { |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
122 |
1 |
@Override... |
123 |
|
public RequestDispatcher getRequestDispatcher(String path) { |
124 |
1 |
return new RequestDispatcher() { |
125 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (23) |
Complexity: 1 |
Complexity Density: 0.04 |
|
126 |
1 |
@Override... |
127 |
|
public void forward(ServletRequest request, ServletResponse response) throws ServletException, IOException { |
128 |
|
|
129 |
1 |
ForwardRequestWrapper wrapper = wrapperHolder.get(); |
130 |
|
|
131 |
1 |
setAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE, getRequestURI()); |
132 |
1 |
setAttribute(WebUtils.FORWARD_CONTEXT_PATH_ATTRIBUTE, getContextPath()); |
133 |
1 |
setAttribute(WebUtils.FORWARD_SERVLET_PATH_ATTRIBUTE, getServletPath()); |
134 |
1 |
setAttribute(WebUtils.FORWARD_PATH_INFO_ATTRIBUTE, getPathInfo()); |
135 |
1 |
setAttribute(WebUtils.FORWARD_QUERY_STRING_ATTRIBUTE, getQueryString()); |
136 |
|
|
137 |
1 |
setRequestURI("/context3/servlet3/path3"); |
138 |
1 |
setContextPath("/context3"); |
139 |
1 |
setServletPath("/servlet3"); |
140 |
1 |
setPathInfo("/path3"); |
141 |
1 |
setQueryString("q=3"); |
142 |
|
|
143 |
|
|
144 |
1 |
assertEquals("/context3/servlet3/path3", wrapper.getRequestURI()); |
145 |
1 |
assertEquals("/context3", wrapper.getContextPath()); |
146 |
1 |
assertEquals("/servlet3", wrapper.getServletPath()); |
147 |
1 |
assertEquals("/path3", wrapper.getPathInfo()); |
148 |
1 |
assertEquals("q=3", wrapper.getQueryString()); |
149 |
|
|
150 |
1 |
assertEquals("/context1/servlet1/path1", wrapper.getAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE)); |
151 |
1 |
assertEquals("/context1", wrapper.getAttribute(WebUtils.FORWARD_CONTEXT_PATH_ATTRIBUTE)); |
152 |
1 |
assertEquals("/servlet1", wrapper.getAttribute(WebUtils.FORWARD_SERVLET_PATH_ATTRIBUTE)); |
153 |
1 |
assertEquals("/path1", wrapper.getAttribute(WebUtils.FORWARD_PATH_INFO_ATTRIBUTE)); |
154 |
1 |
assertEquals("q=1", wrapper.getAttribute(WebUtils.FORWARD_QUERY_STRING_ATTRIBUTE)); |
155 |
1 |
assertTrue(wrapper.isEnabled()); |
156 |
|
|
157 |
1 |
assertsWereRun.set(true); |
158 |
|
} |
159 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
160 |
0 |
@Override... |
161 |
|
public void include(ServletRequest request, ServletResponse response) throws ServletException, IOException { |
162 |
|
} |
163 |
|
}; |
164 |
|
} |
165 |
|
}; |
166 |
1 |
mock.setRequestURI("/context1/servlet1/path1"); |
167 |
1 |
mock.setContextPath("/context1"); |
168 |
1 |
mock.setServletPath("/servlet1"); |
169 |
1 |
mock.setPathInfo("/path1"); |
170 |
1 |
mock.setQueryString("q=1"); |
171 |
1 |
ForwardRequestWrapper wrapper = new ForwardRequestWrapper(mock, "/context2/servlet2/path2", "/context2", "/servlet2", "/path2"); |
172 |
1 |
wrapperHolder.set(wrapper); |
173 |
|
|
174 |
|
|
175 |
1 |
wrapper.getRequestDispatcher("/forward").forward(wrapper, new MockHttpServletResponse()); |
176 |
|
|
177 |
|
|
178 |
1 |
assertTrue(assertsWereRun.get()); |
179 |
|
} |
180 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 1 |
Complexity Density: 0.08 |
1PASS
|
|
181 |
1 |
@Test... |
182 |
|
public void testChangesNothingOnInclude() throws ServletException, IOException { |
183 |
|
|
184 |
|
|
185 |
1 |
final AtomicBoolean assertsWereRun = new AtomicBoolean(false); |
186 |
1 |
final AtomicReference<ForwardRequestWrapper> wrapperHolder = new AtomicReference<ForwardRequestWrapper>(); |
187 |
1 |
MockHttpServletRequest mock = new MockHttpServletRequest() { |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
188 |
1 |
@Override... |
189 |
|
public RequestDispatcher getRequestDispatcher(String path) { |
190 |
1 |
return new RequestDispatcher() { |
191 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
192 |
0 |
@Override... |
193 |
|
public void forward(ServletRequest request, ServletResponse response) throws ServletException, IOException { |
194 |
|
} |
195 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 1 |
Complexity Density: 0.08 |
|
196 |
1 |
@Override... |
197 |
|
public void include(ServletRequest request, ServletResponse response) throws ServletException, IOException { |
198 |
1 |
ForwardRequestWrapper wrapper = wrapperHolder.get(); |
199 |
|
|
200 |
|
|
201 |
1 |
assertEquals("/context2/servlet2/path2", wrapper.getRequestURI()); |
202 |
1 |
assertEquals("/context2", wrapper.getContextPath()); |
203 |
1 |
assertEquals("/servlet2", wrapper.getServletPath()); |
204 |
1 |
assertEquals("/path2", wrapper.getPathInfo()); |
205 |
1 |
assertEquals("q=1", wrapper.getQueryString()); |
206 |
|
|
207 |
1 |
assertEquals("/context1/servlet1/path1", wrapper.getAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE)); |
208 |
1 |
assertEquals("/context1", wrapper.getAttribute(WebUtils.FORWARD_CONTEXT_PATH_ATTRIBUTE)); |
209 |
1 |
assertEquals("/servlet1", wrapper.getAttribute(WebUtils.FORWARD_SERVLET_PATH_ATTRIBUTE)); |
210 |
1 |
assertEquals("/path1", wrapper.getAttribute(WebUtils.FORWARD_PATH_INFO_ATTRIBUTE)); |
211 |
1 |
assertEquals("q=1", wrapper.getAttribute(WebUtils.FORWARD_QUERY_STRING_ATTRIBUTE)); |
212 |
1 |
assertTrue(wrapper.isEnabled()); |
213 |
|
|
214 |
1 |
assertsWereRun.set(true); |
215 |
|
} |
216 |
|
}; |
217 |
|
} |
218 |
|
}; |
219 |
1 |
mock.setRequestURI("/context1/servlet1/path1"); |
220 |
1 |
mock.setContextPath("/context1"); |
221 |
1 |
mock.setServletPath("/servlet1"); |
222 |
1 |
mock.setPathInfo("/path1"); |
223 |
1 |
mock.setQueryString("q=1"); |
224 |
1 |
ForwardRequestWrapper wrapper = new ForwardRequestWrapper(mock, "/context2/servlet2/path2", "/context2", "/servlet2", "/path2"); |
225 |
1 |
wrapperHolder.set(wrapper); |
226 |
|
|
227 |
|
|
228 |
1 |
wrapper.getRequestDispatcher("/include").include(wrapper, new MockHttpServletResponse()); |
229 |
|
|
230 |
|
|
231 |
1 |
assertTrue(assertsWereRun.get()); |
232 |
|
} |
233 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
1PASS
|
|
234 |
1 |
@Test... |
235 |
|
public void testDisablesPreviousForwardWrapperAndRestoresItAfterwards() { |
236 |
|
|
237 |
|
|
238 |
1 |
MockHttpServletRequest mock = new MockHttpServletRequest(); |
239 |
1 |
ForwardRequestWrapper previousWrapper = new ForwardRequestWrapper(mock, "/previousContext/previousServlet/previousPath", "/previousContext", "/previousServlet", "/previousPath"); |
240 |
1 |
assertTrue(previousWrapper.isOverridePathElements()); |
241 |
1 |
assertTrue(previousWrapper.isEnabled()); |
242 |
|
|
243 |
|
|
244 |
1 |
ForwardRequestWrapper wrapper = new ForwardRequestWrapper(previousWrapper, "/context/servlet/path", "/context", "/servlet", "/path"); |
245 |
|
|
246 |
|
|
247 |
1 |
assertFalse(previousWrapper.isOverridePathElements()); |
248 |
1 |
assertFalse(previousWrapper.isEnabled()); |
249 |
|
|
250 |
|
|
251 |
1 |
wrapper.restore(); |
252 |
|
|
253 |
|
|
254 |
1 |
assertTrue(previousWrapper.isOverridePathElements()); |
255 |
1 |
assertTrue(previousWrapper.isEnabled()); |
256 |
|
} |
257 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
258 |
1 |
@Test... |
259 |
|
public void testDisablesPreviousIncludeWrapperAndRestoresItAfterwards() { |
260 |
|
|
261 |
|
|
262 |
1 |
MockHttpServletRequest mock = new MockHttpServletRequest(); |
263 |
1 |
IncludeRequestWrapper previousWrapper = new IncludeRequestWrapper(mock, "/previousContext/previousServlet/previousPath", "/previousContext", "/previousServlet", "/previousPath", null); |
264 |
1 |
assertTrue(previousWrapper.isEnabled()); |
265 |
|
|
266 |
|
|
267 |
1 |
ForwardRequestWrapper wrapper = new ForwardRequestWrapper(previousWrapper, "/context/servlet/path", "/context", "/servlet", "/path"); |
268 |
|
|
269 |
|
|
270 |
1 |
assertFalse(previousWrapper.isEnabled()); |
271 |
|
|
272 |
|
|
273 |
1 |
wrapper.restore(); |
274 |
|
|
275 |
|
|
276 |
1 |
assertTrue(previousWrapper.isEnabled()); |
277 |
|
} |
278 |
|
} |