• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2012 Google Inc. All Rights Reserved.
2 
3 package com.google.inject.servlet;
4 
5 import static org.easymock.EasyMock.createMock;
6 import static org.easymock.EasyMock.expect;
7 import static org.easymock.EasyMock.replay;
8 import static org.easymock.EasyMock.verify;
9 
10 import javax.servlet.http.HttpServletRequest;
11 import junit.framework.TestCase;
12 
13 /**
14  * Unit test for the servlet utility class.
15  *
16  * @author ntang@google.com (Michael Tang)
17  */
18 public class ServletUtilsTest extends TestCase {
testGetContextRelativePath()19   public void testGetContextRelativePath() {
20     assertEquals(
21         "/test.html", getContextRelativePath("/a_context_path", "/a_context_path/test.html"));
22     assertEquals("/test.html", getContextRelativePath("", "/test.html"));
23     assertEquals("/test.html", getContextRelativePath("", "/foo/../test.html"));
24     assertEquals("/test.html", getContextRelativePath("", "/././foo/../test.html"));
25     assertEquals("/test.html", getContextRelativePath("", "/foo/../../../../test.html"));
26     assertEquals("/test.html", getContextRelativePath("", "/foo/%2E%2E/test.html"));
27     // %2E == '.'
28     assertEquals("/test.html", getContextRelativePath("", "/foo/%2E%2E/test.html"));
29     // %2F == '/'
30     assertEquals("/foo/%2F/test.html", getContextRelativePath("", "/foo/%2F/test.html"));
31     // %66 == 'f'
32     assertEquals("/foo.html", getContextRelativePath("", "/%66oo.html"));
33   }
34 
testGetContextRelativePath_preserveQuery()35   public void testGetContextRelativePath_preserveQuery() {
36     assertEquals("/foo?q=f", getContextRelativePath("", "/foo?q=f"));
37     assertEquals("/foo?q=%20+%20", getContextRelativePath("", "/foo?q=%20+%20"));
38   }
39 
testGetContextRelativePathWithWrongPath()40   public void testGetContextRelativePathWithWrongPath() {
41     assertNull(getContextRelativePath("/a_context_path", "/test.html"));
42   }
43 
testGetContextRelativePathWithRootPath()44   public void testGetContextRelativePathWithRootPath() {
45     assertEquals("/", getContextRelativePath("/a_context_path", "/a_context_path"));
46   }
47 
testGetContextRelativePathWithEmptyPath()48   public void testGetContextRelativePathWithEmptyPath() {
49     assertNull(getContextRelativePath("", ""));
50   }
51 
getContextRelativePath(String contextPath, String requestPath)52   private String getContextRelativePath(String contextPath, String requestPath) {
53     HttpServletRequest mock = createMock(HttpServletRequest.class);
54     expect(mock.getContextPath()).andReturn(contextPath);
55     expect(mock.getRequestURI()).andReturn(requestPath);
56     replay(mock);
57     String contextRelativePath = ServletUtils.getContextRelativePath(mock);
58     verify(mock);
59     return contextRelativePath;
60   }
61 }
62