Clover icon

Magnolia Resources Module 2.4.2

  1. Project Clover database Fri Nov 6 2015 16:15:26 CET
  2. Package info.magnolia.module.resources

File ResourcesServletTest.java

 

Code metrics

0
74
15
2
283
165
15
0.2
4.93
7.5
1

Classes

Class Line # Actions
ResourcesServletTest 73 73 0% 14 9
0.896551789.7%
ResourcesServletTest.DummyServletOutputStream 275 1 0% 1 0
1.0100%
 

Contributing tests

This file is covered by 10 tests. .

Source view

1    /**
2    * This file Copyright (c) 2015 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.resources;
35   
36    import static info.magnolia.resourceloader.dummy.DummyResourceOrigin.*;
37    import static org.junit.Assert.*;
38    import static org.mockito.Matchers.eq;
39    import static org.mockito.Mockito.*;
40    import info.magnolia.cms.beans.config.MIMEMapping;
41    import info.magnolia.context.SystemContext;
42    import info.magnolia.objectfactory.guice.GuiceUtils;
43    import info.magnolia.repository.RepositoryConstants;
44    import info.magnolia.resourceloader.dummy.DummyResourceOrigin;
45    import info.magnolia.resourceloader.dummy.DummyResource;
46    import info.magnolia.test.ComponentsTestUtil;
47    import info.magnolia.test.MgnlTestCase;
48    import info.magnolia.test.TestUtil;
49    import info.magnolia.test.mock.MockContext;
50    import info.magnolia.test.mock.MockUtil;
51    import info.magnolia.test.mock.jcr.SessionTestUtil;
52   
53    import java.io.IOException;
54    import java.io.InputStream;
55    import java.nio.file.Files;
56    import java.nio.file.Paths;
57    import java.util.Date;
58   
59    import javax.jcr.Session;
60    import javax.servlet.ServletOutputStream;
61    import javax.servlet.http.HttpServletRequest;
62    import javax.servlet.http.HttpServletResponse;
63   
64    import org.apache.commons.io.input.NullInputStream;
65    import org.junit.Before;
66    import org.junit.BeforeClass;
67    import org.junit.Ignore;
68    import org.junit.Test;
69   
70    import com.google.common.base.Function;
71    import com.google.common.net.HttpHeaders;
72   
 
73    public class ResourcesServletTest extends MgnlTestCase {
74    private static final Date MAGIC_DATE = new Date(123l);
75    private HttpServletRequest request;
76    private HttpServletResponse response;
77    private DummyServletOutputStream servletOutputStream;
78    private ResourceLinker linker;
79   
 
80  1 toggle @BeforeClass
81    public static void shuntSlf4jSimpleLoggerForReflections() {
82  1 TestUtil.shuntLogFor(org.reflections.Reflections.class);
83    }
84   
 
85  10 toggle @Before
86    @Override
87    public void setUp() throws Exception {
88  10 super.setUp();
89   
90  10 Session configMimeMapping = SessionTestUtil.createSession(RepositoryConstants.CONFIG,
91    "/server/MIMEMapping/css.extension=css\n" +
92    "/server/MIMEMapping/css.mime-type=text/css\n"
93    );
94  10 final SystemContext ctx = new MockContext();
95   
96  10 request = mock(HttpServletRequest.class);
97  10 when(request.getContextPath()).thenReturn("/foo");
98   
99  10 response = mock(HttpServletResponse.class);
100  10 servletOutputStream = new DummyServletOutputStream();
101  10 when(response.getOutputStream()).thenReturn(servletOutputStream);
102  10 when(response.getContentType()).thenReturn("text/css");
103   
104  10 final Function<DummyResource, InputStream> fakeStream = DummyResourceOrigin.<InputStream>constant(new NullInputStream(10));
105  10 final DummyResourceOrigin origin = new DummyResourceOrigin(Differentiator.foo, constant(MAGIC_DATE), noReader("test"), fakeStream, pathsFor("/style.css", "/Foo.java", "template.ftl", "/style.that.has.a.lot.of.dots~and~tilde.css", "style.that.has.a.lot.of.dots.css"));
106  10 final ResourcesModule module = new ResourcesModule();
107    // module.setDownloadPath("/.resources); this is the default
108  10 linker = new ResourceLinker(origin, GuiceUtils.providerForInstance(module), null, null);
109   
110  10 ComponentsTestUtil.setInstance(SystemContext.class, ctx);
111  10 MockUtil.setSystemContextSessionAndHierarchyManager(configMimeMapping);
112  10 MIMEMapping.init();
113    }
114   
 
115  1 toggle @Test
116    public void blankResourceReturns400() throws Exception {
117    // GIVEN
118  1 ResourcesServlet servlet = new ResourcesServlet(linker);
119  1 when(request.getPathInfo()).thenReturn("");
120  1 when(request.getRequestURI()).thenReturn("/.resources/");
121   
122    // WHEN
123  1 servlet.doGet(request, response);
124   
125    // THEN
126  1 verify(response).sendError(400);
127  1 assertFalse(servletOutputStream.dataWasWritten);
128    }
129   
 
130  1 toggle @Test
131    public void resourceNotFoundIsNotProcessed() throws Exception {
132    // GIVEN
133  1 ResourcesServlet servlet = new ResourcesServlet(linker);
134  1 when(request.getPathInfo()).thenReturn("/foo/resources/bar.css");
135   
136    // WHEN
137  1 servlet.doGet(request, response);
138   
139    // THEN
140  1 verify(response).sendError(HttpServletResponse.SC_NOT_FOUND);
141  1 assertFalse(servletOutputStream.dataWasWritten);
142    }
143   
 
144  1 toggle @Test
145    public void resourceNotFoundIsNotProcessed2() throws Exception {
146    // GIVEN
147  1 ResourcesServlet servlet = new ResourcesServlet(linker);
148  1 when(request.getPathInfo()).thenReturn("/templating-kit/themes/pop/img/bgs/text-box-300.png");
149   
150    // WHEN
151  1 servlet.doGet(request, response);
152   
153    // THEN
154  1 verify(response).sendError(HttpServletResponse.SC_NOT_FOUND);
155  1 assertFalse(servletOutputStream.dataWasWritten);
156    }
157   
 
158  1 toggle @Test
159    public void newResourceIsServedWithLastModifiedHeaders() throws Exception {
160    // GIVEN
161  1 ResourcesServlet servlet = new ResourcesServlet(linker);
162  1 when(request.getPathInfo()).thenReturn("/style.css");
163  1 long lastModified = MAGIC_DATE.getTime();
164   
165    // WHEN
166  1 servlet.doGet(request, response);
167   
168    // THEN
169  1 verify(response).setDateHeader(eq(HttpHeaders.LAST_MODIFIED), eq(lastModified));
170    }
171   
 
172  1 toggle @Test
173    public void resourceIsServedWithContentTypeHeader() throws Exception {
174    // GIVEN
175  1 ResourcesServlet servlet = new ResourcesServlet(linker);
176  1 when(request.getPathInfo()).thenReturn("/style.css");
177   
178    // WHEN
179  1 servlet.doGet(request, response);
180   
181    // THEN
182  1 verify(response).setContentType("text/css");
183    }
184   
 
185  0 toggle @Test
186    @Ignore("Until MGNLRES-139 is solved")
187    public void unmodifiedResourceReturns304() throws Exception {
188   
189    // GIVEN first request to resource
190  0 ResourcesServlet servlet = new ResourcesServlet(linker);
191   
192  0 long lastModified = Files.getLastModifiedTime(Paths.get("/resources/style.css")).toMillis();
193  0 String etag = "style.css_" + lastModified;
194  0 when(request.getPathInfo()).thenReturn("/resources/style.css");
195   
196  0 servlet.doGet(request, response);
197   
198  0 when(request.getHeader(eq(HttpHeaders.IF_NONE_MATCH))).thenReturn(etag);
199   
200    // WHEN second request
201  0 servlet.doGet(request, response);
202   
203    // THEN
204  0 verify(response).setStatus(eq(HttpServletResponse.SC_NOT_MODIFIED));
205    }
206   
 
207  1 toggle @Test
208    public void cachingOfResourcesIsDisabledInDevMode() throws Exception {
209    // GIVEN
210  1 ResourcesServlet servlet = new ResourcesServlet(linker);
211  1 when(request.getPathInfo()).thenReturn("/style.css");
212   
213    // WHEN
214  1 servlet.doGet(request, response);
215   
216    // THEN
217  1 assertTrue(servletOutputStream.dataWasWritten);
218    }
219   
 
220  1 toggle @Test
221    public void serveResourceWithCacheInfix() throws Exception {
222    // GIVEN
223  1 ResourcesServlet servlet = new ResourcesServlet(linker);
224  1 when(request.getPathInfo()).thenReturn("/style.2015-03-19-13-58-59-450.cache.css");
225   
226    // WHEN
227  1 servlet.doGet(request, response);
228   
229    // THEN
230  1 assertTrue(servletOutputStream.dataWasWritten);
231   
232    }
233   
 
234  1 toggle @Test
235    public void serveResourceWithSimpleCacheInfix() throws Exception {
236    // GIVEN
237  1 ResourcesServlet servlet = new ResourcesServlet(linker);
238  1 when(request.getPathInfo()).thenReturn("/style.that.has.a.lot.of.dots.2015-03-19-13-58-59-999.cache.css");
239   
240    // WHEN
241  1 servlet.doGet(request, response);
242   
243    // THEN
244  1 assertTrue(servletOutputStream.dataWasWritten);
245    }
246   
 
247  1 toggle @Test
248    public void serveResourceWithCacheInfixAndOtherTildesAndDotsInPathName() throws Exception {
249    // GIVEN
250  1 ResourcesServlet servlet = new ResourcesServlet(linker);
251  1 when(request.getPathInfo()).thenReturn("/style.that.has.a.lot.of.dots~and~tilde~2015-03-19-13-58-59-450.cache.css");
252   
253    // WHEN
254  1 servlet.doGet(request, response);
255   
256    // THEN
257  1 assertTrue(servletOutputStream.dataWasWritten);
258    }
259   
 
260  1 toggle @Test
261    public void restrictedResourceTypesShouldReturn403() throws Exception {
262  1 ResourcesServlet servlet = new ResourcesServlet(linker);
263    // Existing resources:
264  1 verifyResourceIsRestricted(servlet, "/template.ftl");
265  1 verifyResourceIsRestricted(servlet, "/Foo.java");
266    }
267   
 
268  2 toggle private void verifyResourceIsRestricted(ResourcesServlet servlet, String requestedResource) throws Exception {
269  2 when(request.getPathInfo()).thenReturn(requestedResource);
270  2 servlet.doGet(request, response);
271  2 verify(response).sendError(HttpServletResponse.SC_FORBIDDEN);
272  2 reset(request, response);
273    }
274   
 
275    private static class DummyServletOutputStream extends ServletOutputStream {
276    boolean dataWasWritten = false;
277   
 
278  60 toggle @Override
279    public void write(int b) throws IOException {
280  60 dataWasWritten = true;
281    }
282    }
283    }