View Javadoc
1   package com.atlassian.plugin.servlet;
2   
3   import com.atlassian.plugin.servlet.descriptors.ServletModuleDescriptor;
4   import com.atlassian.plugin.servlet.descriptors.ServletModuleDescriptorBuilder;
5   import org.junit.Test;
6   
7   import javax.servlet.http.HttpServletRequest;
8   import javax.servlet.http.HttpSession;
9   
10  import static org.junit.Assert.assertEquals;
11  import static org.junit.Assert.assertNull;
12  import static org.junit.Assert.assertTrue;
13  import static org.mockito.Mockito.mock;
14  import static org.mockito.Mockito.when;
15  
16  public class TestPluginHttpRequestWrapper {
17  
18      @Test
19      public void testForwardScenario() throws Exception {
20          final HttpServletRequest mockInnerRequest = mock(HttpServletRequest.class);
21  
22          // Initially mocking it to "/hello" (so that the forward request's basePath is resolved to "/hello". This is replicating the specific issue
23          when(mockInnerRequest.getPathInfo()).thenReturn("/forwardSource");
24  
25          when(mockInnerRequest.getServletPath()).thenReturn("/plugin/servlet");
26  
27          PluginHttpRequestWrapper forwardRequest = new PluginHttpRequestWrapper(mockInnerRequest,
28                  new ServletModuleDescriptorBuilder().withPath("/forwardSource").build());
29  
30          // Mocking it to its actual value "/world"
31          when(mockInnerRequest.getPathInfo()).thenReturn("/forwardDestination");
32  
33          assertEquals("/forwardDestination", forwardRequest.getPathInfo());
34      }
35  
36      @Test
37      public void testWildcardMatching() {
38          PluginHttpRequestWrapper request = getWrappedRequest("/context/plugins", "/plugin/servlet/path/to/resource",
39                  new ServletModuleDescriptorBuilder().withPath("/plugin/servlet/*").build());
40  
41          assertEquals("/path/to/resource", request.getPathInfo());
42          assertEquals("/context/plugins/plugin/servlet", request.getServletPath());
43      }
44  
45      @Test
46      public void testExactPathMatching() {
47          PluginHttpRequestWrapper request = getWrappedRequest("/context/plugins", "/plugin/servlet",
48                  new ServletModuleDescriptorBuilder().withPath("/plugin/servlet").build());
49  
50          assertNull(request.getPathInfo());
51          assertEquals("/context/plugins/plugin/servlet", request.getServletPath());
52      }
53  
54      private PluginHttpRequestWrapper getWrappedRequest(String servletPath, String pathInfo,
55                                                         ServletModuleDescriptor servletModuleDescriptor) {
56          HttpServletRequest mockWrappedRequest = mock(HttpServletRequest.class);
57          when(mockWrappedRequest.getServletPath()).thenReturn(servletPath);
58          when(mockWrappedRequest.getPathInfo()).thenReturn(pathInfo);
59  
60          return new PluginHttpRequestWrapper(mockWrappedRequest, servletModuleDescriptor);
61      }
62  
63      @Test
64      public void testGetSessionFalse() throws Exception {
65          HttpServletRequest mockWrappedRequest = mock(HttpServletRequest.class);
66          when(mockWrappedRequest.getPathInfo()).thenReturn(null);
67          when(mockWrappedRequest.getSession(false)).thenReturn(null);
68  
69          PluginHttpRequestWrapper request = new PluginHttpRequestWrapper(mockWrappedRequest, null);
70  
71          assertNull(request.getSession(false));
72      }
73  
74      @Test
75      public void testGetSession() throws Exception {
76          // Mock the Session
77          HttpSession mockSession = mock(HttpSession.class);
78          when(mockSession.getAttribute("foo")).thenReturn("bar");
79  
80          // Mock the Request
81          HttpServletRequest mockWrappedRequest = mock(HttpServletRequest.class);
82          // getPathInfo(0 gets called in constructor
83          when(mockWrappedRequest.getPathInfo()).thenReturn(null);
84          // delegate will have getSession(true) called and return null.
85          when(mockWrappedRequest.getSession(true)).thenReturn(mockSession);
86  
87          PluginHttpRequestWrapper request = new PluginHttpRequestWrapper(mockWrappedRequest, null);
88  
89          HttpSession wrappedSession = request.getSession();
90          assertTrue(wrappedSession instanceof PluginHttpSessionWrapper);
91          assertEquals("bar", wrappedSession.getAttribute("foo"));
92      }
93  
94      @Test
95      public void testGetSessionTrue() throws Exception {
96          // Mock the Session
97          HttpSession mockSession = mock(HttpSession.class);
98          when(mockSession.getAttribute("foo")).thenReturn("bar");
99  
100         // Mock the Request
101         HttpServletRequest mockWrappedRequest = mock(HttpServletRequest.class);
102         // getPathInfo(0 gets called in constructor
103         when(mockWrappedRequest.getPathInfo()).thenReturn(null);
104         // delegate will have getSession(true) called and return null.
105         when(mockWrappedRequest.getSession(true)).thenReturn(mockSession);
106         PluginHttpRequestWrapper request = new PluginHttpRequestWrapper(mockWrappedRequest, null);
107 
108         HttpSession wrappedSession = request.getSession(true);
109         assertTrue(wrappedSession instanceof PluginHttpSessionWrapper);
110         assertEquals("bar", wrappedSession.getAttribute("foo"));
111     }
112 
113     @Test
114     public void testPrefixingWildcardsMatching() {
115         PluginHttpRequestWrapper request = getWrappedRequest("/context/plugins", "/plugin/servlet-two/path/to/resource",
116                 new ServletModuleDescriptorBuilder()
117                         .withPath("/plugin/servlet/*")
118                         .withPath("/plugin/servlet-two/*")
119                         .build());
120 
121         // should match the second mapping.
122         assertEquals("/context/plugins/plugin/servlet-two", request.getServletPath());
123         assertEquals("/path/to/resource", request.getPathInfo());
124     }
125 }