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