View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.transport.servlet;
8   
9   import org.mule.transport.http.HttpConstants;
10  
11  import java.util.Enumeration;
12  import java.util.HashMap;
13  import java.util.Hashtable;
14  import java.util.Map;
15  import java.util.Set;
16  
17  import javax.servlet.ServletInputStream;
18  import javax.servlet.http.HttpServletRequest;
19  import javax.servlet.http.HttpSession;
20  
21  import org.apache.commons.collections.iterators.IteratorEnumeration;
22  
23  import static org.mockito.Matchers.anyBoolean;
24  import static org.mockito.Matchers.eq;
25  import static org.mockito.Mockito.mock;
26  import static org.mockito.Mockito.when;
27  
28  public class MockHttpServletRequestBuilder
29  {
30      public static final String REQUEST_URI = "/services/Echo";
31  
32      public String method = HttpConstants.METHOD_GET;
33      public String requestUri = REQUEST_URI;
34      public ServletInputStream inputStream = null;
35      public String payload = null;
36      public String queryString = null;
37      public Map<String, String[]> parameters = null;
38      public String contentType = null;
39      public HttpSession session = null;
40      public String characterEncoding = null;
41      public Map<String, String> attributes = new HashMap<String, String>();
42      public Map<String, Object> headers = new HashMap<String, Object>();
43      public String host = "localhost";
44      public int localPort = 8080;
45  
46      public HttpServletRequest buildRequest() throws Exception
47      {
48          HttpServletRequest mockRequest = mock(HttpServletRequest.class);
49          when(mockRequest.getMethod()).thenReturn(method);
50  
51          Enumeration<?> emptyEnumeration = new Hashtable<Object, Object>().elements();
52          when(mockRequest.getParameterNames()).thenReturn(emptyEnumeration);
53  
54          when(mockRequest.getRequestURI()).thenReturn(requestUri);
55          when(mockRequest.getQueryString()).thenReturn(queryString);
56          when(mockRequest.getInputStream()).thenReturn(inputStream);
57          when(mockRequest.getSession(anyBoolean())).thenReturn(session);
58          when(mockRequest.getCharacterEncoding()).thenReturn(characterEncoding);
59          when(mockRequest.getLocalPort()).thenReturn(localPort);
60          when(mockRequest.getContentType()).thenReturn(contentType);
61          when(mockRequest.getRemoteAddr()).thenReturn(host);
62          when(mockRequest.getHeader(eq(HttpConstants.HEADER_HOST))).thenReturn(host);
63  
64          addParameterExpectations(mockRequest);
65          addAttributeExpectations(mockRequest);
66          addHeaderExpectations(mockRequest);
67  
68          return mockRequest;
69      }
70  
71      private void addParameterExpectations(HttpServletRequest mockRequest)
72      {
73          Enumeration<?> nameEnum = null;
74  
75          if (parameters != null)
76          {
77              Set<String> keys = parameters.keySet();
78              nameEnum = new IteratorEnumeration(keys.iterator());
79  
80              for (Map.Entry<String, String[]> entry : parameters.entrySet())
81              {
82                  String key = entry.getKey();
83                  String[] value = entry.getValue();
84                  when(mockRequest.getParameterValues(eq(key))).thenReturn(value);
85              }
86          }
87  
88          when(mockRequest.getParameterNames()).thenReturn(nameEnum);
89          when(mockRequest.getParameterMap()).thenReturn(parameters);
90      }
91  
92      private void addAttributeExpectations(HttpServletRequest mockRequest)
93      {
94          Enumeration<?> nameEnum = null;
95  
96          if (attributes != null)
97          {
98              nameEnum = keyEnumeration(attributes);
99  
100             for (Map.Entry<String, String> entry : attributes.entrySet())
101             {
102                 String key = entry.getKey();
103                 String value = entry.getValue();
104 
105                 when(mockRequest.getAttribute(eq(key))).thenReturn(value);
106             }
107         }
108 
109         when(mockRequest.getAttributeNames()).thenReturn(nameEnum);
110     }
111 
112     private void addHeaderExpectations(HttpServletRequest mockRequest)
113     {
114         Enumeration<?> nameEnum = null;
115         if (headers != null)
116         {
117             nameEnum = keyEnumeration(headers);
118 
119             for (Map.Entry<String, Object> entry : headers.entrySet())
120             {
121                 String key = entry.getKey();
122                 Object value = entry.getValue();
123 
124                 Enumeration<?> valueAsEnumeration = null;
125                 if ((value instanceof Enumeration<?>) == false)
126                 {
127                     valueAsEnumeration = new SingleElementEnumeration(value);
128                 }
129                 else
130                 {
131                     valueAsEnumeration = (Enumeration<?>) value;
132                 }
133 
134                 when(mockRequest.getHeaders(eq(key))).thenReturn(valueAsEnumeration);
135             }
136         }
137 
138         when(mockRequest.getHeaderNames()).thenReturn(nameEnum);
139     }
140 
141     private Enumeration<?> keyEnumeration(Map<?, ?> map)
142     {
143         Set<?> keys = map.keySet();
144         return new IteratorEnumeration(keys.iterator());
145     }
146 
147     private static class SingleElementEnumeration implements Enumeration<Object>
148     {
149         private Object element;
150 
151         public SingleElementEnumeration(Object singleElement)
152         {
153             super();
154             element = singleElement;
155         }
156 
157         public boolean hasMoreElements()
158         {
159             return (element != null);
160         }
161 
162         public Object nextElement()
163         {
164             Object retValue = element;
165             if (element != null)
166             {
167                 element = null;
168             }
169             return retValue;
170         }
171     }
172 }