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 |
|
|
|
|
| 97.8% |
Uncovered Elements: 2 (92) |
Complexity: 12 |
Complexity Density: 0.15 |
|
59 |
|
public class IncludeRequestWrapperTest { |
60 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
61 |
1 |
@Test... |
62 |
|
public void testSetsIncludeAttributes() { |
63 |
|
|
64 |
|
|
65 |
1 |
MockHttpServletRequest mock = new MockHttpServletRequest(); |
66 |
1 |
IncludeRequestWrapper wrapper = new IncludeRequestWrapper(mock, "/context/servlet/path", "/context", "/servlet", "/path", "q=1"); |
67 |
|
|
68 |
|
|
69 |
1 |
assertEquals("/context/servlet/path", wrapper.getAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE)); |
70 |
1 |
assertEquals("/context", wrapper.getAttribute(WebUtils.INCLUDE_CONTEXT_PATH_ATTRIBUTE)); |
71 |
1 |
assertEquals("/servlet", wrapper.getAttribute(WebUtils.INCLUDE_SERVLET_PATH_ATTRIBUTE)); |
72 |
1 |
assertEquals("/path", wrapper.getAttribute(WebUtils.INCLUDE_PATH_INFO_ATTRIBUTE)); |
73 |
|
} |
74 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 1 |
Complexity Density: 0.08 |
1PASS
|
|
75 |
1 |
@Test... |
76 |
|
public void testHidesPreviousIncludeAttributes() { |
77 |
|
|
78 |
|
|
79 |
1 |
MockHttpServletRequest mock = new MockHttpServletRequest(); |
80 |
1 |
mock.setAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE, "/foo/bar/zed"); |
81 |
1 |
mock.setAttribute(WebUtils.INCLUDE_CONTEXT_PATH_ATTRIBUTE, "/foo"); |
82 |
1 |
mock.setAttribute(WebUtils.INCLUDE_SERVLET_PATH_ATTRIBUTE, "/bar"); |
83 |
1 |
mock.setAttribute(WebUtils.INCLUDE_PATH_INFO_ATTRIBUTE, "/zed"); |
84 |
1 |
mock.setAttribute(WebUtils.INCLUDE_QUERY_STRING_ATTRIBUTE, "p=1"); |
85 |
1 |
IncludeRequestWrapper wrapper = new IncludeRequestWrapper(mock, "/context/servlet/path", "/context", "/servlet", "/path", "q=1"); |
86 |
|
|
87 |
|
|
88 |
1 |
assertEquals("/context/servlet/path", wrapper.getAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE)); |
89 |
1 |
assertEquals("/context", wrapper.getAttribute(WebUtils.INCLUDE_CONTEXT_PATH_ATTRIBUTE)); |
90 |
1 |
assertEquals("/servlet", wrapper.getAttribute(WebUtils.INCLUDE_SERVLET_PATH_ATTRIBUTE)); |
91 |
1 |
assertEquals("/path", wrapper.getAttribute(WebUtils.INCLUDE_PATH_INFO_ATTRIBUTE)); |
92 |
1 |
assertEquals("q=1", wrapper.getAttribute(WebUtils.INCLUDE_QUERY_STRING_ATTRIBUTE)); |
93 |
|
} |
94 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 1 |
Complexity Density: 0.08 |
1PASS
|
|
95 |
1 |
@Test... |
96 |
|
public void testOverridesPreviousIncludeWrapper() { |
97 |
|
|
98 |
|
|
99 |
1 |
MockHttpServletRequest mock = new MockHttpServletRequest(); |
100 |
1 |
mock.setAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE, "/foo/bar/zed"); |
101 |
1 |
mock.setAttribute(WebUtils.INCLUDE_CONTEXT_PATH_ATTRIBUTE, "/foo"); |
102 |
1 |
mock.setAttribute(WebUtils.INCLUDE_SERVLET_PATH_ATTRIBUTE, "/bar"); |
103 |
1 |
mock.setAttribute(WebUtils.INCLUDE_PATH_INFO_ATTRIBUTE, "/zed"); |
104 |
1 |
mock.setAttribute(WebUtils.INCLUDE_QUERY_STRING_ATTRIBUTE, "p=1"); |
105 |
1 |
IncludeRequestWrapper wrapper = new IncludeRequestWrapper(mock, "/context/servlet/path", "/context", "/servlet", "/path", "q=1"); |
106 |
|
|
107 |
|
|
108 |
1 |
wrapper = new IncludeRequestWrapper(wrapper, "/newcontext/newservlet/newpath", "/newcontext", "/newservlet", "/newpath", "newq=1"); |
109 |
|
|
110 |
|
|
111 |
1 |
assertEquals("/newcontext/newservlet/newpath", wrapper.getAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE)); |
112 |
1 |
assertEquals("/newcontext", wrapper.getAttribute(WebUtils.INCLUDE_CONTEXT_PATH_ATTRIBUTE)); |
113 |
1 |
assertEquals("/newservlet", wrapper.getAttribute(WebUtils.INCLUDE_SERVLET_PATH_ATTRIBUTE)); |
114 |
1 |
assertEquals("/newpath", wrapper.getAttribute(WebUtils.INCLUDE_PATH_INFO_ATTRIBUTE)); |
115 |
1 |
assertEquals("newq=1", wrapper.getAttribute(WebUtils.INCLUDE_QUERY_STRING_ATTRIBUTE)); |
116 |
|
} |
117 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
118 |
1 |
@Test... |
119 |
|
public void testHidesAttributesWhenPerformingInclude() throws ServletException, IOException { |
120 |
|
|
121 |
|
|
122 |
1 |
final AtomicBoolean assertsWereRun = new AtomicBoolean(false); |
123 |
1 |
final AtomicReference<IncludeRequestWrapper> wrapperHolder = new AtomicReference<IncludeRequestWrapper>(); |
124 |
1 |
MockHttpServletRequest mock = new MockHttpServletRequest() { |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
125 |
1 |
@Override... |
126 |
|
public RequestDispatcher getRequestDispatcher(String path) { |
127 |
1 |
return new RequestDispatcher() { |
128 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
129 |
0 |
@Override... |
130 |
|
public void forward(ServletRequest request, ServletResponse response) throws ServletException, IOException { |
131 |
|
} |
132 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 1 |
Complexity Density: 0.08 |
|
133 |
1 |
@Override... |
134 |
|
public void include(ServletRequest request, ServletResponse response) throws ServletException, IOException { |
135 |
|
|
136 |
1 |
IncludeRequestWrapper wrapper = wrapperHolder.get(); |
137 |
|
|
138 |
1 |
setAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE, "/foo/bar/zed"); |
139 |
1 |
setAttribute(WebUtils.INCLUDE_CONTEXT_PATH_ATTRIBUTE, "/foo"); |
140 |
1 |
setAttribute(WebUtils.INCLUDE_SERVLET_PATH_ATTRIBUTE, "/bar"); |
141 |
1 |
setAttribute(WebUtils.INCLUDE_PATH_INFO_ATTRIBUTE, "/zed"); |
142 |
1 |
setAttribute(WebUtils.INCLUDE_QUERY_STRING_ATTRIBUTE, "p=1"); |
143 |
|
|
144 |
|
|
145 |
1 |
assertEquals("/foo/bar/zed", wrapper.getAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE)); |
146 |
1 |
assertEquals("/foo", wrapper.getAttribute(WebUtils.INCLUDE_CONTEXT_PATH_ATTRIBUTE)); |
147 |
1 |
assertEquals("/bar", wrapper.getAttribute(WebUtils.INCLUDE_SERVLET_PATH_ATTRIBUTE)); |
148 |
1 |
assertEquals("/zed", wrapper.getAttribute(WebUtils.INCLUDE_PATH_INFO_ATTRIBUTE)); |
149 |
1 |
assertEquals("p=1", wrapper.getAttribute(WebUtils.INCLUDE_QUERY_STRING_ATTRIBUTE)); |
150 |
1 |
assertFalse(wrapper.isEnabled()); |
151 |
|
|
152 |
1 |
assertsWereRun.set(true); |
153 |
|
} |
154 |
|
}; |
155 |
|
} |
156 |
|
}; |
157 |
1 |
IncludeRequestWrapper wrapper = new IncludeRequestWrapper(mock, "/context/servlet/path", "/context", "/servlet", "/path", "q=1"); |
158 |
1 |
wrapperHolder.set(wrapper); |
159 |
|
|
160 |
|
|
161 |
1 |
wrapper.getRequestDispatcher("/include").include(wrapper, new MockHttpServletResponse()); |
162 |
|
|
163 |
|
|
164 |
1 |
assertTrue(assertsWereRun.get()); |
165 |
|
} |
166 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
167 |
1 |
@Test... |
168 |
|
public void testHidesAttributesWhenPerformingForward() throws ServletException, IOException { |
169 |
|
|
170 |
|
|
171 |
1 |
final AtomicBoolean assertsWereRun = new AtomicBoolean(false); |
172 |
1 |
final AtomicReference<IncludeRequestWrapper> wrapperHolder = new AtomicReference<IncludeRequestWrapper>(); |
173 |
1 |
MockHttpServletRequest mock = new MockHttpServletRequest() { |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
174 |
1 |
@Override... |
175 |
|
public RequestDispatcher getRequestDispatcher(String path) { |
176 |
1 |
return new RequestDispatcher() { |
177 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 1 |
Complexity Density: 0.08 |
|
178 |
1 |
@Override... |
179 |
|
public void forward(ServletRequest request, ServletResponse response) throws ServletException, IOException { |
180 |
|
|
181 |
1 |
IncludeRequestWrapper wrapper = wrapperHolder.get(); |
182 |
|
|
183 |
1 |
setAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE, "/foo/bar/zed"); |
184 |
1 |
setAttribute(WebUtils.INCLUDE_CONTEXT_PATH_ATTRIBUTE, "/foo"); |
185 |
1 |
setAttribute(WebUtils.INCLUDE_SERVLET_PATH_ATTRIBUTE, "/bar"); |
186 |
1 |
setAttribute(WebUtils.INCLUDE_PATH_INFO_ATTRIBUTE, "/zed"); |
187 |
1 |
setAttribute(WebUtils.INCLUDE_QUERY_STRING_ATTRIBUTE, "p=1"); |
188 |
|
|
189 |
|
|
190 |
1 |
assertEquals("/foo/bar/zed", wrapper.getAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE)); |
191 |
1 |
assertEquals("/foo", wrapper.getAttribute(WebUtils.INCLUDE_CONTEXT_PATH_ATTRIBUTE)); |
192 |
1 |
assertEquals("/bar", wrapper.getAttribute(WebUtils.INCLUDE_SERVLET_PATH_ATTRIBUTE)); |
193 |
1 |
assertEquals("/zed", wrapper.getAttribute(WebUtils.INCLUDE_PATH_INFO_ATTRIBUTE)); |
194 |
1 |
assertEquals("p=1", wrapper.getAttribute(WebUtils.INCLUDE_QUERY_STRING_ATTRIBUTE)); |
195 |
1 |
assertFalse(wrapper.isEnabled()); |
196 |
|
|
197 |
1 |
assertsWereRun.set(true); |
198 |
|
} |
199 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
200 |
0 |
@Override... |
201 |
|
public void include(ServletRequest request, ServletResponse response) throws ServletException, IOException { |
202 |
|
} |
203 |
|
}; |
204 |
|
} |
205 |
|
}; |
206 |
1 |
IncludeRequestWrapper wrapper = new IncludeRequestWrapper(mock, "/context/servlet/path", "/context", "/servlet", "/path", "q=1"); |
207 |
1 |
wrapperHolder.set(wrapper); |
208 |
|
|
209 |
|
|
210 |
1 |
wrapper.getRequestDispatcher("/forward").forward(wrapper, new MockHttpServletResponse()); |
211 |
|
|
212 |
|
|
213 |
1 |
assertTrue(assertsWereRun.get()); |
214 |
|
} |
215 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
216 |
1 |
@Test... |
217 |
|
public void testDisablesPreviousIncludeWrapperAndRestoresItAfterwards() { |
218 |
|
|
219 |
|
|
220 |
1 |
MockHttpServletRequest mock = new MockHttpServletRequest(); |
221 |
1 |
IncludeRequestWrapper previousWrapper = new IncludeRequestWrapper(mock, "/previousContext/previousServlet/previousPath", "/previousContext", "/previousServlet", "/previousPath", null); |
222 |
1 |
assertTrue(previousWrapper.isEnabled()); |
223 |
|
|
224 |
|
|
225 |
1 |
IncludeRequestWrapper wrapper = new IncludeRequestWrapper(previousWrapper, "/context/servlet/path", "/context", "/servlet", "/path", null); |
226 |
|
|
227 |
|
|
228 |
1 |
assertFalse(previousWrapper.isEnabled()); |
229 |
|
|
230 |
|
|
231 |
1 |
wrapper.restore(); |
232 |
|
|
233 |
|
|
234 |
1 |
assertTrue(previousWrapper.isEnabled()); |
235 |
|
}} |