View Javadoc

1   package com.atlassian.plugin.servlet.util;
2   
3   import junit.framework.TestCase;
4   
5   public class TestDefaultPathMapper extends TestCase
6   {
7       /**
8        * For more info, see:
9        * https://studio.atlassian.com/browse/PLUG-597
10       * https://studio.atlassian.com/browse/APL-170
11       * https://extranet.atlassian.com/display/~evzijst/2010/06/02/Bypassing+Servlet+Filters+with+Double+Slashes
12       * https://studio.atlassian.com/source/cru/CR-PLUG-193#c17487
13       * https://studio.atlassian.com/browse/PLUG-605
14       */
15      public void testDoubleSlashes()
16      {
17          final PathMapper pathMapper = new DefaultPathMapper();
18  
19          pathMapper.put("key", "/foo/bar*");
20          assertEquals("key", pathMapper.get("/foo/bar"));
21          assertEquals("key", pathMapper.get("/foo//bar"));
22          assertEquals("key", pathMapper.get("/foo///bar"));
23          assertEquals("key", pathMapper.get("/foo///bar 2"));
24          assertNull(pathMapper.get("/images/ddtree/black spinner/12.png"));
25      }
26  
27      public void testSlashRemover()
28      {
29          final DefaultPathMapper pathMapper = new DefaultPathMapper();
30  
31          assertNull(pathMapper.removeRedundantSlashes(null));
32          assertEquals("foo", pathMapper.removeRedundantSlashes("foo"));
33          assertEquals("foo/bar", pathMapper.removeRedundantSlashes("foo/bar"));
34          assertEquals("/", pathMapper.removeRedundantSlashes("/"));
35          assertEquals("/", pathMapper.removeRedundantSlashes("//"));
36          assertEquals("/", pathMapper.removeRedundantSlashes("///"));
37          assertEquals("foo/bar", pathMapper.removeRedundantSlashes("foo//bar"));
38          assertEquals("foo/bar", pathMapper.removeRedundantSlashes("foo///bar"));
39          assertEquals("foo/bar", pathMapper.removeRedundantSlashes("foo////bar"));
40          assertEquals("foo/bar/", pathMapper.removeRedundantSlashes("foo////bar/"));
41          assertEquals("/f oo/b/ar/", pathMapper.removeRedundantSlashes("//f oo////b/ar//"));
42      }
43  
44      public void testRemovePath()
45      {
46          final PathMapper pathMapper = new DefaultPathMapper();
47  
48          pathMapper.put("foo.bar", "/foo*");
49          pathMapper.put("foo.baz", "/bar*");
50          assertEquals("foo.bar", pathMapper.get("/foo/bar"));
51          assertEquals("foo.baz", pathMapper.get("/bar/foo"));
52  
53          pathMapper.put("foo.bar", null);
54          assertNull(pathMapper.get("/foo/bar"));
55          assertEquals(0, pathMapper.getAll("/foo/bar").size());
56          assertEquals("foo.baz", pathMapper.get("/bar/foo"));
57      }
58  
59      public void testComplexKeyMultipleMatches()
60      {
61          final PathMapper pathMapper = new DefaultPathMapper();
62  
63          pathMapper.put("key1", "/foo/*");
64          pathMapper.put("key2", "/foo/bar/doo/*");
65          pathMapper.put("key3", "/foo/b?r/doo/*");
66          pathMapper.put("key4", "/foo/bar/doo*");
67          pathMapper.put("key5", "/foo/bar/*");
68  
69          assertEquals("key2", pathMapper.get("/foo/bar/doo/blah"));
70      }
71  }