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