1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.struts2.dispatcher;
22
23 import java.io.IOException;
24
25 import javax.servlet.http.HttpServletResponse;
26
27 import junit.framework.TestCase;
28
29 import org.apache.struts2.dispatcher.ng.HostConfig;
30 import org.springframework.mock.web.MockHttpServletRequest;
31 import org.springframework.mock.web.MockHttpServletResponse;
32 import org.springframework.mock.web.MockServletContext;
33
34 import com.mockobjects.dynamic.C;
35 import com.mockobjects.dynamic.Mock;
36
37
38 public class StaticContentLoaderTest extends TestCase {
39
40 private DefaultStaticContentLoader contentLoader;
41 private MockHttpServletRequest req;
42 private MockHttpServletResponse res;
43
44 public void testCantHandleWithoutServingStatic() {
45 StaticContentLoader contentLoader = new DefaultStaticContentLoader();
46
47 assertFalse(contentLoader.canHandle("/static/test1.css"));
48 assertFalse(contentLoader.canHandle("/struts/test1.css"));
49 assertFalse(contentLoader.canHandle("test1.css"));
50 }
51
52 public void testCanHandle() {
53 DefaultStaticContentLoader contentLoader = new DefaultStaticContentLoader();
54 contentLoader.setServeStaticContent("true");
55
56 assertTrue(contentLoader.canHandle("/static/test1.css"));
57 assertTrue(contentLoader.canHandle("/struts/test1.css"));
58 assertFalse(contentLoader.canHandle("test1.css"));
59 }
60
61 public void testValidRersources() throws IOException {
62 contentLoader.findStaticResource("/struts/resource.css", req, res);
63 assertEquals("heya!", res.getContentAsString());
64 }
65
66 public void testInvalidRersources1() throws IOException {
67 contentLoader.findStaticResource("/struts..", req, res);
68 assertEquals(HttpServletResponse.SC_NOT_FOUND, res.getStatus());
69 assertEquals(0, res.getContentLength());
70 }
71
72 public void testInvalidRersources2() throws IOException {
73 contentLoader.findStaticResource("/struts/..", req, res);
74 assertEquals(HttpServletResponse.SC_NOT_FOUND, res.getStatus());
75 assertEquals(0, res.getContentLength());
76 }
77
78 public void testInvalidRersources3() throws IOException {
79 contentLoader.findStaticResource("/struts/../othertest.properties", req, res);
80 assertEquals(HttpServletResponse.SC_NOT_FOUND, res.getStatus());
81 assertEquals(0, res.getContentLength());
82 }
83
84 public void testInvalidRersources4() throws IOException {
85 contentLoader.findStaticResource("/struts/..%252f", req, res);
86 assertEquals(HttpServletResponse.SC_NOT_FOUND, res.getStatus());
87 assertEquals(0, res.getContentLength());
88 }
89
90 public void testInvalidRersources5() throws IOException {
91 contentLoader.findStaticResource("/struts/..%252fothertest.properties", req, res);
92 assertEquals(HttpServletResponse.SC_NOT_FOUND, res.getStatus());
93 assertEquals(0, res.getContentLength());
94 }
95
96 @Override
97 protected void setUp() throws Exception {
98 super.setUp();
99
100 this.contentLoader = new DefaultStaticContentLoader();
101 MockServletContext servletContext = new MockServletContext();
102 req = new MockHttpServletRequest(servletContext);
103 res = new MockHttpServletResponse();
104
105
106 Mock hostConfigMock = new Mock(HostConfig.class);
107 hostConfigMock.expectAndReturn("getInitParameter", C.args(C.eq("packages")), null);
108 hostConfigMock.expectAndReturn("getInitParameter", C.args(C.eq("loggerFactory")), null);
109
110 contentLoader.setEncoding("utf-8");
111
112 contentLoader.setHostConfig((HostConfig) hostConfigMock.proxy());
113 }
114
115
116 }