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 com.mockobjects.dynamic.Mock;
6   import junit.framework.TestCase;
7   
8   import javax.servlet.http.HttpServletRequest;
9   import javax.servlet.http.HttpSession;
10  
11  public class TestPluginHttpRequestWrapper extends TestCase
12  {
13      public void testWildcardMatching()
14      {
15          PluginHttpRequestWrapper request = getWrappedRequest("/context/plugins", "/plugin/servlet/path/to/resource",
16              new ServletModuleDescriptorBuilder().withPath("/plugin/servlet/*").build());
17  
18          assertEquals("/path/to/resource", request.getPathInfo());
19          assertEquals("/context/plugins/plugin/servlet", request.getServletPath());
20      }
21  
22      public void testExactPathMatching()
23      {
24          PluginHttpRequestWrapper request = getWrappedRequest("/context/plugins", "/plugin/servlet",
25              new ServletModuleDescriptorBuilder().withPath("/plugin/servlet").build());
26  
27          assertNull(request.getPathInfo());
28          assertEquals("/context/plugins/plugin/servlet", request.getServletPath());
29      }
30  
31      private PluginHttpRequestWrapper getWrappedRequest(String servletPath, String pathInfo,
32          ServletModuleDescriptor servletModuleDescriptor)
33      {
34          Mock mockWrappedRequest = new Mock(HttpServletRequest.class);
35          mockWrappedRequest.matchAndReturn("getServletPath", servletPath);
36          mockWrappedRequest.matchAndReturn("getPathInfo", pathInfo);
37          return new PluginHttpRequestWrapper((HttpServletRequest) mockWrappedRequest.proxy(), servletModuleDescriptor);
38      }
39  
40      public void testGetSessionFalse() throws Exception
41      {
42          Mock mockWrappedRequest = new Mock(HttpServletRequest.class);
43          mockWrappedRequest.matchAndReturn("getPathInfo", null);
44          // delegate will have getSession(false) called and return null.
45          mockWrappedRequest.matchAndReturn("getSession", false, null);
46          PluginHttpRequestWrapper request = new PluginHttpRequestWrapper((HttpServletRequest) mockWrappedRequest.proxy(), null);
47  
48          assertNull(request.getSession(false));
49      }
50  
51      public void testGetSession() throws Exception
52      {
53          // Mock the Session
54          Mock mockSession = new Mock(HttpSession.class);
55          mockSession.matchAndReturn("getAttribute", "foo", "bar");
56          HttpSession realSession = (HttpSession) mockSession.proxy();
57  
58          // Mock the Request
59          Mock mockWrappedRequest = new Mock(HttpServletRequest.class);
60          // getPathInfo(0 gets called in constructor
61          mockWrappedRequest.matchAndReturn("getPathInfo", null);
62          // delegate will have getSession(true) called and return null.
63          mockWrappedRequest.matchAndReturn("getSession", true, realSession);
64          PluginHttpRequestWrapper request = new PluginHttpRequestWrapper((HttpServletRequest) mockWrappedRequest.proxy(), null);
65  
66          HttpSession wrappedSession = request.getSession();
67          assertTrue(wrappedSession instanceof PluginHttpSessionWrapper);
68          assertEquals("bar", wrappedSession.getAttribute("foo"));
69      }
70  
71      public void testGetSessionTrue() throws Exception
72      {
73          // Mock the Session
74          Mock mockSession = new Mock(HttpSession.class);
75          mockSession.matchAndReturn("getAttribute", "foo", "bar");
76          HttpSession realSession = (HttpSession) mockSession.proxy();
77  
78          // Mock the Request
79          Mock mockWrappedRequest = new Mock(HttpServletRequest.class);
80          // getPathInfo(0 gets called in constructor
81          mockWrappedRequest.matchAndReturn("getPathInfo", null);
82          // delegate will have getSession(true) called and return null.
83          mockWrappedRequest.matchAndReturn("getSession", true, realSession);
84          PluginHttpRequestWrapper request = new PluginHttpRequestWrapper((HttpServletRequest) mockWrappedRequest.proxy(), null);
85  
86          HttpSession wrappedSession = request.getSession(true);
87          assertTrue(wrappedSession instanceof PluginHttpSessionWrapper);
88          assertEquals("bar", wrappedSession.getAttribute("foo"));
89      }
90  
91      public void testPrefixingWildcardsMatching()
92      {
93          PluginHttpRequestWrapper request = getWrappedRequest("/context/plugins", "/plugin/servlet-two/path/to/resource",
94              new ServletModuleDescriptorBuilder()
95                      .withPath("/plugin/servlet/*")
96                      .withPath("/plugin/servlet-two/*")
97                      .build());
98  
99          // should match the second mapping.
100         assertEquals("/context/plugins/plugin/servlet-two", request.getServletPath());
101         assertEquals("/path/to/resource", request.getPathInfo());
102     }
103 }