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