1. Project Clover database Fri Apr 29 2016 13:24:33 CEST
  2. Package info.magnolia.module.blossom.support

File ForwardRequestWrapperTest.java

 

Code metrics

0
111
12
1
278
168
12
0.11
9.25
12
1

Classes

Class Line # Actions
ForwardRequestWrapperTest 59 111 0% 12 2
0.9837398598.4%
 

Contributing tests

This file is covered by 6 tests. .

Source view

1    /**
2    * This file Copyright (c) 2010-2016 Magnolia International
3    * Ltd. (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10    * This file is distributed in the hope that it will be
11    * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12    * implied warranty of MERCHANTABILITY or FITNESS FOR A
13    * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14    * Redistribution, except as permitted by whichever of the GPL
15    * or MNA you select, is prohibited.
16    *
17    * 1. For the GPL license (GPL), you can redistribute and/or
18    * modify this file under the terms of the GNU General
19    * Public License, Version 3, as published by the Free Software
20    * Foundation. You should have received a copy of the GNU
21    * General Public License, Version 3 along with this program;
22    * if not, write to the Free Software Foundation, Inc., 51
23    * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24    *
25    * 2. For the Magnolia Network Agreement (MNA), this file
26    * and the accompanying materials are made available under the
27    * terms of the MNA which accompanies this distribution, and
28    * is available at http://www.magnolia-cms.com/mna.html
29    *
30    * Any modifications to this file must keep this entire header
31    * intact.
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    * Test case for ForwardRequestWrapper.
56    *
57    * @since 1.2
58    */
 
59    public class ForwardRequestWrapperTest {
60   
 
61  1 toggle @Test
62    public void testSetsForwardAttributesAndChangesPathElements() {
63   
64    // GIVEN
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    // WHEN
73  1 ForwardRequestWrapper wrapper = new ForwardRequestWrapper(mock, "/context/servlet/path", "/context", "/servlet", "/path");
74   
75    // THEN
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   
 
88  1 toggle @Test
89    public void testDoesNotSetForwardAttributesIfAlreadyPerformingForwardButChangesPathElements() {
90   
91    // GIVEN
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    // WHEN
100  1 ForwardRequestWrapper wrapper = new ForwardRequestWrapper(mock, "/context/servlet/path", "/context", "/servlet", "/path");
101   
102    // THEN
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   
 
115  1 toggle @Test
116    public void testHidesUpstreamAttributesButNotPathElementsOnForward() throws ServletException, IOException {
117   
118    // GIVEN
119  1 final AtomicBoolean assertsWereRun = new AtomicBoolean(false);
120  1 final AtomicReference<ForwardRequestWrapper> wrapperHolder = new AtomicReference<ForwardRequestWrapper>();
121  1 MockHttpServletRequest mock = new MockHttpServletRequest() {
 
122  1 toggle @Override
123    public RequestDispatcher getRequestDispatcher(String path) {
124  1 return new RequestDispatcher() {
125   
 
126  1 toggle @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    // WHEN
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   
 
160  0 toggle @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    // THEN
175  1 wrapper.getRequestDispatcher("/forward").forward(wrapper, new MockHttpServletResponse());
176   
177    // WHEN
178  1 assertTrue(assertsWereRun.get());
179    }
180   
 
181  1 toggle @Test
182    public void testChangesNothingOnInclude() throws ServletException, IOException {
183   
184    // GIVEN
185  1 final AtomicBoolean assertsWereRun = new AtomicBoolean(false);
186  1 final AtomicReference<ForwardRequestWrapper> wrapperHolder = new AtomicReference<ForwardRequestWrapper>();
187  1 MockHttpServletRequest mock = new MockHttpServletRequest() {
 
188  1 toggle @Override
189    public RequestDispatcher getRequestDispatcher(String path) {
190  1 return new RequestDispatcher() {
191   
 
192  0 toggle @Override
193    public void forward(ServletRequest request, ServletResponse response) throws ServletException, IOException {
194    }
195   
 
196  1 toggle @Override
197    public void include(ServletRequest request, ServletResponse response) throws ServletException, IOException {
198  1 ForwardRequestWrapper wrapper = wrapperHolder.get();
199   
200    // WHEN
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    // THEN
228  1 wrapper.getRequestDispatcher("/include").include(wrapper, new MockHttpServletResponse());
229   
230    // WHEN
231  1 assertTrue(assertsWereRun.get());
232    }
233   
 
234  1 toggle @Test
235    public void testDisablesPreviousForwardWrapperAndRestoresItAfterwards() {
236   
237    // GIVEN
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    // WHEN
244  1 ForwardRequestWrapper wrapper = new ForwardRequestWrapper(previousWrapper, "/context/servlet/path", "/context", "/servlet", "/path");
245   
246    // THEN
247  1 assertFalse(previousWrapper.isOverridePathElements());
248  1 assertFalse(previousWrapper.isEnabled());
249   
250    // WHEN
251  1 wrapper.restore();
252   
253    // THEN
254  1 assertTrue(previousWrapper.isOverridePathElements());
255  1 assertTrue(previousWrapper.isEnabled());
256    }
257   
 
258  1 toggle @Test
259    public void testDisablesPreviousIncludeWrapperAndRestoresItAfterwards() {
260   
261    // GIVEN
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    // WHEN
267  1 ForwardRequestWrapper wrapper = new ForwardRequestWrapper(previousWrapper, "/context/servlet/path", "/context", "/servlet", "/path");
268   
269    // THEN
270  1 assertFalse(previousWrapper.isEnabled());
271   
272    // WHEN
273  1 wrapper.restore();
274   
275    // THEN
276  1 assertTrue(previousWrapper.isEnabled());
277    }
278    }