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.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 |
|
|
|
|
| 89.7% |
Uncovered Elements: 9 (87) |
Complexity: 14 |
Complexity Density: 0.19 |
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
80 |
1 |
@BeforeClass... |
81 |
|
public static void shuntSlf4jSimpleLoggerForReflections() { |
82 |
1 |
TestUtil.shuntLogFor(org.reflections.Reflections.class); |
83 |
|
} |
84 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (16) |
Complexity: 1 |
Complexity Density: 0.06 |
|
85 |
10 |
@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 |
|
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
115 |
1 |
@Test... |
116 |
|
public void blankResourceReturns400() throws Exception { |
117 |
|
|
118 |
1 |
ResourcesServlet servlet = new ResourcesServlet(linker); |
119 |
1 |
when(request.getPathInfo()).thenReturn(""); |
120 |
1 |
when(request.getRequestURI()).thenReturn("/.resources/"); |
121 |
|
|
122 |
|
|
123 |
1 |
servlet.doGet(request, response); |
124 |
|
|
125 |
|
|
126 |
1 |
verify(response).sendError(400); |
127 |
1 |
assertFalse(servletOutputStream.dataWasWritten); |
128 |
|
} |
129 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
130 |
1 |
@Test... |
131 |
|
public void resourceNotFoundIsNotProcessed() throws Exception { |
132 |
|
|
133 |
1 |
ResourcesServlet servlet = new ResourcesServlet(linker); |
134 |
1 |
when(request.getPathInfo()).thenReturn("/foo/resources/bar.css"); |
135 |
|
|
136 |
|
|
137 |
1 |
servlet.doGet(request, response); |
138 |
|
|
139 |
|
|
140 |
1 |
verify(response).sendError(HttpServletResponse.SC_NOT_FOUND); |
141 |
1 |
assertFalse(servletOutputStream.dataWasWritten); |
142 |
|
} |
143 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
144 |
1 |
@Test... |
145 |
|
public void resourceNotFoundIsNotProcessed2() throws Exception { |
146 |
|
|
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 |
|
|
151 |
1 |
servlet.doGet(request, response); |
152 |
|
|
153 |
|
|
154 |
1 |
verify(response).sendError(HttpServletResponse.SC_NOT_FOUND); |
155 |
1 |
assertFalse(servletOutputStream.dataWasWritten); |
156 |
|
} |
157 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
158 |
1 |
@Test... |
159 |
|
public void newResourceIsServedWithLastModifiedHeaders() throws Exception { |
160 |
|
|
161 |
1 |
ResourcesServlet servlet = new ResourcesServlet(linker); |
162 |
1 |
when(request.getPathInfo()).thenReturn("/style.css"); |
163 |
1 |
long lastModified = MAGIC_DATE.getTime(); |
164 |
|
|
165 |
|
|
166 |
1 |
servlet.doGet(request, response); |
167 |
|
|
168 |
|
|
169 |
1 |
verify(response).setDateHeader(eq(HttpHeaders.LAST_MODIFIED), eq(lastModified)); |
170 |
|
} |
171 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
172 |
1 |
@Test... |
173 |
|
public void resourceIsServedWithContentTypeHeader() throws Exception { |
174 |
|
|
175 |
1 |
ResourcesServlet servlet = new ResourcesServlet(linker); |
176 |
1 |
when(request.getPathInfo()).thenReturn("/style.css"); |
177 |
|
|
178 |
|
|
179 |
1 |
servlet.doGet(request, response); |
180 |
|
|
181 |
|
|
182 |
1 |
verify(response).setContentType("text/css"); |
183 |
|
} |
184 |
|
|
|
|
| 0% |
Uncovered Elements: 8 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
|
185 |
0 |
@Test... |
186 |
|
@Ignore("Until MGNLRES-139 is solved") |
187 |
|
public void unmodifiedResourceReturns304() throws Exception { |
188 |
|
|
189 |
|
|
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 |
|
|
201 |
0 |
servlet.doGet(request, response); |
202 |
|
|
203 |
|
|
204 |
0 |
verify(response).setStatus(eq(HttpServletResponse.SC_NOT_MODIFIED)); |
205 |
|
} |
206 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
207 |
1 |
@Test... |
208 |
|
public void cachingOfResourcesIsDisabledInDevMode() throws Exception { |
209 |
|
|
210 |
1 |
ResourcesServlet servlet = new ResourcesServlet(linker); |
211 |
1 |
when(request.getPathInfo()).thenReturn("/style.css"); |
212 |
|
|
213 |
|
|
214 |
1 |
servlet.doGet(request, response); |
215 |
|
|
216 |
|
|
217 |
1 |
assertTrue(servletOutputStream.dataWasWritten); |
218 |
|
} |
219 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
220 |
1 |
@Test... |
221 |
|
public void serveResourceWithCacheInfix() throws Exception { |
222 |
|
|
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 |
|
|
227 |
1 |
servlet.doGet(request, response); |
228 |
|
|
229 |
|
|
230 |
1 |
assertTrue(servletOutputStream.dataWasWritten); |
231 |
|
|
232 |
|
} |
233 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
234 |
1 |
@Test... |
235 |
|
public void serveResourceWithSimpleCacheInfix() throws Exception { |
236 |
|
|
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 |
|
|
241 |
1 |
servlet.doGet(request, response); |
242 |
|
|
243 |
|
|
244 |
1 |
assertTrue(servletOutputStream.dataWasWritten); |
245 |
|
} |
246 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
247 |
1 |
@Test... |
248 |
|
public void serveResourceWithCacheInfixAndOtherTildesAndDotsInPathName() throws Exception { |
249 |
|
|
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 |
|
|
254 |
1 |
servlet.doGet(request, response); |
255 |
|
|
256 |
|
|
257 |
1 |
assertTrue(servletOutputStream.dataWasWritten); |
258 |
|
} |
259 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
260 |
1 |
@Test... |
261 |
|
public void restrictedResourceTypesShouldReturn403() throws Exception { |
262 |
1 |
ResourcesServlet servlet = new ResourcesServlet(linker); |
263 |
|
|
264 |
1 |
verifyResourceIsRestricted(servlet, "/template.ftl"); |
265 |
1 |
verifyResourceIsRestricted(servlet, "/Foo.java"); |
266 |
|
} |
267 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
268 |
2 |
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 1 |
|
275 |
|
private static class DummyServletOutputStream extends ServletOutputStream { |
276 |
|
boolean dataWasWritten = false; |
277 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
278 |
60 |
@Override... |
279 |
|
public void write(int b) throws IOException { |
280 |
60 |
dataWasWritten = true; |
281 |
|
} |
282 |
|
} |
283 |
|
} |