View Javadoc

1   /*
2    * $Id: ServletDispatchedTestAssertInterceptor.java 651946 2008-04-27 13:41:38Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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 }